diff --git a/programs/omnipair/src/instructions/spot/swap.rs b/programs/omnipair/src/instructions/spot/swap.rs index 05cbaec..8b1f079 100644 --- a/programs/omnipair/src/instructions/spot/swap.rs +++ b/programs/omnipair/src/instructions/spot/swap.rs @@ -28,7 +28,8 @@ pub struct Swap<'info> { seeds = [PAIR_SEED_PREFIX, pair.token0.as_ref(), pair.token1.as_ref(), pair.pair_nonce.as_ref()], bump )] - pub pair: Account<'info, Pair>, + // Box used to avoid Access violation in stack frame... error + pub pair: Box>, #[account( mut, diff --git a/trident-tests/Cargo.lock b/trident-tests/Cargo.lock new file mode 100644 index 0000000..bee8d57 --- /dev/null +++ b/trident-tests/Cargo.lock @@ -0,0 +1,6254 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm-siv" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae0784134ba9375416d469ec31e7c5f9fa94405049cf08c5ce5b4698be673e0d" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "polyval", + "subtle", + "zeroize", +] + +[[package]] +name = "agave-feature-set" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a2c365c0245cbb8959de725fc2b44c754b673fdf34c9a7f9d4a25c35a7bf1" +dependencies = [ + "ahash", + "solana-epoch-schedule", + "solana-hash", + "solana-pubkey", + "solana-sha256-hasher", + "solana-svm-feature-set", +] + +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "getrandom 0.3.4", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "alloc-no-stdlib" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" + +[[package]] +name = "alloc-stdlib" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94fb8275041c72129eb51b7d0322c29b8387a0386127718b096429201a5d6ece" +dependencies = [ + "alloc-no-stdlib", +] + +[[package]] +name = "anyhow" +version = "1.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" + +[[package]] +name = "ark-bn254" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a22f4561524cd949590d78d7d4c5df8f592430d221f7f3c9497bbafd8972120f" +dependencies = [ + "ark-ec", + "ark-ff", + "ark-std", +] + +[[package]] +name = "ark-ec" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defd9a439d56ac24968cca0571f598a61bc8c55f71d50a89cda591cb750670ba" +dependencies = [ + "ark-ff", + "ark-poly", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.13.2", + "itertools 0.10.5", + "num-traits", + "zeroize", +] + +[[package]] +name = "ark-ff" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" +dependencies = [ + "ark-ff-asm", + "ark-ff-macros", + "ark-serialize", + "ark-std", + "derivative", + "digest 0.10.7", + "itertools 0.10.5", + "num-bigint 0.4.6", + "num-traits", + "paste", + "rustc_version", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" +dependencies = [ + "num-bigint 0.4.6", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-poly" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d320bfc44ee185d899ccbadfa8bc31aab923ce1558716e1997a1e74057fe86bf" +dependencies = [ + "ark-ff", + "ark-serialize", + "ark-std", + "derivative", + "hashbrown 0.13.2", +] + +[[package]] +name = "ark-serialize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" +dependencies = [ + "ark-serialize-derive", + "ark-std", + "digest 0.10.7", + "num-bigint 0.4.6", +] + +[[package]] +name = "ark-serialize-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae3281bc6d0fd7e549af32b52511e1302185bd688fd3359fa36423346ff682ea" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-std" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" +dependencies = [ + "num-traits", + "rand 0.8.5", +] + +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] +name = "ascii" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e" + +[[package]] +name = "async-compression" +version = "0.4.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93c1f86859c1af3d514fa19e8323147ff10ea98684e6c7b307912509f50e67b2" +dependencies = [ + "compression-codecs", + "compression-core", + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" +dependencies = [ + "serde_core", +] + +[[package]] +name = "blake3" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3888aaa89e4b2a40fca9848e400f6a658a5a3978de7be858e209cafa8be9a4a0" +dependencies = [ + "arrayref", + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq", + "digest 0.10.7", +] + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "borsh" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115e54d64eb62cdebad391c19efc9dce4981c690c85a33a12199d99bb9546fee" +dependencies = [ + "borsh-derive 0.10.4", + "hashbrown 0.13.2", +] + +[[package]] +name = "borsh" +version = "1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8646f98db542e39fc66e68a20b2144f6a732636df7c2354e74645faaa433ce" +dependencies = [ + "borsh-derive 1.5.7", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831213f80d9423998dd696e2c5345aba6be7a0bd8cd19e31c5243e13df1cef89" +dependencies = [ + "borsh-derive-internal", + "borsh-schema-derive-internal", + "proc-macro-crate 0.1.5", + "proc-macro2", + "syn 1.0.109", +] + +[[package]] +name = "borsh-derive" +version = "1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdd1d3c0c2f5833f22386f252fe8ed005c7f59fdcddeef025c01b4c3b9fd9ac3" +dependencies = [ + "once_cell", + "proc-macro-crate 3.4.0", + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "borsh-derive-internal" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65d6ba50644c98714aa2a70d13d7df3cd75cd2b523a2b452bf010443800976b3" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "borsh-schema-derive-internal" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "276691d96f063427be83e6692b86148e488ebba9f48f77788724ca027ba3b6d4" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "brotli" +version = "8.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd8b9603c7aa97359dbd97ecf258968c95f3adddd6db2f7e7a5bef101c84560" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "874bb8112abecc98cbd6d81ea4fa7e94fb9449648c93cc89aa40c81c24d7de03" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + +[[package]] +name = "bv" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8834bb1d8ee5dc048ee3124f2c7c1afcc6bc9aed03f11e9dfd8c69470a5db340" +dependencies = [ + "feature-probe", + "serde", +] + +[[package]] +name = "bytemuck" +version = "1.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbdf580320f38b612e485521afda1ee26d10cc9884efaaa750d383e13e3c5f4" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9abbd1bc6865053c427f7198e6af43bfdedc55ab791faed4fbd361d789575ff" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" + +[[package]] +name = "cc" +version = "1.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97463e1064cb1b1c1384ad0a0b9c8abd0988e2a91f52606c80ef14aadb63e36" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "cfg_eval" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45565fc9416b9896014f5732ac776f810ee53a66730c17e4020c3ec064a8f88f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" +dependencies = [ + "num-traits", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + +[[package]] +name = "combine" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680" +dependencies = [ + "ascii", + "byteorder", + "either", + "memchr", + "unreachable", +] + +[[package]] +name = "compression-codecs" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "680dc087785c5230f8e8843e2e57ac7c1c90488b6a91b88caa265410568f441b" +dependencies = [ + "brotli", + "compression-core", + "flate2", + "memchr", +] + +[[package]] +name = "compression-core" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a9b614a5787ef0c8802a55766480563cb3a93b435898c422ed2a359cf811582" + +[[package]] +name = "console" +version = "0.15.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8" +dependencies = [ + "encode_unicode", + "libc", + "once_cell", + "unicode-width 0.2.2", + "windows-sys 0.59.0", +] + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "console_log" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89f72f65e8501878b8a004d5a1afb780987e2ce2b4532c562e367a72c57499f" +dependencies = [ + "log", + "web-sys", +] + +[[package]] +name = "constant_time_eq" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6" + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "typenum", +] + +[[package]] +name = "crypto-mac" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] +name = "csv" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde_core", +] + +[[package]] +name = "csv-core" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" +dependencies = [ + "memchr", +] + +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + +[[package]] +name = "curve25519-dalek" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b9fdf9972b2bd6af2d913799d9ebc165ea4d2e65878e329d9c6b372c4491b61" +dependencies = [ + "byteorder", + "digest 0.9.0", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "rand_core 0.6.4", + "rustc_version", + "serde", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "darling" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.110", +] + +[[package]] +name = "darling_macro" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "derivation-path" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e5c37193a1db1d8ed868c03ec7b152175f26160a5b740e5e484143877e0adf0" + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common", + "subtle", +] + +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "eager" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abe71d579d1812060163dff96056261deb5bf6729b100fa2e36a68b9649ba3d3" + +[[package]] +name = "ed25519" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91cff35c70bba8a626e3185d8cd48cc11b5437e1a5bcd15b9b5fa3c64b6dfee7" +dependencies = [ + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" +dependencies = [ + "curve25519-dalek 3.2.0", + "ed25519", + "rand 0.7.3", + "serde", + "sha2 0.9.9", + "zeroize", +] + +[[package]] +name = "ed25519-dalek-bip32" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d2be62a4061b872c8c0873ee4fc6f101ce7b889d039f019c5fa2af471a59908" +dependencies = [ + "derivation-path", + "ed25519-dalek", + "hmac 0.12.1", + "sha2 0.10.9", +] + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "enum-iterator" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fd242f399be1da0a5354aa462d57b4ab2b4ee0683cc552f7c007d2d12d36e94" +dependencies = [ + "enum-iterator-derive", +] + +[[package]] +name = "enum-iterator-derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685adfa4d6f3d765a26bc5dbc936577de9abf756c1feeb3089b01dd395034842" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "env_logger" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "feature-probe" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "835a3dc7d1ec9e75e2b5fb4ba75396837112d2060b03f7d43bc1897c7f7211da" + +[[package]] +name = "fehler" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5729fe49ba028cd550747b6e62cd3d841beccab5390aa398538c31a2d983635" +dependencies = [ + "fehler-macros", +] + +[[package]] +name = "fehler-macros" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccb5acb1045ebbfa222e2c50679e392a71dd77030b78fb0189f2d9c5974400f9" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "find-msvc-tools" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" + +[[package]] +name = "five8" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75b8549488b4715defcb0d8a8a1c1c76a80661b5fa106b4ca0e7fce59d7d875" +dependencies = [ + "five8_core", +] + +[[package]] +name = "five8_const" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26dec3da8bc3ef08f2c04f61eab298c3ab334523e55f076354d6d6f613799a7b" +dependencies = [ + "five8_core", +] + +[[package]] +name = "five8_core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2551bf44bc5f776c15044b9b94153a00198be06743e262afaaa61f11ac7523a5" + +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + +[[package]] +name = "flate2" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfe33edd8e85a12a67454e37f8c75e730830d83e313556ab9ebf9ee7fbeb3bfb" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "fuzz_tests" +version = "0.1.0" +dependencies = [ + "borsh 1.5.7", + "trident-fuzz", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "gethostname" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ebd34e35c46e00bb73e81363248d627782724609fe1b6396f553f68fe3862e" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi 0.11.1+wasi-snapshot-preview1", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "h2" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0beca50380b1fc32983fc1cb4587bfa4bb9e78fc259aad4a0032d2080309222d" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.12", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashbrown" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hmac" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "126888268dcc288495a26bf004b38c5fdbb31682f992c84ceb046a1f0fe38840" +dependencies = [ + "crypto-mac", + "digest 0.9.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "hmac-drbg" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17ea0a1394df5b6574da6e0c1ade9e78868c9fb0a4e5ef4428e32da4676b85b1" +dependencies = [ + "digest 0.9.0", + "generic-array", + "hmac 0.8.1", +] + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.3.1", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http 1.3.1", + "http-body 1.0.1", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "humantime" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424" + +[[package]] +name = "hyper" +version = "0.14.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2 0.5.10", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http 1.3.1", + "http-body 1.0.1", + "httparse", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58" +dependencies = [ + "http 1.3.1", + "hyper 1.8.1", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.32", + "native-tls", + "tokio", + "tokio-native-tls", +] + +[[package]] +name = "hyper-util" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52e9a2a24dc5c6821e71a7030e1e14b7b632acac55c40e9d2e082c621261bb56" +dependencies = [ + "base64 0.22.1", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http 1.3.1", + "http-body 1.0.1", + "hyper 1.8.1", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2 0.6.1", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e93fcd3157766c0c8da2f8cff6ce651a31f0810eaa1c51ec363ef790bbb5fb99" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02845b3647bb045f1100ecd6480ff52f34c35f82d9880e029d329c21d1054899" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" +dependencies = [ + "equivalent", + "hashbrown 0.16.0", +] + +[[package]] +name = "indicatif" +version = "0.17.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "183b3088984b400f4cfac3620d5e076c84da5364016b4f49473de574b2586235" +dependencies = [ + "console", + "number_prefix", + "portable-atomic", + "unicode-width 0.2.2", + "web-time", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "ipnet" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" + +[[package]] +name = "iri-string" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f867b9d1d896b67beb18518eda36fdb77a32ea590de864f1325b294a6d14397" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "is-terminal" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" +dependencies = [ + "hermit-abi 0.5.2", + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "js-sys" +version = "0.3.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b011eec8cc36da2aab2d5cff675ec18454fad408585853910a202391cf9f8e65" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "kaigan" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ba15de5aeb137f0f65aa3bf82187647f1285abfe5b20c80c2c37f7007ad519a" +dependencies = [ + "borsh 0.10.4", + "serde", +] + +[[package]] +name = "keccak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654" +dependencies = [ + "cpufeatures", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.177" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" + +[[package]] +name = "libredox" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "416f7e718bdb06000964960ffa43b4335ad4012ae8b99060261aa4a8088d5ccb" +dependencies = [ + "bitflags 2.10.0", + "libc", +] + +[[package]] +name = "libsecp256k1" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d220bc1feda2ac231cb78c3d26f27676b8cf82c96971f7aeef3d0cf2797c73" +dependencies = [ + "arrayref", + "base64 0.12.3", + "digest 0.9.0", + "hmac-drbg", + "libsecp256k1-core", + "libsecp256k1-gen-ecmult", + "libsecp256k1-gen-genmult", + "rand 0.7.3", + "serde", + "sha2 0.9.9", + "typenum", +] + +[[package]] +name = "libsecp256k1-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0f6ab710cec28cef759c5f18671a27dae2a5f952cdaaee1d8e2908cb2478a80" +dependencies = [ + "crunchy", + "digest 0.9.0", + "subtle", +] + +[[package]] +name = "libsecp256k1-gen-ecmult" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccab96b584d38fac86a83f07e659f0deafd0253dc096dab5a36d53efe653c5c3" +dependencies = [ + "libsecp256k1-core", +] + +[[package]] +name = "libsecp256k1-gen-genmult" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67abfe149395e3aa1c48a2beb32b068e2334402df8181f818d3aee2b304c4f5d" +dependencies = [ + "libsecp256k1-core", +] + +[[package]] +name = "light-poseidon" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c9a85a9752c549ceb7578064b4ed891179d20acd85f27318573b64d2d7ee7ee" +dependencies = [ + "ark-bn254", + "ark-ff", + "num-bigint 0.4.6", + "thiserror 1.0.69", +] + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "memmap2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" +dependencies = [ + "libc", +] + +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + +[[package]] +name = "merlin" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58c38e2799fc0978b65dfff8023ec7843e2330bb462f19198840b34b6582397d" +dependencies = [ + "byteorder", + "keccak", + "rand_core 0.6.4", + "zeroize", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69d83b0086dc8ecf3ce9ae2874b2d1290252e2a30720bea58a5c6639b0092873" +dependencies = [ + "libc", + "wasi 0.11.1+wasi-snapshot-preview1", + "windows-sys 0.61.2", +] + +[[package]] +name = "native-tls" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87de3442987e9dbec73158d5c715e7ad9072fda936bb03d19d7fa10e00520f0e" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "num" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36" +dependencies = [ + "num-bigint 0.2.6", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "090c7f9998ee0ff65aa5b723e4009f7b217707f1fb5ea551329cc4d6231fb304" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg", + "num-bigint 0.2.6", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_enum" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1207a7e20ad57b847bbddc6776b968420d38292bbfe2089accff5e19e82454c" +dependencies = [ + "num_enum_derive", + "rustversion", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff32365de1b6743cb203b710788263c44a03de03802daf96092f2da4fe6ba4d7" +dependencies = [ + "proc-macro-crate 3.4.0", + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + +[[package]] +name = "openssl" +version = "0.10.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08838db121398ad17ab8531ce9de97b244589089e290a384c900cb9ff7434328" +dependencies = [ + "bitflags 2.10.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "openssl-probe" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" + +[[package]] +name = "openssl-sys" +version = "0.9.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82cab2d520aa75e3c58898289429321eb788c3106963d0dc886ec7a5f4adc321" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pbkdf2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "percentage" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd23b938276f14057220b707937bcb42fa76dda7560e57a2da30cb52d557937" +dependencies = [ + "num", +] + +[[package]] +name = "petgraph" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "portable-atomic" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" + +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettytable" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46480520d1b77c9a3482d39939fcf96831537a250ec62d4fd8fbdf8e0302e781" +dependencies = [ + "csv", + "encode_unicode", + "is-terminal", + "lazy_static", + "term", + "unicode-width 0.1.14", +] + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml 0.5.11", +] + +[[package]] +name = "proc-macro-crate" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983" +dependencies = [ + "toml_edit 0.23.7", +] + +[[package]] +name = "proc-macro2" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "qstring" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d464fae65fff2680baf48019211ce37aaec0c78e9264c84a3e484717f965104e" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "qualifier_attr" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e2e25ee72f5b24d773cae88422baddefff7714f97aab68d96fe2b6fc4a28fb2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "quinn" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2 0.6.1", + "thiserror 2.0.17", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31" +dependencies = [ + "bytes", + "getrandom 0.3.4", + "lru-slab", + "rand 0.9.2", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.17", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2 0.6.1", + "tracing", + "windows-sys 0.60.2", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.16", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.10.0", +] + +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom 0.2.16", + "libredox", + "thiserror 1.0.69", +] + +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + +[[package]] +name = "reqwest" +version = "0.11.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +dependencies = [ + "base64 0.21.7", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body 0.4.6", + "hyper 0.14.32", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 0.1.2", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + +[[package]] +name = "reqwest" +version = "0.12.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d0946410b9f7b082a427e4ef5c8ff541a88b357bc6c637c40db3a68ac70a36f" +dependencies = [ + "async-compression", + "base64 0.22.1", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http 1.3.1", + "http-body 1.0.1", + "http-body-util", + "hyper 1.8.1", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 1.0.2", + "tokio", + "tokio-rustls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.16", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" + +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +dependencies = [ + "bitflags 2.10.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "533f54bc6a7d4f647e46ad909549eda97bf5afc1585190ef692b4286b198bd8f" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + +[[package]] +name = "rustls-pki-types" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94182ad936a0c91c324cd46c6511b9510ed16af436d7b5bab34beab0afd55f7a" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ffdfa2f5286e2247234e03f680868ac2815974dc39e00ea15adc445d0aafe52" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "schannel" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891d81b926048e76efe18581bf793546b4c0eaf8448d72be8de2bbee5fd166e1" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.10.0", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc1f0cbffaac4852523ce30d8bd3c5cdc873501d96ff467ca09b6767bb8cd5c0" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde-big-array" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11fc7cc2c76d73e0f27ee52abbd64eec84d46f370c88371120433196934e4b7f" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_bytes" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "serde_json" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", + "serde_core", +] + +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_with" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10574371d41b0d9b2cff89418eda27da52bcaff2cc8741db26382a77c29131f1" +dependencies = [ + "serde_core", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08a72d8216842fdd57820dc78d840bef99248e35fb2554ff923319e60f2d686b" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.7", +] + +[[package]] +name = "sha3" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" +dependencies = [ + "digest 0.10.7", + "keccak", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a4719bff48cee6b39d12c020eeb490953ad2443b7055bd0b21fca26bd8c28b" +dependencies = [ + "libc", +] + +[[package]] +name = "signature" +version = "1.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" + +[[package]] +name = "simd-adler32" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" + +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + +[[package]] +name = "slab" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "socket2" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17129e116933cf371d018bb80ae557e889637989d8638274fb25622827b03881" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + +[[package]] +name = "solana-account" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f949fe4edaeaea78c844023bfc1c898e0b1f5a100f8a8d2d0f85d0a7b090258" +dependencies = [ + "bincode", + "serde", + "serde_bytes", + "serde_derive", + "solana-account-info", + "solana-clock", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", + "solana-sysvar", +] + +[[package]] +name = "solana-account-info" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8f5152a288ef1912300fc6efa6c2d1f9bb55d9398eb6c72326360b8063987da" +dependencies = [ + "bincode", + "serde", + "solana-program-error", + "solana-program-memory", + "solana-pubkey", +] + +[[package]] +name = "solana-address-lookup-table-interface" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1673f67efe870b64a65cb39e6194be5b26527691ce5922909939961a6e6b395" +dependencies = [ + "bincode", + "bytemuck", + "serde", + "serde_derive", + "solana-clock", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", + "solana-slot-hashes", +] + +[[package]] +name = "solana-atomic-u64" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52e52720efe60465b052b9e7445a01c17550666beec855cce66f44766697bc2" +dependencies = [ + "parking_lot", +] + +[[package]] +name = "solana-big-mod-exp" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75db7f2bbac3e62cfd139065d15bcda9e2428883ba61fc8d27ccb251081e7567" +dependencies = [ + "num-bigint 0.4.6", + "num-traits", + "solana-define-syscall", +] + +[[package]] +name = "solana-bincode" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19a3787b8cf9c9fe3dd360800e8b70982b9e5a8af9e11c354b6665dd4a003adc" +dependencies = [ + "bincode", + "serde", + "solana-instruction", +] + +[[package]] +name = "solana-blake3-hasher" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a0801e25a1b31a14494fc80882a036be0ffd290efc4c2d640bfcca120a4672" +dependencies = [ + "blake3", + "solana-define-syscall", + "solana-hash", + "solana-sanitize", +] + +[[package]] +name = "solana-bn254" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4420f125118732833f36facf96a27e7b78314b2d642ba07fa9ffdacd8d79e243" +dependencies = [ + "ark-bn254", + "ark-ec", + "ark-ff", + "ark-serialize", + "bytemuck", + "solana-define-syscall", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-borsh" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "718333bcd0a1a7aed6655aa66bef8d7fb047944922b2d3a18f49cbc13e73d004" +dependencies = [ + "borsh 0.10.4", + "borsh 1.5.7", +] + +[[package]] +name = "solana-bpf-loader-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aec57dcd80d0f6879956cad28854a6eebaed6b346ce56908ea01a9f36ab259" +dependencies = [ + "bincode", + "libsecp256k1", + "num-traits", + "qualifier_attr", + "scopeguard", + "solana-account", + "solana-account-info", + "solana-big-mod-exp", + "solana-bincode", + "solana-blake3-hasher", + "solana-bn254", + "solana-clock", + "solana-cpi", + "solana-curve25519", + "solana-hash", + "solana-instruction", + "solana-keccak-hasher", + "solana-loader-v3-interface", + "solana-loader-v4-interface", + "solana-log-collector", + "solana-measure", + "solana-packet", + "solana-poseidon", + "solana-program-entrypoint", + "solana-program-runtime", + "solana-pubkey", + "solana-sbpf", + "solana-sdk-ids", + "solana-secp256k1-recover", + "solana-sha256-hasher", + "solana-stable-layout", + "solana-svm-feature-set", + "solana-system-interface", + "solana-sysvar", + "solana-sysvar-id", + "solana-timings", + "solana-transaction-context", + "solana-type-overrides", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-builtins" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d61a31b63b52b0d268cbcd56c76f50314867d7f8e07a0f2c62ee7c9886e07b2" +dependencies = [ + "agave-feature-set", + "solana-bpf-loader-program", + "solana-compute-budget-program", + "solana-hash", + "solana-loader-v4-program", + "solana-program-runtime", + "solana-pubkey", + "solana-sdk-ids", + "solana-stake-program", + "solana-system-program", + "solana-vote-program", + "solana-zk-elgamal-proof-program", + "solana-zk-token-proof-program", +] + +[[package]] +name = "solana-client-traits" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83f0071874e629f29e0eb3dab8a863e98502ac7aba55b7e0df1803fc5cac72a7" +dependencies = [ + "solana-account", + "solana-commitment-config", + "solana-epoch-info", + "solana-hash", + "solana-instruction", + "solana-keypair", + "solana-message", + "solana-pubkey", + "solana-signature", + "solana-signer", + "solana-system-interface", + "solana-transaction", + "solana-transaction-error", +] + +[[package]] +name = "solana-clock" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bb482ab70fced82ad3d7d3d87be33d466a3498eb8aa856434ff3c0dfc2e2e31" +dependencies = [ + "serde", + "serde_derive", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-sysvar-id", +] + +[[package]] +name = "solana-cluster-type" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ace9fea2daa28354d107ea879cff107181d85cd4e0f78a2bedb10e1a428c97e" +dependencies = [ + "serde", + "serde_derive", + "solana-hash", +] + +[[package]] +name = "solana-commitment-config" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac49c4dde3edfa832de1697e9bcdb7c3b3f7cb7a1981b7c62526c8bb6700fb73" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "solana-compute-budget" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f4fc63bc2276a1618ca0bfc609da7448534ecb43a1cb387cdf9eaa2dc7bc272" +dependencies = [ + "solana-fee-structure", + "solana-program-runtime", +] + +[[package]] +name = "solana-compute-budget-interface" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8432d2c4c22d0499aa06d62e4f7e333f81777b3d7c96050ae9e5cb71a8c3aee4" +dependencies = [ + "borsh 1.5.7", + "serde", + "serde_derive", + "solana-instruction", + "solana-sdk-ids", +] + +[[package]] +name = "solana-compute-budget-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "072b02beed1862c6b7b7a8a699379594c4470a9371c711856a0a3c266dcf57e5" +dependencies = [ + "solana-program-runtime", +] + +[[package]] +name = "solana-config-program-client" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53aceac36f105fd4922e29b4f0c1f785b69d7b3e7e387e384b8985c8e0c3595e" +dependencies = [ + "bincode", + "borsh 0.10.4", + "kaigan", + "serde", + "solana-program", +] + +[[package]] +name = "solana-cpi" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc71126edddc2ba014622fc32d0f5e2e78ec6c5a1e0eb511b85618c09e9ea11" +dependencies = [ + "solana-account-info", + "solana-define-syscall", + "solana-instruction", + "solana-program-error", + "solana-pubkey", + "solana-stable-layout", +] + +[[package]] +name = "solana-curve25519" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae4261b9a8613d10e77ac831a8fa60b6fa52b9b103df46d641deff9f9812a23" +dependencies = [ + "bytemuck", + "bytemuck_derive", + "curve25519-dalek 4.1.3", + "solana-define-syscall", + "subtle", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-decode-error" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c781686a18db2f942e70913f7ca15dc120ec38dcab42ff7557db2c70c625a35" +dependencies = [ + "num-traits", +] + +[[package]] +name = "solana-define-syscall" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ae3e2abcf541c8122eafe9a625d4d194b4023c20adde1e251f94e056bb1aee2" + +[[package]] +name = "solana-derivation-path" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "939756d798b25c5ec3cca10e06212bdca3b1443cb9bb740a38124f58b258737b" +dependencies = [ + "derivation-path", + "qstring", + "uriparse", +] + +[[package]] +name = "solana-ed25519-program" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1feafa1691ea3ae588f99056f4bdd1293212c7ece28243d7da257c443e84753" +dependencies = [ + "bytemuck", + "bytemuck_derive", + "ed25519-dalek", + "solana-feature-set", + "solana-instruction", + "solana-precompile-error", + "solana-sdk-ids", +] + +[[package]] +name = "solana-epoch-info" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ef6f0b449290b0b9f32973eefd95af35b01c5c0c34c569f936c34c5b20d77b" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "solana-epoch-rewards" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b575d3dd323b9ea10bb6fe89bf6bf93e249b215ba8ed7f68f1a3633f384db7" +dependencies = [ + "serde", + "serde_derive", + "solana-hash", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-sysvar-id", +] + +[[package]] +name = "solana-epoch-rewards-hasher" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c5fd2662ae7574810904585fd443545ed2b568dbd304b25a31e79ccc76e81b" +dependencies = [ + "siphasher", + "solana-hash", + "solana-pubkey", +] + +[[package]] +name = "solana-epoch-schedule" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fce071fbddecc55d727b1d7ed16a629afe4f6e4c217bc8d00af3b785f6f67ed" +dependencies = [ + "serde", + "serde_derive", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-sysvar-id", +] + +[[package]] +name = "solana-example-mocks" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84461d56cbb8bb8d539347151e0525b53910102e4bced875d49d5139708e39d3" +dependencies = [ + "serde", + "serde_derive", + "solana-address-lookup-table-interface", + "solana-clock", + "solana-hash", + "solana-instruction", + "solana-keccak-hasher", + "solana-message", + "solana-nonce", + "solana-pubkey", + "solana-sdk-ids", + "solana-system-interface", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-feature-gate-interface" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43f5c5382b449e8e4e3016fb05e418c53d57782d8b5c30aa372fc265654b956d" +dependencies = [ + "bincode", + "serde", + "serde_derive", + "solana-account", + "solana-account-info", + "solana-instruction", + "solana-program-error", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", + "solana-system-interface", +] + +[[package]] +name = "solana-feature-set" +version = "2.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93b93971e289d6425f88e6e3cb6668c4b05df78b3c518c249be55ced8efd6b6d" +dependencies = [ + "ahash", + "lazy_static", + "solana-epoch-schedule", + "solana-hash", + "solana-pubkey", + "solana-sha256-hasher", +] + +[[package]] +name = "solana-fee-calculator" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89bc408da0fb3812bc3008189d148b4d3e08252c79ad810b245482a3f70cd8d" +dependencies = [ + "log", + "serde", + "serde_derive", +] + +[[package]] +name = "solana-fee-structure" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33adf673581c38e810bf618f745bf31b683a0a4a4377682e6aaac5d9a058dd4e" +dependencies = [ + "serde", + "serde_derive", + "solana-message", + "solana-native-token", +] + +[[package]] +name = "solana-genesis-config" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3725085d47b96d37fef07a29d78d2787fc89a0b9004c66eed7753d1e554989f" +dependencies = [ + "bincode", + "chrono", + "memmap2", + "serde", + "serde_derive", + "solana-account", + "solana-clock", + "solana-cluster-type", + "solana-epoch-schedule", + "solana-fee-calculator", + "solana-hash", + "solana-inflation", + "solana-keypair", + "solana-logger", + "solana-poh-config", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", + "solana-sha256-hasher", + "solana-shred-version", + "solana-signer", + "solana-time-utils", +] + +[[package]] +name = "solana-hard-forks" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c28371f878e2ead55611d8ba1b5fb879847156d04edea13693700ad1a28baf" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "solana-hash" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b96e9f0300fa287b545613f007dfe20043d7812bee255f418c1eb649c93b63" +dependencies = [ + "borsh 1.5.7", + "bytemuck", + "bytemuck_derive", + "five8", + "js-sys", + "serde", + "serde_derive", + "solana-atomic-u64", + "solana-sanitize", + "wasm-bindgen", +] + +[[package]] +name = "solana-inflation" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23eef6a09eb8e568ce6839573e4966850e85e9ce71e6ae1a6c930c1c43947de3" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "solana-instruction" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab5682934bd1f65f8d2c16f21cb532526fcc1a09f796e2cacdb091eee5774ad" +dependencies = [ + "bincode", + "borsh 1.5.7", + "getrandom 0.2.16", + "js-sys", + "num-traits", + "serde", + "serde_derive", + "serde_json", + "solana-define-syscall", + "solana-pubkey", + "wasm-bindgen", +] + +[[package]] +name = "solana-instructions-sysvar" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0e85a6fad5c2d0c4f5b91d34b8ca47118fc593af706e523cdbedf846a954f57" +dependencies = [ + "bitflags 2.10.0", + "solana-account-info", + "solana-instruction", + "solana-program-error", + "solana-pubkey", + "solana-sanitize", + "solana-sdk-ids", + "solana-serialize-utils", + "solana-sysvar-id", +] + +[[package]] +name = "solana-keccak-hasher" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7aeb957fbd42a451b99235df4942d96db7ef678e8d5061ef34c9b34cae12f79" +dependencies = [ + "sha3", + "solana-define-syscall", + "solana-hash", + "solana-sanitize", +] + +[[package]] +name = "solana-keypair" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd3f04aa1a05c535e93e121a95f66e7dcccf57e007282e8255535d24bf1e98bb" +dependencies = [ + "ed25519-dalek", + "ed25519-dalek-bip32", + "five8", + "rand 0.7.3", + "solana-derivation-path", + "solana-pubkey", + "solana-seed-derivable", + "solana-seed-phrase", + "solana-signature", + "solana-signer", + "wasm-bindgen", +] + +[[package]] +name = "solana-last-restart-slot" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6360ac2fdc72e7463565cd256eedcf10d7ef0c28a1249d261ec168c1b55cdd" +dependencies = [ + "serde", + "serde_derive", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-sysvar-id", +] + +[[package]] +name = "solana-loader-v2-interface" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8ab08006dad78ae7cd30df8eea0539e207d08d91eaefb3e1d49a446e1c49654" +dependencies = [ + "serde", + "serde_bytes", + "serde_derive", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", +] + +[[package]] +name = "solana-loader-v3-interface" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f7162a05b8b0773156b443bccd674ea78bb9aa406325b467ea78c06c99a63a2" +dependencies = [ + "serde", + "serde_bytes", + "serde_derive", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", + "solana-system-interface", +] + +[[package]] +name = "solana-loader-v4-interface" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "706a777242f1f39a83e2a96a2a6cb034cb41169c6ecbee2cf09cb873d9659e7e" +dependencies = [ + "serde", + "serde_bytes", + "serde_derive", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", + "solana-system-interface", +] + +[[package]] +name = "solana-loader-v4-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ab01855d851fa2fb6034b0d48de33d77d5c5f5fb4b0353d8e4a934cc03d48a" +dependencies = [ + "log", + "qualifier_attr", + "solana-account", + "solana-bincode", + "solana-bpf-loader-program", + "solana-instruction", + "solana-loader-v3-interface", + "solana-loader-v4-interface", + "solana-log-collector", + "solana-measure", + "solana-packet", + "solana-program-runtime", + "solana-pubkey", + "solana-sbpf", + "solana-sdk-ids", + "solana-transaction-context", + "solana-type-overrides", +] + +[[package]] +name = "solana-log-collector" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d945b1cf5bf7cbd6f5b78795beda7376370c827640df43bb2a1c17b492dc106" +dependencies = [ + "log", +] + +[[package]] +name = "solana-logger" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8e777ec1afd733939b532a42492d888ec7c88d8b4127a5d867eb45c6eb5cd5" +dependencies = [ + "env_logger", + "lazy_static", + "libc", + "log", + "signal-hook", +] + +[[package]] +name = "solana-measure" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11dcd67cd2ae6065e494b64e861e0498d046d95a61cbbf1ae3d58be1ea0f42ed" + +[[package]] +name = "solana-message" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1796aabce376ff74bf89b78d268fa5e683d7d7a96a0a4e4813ec34de49d5314b" +dependencies = [ + "bincode", + "blake3", + "lazy_static", + "serde", + "serde_derive", + "solana-bincode", + "solana-hash", + "solana-instruction", + "solana-pubkey", + "solana-sanitize", + "solana-sdk-ids", + "solana-short-vec", + "solana-system-interface", + "solana-transaction-error", + "wasm-bindgen", +] + +[[package]] +name = "solana-metrics" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0375159d8460f423d39e5103dcff6e07796a5ec1850ee1fcfacfd2482a8f34b5" +dependencies = [ + "crossbeam-channel", + "gethostname", + "log", + "reqwest 0.12.24", + "solana-cluster-type", + "solana-sha256-hasher", + "solana-time-utils", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-msg" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36a1a14399afaabc2781a1db09cb14ee4cc4ee5c7a5a3cfcc601811379a8092" +dependencies = [ + "solana-define-syscall", +] + +[[package]] +name = "solana-native-token" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61515b880c36974053dd499c0510066783f0cc6ac17def0c7ef2a244874cf4a9" + +[[package]] +name = "solana-nonce" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703e22eb185537e06204a5bd9d509b948f0066f2d1d814a6f475dafb3ddf1325" +dependencies = [ + "serde", + "serde_derive", + "solana-fee-calculator", + "solana-hash", + "solana-pubkey", + "solana-sha256-hasher", +] + +[[package]] +name = "solana-nonce-account" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cde971a20b8dbf60144d6a84439dda86b5466e00e2843091fe731083cda614da" +dependencies = [ + "solana-account", + "solana-hash", + "solana-nonce", + "solana-sdk-ids", +] + +[[package]] +name = "solana-offchain-message" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b526398ade5dea37f1f147ce55dae49aa017a5d7326606359b0445ca8d946581" +dependencies = [ + "num_enum", + "solana-hash", + "solana-packet", + "solana-pubkey", + "solana-sanitize", + "solana-sha256-hasher", + "solana-signature", + "solana-signer", +] + +[[package]] +name = "solana-packet" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "004f2d2daf407b3ec1a1ca5ec34b3ccdfd6866dd2d3c7d0715004a96e4b6d127" +dependencies = [ + "bincode", + "bitflags 2.10.0", + "cfg_eval", + "serde", + "serde_derive", + "serde_with", +] + +[[package]] +name = "solana-poh-config" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d650c3b4b9060082ac6b0efbbb66865089c58405bfb45de449f3f2b91eccee75" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "solana-poseidon" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbac4eb90016eeb1d37fa36e592d3a64421510c49666f81020736611c319faff" +dependencies = [ + "ark-bn254", + "light-poseidon", + "solana-define-syscall", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-precompile-error" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d87b2c1f5de77dfe2b175ee8dd318d196aaca4d0f66f02842f80c852811f9f8" +dependencies = [ + "num-traits", + "solana-decode-error", +] + +[[package]] +name = "solana-precompiles" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36e92768a57c652edb0f5d1b30a7d0bc64192139c517967c18600debe9ae3832" +dependencies = [ + "lazy_static", + "solana-ed25519-program", + "solana-feature-set", + "solana-message", + "solana-precompile-error", + "solana-pubkey", + "solana-sdk-ids", + "solana-secp256k1-program", + "solana-secp256r1-program", +] + +[[package]] +name = "solana-presigner" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81a57a24e6a4125fc69510b6774cd93402b943191b6cddad05de7281491c90fe" +dependencies = [ + "solana-pubkey", + "solana-signature", + "solana-signer", +] + +[[package]] +name = "solana-program" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98eca145bd3545e2fbb07166e895370576e47a00a7d824e325390d33bf467210" +dependencies = [ + "bincode", + "blake3", + "borsh 0.10.4", + "borsh 1.5.7", + "bs58", + "bytemuck", + "console_error_panic_hook", + "console_log", + "getrandom 0.2.16", + "lazy_static", + "log", + "memoffset", + "num-bigint 0.4.6", + "num-derive", + "num-traits", + "rand 0.8.5", + "serde", + "serde_bytes", + "serde_derive", + "solana-account-info", + "solana-address-lookup-table-interface", + "solana-atomic-u64", + "solana-big-mod-exp", + "solana-bincode", + "solana-blake3-hasher", + "solana-borsh", + "solana-clock", + "solana-cpi", + "solana-decode-error", + "solana-define-syscall", + "solana-epoch-rewards", + "solana-epoch-schedule", + "solana-example-mocks", + "solana-feature-gate-interface", + "solana-fee-calculator", + "solana-hash", + "solana-instruction", + "solana-instructions-sysvar", + "solana-keccak-hasher", + "solana-last-restart-slot", + "solana-loader-v2-interface", + "solana-loader-v3-interface", + "solana-loader-v4-interface", + "solana-message", + "solana-msg", + "solana-native-token", + "solana-nonce", + "solana-program-entrypoint", + "solana-program-error", + "solana-program-memory", + "solana-program-option", + "solana-program-pack", + "solana-pubkey", + "solana-rent", + "solana-sanitize", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-secp256k1-recover", + "solana-serde-varint", + "solana-serialize-utils", + "solana-sha256-hasher", + "solana-short-vec", + "solana-slot-hashes", + "solana-slot-history", + "solana-stable-layout", + "solana-stake-interface", + "solana-system-interface", + "solana-sysvar", + "solana-sysvar-id", + "solana-vote-interface", + "thiserror 2.0.17", + "wasm-bindgen", +] + +[[package]] +name = "solana-program-entrypoint" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32ce041b1a0ed275290a5008ee1a4a6c48f5054c8a3d78d313c08958a06aedbd" +dependencies = [ + "solana-account-info", + "solana-msg", + "solana-program-error", + "solana-pubkey", +] + +[[package]] +name = "solana-program-error" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ee2e0217d642e2ea4bee237f37bd61bb02aec60da3647c48ff88f6556ade775" +dependencies = [ + "borsh 1.5.7", + "num-traits", + "serde", + "serde_derive", + "solana-decode-error", + "solana-instruction", + "solana-msg", + "solana-pubkey", +] + +[[package]] +name = "solana-program-memory" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a5426090c6f3fd6cfdc10685322fede9ca8e5af43cd6a59e98bfe4e91671712" +dependencies = [ + "solana-define-syscall", +] + +[[package]] +name = "solana-program-option" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc677a2e9bc616eda6dbdab834d463372b92848b2bfe4a1ed4e4b4adba3397d0" + +[[package]] +name = "solana-program-pack" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "319f0ef15e6e12dc37c597faccb7d62525a509fec5f6975ecb9419efddeb277b" +dependencies = [ + "solana-program-error", +] + +[[package]] +name = "solana-program-runtime" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5653001e07b657c9de6f0417cf9add1cf4325903732c480d415655e10cc86704" +dependencies = [ + "base64 0.22.1", + "bincode", + "enum-iterator", + "itertools 0.12.1", + "log", + "percentage", + "rand 0.8.5", + "serde", + "solana-account", + "solana-clock", + "solana-epoch-rewards", + "solana-epoch-schedule", + "solana-fee-structure", + "solana-hash", + "solana-instruction", + "solana-last-restart-slot", + "solana-log-collector", + "solana-measure", + "solana-metrics", + "solana-program-entrypoint", + "solana-pubkey", + "solana-rent", + "solana-sbpf", + "solana-sdk-ids", + "solana-slot-hashes", + "solana-stable-layout", + "solana-svm-callback", + "solana-svm-feature-set", + "solana-system-interface", + "solana-sysvar", + "solana-sysvar-id", + "solana-timings", + "solana-transaction-context", + "solana-type-overrides", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-pubkey" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b62adb9c3261a052ca1f999398c388f1daf558a1b492f60a6d9e64857db4ff1" +dependencies = [ + "borsh 0.10.4", + "borsh 1.5.7", + "bytemuck", + "bytemuck_derive", + "curve25519-dalek 4.1.3", + "five8", + "five8_const", + "getrandom 0.2.16", + "js-sys", + "num-traits", + "rand 0.8.5", + "serde", + "serde_derive", + "solana-atomic-u64", + "solana-decode-error", + "solana-define-syscall", + "solana-sanitize", + "solana-sha256-hasher", + "wasm-bindgen", +] + +[[package]] +name = "solana-quic-definitions" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf0d4d5b049eb1d0c35f7b18f305a27c8986fc5c0c9b383e97adaa35334379e" +dependencies = [ + "solana-keypair", +] + +[[package]] +name = "solana-rent" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1aea8fdea9de98ca6e8c2da5827707fb3842833521b528a713810ca685d2480" +dependencies = [ + "serde", + "serde_derive", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-sysvar-id", +] + +[[package]] +name = "solana-rent-collector" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "127e6dfa51e8c8ae3aa646d8b2672bc4ac901972a338a9e1cd249e030564fb9d" +dependencies = [ + "serde", + "serde_derive", + "solana-account", + "solana-clock", + "solana-epoch-schedule", + "solana-genesis-config", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", +] + +[[package]] +name = "solana-rent-debits" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f6f9113c6003492e74438d1288e30cffa8ccfdc2ef7b49b9e816d8034da18cd" +dependencies = [ + "solana-pubkey", + "solana-reward-info", +] + +[[package]] +name = "solana-reserved-account-keys" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4b22ea19ca2a3f28af7cd047c914abf833486bf7a7c4a10fc652fff09b385b1" +dependencies = [ + "lazy_static", + "solana-feature-set", + "solana-pubkey", + "solana-sdk-ids", +] + +[[package]] +name = "solana-reward-info" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18205b69139b1ae0ab8f6e11cdcb627328c0814422ad2482000fa2ca54ae4a2f" +dependencies = [ + "serde", + "serde_derive", +] + +[[package]] +name = "solana-sanitize" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61f1bc1357b8188d9c4a3af3fc55276e56987265eb7ad073ae6f8180ee54cecf" + +[[package]] +name = "solana-sbpf" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "474a2d95dc819898ded08d24f29642d02189d3e1497bbb442a92a3997b7eb55f" +dependencies = [ + "byteorder", + "combine", + "hash32", + "libc", + "log", + "rand 0.8.5", + "rustc-demangle", + "thiserror 2.0.17", + "winapi", +] + +[[package]] +name = "solana-sdk" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cc0e4a7635b902791c44b6581bfb82f3ada32c5bc0929a64f39fe4bb384c86a" +dependencies = [ + "bincode", + "bs58", + "getrandom 0.1.16", + "js-sys", + "serde", + "serde_json", + "solana-account", + "solana-bn254", + "solana-client-traits", + "solana-cluster-type", + "solana-commitment-config", + "solana-compute-budget-interface", + "solana-decode-error", + "solana-derivation-path", + "solana-ed25519-program", + "solana-epoch-info", + "solana-epoch-rewards-hasher", + "solana-feature-set", + "solana-fee-structure", + "solana-genesis-config", + "solana-hard-forks", + "solana-inflation", + "solana-instruction", + "solana-keypair", + "solana-message", + "solana-native-token", + "solana-nonce-account", + "solana-offchain-message", + "solana-packet", + "solana-poh-config", + "solana-precompile-error", + "solana-precompiles", + "solana-presigner", + "solana-program", + "solana-program-memory", + "solana-pubkey", + "solana-quic-definitions", + "solana-rent-collector", + "solana-rent-debits", + "solana-reserved-account-keys", + "solana-reward-info", + "solana-sanitize", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-secp256k1-program", + "solana-secp256k1-recover", + "solana-secp256r1-program", + "solana-seed-derivable", + "solana-seed-phrase", + "solana-serde", + "solana-serde-varint", + "solana-short-vec", + "solana-shred-version", + "solana-signature", + "solana-signer", + "solana-system-transaction", + "solana-time-utils", + "solana-transaction", + "solana-transaction-context", + "solana-transaction-error", + "solana-validator-exit", + "thiserror 2.0.17", + "wasm-bindgen", +] + +[[package]] +name = "solana-sdk-ids" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5d8b9cc68d5c88b062a33e23a6466722467dde0035152d8fb1afbcdf350a5f" +dependencies = [ + "solana-pubkey", +] + +[[package]] +name = "solana-sdk-macro" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86280da8b99d03560f6ab5aca9de2e38805681df34e0bb8f238e69b29433b9df" +dependencies = [ + "bs58", + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "solana-secp256k1-program" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f19833e4bc21558fe9ec61f239553abe7d05224347b57d65c2218aeeb82d6149" +dependencies = [ + "bincode", + "digest 0.10.7", + "libsecp256k1", + "serde", + "serde_derive", + "sha3", + "solana-feature-set", + "solana-instruction", + "solana-precompile-error", + "solana-sdk-ids", + "solana-signature", +] + +[[package]] +name = "solana-secp256k1-recover" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa3120b6cdaa270f39444f5093a90a7b03d296d362878f7a6991d6de3bbe496" +dependencies = [ + "borsh 1.5.7", + "libsecp256k1", + "solana-define-syscall", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-secp256r1-program" +version = "2.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce0ae46da3071a900f02d367d99b2f3058fe2e90c5062ac50c4f20cfedad8f0f" +dependencies = [ + "bytemuck", + "openssl", + "solana-feature-set", + "solana-instruction", + "solana-precompile-error", + "solana-sdk-ids", +] + +[[package]] +name = "solana-seed-derivable" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beb82b5adb266c6ea90e5cf3967235644848eac476c5a1f2f9283a143b7c97f" +dependencies = [ + "solana-derivation-path", +] + +[[package]] +name = "solana-seed-phrase" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36187af2324f079f65a675ec22b31c24919cb4ac22c79472e85d819db9bbbc15" +dependencies = [ + "hmac 0.12.1", + "pbkdf2", + "sha2 0.10.9", +] + +[[package]] +name = "solana-serde" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1931484a408af466e14171556a47adaa215953c7f48b24e5f6b0282763818b04" +dependencies = [ + "serde", +] + +[[package]] +name = "solana-serde-varint" +version = "2.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a7e155eba458ecfb0107b98236088c3764a09ddf0201ec29e52a0be40857113" +dependencies = [ + "serde", +] + +[[package]] +name = "solana-serialize-utils" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "817a284b63197d2b27afdba829c5ab34231da4a9b4e763466a003c40ca4f535e" +dependencies = [ + "solana-instruction", + "solana-pubkey", + "solana-sanitize", +] + +[[package]] +name = "solana-sha256-hasher" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa3feb32c28765f6aa1ce8f3feac30936f16c5c3f7eb73d63a5b8f6f8ecdc44" +dependencies = [ + "sha2 0.10.9", + "solana-define-syscall", + "solana-hash", +] + +[[package]] +name = "solana-short-vec" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c54c66f19b9766a56fa0057d060de8378676cb64987533fa088861858fc5a69" +dependencies = [ + "serde", +] + +[[package]] +name = "solana-shred-version" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afd3db0461089d1ad1a78d9ba3f15b563899ca2386351d38428faa5350c60a98" +dependencies = [ + "solana-hard-forks", + "solana-hash", + "solana-sha256-hasher", +] + +[[package]] +name = "solana-signature" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64c8ec8e657aecfc187522fc67495142c12f35e55ddeca8698edbb738b8dbd8c" +dependencies = [ + "ed25519-dalek", + "five8", + "rand 0.8.5", + "serde", + "serde-big-array", + "serde_derive", + "solana-sanitize", +] + +[[package]] +name = "solana-signer" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c41991508a4b02f021c1342ba00bcfa098630b213726ceadc7cb032e051975b" +dependencies = [ + "solana-pubkey", + "solana-signature", + "solana-transaction-error", +] + +[[package]] +name = "solana-slot-hashes" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c8691982114513763e88d04094c9caa0376b867a29577939011331134c301ce" +dependencies = [ + "serde", + "serde_derive", + "solana-hash", + "solana-sdk-ids", + "solana-sysvar-id", +] + +[[package]] +name = "solana-slot-history" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97ccc1b2067ca22754d5283afb2b0126d61eae734fc616d23871b0943b0d935e" +dependencies = [ + "bv", + "serde", + "serde_derive", + "solana-sdk-ids", + "solana-sysvar-id", +] + +[[package]] +name = "solana-stable-layout" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f14f7d02af8f2bc1b5efeeae71bc1c2b7f0f65cd75bcc7d8180f2c762a57f54" +dependencies = [ + "solana-instruction", + "solana-pubkey", +] + +[[package]] +name = "solana-stake-interface" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5269e89fde216b4d7e1d1739cf5303f8398a1ff372a81232abbee80e554a838c" +dependencies = [ + "borsh 0.10.4", + "borsh 1.5.7", + "num-traits", + "serde", + "serde_derive", + "solana-clock", + "solana-cpi", + "solana-decode-error", + "solana-instruction", + "solana-program-error", + "solana-pubkey", + "solana-system-interface", + "solana-sysvar-id", +] + +[[package]] +name = "solana-stake-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500e9b9d11573f12de91e94f9c4459882cd5ffc692776af49b610d6fcc0b167f" +dependencies = [ + "agave-feature-set", + "bincode", + "log", + "solana-account", + "solana-bincode", + "solana-clock", + "solana-config-program-client", + "solana-genesis-config", + "solana-instruction", + "solana-log-collector", + "solana-native-token", + "solana-packet", + "solana-program-runtime", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", + "solana-stake-interface", + "solana-sysvar", + "solana-transaction-context", + "solana-type-overrides", + "solana-vote-interface", +] + +[[package]] +name = "solana-svm" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "006180b920e8d8c1dab4f6a0fda248b5b97d912eda4c872534d178bc31231bec" +dependencies = [ + "ahash", + "log", + "percentage", + "serde", + "serde_derive", + "solana-account", + "solana-clock", + "solana-fee-structure", + "solana-hash", + "solana-instruction", + "solana-instructions-sysvar", + "solana-loader-v3-interface", + "solana-loader-v4-interface", + "solana-loader-v4-program", + "solana-log-collector", + "solana-measure", + "solana-message", + "solana-nonce", + "solana-nonce-account", + "solana-program-entrypoint", + "solana-program-pack", + "solana-program-runtime", + "solana-pubkey", + "solana-rent", + "solana-rent-collector", + "solana-rent-debits", + "solana-sdk-ids", + "solana-svm-callback", + "solana-svm-feature-set", + "solana-svm-rent-collector", + "solana-svm-transaction", + "solana-system-interface", + "solana-sysvar-id", + "solana-timings", + "solana-transaction-context", + "solana-transaction-error", + "solana-type-overrides", + "spl-generic-token", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-svm-callback" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cef9f7d5cfb5d375081a6c8ad712a6f0e055a15890081f845acf55d8254a7a2" +dependencies = [ + "solana-account", + "solana-precompile-error", + "solana-pubkey", +] + +[[package]] +name = "solana-svm-feature-set" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f24b836eb4d74ec255217bdbe0f24f64a07adeac31aca61f334f91cd4a3b1d5" + +[[package]] +name = "solana-svm-rent-collector" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "030200d7f3ce4879f9d8c980ceb9e1d5e9a302866db035776496069b20c427b4" +dependencies = [ + "solana-account", + "solana-clock", + "solana-pubkey", + "solana-rent", + "solana-rent-collector", + "solana-sdk-ids", + "solana-transaction-context", + "solana-transaction-error", +] + +[[package]] +name = "solana-svm-transaction" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab717b9539375ebb088872c6c87d1d8832d19f30f154ecc530154d23f60a6f0c" +dependencies = [ + "solana-hash", + "solana-message", + "solana-pubkey", + "solana-sdk-ids", + "solana-signature", + "solana-transaction", +] + +[[package]] +name = "solana-system-interface" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94d7c18cb1a91c6be5f5a8ac9276a1d7c737e39a21beba9ea710ab4b9c63bc90" +dependencies = [ + "js-sys", + "num-traits", + "serde", + "serde_derive", + "solana-decode-error", + "solana-instruction", + "solana-pubkey", + "wasm-bindgen", +] + +[[package]] +name = "solana-system-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23ca36cef39aea7761be58d4108a56a2e27042fb1e913355fdb142a05fc7eab7" +dependencies = [ + "bincode", + "log", + "serde", + "serde_derive", + "solana-account", + "solana-bincode", + "solana-fee-calculator", + "solana-instruction", + "solana-log-collector", + "solana-nonce", + "solana-nonce-account", + "solana-packet", + "solana-program-runtime", + "solana-pubkey", + "solana-sdk-ids", + "solana-system-interface", + "solana-sysvar", + "solana-transaction-context", + "solana-type-overrides", +] + +[[package]] +name = "solana-system-transaction" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd98a25e5bcba8b6be8bcbb7b84b24c2a6a8178d7fb0e3077a916855ceba91a" +dependencies = [ + "solana-hash", + "solana-keypair", + "solana-message", + "solana-pubkey", + "solana-signer", + "solana-system-interface", + "solana-transaction", +] + +[[package]] +name = "solana-sysvar" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8c3595f95069f3d90f275bb9bd235a1973c4d059028b0a7f81baca2703815db" +dependencies = [ + "base64 0.22.1", + "bincode", + "bytemuck", + "bytemuck_derive", + "lazy_static", + "serde", + "serde_derive", + "solana-account-info", + "solana-clock", + "solana-define-syscall", + "solana-epoch-rewards", + "solana-epoch-schedule", + "solana-fee-calculator", + "solana-hash", + "solana-instruction", + "solana-instructions-sysvar", + "solana-last-restart-slot", + "solana-program-entrypoint", + "solana-program-error", + "solana-program-memory", + "solana-pubkey", + "solana-rent", + "solana-sanitize", + "solana-sdk-ids", + "solana-sdk-macro", + "solana-slot-hashes", + "solana-slot-history", + "solana-stake-interface", + "solana-sysvar-id", +] + +[[package]] +name = "solana-sysvar-id" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5762b273d3325b047cfda250787f8d796d781746860d5d0a746ee29f3e8812c1" +dependencies = [ + "solana-pubkey", + "solana-sdk-ids", +] + +[[package]] +name = "solana-time-utils" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af261afb0e8c39252a04d026e3ea9c405342b08c871a2ad8aa5448e068c784c" + +[[package]] +name = "solana-timings" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c49b842dfc53c1bf9007eaa6730296dea93b4fce73f457ce1080af43375c0d6" +dependencies = [ + "eager", + "enum-iterator", + "solana-pubkey", +] + +[[package]] +name = "solana-transaction" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80657d6088f721148f5d889c828ca60c7daeedac9a8679f9ec215e0c42bcbf41" +dependencies = [ + "bincode", + "serde", + "serde_derive", + "solana-bincode", + "solana-feature-set", + "solana-hash", + "solana-instruction", + "solana-keypair", + "solana-message", + "solana-precompiles", + "solana-pubkey", + "solana-sanitize", + "solana-sdk-ids", + "solana-short-vec", + "solana-signature", + "solana-signer", + "solana-system-interface", + "solana-transaction-error", + "wasm-bindgen", +] + +[[package]] +name = "solana-transaction-context" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a312304361987a85b2ef2293920558e6612876a639dd1309daf6d0d59ef2fe" +dependencies = [ + "bincode", + "serde", + "serde_derive", + "solana-account", + "solana-instruction", + "solana-instructions-sysvar", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", +] + +[[package]] +name = "solana-transaction-error" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a9dc8fdb61c6088baab34fc3a8b8473a03a7a5fd404ed8dd502fa79b67cb1" +dependencies = [ + "serde", + "serde_derive", + "solana-instruction", + "solana-sanitize", +] + +[[package]] +name = "solana-type-overrides" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d80c44761eb398a157d809a04840865c347e1831ae3859b6100c0ee457bc1a" +dependencies = [ + "rand 0.8.5", +] + +[[package]] +name = "solana-validator-exit" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bbf6d7a3c0b28dd5335c52c0e9eae49d0ae489a8f324917faf0ded65a812c1d" + +[[package]] +name = "solana-vote-interface" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b80d57478d6599d30acc31cc5ae7f93ec2361a06aefe8ea79bc81739a08af4c3" +dependencies = [ + "bincode", + "num-derive", + "num-traits", + "serde", + "serde_derive", + "solana-clock", + "solana-decode-error", + "solana-hash", + "solana-instruction", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", + "solana-serde-varint", + "solana-serialize-utils", + "solana-short-vec", + "solana-system-interface", +] + +[[package]] +name = "solana-vote-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "908d0e72c8b83e48762eb3e8c9114497cf4b1d66e506e360c46aba9308e71299" +dependencies = [ + "agave-feature-set", + "bincode", + "log", + "num-derive", + "num-traits", + "serde", + "serde_derive", + "solana-account", + "solana-bincode", + "solana-clock", + "solana-epoch-schedule", + "solana-hash", + "solana-instruction", + "solana-keypair", + "solana-packet", + "solana-program-runtime", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", + "solana-signer", + "solana-slot-hashes", + "solana-transaction", + "solana-transaction-context", + "solana-vote-interface", + "thiserror 2.0.17", +] + +[[package]] +name = "solana-zk-elgamal-proof-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70cea14481d8efede6b115a2581f27bc7c6fdfba0752c20398456c3ac1245fc4" +dependencies = [ + "agave-feature-set", + "bytemuck", + "num-derive", + "num-traits", + "solana-instruction", + "solana-log-collector", + "solana-program-runtime", + "solana-sdk-ids", + "solana-zk-sdk", +] + +[[package]] +name = "solana-zk-sdk" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b9fc6ec37d16d0dccff708ed1dd6ea9ba61796700c3bb7c3b401973f10f63b" +dependencies = [ + "aes-gcm-siv", + "base64 0.22.1", + "bincode", + "bytemuck", + "bytemuck_derive", + "curve25519-dalek 4.1.3", + "itertools 0.12.1", + "js-sys", + "merlin", + "num-derive", + "num-traits", + "rand 0.8.5", + "serde", + "serde_derive", + "serde_json", + "sha3", + "solana-derivation-path", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", + "solana-seed-derivable", + "solana-seed-phrase", + "solana-signature", + "solana-signer", + "subtle", + "thiserror 2.0.17", + "wasm-bindgen", + "zeroize", +] + +[[package]] +name = "solana-zk-token-proof-program" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579752ad6ea2a671995f13c763bf28288c3c895cb857a518cc4ebab93c9a8dde" +dependencies = [ + "agave-feature-set", + "bytemuck", + "num-derive", + "num-traits", + "solana-instruction", + "solana-log-collector", + "solana-program-runtime", + "solana-sdk-ids", + "solana-zk-token-sdk", +] + +[[package]] +name = "solana-zk-token-sdk" +version = "2.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5055e5df94abd5badf4f947681c893375bdb6f8f543c05d2a7ab9647a6a9d205" +dependencies = [ + "aes-gcm-siv", + "base64 0.22.1", + "bincode", + "bytemuck", + "bytemuck_derive", + "curve25519-dalek 4.1.3", + "itertools 0.12.1", + "merlin", + "num-derive", + "num-traits", + "rand 0.8.5", + "serde", + "serde_derive", + "serde_json", + "sha3", + "solana-curve25519", + "solana-derivation-path", + "solana-instruction", + "solana-pubkey", + "solana-sdk-ids", + "solana-seed-derivable", + "solana-seed-phrase", + "solana-signature", + "solana-signer", + "subtle", + "thiserror 2.0.17", + "zeroize", +] + +[[package]] +name = "spl-associated-token-account-interface" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6bbe0794e532ac08428d3abf5bf8ae75bd81dfddd785c388e326c00c92c6f5" +dependencies = [ + "solana-instruction", + "solana-pubkey", +] + +[[package]] +name = "spl-discriminator" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7398da23554a31660f17718164e31d31900956054f54f52d5ec1be51cb4f4b3" +dependencies = [ + "bytemuck", + "solana-program-error", + "solana-sha256-hasher", + "spl-discriminator-derive", +] + +[[package]] +name = "spl-discriminator-derive" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9e8418ea6269dcfb01c712f0444d2c75542c04448b480e87de59d2865edc750" +dependencies = [ + "quote", + "spl-discriminator-syn", + "syn 2.0.110", +] + +[[package]] +name = "spl-discriminator-syn" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d1dbc82ab91422345b6df40a79e2b78c7bce1ebb366da323572dd60b7076b67" +dependencies = [ + "proc-macro2", + "quote", + "sha2 0.10.9", + "syn 2.0.110", + "thiserror 1.0.69", +] + +[[package]] +name = "spl-generic-token" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "741a62a566d97c58d33f9ed32337ceedd4e35109a686e31b1866c5dfa56abddc" +dependencies = [ + "bytemuck", + "solana-pubkey", +] + +[[package]] +name = "spl-pod" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d994afaf86b779104b4a95ba9ca75b8ced3fdb17ee934e38cb69e72afbe17799" +dependencies = [ + "borsh 1.5.7", + "bytemuck", + "bytemuck_derive", + "num-derive", + "num-traits", + "solana-decode-error", + "solana-msg", + "solana-program-error", + "solana-program-option", + "solana-pubkey", + "solana-zk-sdk", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-token-2022-interface" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62d7ae2ee6b856f8ddcbdc3b3a9f4d2141582bbe150f93e5298ee97e0251fa04" +dependencies = [ + "arrayref", + "bytemuck", + "num-derive", + "num-traits", + "num_enum", + "solana-account-info", + "solana-decode-error", + "solana-instruction", + "solana-msg", + "solana-program-error", + "solana-program-option", + "solana-program-pack", + "solana-pubkey", + "solana-sdk-ids", + "solana-zk-sdk", + "spl-pod", + "spl-token-confidential-transfer-proof-extraction", + "spl-token-confidential-transfer-proof-generation", + "spl-token-group-interface", + "spl-token-metadata-interface", + "spl-type-length-value", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-token-confidential-transfer-proof-extraction" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512c85bdbbb4cbcc2038849a9e164c958b16541f252b53ea1a3933191c0a4a1a" +dependencies = [ + "bytemuck", + "solana-account-info", + "solana-curve25519", + "solana-instruction", + "solana-instructions-sysvar", + "solana-msg", + "solana-program-error", + "solana-pubkey", + "solana-sdk-ids", + "solana-zk-sdk", + "spl-pod", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-token-confidential-transfer-proof-generation" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa27b9174bea869a7ebf31e0be6890bce90b1a4288bc2bbf24bd413f80ae3fde" +dependencies = [ + "curve25519-dalek 4.1.3", + "solana-zk-sdk", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-token-group-interface" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5597b4cd76f85ce7cd206045b7dc22da8c25516573d42d267c8d1fd128db5129" +dependencies = [ + "bytemuck", + "num-derive", + "num-traits", + "solana-decode-error", + "solana-instruction", + "solana-msg", + "solana-program-error", + "solana-pubkey", + "spl-discriminator", + "spl-pod", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-token-interface" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06e0c2d4e38ef5834cf7fb1b592b8a8c6eab8485f5ac7a04a151b502c63a0aaa" +dependencies = [ + "arrayref", + "bytemuck", + "num-derive", + "num-traits", + "num_enum", + "solana-instruction", + "solana-program-error", + "solana-program-option", + "solana-program-pack", + "solana-pubkey", + "solana-sdk-ids", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-token-metadata-interface" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "304d6e06f0de0c13a621464b1fd5d4b1bebf60d15ca71a44d3839958e0da16ee" +dependencies = [ + "borsh 1.5.7", + "num-derive", + "num-traits", + "solana-borsh", + "solana-decode-error", + "solana-instruction", + "solana-msg", + "solana-program-error", + "solana-pubkey", + "spl-discriminator", + "spl-pod", + "spl-type-length-value", + "thiserror 2.0.17", +] + +[[package]] +name = "spl-type-length-value" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d417eb548214fa822d93f84444024b4e57c13ed6719d4dcc68eec24fb481e9f5" +dependencies = [ + "bytemuck", + "num-derive", + "num-traits", + "solana-account-info", + "solana-decode-error", + "solana-msg", + "solana-program-error", + "spl-discriminator", + "spl-pod", + "thiserror 2.0.17", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.110" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a99801b5bd34ede4cf3fc688c5919368fea4e4814a4664359503e6015b280aea" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "tempfile" +version = "3.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" +dependencies = [ + "fastrand", + "getrandom 0.3.4", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" +dependencies = [ + "thiserror-impl 2.0.17", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2 0.6.1", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2efa149fe76073d6e8fd97ef4f4eca7b67f599660115591483572e406e165594" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime 0.6.11", + "toml_edit 0.22.27", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_datetime" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2cdb639ebbc97961c51720f858597f7f24c4fc295327923af55b74c3c724533" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime 0.6.11", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_edit" +version = "0.23.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6485ef6d0d9b5d0ec17244ff7eb05310113c3f316f2d14200d4de56b3cb98f8d" +dependencies = [ + "indexmap", + "toml_datetime 0.7.3", + "toml_parser", + "winnow", +] + +[[package]] +name = "toml_parser" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0cbe268d35bdb4bb5a56a2de88d0ad0eb70af5384a99d648cd4b3d04039800e" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + +[[package]] +name = "tower" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 1.0.2", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2" +dependencies = [ + "bitflags 2.10.0", + "bytes", + "futures-util", + "http 1.3.1", + "http-body 1.0.1", + "iri-string", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d12581f227e93f094d3af2ae690a574abb8a2b9b7a96e7cfe9647b2b617678" +dependencies = [ + "once_cell", +] + +[[package]] +name = "trident-config" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add342a68fb3779b2d22b14c41a934b1470d5169405817abd1ab9c5c0721d057" +dependencies = [ + "anyhow", + "base64 0.22.1", + "fehler", + "rand 0.8.5", + "serde", + "serde_json", + "solana-sdk", + "thiserror 1.0.69", + "toml 0.8.23", +] + +[[package]] +name = "trident-derive-flow-executor" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ab0355c2ec30af4dd0582f28ce6c590610809cf78daff89ef232fabf1daf67a" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", + "trident-syn", +] + +[[package]] +name = "trident-derive-fuzz-test-methods" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b11aa682532d66e3cbfd11e3557a786a4a7e6a9094ba2cdb33e02077de639ef8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", + "trident-syn", +] + +[[package]] +name = "trident-fuzz" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee34444691415bb833b0d04afde774cade7175763a8ee2a315ae878547a1ee8" +dependencies = [ + "borsh 1.5.7", + "getrandom 0.3.4", + "hex", + "indicatif", + "itertools 0.14.0", + "rand 0.8.5", + "reqwest 0.11.27", + "serde", + "sha2 0.10.9", + "solana-sdk", + "spl-associated-token-account-interface", + "spl-pod", + "spl-token-2022-interface", + "spl-token-group-interface", + "spl-token-interface", + "spl-token-metadata-interface", + "thiserror 1.0.69", + "tokio", + "trident-config", + "trident-derive-flow-executor", + "trident-derive-fuzz-test-methods", + "trident-fuzz-metrics", + "trident-svm", +] + +[[package]] +name = "trident-fuzz-metrics" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de2396b728035bc2f52948a7dfa20ddbbf9ac623e78b3da58630dc2757cda31a" +dependencies = [ + "hex", + "prettytable", + "serde", + "serde_json", + "sha2 0.10.9", + "solana-sdk", +] + +[[package]] +name = "trident-svm" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee534d7874e8e2b5e4e283c579eef81bb0a6789f363ee83a2d392a23eae5947" +dependencies = [ + "bincode", + "log", + "serde", + "solana-account", + "solana-account-info", + "solana-bpf-loader-program", + "solana-builtins", + "solana-clock", + "solana-compute-budget", + "solana-epoch-rewards", + "solana-epoch-schedule", + "solana-fee-structure", + "solana-hash", + "solana-instruction", + "solana-keypair", + "solana-loader-v3-interface", + "solana-logger", + "solana-program-runtime", + "solana-pubkey", + "solana-rent", + "solana-sdk-ids", + "solana-signer", + "solana-slot-hashes", + "solana-slot-history", + "solana-stake-interface", + "solana-svm", + "solana-svm-callback", + "solana-svm-feature-set", + "solana-sysvar", + "solana-sysvar-id", + "solana-transaction", + "solana-transaction-error", +] + +[[package]] +name = "trident-syn" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f2f33144e591097a2bdce81e767ef136c484d7944e4a7bae2da85d715fce65" +dependencies = [ + "petgraph", + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typenum" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +dependencies = [ + "void", +] + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "uriparse" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0200d0fc04d809396c2ad43f3c95da3582a2556eba8d453c1087f4120ee352ff" +dependencies = [ + "fnv", + "lazy_static", +] + +[[package]] +name = "url" +version = "2.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08bc136a29a3d1758e07a9cca267be308aeebf5cfd5a10f3f67ab2097683ef5b" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da95793dfc411fbbd93f5be7715b0578ec61fe87cb1a42b12eb625caa5c5ea60" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "551f88106c6d5e7ccc7cd9a16f312dd3b5d36ea8b4954304657d5dfba115d4a0" +dependencies = [ + "cfg-if", + "js-sys", + "once_cell", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04264334509e04a7bf8690f2384ef5265f05143a4bff3889ab7a3269adab59c2" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "420bc339d9f322e562942d52e115d57e950d12d88983a14c79b86859ee6c7ebc" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.110", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76f218a38c84bcb33c25ec7059b07847d465ce0e0a76b995e134a45adcb6af76" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a1f95c0d03a47f4ae1f7a64643a6bb97465d9b740f0fa8f90ea33915c99a9a1" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2878ef029c47c6e8cf779119f20fcf52bde7ad42a731b2a304bc221df17571e" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "winnow" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf" +dependencies = [ + "memchr", +] + +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + +[[package]] +name = "wit-bindgen" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "writeable" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" + +[[package]] +name = "yoke" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + +[[package]] +name = "zerotrie" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] diff --git a/trident-tests/Cargo.toml b/trident-tests/Cargo.toml new file mode 100644 index 0000000..073b711 --- /dev/null +++ b/trident-tests/Cargo.toml @@ -0,0 +1,26 @@ +[workspace] + +[package] +name = "fuzz_tests" +version = "0.1.0" +description = "Created with Trident" +edition = "2021" + +[dependencies] +borsh = "1.5.3" + +[dependencies.trident-fuzz] +version = "0.12.0" +features = ["token"] + +[[bin]] +name = "fuzz" +path = "fuzz/test_fuzz.rs" + +[[bin]] +name = "fuzz_lending" +path = "fuzz_lending/test_fuzz.rs" + +[[bin]] +name = "fuzz_liquidity_swaps" +path = "fuzz_liquidity_swaps/test_fuzz.rs" diff --git a/trident-tests/README.md b/trident-tests/README.md new file mode 100644 index 0000000..9c929b3 --- /dev/null +++ b/trident-tests/README.md @@ -0,0 +1,27 @@ +## Trident fuzz tests (quick start) + +### Build programs (required `.so` artifacts) +- Build from the program repo root (one level up): + - Anchor: `anchor build` + - Or Solana: `cargo build-sbf` +- Ensure `../target/deploy/omnipair.so` and `../target/deploy/flashloan_receiver_example.so` exist (paths are in `Trident.toml`). + +### Install Trident CLI +```bash +cargo install trident-cli +``` + +### Run fuzz tests (from this folder) +```bash +trident fuzz run fuzz +trident fuzz run fuzz_lending +trident fuzz run fuzz_liquidity_swaps +``` + +### Tweak behavior +- Iterations and flows/iteration: edit `FuzzTest::fuzz(iterations, flows_per_iteration)` at the bottom of each `test_fuzz.rs`. +- Flow selection: adjust `#[flow(weight = N)]` on flow methods. Higher weight = more frequent. + +### Docs +- Trident Documentation: [https://ackee.xyz/trident/docs/latest/](https://ackee.xyz/trident/docs/latest/) + diff --git a/trident-tests/Trident.toml b/trident-tests/Trident.toml new file mode 100644 index 0000000..69efa46 --- /dev/null +++ b/trident-tests/Trident.toml @@ -0,0 +1,23 @@ +[fuzz.metrics] +enabled = true +dashboard = true + +# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- +# PROGRAMS + +[[fuzz.programs]] +address = "Bd9Uhf5S8yzfop8cG9oqRs6jVcLtu8B4cb2gvRmtbNzk" +program = "../target/deploy/omnipair.so" + +[[fuzz.programs]] +address = "GmtswKBDrFZ9DfUfP7jbPFvbtuG7AJcX73SvoKWGxJbu" +program = "../target/deploy/flashloan_receiver_example.so" + + +# *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- +# MINTS + +# WSOL +[[fuzz.accounts]] +address = "So11111111111111111111111111111111111111112" +filename = "./accounts/mints/wsol.json" diff --git a/trident-tests/accounts/mints/wsol.json b/trident-tests/accounts/mints/wsol.json new file mode 100644 index 0000000..069a6d8 --- /dev/null +++ b/trident-tests/accounts/mints/wsol.json @@ -0,0 +1,14 @@ +{ + "pubkey": "So11111111111111111111111111111111111111112", + "account": { + "lamports": 939923589911, + "data": [ + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", + "base64" + ], + "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", + "executable": false, + "rentEpoch": 18446744073709551615, + "space": 82 + } +} diff --git a/trident-tests/fuzz/futarchy/claim_protocol_fees.rs b/trident-tests/fuzz/futarchy/claim_protocol_fees.rs new file mode 100644 index 0000000..87be3fb --- /dev/null +++ b/trident-tests/fuzz/futarchy/claim_protocol_fees.rs @@ -0,0 +1,305 @@ +use crate::FuzzTest; +use crate::{ + types::{ + omnipair::{ + ClaimProtocolFeesInstruction, ClaimProtocolFeesInstructionAccounts, + ClaimProtocolFeesInstructionData, + }, + ClaimProtocolFeesArgs, Pair, + }, + utils::{TOKEN_PROGRAM}, +}; + +impl FuzzTest { + pub fn claim_protocol_fees(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_claim_protocol_fees(); + let data = self.get_data_claim_protocol_fees(); + + // Capture initial state before the transaction + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_authority_token0_balance = self + .trident + .get_token_account(accounts.authority_token0_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_authority_token1_balance = self + .trident + .get_token_account(accounts.authority_token1_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = + ClaimProtocolFeesInstruction::data(ClaimProtocolFeesInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Claim Protocol Fees")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientAmount) + if res.is_success() { + self.verify_claim_protocol_fees_invariants( + &data, + &accounts, + &initial_pair, + initial_authority_token0_balance, + initial_authority_token1_balance, + ); + } + } + + fn get_data_claim_protocol_fees(&mut self) -> ClaimProtocolFeesArgs { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair account should exist"); + + // Get protocol fees from the pair account + // Claim partial amounts (or 0 if no fees available) + let amount0 = if pair_account.protocol_revenue_reserve0 > 0 { + self.trident + .random_from_range(1..=pair_account.protocol_revenue_reserve0) + } else { + 0 + }; + + let amount1 = if pair_account.protocol_revenue_reserve1 > 0 { + self.trident + .random_from_range(1..=pair_account.protocol_revenue_reserve1) + } else { + 0 + }; + + self.trident + .record_histogram("CLAIM_PROTOCOL_FEES_AMOUNT0", amount0 as f64); + self.trident + .record_histogram("CLAIM_PROTOCOL_FEES_AMOUNT1", amount1 as f64); + + ClaimProtocolFeesArgs::new(amount0, amount1) + } + + fn get_accounts_claim_protocol_fees(&mut self) -> ClaimProtocolFeesInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair account should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // authority token0 account + let authority_token0_account = self.trident.get_associated_token_address( + &pair_account.token0, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + + // authority token1 account + let authority_token1_account = self.trident.get_associated_token_address( + &pair_account.token1, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + + // caller (whoever) + let caller = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + ClaimProtocolFeesInstructionAccounts::new( + caller, + pair_pubkey, + futarchy_authority, + token0_vault, + token1_vault, + authority_token0_account, + authority_token1_account, + pair_account.token0, + pair_account.token1, + ) + } + + fn verify_claim_protocol_fees_invariants( + &mut self, + args: &ClaimProtocolFeesArgs, + accounts: &ClaimProtocolFeesInstructionAccounts, + initial_pair: &Pair, + initial_authority_token0_balance: u64, + initial_authority_token1_balance: u64, + ) { + // Fetch final state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_authority_token0_balance = self + .trident + .get_token_account(accounts.authority_token0_account) + .expect("Authority token0 account should exist") + .account + .amount; + + let final_authority_token1_balance = self + .trident + .get_token_account(accounts.authority_token1_account) + .expect("Authority token1 account should exist") + .account + .amount; + + // INVARIANT 1: Authority received exactly the claimed amounts + if args.amount0 > 0 { + assert_eq!( + final_authority_token0_balance, + initial_authority_token0_balance + .checked_add(args.amount0) + .expect("Authority token0 balance increase"), + "Authority should receive exactly amount0 claimed" + ); + } + + if args.amount1 > 0 { + assert_eq!( + final_authority_token1_balance, + initial_authority_token1_balance + .checked_add(args.amount1) + .expect("Authority token1 balance increase"), + "Authority should receive exactly amount1 claimed" + ); + } + + // INVARIANT 2: Protocol revenue reserves decreased by exactly the claimed amounts + assert_eq!( + final_pair.protocol_revenue_reserve0, + initial_pair + .protocol_revenue_reserve0 + .checked_sub(args.amount0) + .expect("Protocol revenue reserve0 decrease"), + "Protocol revenue reserve0 should decrease by amount0" + ); + + assert_eq!( + final_pair.protocol_revenue_reserve1, + initial_pair + .protocol_revenue_reserve1 + .checked_sub(args.amount1) + .expect("Protocol revenue reserve1 decrease"), + "Protocol revenue reserve1 should decrease by amount1" + ); + + // INVARIANT 3: Vault solvency check - vaults must hold at least reserves + collateral - debt + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .checked_add(final_pair.protocol_revenue_reserve0) + .expect("Adding protocol revenue overflow") + .saturating_sub(final_pair.total_debt0); + + // Allow small tolerance for accumulated rounding errors (0.01% of required or claimed amount) + let tolerance0 = args.amount0.max(required0.checked_div(10000).unwrap_or(0)); + assert!( + vault0_balance >= required0.saturating_sub(tolerance0), + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .checked_add(final_pair.protocol_revenue_reserve1) + .expect("Adding protocol revenue overflow") + .saturating_sub(final_pair.total_debt1); + + // Allow small tolerance for accumulated rounding errors (0.01% of required or claimed amount) + let tolerance1 = args.amount1.max(required1.checked_div(10000).unwrap_or(0)); + assert!( + vault1_balance >= required1.saturating_sub(tolerance1), + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 4: Pair core state should not have changed + // (claiming fees doesn't affect liquidity, collateral, or debt) + assert_eq!( + final_pair.reserve0, initial_pair.reserve0, + "Reserve0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.reserve1, initial_pair.reserve1, + "Reserve1 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_supply, initial_pair.total_supply, + "Total supply should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_collateral0, initial_pair.total_collateral0, + "Total collateral0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_collateral1, initial_pair.total_collateral1, + "Total collateral1 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_debt0, initial_pair.total_debt0, + "Total debt0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_debt1, initial_pair.total_debt1, + "Total debt1 should not change when claiming protocol fees" + ); + + // INVARIANT 5: Pair identity should not have changed + assert_eq!( + final_pair.token0, initial_pair.token0, + "Pair token0 should not change" + ); + assert_eq!( + final_pair.token1, initial_pair.token1, + "Pair token1 should not change" + ); + assert_eq!( + final_pair.lp_mint, initial_pair.lp_mint, + "Pair LP mint should not change" + ); + } +} diff --git a/trident-tests/fuzz/futarchy/distribute_tokens.rs b/trident-tests/fuzz/futarchy/distribute_tokens.rs new file mode 100644 index 0000000..1393a26 --- /dev/null +++ b/trident-tests/fuzz/futarchy/distribute_tokens.rs @@ -0,0 +1,77 @@ +use crate::types::{ + omnipair::{ + DistributeTokensInstruction, DistributeTokensInstructionAccounts, + DistributeTokensInstructionData, + }, + DistributeTokensArgs, +}; +use crate::utils::WSOL_MINT_ADDRESS; +use crate::FuzzTest; + +impl FuzzTest { + pub fn distribute_tokens(&mut self) { + let accounts = self.get_accounts_distribute_tokens(); + let data = self.get_data_distribute_tokens(); + + let pre_src_acc = self.trident.get_token_account(accounts.source_token_account); + let pre_amount = pre_src_acc.unwrap().account.amount; + + let ix = DistributeTokensInstruction::data(DistributeTokensInstructionData::new(data)) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Distribute Tokens")); + + // Verify the transaction was successful + assert!(res.is_success()); + + // INVARIANT 1: if the source had a positive balance, + // the distribution should drain the entire source (no dust left behind). + if pre_amount > 0 { + let post_src_acc = self.trident.get_token_account(accounts.source_token_account); + let post_amount = post_src_acc.unwrap().account.amount; + assert!(post_amount == 0, "DistributeTokens left dust in source token account (amount={})", post_amount); + } + } + + fn get_data_distribute_tokens(&mut self) -> DistributeTokensArgs { + DistributeTokensArgs::new() + } + + fn get_accounts_distribute_tokens(&mut self) -> DistributeTokensInstructionAccounts { + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Get the source token account - this should be an associated token account + // that holds tokens to be distributed + let source_token_account = self + .fuzz_accounts + .authority_wsol_account + .get(&mut self.trident).expect("Authority WSOl account should exist"); + + let futarchy_treasury_token_account = self + .fuzz_accounts + .futarchy_treasury_token_account + .get(&mut self.trident).expect("Futarchy treasury token account should exist"); + + let buybacks_vault_token_account = self + .fuzz_accounts + .buybacks_vault_token_account + .get(&mut self.trident).expect("Buybacks vault token account should exist"); + + let team_treasury_token_account = self + .fuzz_accounts + .team_treasury_token_account + .get(&mut self.trident).expect("Team treasury token account should exist"); + + DistributeTokensInstructionAccounts::new( + futarchy_authority, + WSOL_MINT_ADDRESS, + source_token_account, + futarchy_treasury_token_account, + buybacks_vault_token_account, + team_treasury_token_account, + ) + } +} diff --git a/trident-tests/fuzz/futarchy/init_futarchy.rs b/trident-tests/fuzz/futarchy/init_futarchy.rs new file mode 100644 index 0000000..7ca72c6 --- /dev/null +++ b/trident-tests/fuzz/futarchy/init_futarchy.rs @@ -0,0 +1,193 @@ +use crate::utils::{WSOL_MINT_ADDRESS}; +use crate::FuzzTest; +use crate::{ + types::{ + omnipair::{ + InitFutarchyAuthorityInstruction, InitFutarchyAuthorityInstructionAccounts, + InitFutarchyAuthorityInstructionData, + }, + FutarchyAuthority, InitFutarchyAuthorityArgs, + }, + utils::DEPLOYER_ADDRESS, +}; +use trident_fuzz::fuzzing::*; + +impl FuzzTest { + pub fn init_futarchy(&mut self) { + // Init futarchy authority + let data = self.get_data_init_futarchy(); + let accounts = self.get_accounts_init_futarchy(); + + let ix = InitFutarchyAuthorityInstruction::data(InitFutarchyAuthorityInstructionData::new( + data.clone(), + )) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Init Futarchy Authority")); + + // MUST BE SUCCESSFUL + assert!(res.is_success()); + + // Verify invariants + self.verify_futarchy_invariants(&data, &accounts); + } + + fn get_data_init_futarchy(&mut self) -> InitFutarchyAuthorityArgs { + let swap_bps = self.trident.random_from_range(1..=5_000); + let interest_bps = self.trident.random_from_range(1..=5_000); + + self.trident + .record_histogram("INIT_FUTARCHY_SWAP_BPS", swap_bps as f64); + self.trident + .record_histogram("INIT_FUTARCHY_INTEREST_BPS", interest_bps as f64); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Futarchy treasury token account + let futarchy_treasury_token_account = self + .fuzz_accounts + .futarchy_treasury_token_account + .insert(&mut self.trident, None); + let init_futarchy_treasury_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &futarchy_treasury_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + + // Buybacks vault token account + let buybacks_vault_token_account = self + .fuzz_accounts + .buybacks_vault_token_account + .insert(&mut self.trident, None); + let init_buybacks_vault_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &buybacks_vault_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + // Team treasury token account + let team_treasury_token_account = self + .fuzz_accounts + .team_treasury_token_account + .insert(&mut self.trident, None); + let init_team_treasury_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &team_treasury_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + let ixs: Vec = vec![ + init_futarchy_treasury_token_account_ix, + init_buybacks_vault_token_account_ix, + init_team_treasury_token_account_ix, + ] + .into_iter() + .flatten() + .collect(); + let res = self.trident.process_transaction(&ixs, None); + assert!(res.is_success()); + + // Generate random bps values that sum to exactly 10_000 + let futarchy_treasury_bps = self.trident.random_from_range(0..=10_000); + let remaining_after_first = 10_000 - futarchy_treasury_bps; + + let buybacks_vault_bps = if remaining_after_first > 0 { + self.trident.random_from_range(0..=remaining_after_first) + } else { + 0 + }; + + // Calculate the remaining to ensure sum equals 10_000 + let team_treasury_bps = 10_000u16 + .saturating_sub(futarchy_treasury_bps) + .saturating_sub(buybacks_vault_bps); + + self.trident.record_histogram( + "INIT_FUTARCHY_FUTARCHY_TREASURY_BPS", + futarchy_treasury_bps as f64, + ); + self.trident.record_histogram( + "INIT_FUTARCHY_BUYBACKS_VAULT_BPS", + buybacks_vault_bps as f64, + ); + self.trident + .record_histogram("INIT_FUTARCHY_TEAM_TREASURY_BPS", team_treasury_bps as f64); + + InitFutarchyAuthorityArgs { + authority: DEPLOYER_ADDRESS, + swap_bps, + interest_bps, + futarchy_treasury: futarchy_treasury_token_account, + futarchy_treasury_bps, + buybacks_vault: buybacks_vault_token_account, + buybacks_vault_bps, + team_treasury: team_treasury_token_account, + team_treasury_bps, + } + } + + fn get_accounts_init_futarchy(&mut self) -> InitFutarchyAuthorityInstructionAccounts { + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + InitFutarchyAuthorityInstructionAccounts::new(futarchy_authority) + } + + fn verify_futarchy_invariants( + &mut self, + data: &InitFutarchyAuthorityArgs, + accounts: &InitFutarchyAuthorityInstructionAccounts, + ) { + let futarchy_authority_account = self + .trident + .get_account_with_type::(&accounts.futarchy_authority, 8); + + if let Some(futarchy_authority_account) = futarchy_authority_account { + // Verify recipients are set correctly + assert_eq!( + futarchy_authority_account.recipients.futarchy_treasury, data.futarchy_treasury, + "Futarchy treasury recipient mismatch" + ); + assert_eq!( + futarchy_authority_account.recipients.buybacks_vault, data.buybacks_vault, + "Buybacks vault recipient mismatch" + ); + assert_eq!( + futarchy_authority_account.recipients.team_treasury, data.team_treasury, + "Team treasury recipient mismatch" + ); + + // Verify revenue distribution BPS sum to 10_000 + let total_bps = futarchy_authority_account + .revenue_distribution + .futarchy_treasury_bps + .saturating_add( + futarchy_authority_account + .revenue_distribution + .buybacks_vault_bps, + ) + .saturating_add( + futarchy_authority_account + .revenue_distribution + .team_treasury_bps, + ); + assert_eq!( + total_bps, 10_000, + "Revenue distribution BPS must sum to 10_000" + ); + + // Verify revenue share BPS are within valid range + assert!( + futarchy_authority_account.revenue_share.swap_bps <= 10_000, + "Swap BPS must be <= 10_000" + ); + assert!( + futarchy_authority_account.revenue_share.interest_bps <= 10_000, + "Interest BPS must be <= 10_000" + ); + } + } +} diff --git a/trident-tests/fuzz/futarchy/mod.rs b/trident-tests/fuzz/futarchy/mod.rs new file mode 100644 index 0000000..4b1cb1a --- /dev/null +++ b/trident-tests/fuzz/futarchy/mod.rs @@ -0,0 +1,3 @@ +mod claim_protocol_fees; +mod distribute_tokens; +mod init_futarchy; diff --git a/trident-tests/fuzz/fuzz_accounts.rs b/trident-tests/fuzz/fuzz_accounts.rs new file mode 100644 index 0000000..3f9d329 --- /dev/null +++ b/trident-tests/fuzz/fuzz_accounts.rs @@ -0,0 +1,90 @@ +use trident_fuzz::fuzzing::*; + +/// Storage for all account addresses used in fuzz testing. +/// +/// This struct serves as a centralized repository for account addresses, +/// enabling their reuse across different instruction flows and test scenarios. +/// +/// Docs: https://ackee.xyz/trident/docs/latest/trident-api-macro/trident-types/fuzz-accounts/ +#[derive(Default)] +pub struct AccountAddresses { + pub user: AddressStorage, + + pub token_mint: AddressStorage, + + pub pair: AddressStorage, + + pub rate_model: AddressStorage, + + pub futarchy_authority: AddressStorage, + + pub user_position: AddressStorage, + + pub collateral_vault: AddressStorage, + + pub user_collateral_token_account: AddressStorage, + + pub collateral_token_mint: AddressStorage, + + pub token0_vault: AddressStorage, + + pub token1_vault: AddressStorage, + + pub token0_vault_mint: AddressStorage, + + pub token1_vault_mint: AddressStorage, + + pub lp_mint: AddressStorage, + + pub user_lp_token_account: AddressStorage, + + pub token_vault: AddressStorage, + + pub user_token_account: AddressStorage, + + pub vault_token_mint: AddressStorage, + + pub caller: AddressStorage, + + pub authority_token0_account: AddressStorage, + + pub authority_token1_account: AddressStorage, + + pub source_mint: AddressStorage, + + pub source_token_account: AddressStorage, + + pub futarchy_treasury_token_account: AddressStorage, + + pub buybacks_vault_token_account: AddressStorage, + + pub team_treasury_token_account: AddressStorage, + + pub receiver_token0_account: AddressStorage, + + pub receiver_token1_account: AddressStorage, + + pub deployer_token0_account: AddressStorage, + + pub deployer_token1_account: AddressStorage, + + pub authority_wsol_account: AddressStorage, + + pub caller_token_account: AddressStorage, + + pub position_owner: AddressStorage, + + pub token_in_vault: AddressStorage, + + pub token_out_vault: AddressStorage, + + pub user_token_in_account: AddressStorage, + + pub user_token_out_account: AddressStorage, + + pub token_in_mint: AddressStorage, + + pub token_out_mint: AddressStorage, + + pub authority_token_in_account: AddressStorage, +} diff --git a/trident-tests/fuzz/lending/add_collateral.rs b/trident-tests/fuzz/lending/add_collateral.rs new file mode 100644 index 0000000..46ca124 --- /dev/null +++ b/trident-tests/fuzz/lending/add_collateral.rs @@ -0,0 +1,246 @@ +use crate::{ + types::{ + omnipair::{ + self, AddCollateralInstruction, AddCollateralInstructionAccounts, + AddCollateralInstructionData, + }, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn add_collateral(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_add_collateral(); + let accounts = self.get_accounts_add_collateral(); + + // Store initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_collateral_token_account) + .expect("User collateral account should exist") + .account + .amount; + + // Get initial pair and position state (position may not exist yet - init_if_needed) + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + let ix = AddCollateralInstruction::data(AddCollateralInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Add Collateral")); + + if res.is_success() { + self.verify_add_collateral_invariants( + &data, + &accounts, + initial_user_balance, + &initial_pair, + initial_position.as_ref(), + ); + } + } + + fn get_accounts_add_collateral(&mut self) -> AddCollateralInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // user related accounts + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let collateral_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + // let collateral_token_mint = pair_account.token0; + + let collateral_vault = self.trident.get_associated_token_address( + &collateral_token_mint, + &pair, + &TOKEN_PROGRAM, + ); + + let user_collateral_token_account = self.trident.get_associated_token_address( + &collateral_token_mint, + &user, + &TOKEN_PROGRAM, + ); + + AddCollateralInstructionAccounts::new( + pair, + pair_account.rate_model, + futarchy_authority, + user_position, + collateral_vault, + user_collateral_token_account, + collateral_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn get_data_add_collateral(&mut self) -> AdjustPositionArgs { + // Use reasonable collateral amounts that match typical use cases + let amount = self.trident.random_from_range(100_000..=100_000_000); + + self.trident + .record_histogram("ADD_COLLATERAL_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn verify_add_collateral_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &AddCollateralInstructionAccounts, + initial_user_balance: u64, + initial_pair: &Pair, + + initial_position: Option<&UserPosition>, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after add collateral"); + + // Get user position + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist after add collateral"); + + // Determine which token is being used as collateral + let is_token0 = accounts.collateral_token_mint == final_pair.token0; + + // Verify user balance decreased by exactly the amount + let final_user_balance = self + .trident + .get_token_account(accounts.user_collateral_token_account) + .expect("User collateral account should exist") + .account + .amount; + + let amount_transferred = initial_user_balance + .checked_sub(final_user_balance) + .expect("User balance should decrease"); + assert_eq!( + amount_transferred, args.amount, + "User balance should decrease by exactly the collateral amount" + ); + + // Verify user position collateral increased by EXACTLY the amount + // (position may be newly created, so initial collateral is 0 if it didn't exist) + if is_token0 { + let initial_collateral = initial_position.map(|p| p.collateral0).unwrap_or(0); + let expected_collateral = initial_collateral + .checked_add(args.amount) + .expect("User position collateral0 should not overflow"); + assert_eq!( + user_position.collateral0, expected_collateral, + "User position collateral0 should increase by exactly the amount added" + ); + } else { + let initial_collateral = initial_position.map(|p| p.collateral1).unwrap_or(0); + let expected_collateral = initial_collateral + .checked_add(args.amount) + .expect("User position collateral1 should not overflow"); + assert_eq!( + user_position.collateral1, expected_collateral, + "User position collateral1 should increase by exactly the amount added" + ); + } + + // Verify pair total_collateral increased by EXACTLY the amount (atomic transaction) + if is_token0 { + let expected_total = initial_pair + .total_collateral0 + .checked_add(args.amount) + .expect("Total collateral0 should not overflow"); + assert_eq!( + final_pair.total_collateral0, expected_total, + "Pair total_collateral0 should increase by exactly the amount added" + ); + } else { + let expected_total = initial_pair + .total_collateral1 + .checked_add(args.amount) + .expect("Total collateral1 should not overflow"); + assert_eq!( + final_pair.total_collateral1, expected_total, + "Pair total_collateral1 should increase by exactly the amount added" + ); + } + + // Critical accounting invariant: vault balance should be >= reserves + collateral - debt + // (vaults hold LP liquidity + borrower collateral, minus what was borrowed out) + let vault_balance = self + .trident + .get_token_account(accounts.collateral_vault) + .expect("Collateral vault should exist") + .account + .amount; + + if is_token0 { + let total_required = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); // Subtract debt (borrowed tokens are out of vault) + assert!( + vault_balance >= total_required, + "Vault balance must be >= reserve0 + total_collateral0 - total_debt0" + ); + } else { + let total_required = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); // Subtract debt (borrowed tokens are out of vault) + assert!( + vault_balance >= total_required, + "Vault balance must be >= reserve1 + total_collateral1 - total_debt1" + ); + } + + // Verify user position is properly initialized (always, whether new or existing) + assert_eq!( + user_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + user_position.pair, accounts.pair, + "User position pair should match pair" + ); + } +} diff --git a/trident-tests/fuzz/lending/borrow.rs b/trident-tests/fuzz/lending/borrow.rs new file mode 100644 index 0000000..8962bc4 --- /dev/null +++ b/trident-tests/fuzz/lending/borrow.rs @@ -0,0 +1,334 @@ +use crate::{ + types::{ + omnipair::{self, BorrowInstruction, BorrowInstructionAccounts, BorrowInstructionData}, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn borrow(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_borrow(); + let accounts = self.get_accounts_borrow(); + + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if user_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = user_position.expect("User position should exist"); + + let ix = BorrowInstruction::data(BorrowInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Borrow")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., BorrowingPowerExceeded) + if res.is_success() { + self.verify_borrow_invariants( + &accounts, + &initial_pair, + &initial_position, + initial_user_balance, + ); + } + } + + fn get_data_borrow(&mut self) -> AdjustPositionArgs { + // Use very small amounts to increase success rate + // Borrowing power depends on collateral, which may be limited + // Use weighted distribution: 80% small amounts, 20% larger amounts + let amount = if self.trident.random_from_range(0..=9) < 8 { + // 80% chance: very small borrow (10 to 10,000) + self.trident.random_from_range(10..=10_000) + } else { + // 20% chance: larger borrow (10,000 to 100,000) + self.trident.random_from_range(10_000..=100_000) + }; + + self.trident + .record_histogram("BORROW_AMOUNT", amount as f64); + + AdjustPositionArgs::new(amount) + } + + fn get_accounts_borrow(&mut self) -> BorrowInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + // let vault_token_mint = pair_account.token1; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + BorrowInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_borrow_invariants( + &mut self, + accounts: &BorrowInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being borrowed + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Calculate actual borrow amount (may differ from args.amount if u64::MAX was used) + let actual_borrow_amount = final_user_balance + .checked_sub(initial_user_balance) + .expect("User balance should increase"); + + // INVARIANT 1: User should have received borrowed tokens + assert!( + actual_borrow_amount > 0, + "User should receive borrowed tokens" + ); + + // INVARIANT 2: Pair's total debt should increase by at least the borrow amount + // Note: update() accrues interest before borrowing, so total_debt may increase more than just the borrow amount + let (initial_total_debt, final_total_debt) = if is_token0 { + (initial_pair.total_debt0, final_pair.total_debt0) + } else { + (initial_pair.total_debt1, final_pair.total_debt1) + }; + let min_expected_debt = initial_total_debt + .checked_add(actual_borrow_amount) + .expect("Total debt increase calculation"); + assert!( + final_total_debt >= min_expected_debt, + "Pair total debt should be at least initial + borrow amount (was {}, expected >= {})", + final_total_debt, + min_expected_debt + ); + + // INVARIANT 3: User position debt shares should increase correctly + let (initial_debt_shares, final_debt_shares, initial_total_shares, final_total_shares) = + if is_token0 { + ( + initial_position.debt0_shares, + final_position.debt0_shares, + initial_pair.total_debt0_shares, + final_pair.total_debt0_shares, + ) + } else { + ( + initial_position.debt1_shares, + final_position.debt1_shares, + initial_pair.total_debt1_shares, + final_pair.total_debt1_shares, + ) + }; + + // Calculate expected shares increase + let expected_shares = if initial_total_shares == 0 { + // First debt: shares = amount (1:1 ratio) + actual_borrow_amount + } else { + // Subsequent debt: shares = amount * total_shares / total_debt + ((actual_borrow_amount as u128) + .checked_mul(initial_total_shares as u128) + .expect("Shares calculation") + .checked_div(initial_total_debt as u128) + .expect("Shares division")) as u64 + }; + + // Allow for 1-unit rounding tolerance in shares calculation + let expected_final_debt_shares = initial_debt_shares + .checked_add(expected_shares) + .expect("User debt shares increase"); + assert!( + final_debt_shares.abs_diff(expected_final_debt_shares) <= 1, + "User debt shares should increase correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_debt_shares, + final_debt_shares + ); + + let expected_final_total_shares = initial_total_shares + .checked_add(expected_shares) + .expect("Total debt shares increase"); + assert!( + final_total_shares.abs_diff(expected_final_total_shares) <= 1, + "Pair total debt shares should increase correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_total_shares, + final_total_shares + ); + + if is_token0 { + assert!( + final_position.debt0_shares > initial_position.debt0_shares, + "Debt0 shares should increase {:?} > {:?}", + final_position.debt0_shares, + initial_position.debt0_shares + ); + } else { + assert!( + final_position.debt1_shares > initial_position.debt1_shares, + "Debt1 shares should increase {:?} > {:?}", + final_position.debt1_shares, + initial_position.debt1_shares + ); + } + + // INVARIANT 4: Vault solvency check - vault balance >= reserves + collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 5: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 6: Collateral amounts should not change during borrow + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Collateral0 should not change during borrow" + ); + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Collateral1 should not change during borrow" + ); + + // INVARIANT 7: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "Borrow accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz/lending/flashloan.rs b/trident-tests/fuzz/lending/flashloan.rs new file mode 100644 index 0000000..8629c50 --- /dev/null +++ b/trident-tests/fuzz/lending/flashloan.rs @@ -0,0 +1,292 @@ +use trident_fuzz::fuzzing::AccountMeta; + +use crate::{ + types::{ + omnipair::{ + self, FlashloanInstruction, FlashloanInstructionAccounts, FlashloanInstructionData, + }, + FlashloanArgs, Pair, + }, + utils::{EVENT_AUTHORITY_ADDRESS, FLASHLOAN_CALLBACK_RECEIVER_PROGRAM, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn flashloan(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + // NOTE: Flashloan failures (InsufficientAmount0/1) are expected and normal during fuzzing + // The callback receiver program needs to repay the loan + fees, which may not always succeed + let data = self.get_data_flashloan(); + let accounts = self.get_accounts_flashloan(); + + // Capture initial state before the transaction + let initial_vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let initial_vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let ix = FlashloanInstruction::data(data.clone()) + .accounts(accounts.clone()) + .remaining_accounts(vec![ + AccountMeta::new(accounts.token0_vault, false), + AccountMeta::new(accounts.token1_vault, false), + ]) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Flashloan")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., BorrowExceedsReserve, InsufficientAmount) + if res.is_success() { + self.verify_flashloan_invariants( + &data.args, + &accounts, + &initial_pair, + initial_vault0_balance, + initial_vault1_balance, + ); + } + } + + pub fn get_accounts_flashloan(&mut self) -> FlashloanInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let token0_vault = + self.trident + .get_associated_token_address(&pair_account.token0, &pair, &TOKEN_PROGRAM); + + let token1_vault = + self.trident + .get_associated_token_address(&pair_account.token1, &pair, &TOKEN_PROGRAM); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let receiver_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + let receiver_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + FlashloanInstructionAccounts::new( + pair, + pair_account.rate_model, + futarchy_authority, + token0_vault, + token1_vault, + pair_account.token0, + pair_account.token1, + receiver_token0_account, + receiver_token1_account, + FLASHLOAN_CALLBACK_RECEIVER_PROGRAM, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + pub fn get_data_flashloan(&mut self) -> FlashloanInstructionData { + // Strategy: Use very small amounts to maximize success rate + // Flashloans require the callback receiver to repay loan + fees, which often fails in fuzzing + let (amount0, amount1) = match self.trident.random_from_range(0..=100) { + // 40% - Flash only token0 (very small amount) + 0..=39 => (self.trident.random_from_range(100..=1_000), 0), + // 40% - Flash only token1 (very small amount) + 40..=79 => (0, self.trident.random_from_range(100..=1_000)), + // 15% - Flash both tokens (very small amounts) + 80..=94 => ( + self.trident.random_from_range(100..=500), + self.trident.random_from_range(100..=500), + ), + // 5% - Slightly larger amounts (stress test) + _ => ( + self.trident.random_from_range(1_000..=5_000), + self.trident.random_from_range(1_000..=5_000), + ), + }; + + self.trident + .record_histogram("FLASHLOAN_AMOUNT0", amount0 as f64); + self.trident + .record_histogram("FLASHLOAN_AMOUNT1", amount1 as f64); + + FlashloanInstructionData::new(FlashloanArgs::new(amount0, amount1, vec![])) + } + + fn verify_flashloan_invariants( + &mut self, + args: &FlashloanArgs, + accounts: &FlashloanInstructionAccounts, + initial_pair: &Pair, + initial_vault0_balance: u64, + initial_vault1_balance: u64, + ) { + // Fetch final state + let final_vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let final_vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + // Calculate expected fees (FLASHLOAN_FEE_BPS = 5 bps = 0.05%) + const FLASHLOAN_FEE_BPS: u64 = 5; + const BPS_DENOMINATOR: u64 = 10_000; + + let fee0 = (args.amount0 as u128) + .checked_mul(FLASHLOAN_FEE_BPS as u128) + .expect("Fee0 calculation") + .checked_div(BPS_DENOMINATOR as u128) + .expect("Fee0 division") as u64; + + let fee1 = (args.amount1 as u128) + .checked_mul(FLASHLOAN_FEE_BPS as u128) + .expect("Fee1 calculation") + .checked_div(BPS_DENOMINATOR as u128) + .expect("Fee1 division") as u64; + + // INVARIANT 1: Vault balances must have increased by at least the fee + // Critical: This ensures flashloan was repaid with fee + let required_balance0 = initial_vault0_balance + .checked_add(fee0) + .expect("Required balance0 calculation"); + let required_balance1 = initial_vault1_balance + .checked_add(fee1) + .expect("Required balance1 calculation"); + + assert!( + final_vault0_balance >= required_balance0, + "Token0 vault must have at least initial balance + fee after flashloan (repayment check)" + ); + assert!( + final_vault1_balance >= required_balance1, + "Token1 vault must have at least initial balance + fee after flashloan (repayment check)" + ); + + // INVARIANT 2: Net profit check - vault balances increased (fees were collected) + // Only check if fee > 0 (small amounts can round to 0 fee) + if fee0 > 0 { + assert!( + final_vault0_balance > initial_vault0_balance, + "Token0 vault should have more tokens after flashloan (fee collected)" + ); + } + if fee1 > 0 { + assert!( + final_vault1_balance > initial_vault1_balance, + "Token1 vault should have more tokens after flashloan (fee collected)" + ); + } + + // INVARIANT 3: Vault solvency check - vaults must hold at least reserves + collateral - debt + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + final_vault0_balance >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + final_vault1_balance >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 4: Pair state should not have fundamentally changed + // (reserves may have changed due to update() interest accrual, but core structure remains) + assert_eq!( + final_pair.token0, initial_pair.token0, + "Pair token0 should not change" + ); + assert_eq!( + final_pair.token1, initial_pair.token1, + "Pair token1 should not change" + ); + assert_eq!( + final_pair.lp_mint, initial_pair.lp_mint, + "Pair LP mint should not change" + ); + + // INVARIANT 5: Collateral and debt shares should not have changed + // (flashloans shouldn't affect lending positions) + // Note: total_debt can increase due to interest accrual, but debt_shares should remain constant + assert_eq!( + final_pair.total_collateral0, initial_pair.total_collateral0, + "Total collateral0 should not change during flashloan" + ); + assert_eq!( + final_pair.total_collateral1, initial_pair.total_collateral1, + "Total collateral1 should not change during flashloan" + ); + assert!( + final_pair.total_debt0 >= initial_pair.total_debt0, + "Total debt0 should be at least initial (interest can accrue). Was {}, initial {}", + final_pair.total_debt0, + initial_pair.total_debt0 + ); + assert!( + final_pair.total_debt1 >= initial_pair.total_debt1, + "Total debt1 should be at least initial (interest can accrue). Was {}, initial {}", + final_pair.total_debt1, + initial_pair.total_debt1 + ); + assert_eq!( + final_pair.total_debt0_shares, initial_pair.total_debt0_shares, + "Total debt0 shares should not change during flashloan" + ); + assert_eq!( + final_pair.total_debt1_shares, initial_pair.total_debt1_shares, + "Total debt1 shares should not change during flashloan" + ); + + // INVARIANT 6: Total supply should not have changed + // (flashloans don't mint or burn LP tokens) + assert_eq!( + final_pair.total_supply, initial_pair.total_supply, + "Total supply should not change during flashloan" + ); + } +} diff --git a/trident-tests/fuzz/lending/liquidate.rs b/trident-tests/fuzz/lending/liquidate.rs new file mode 100644 index 0000000..3afe70b --- /dev/null +++ b/trident-tests/fuzz/lending/liquidate.rs @@ -0,0 +1,331 @@ +use trident_fuzz::fuzzing::LAMPORTS_PER_SOL; + +use crate::{ + types::{ + omnipair::{ + self, LiquidateInstruction, LiquidateInstructionAccounts, LiquidateInstructionData, + }, + Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn liquidate(&mut self) { + if self.fuzz_accounts.pair.is_empty() || self.fuzz_accounts.user_position.is_empty() { + return; + } + + let accounts = self.get_accounts_liquidate(); + + // Capture initial state before the transaction + let initial_caller_balance = self + .trident + .get_token_account(accounts.caller_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + let ix = LiquidateInstruction::data(LiquidateInstructionData::new()) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Liquidate")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., NotUndercollateralized, no debt) + if res.is_success() { + self.verify_liquidate_invariants( + &accounts, + &initial_pair, + &initial_position, + initial_caller_balance, + ); + } + } + + fn get_accounts_liquidate(&mut self) -> LiquidateInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user_position = self.fuzz_accounts.user_position.get(&mut self.trident).expect("User position should exist"); + let user_position_account_data = self + .trident + .get_account_with_type::(&user_position, 8) + .expect("User position should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let collateral_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let collateral_vault = self.trident.get_associated_token_address( + &collateral_token_mint, + &pair, + &TOKEN_PROGRAM, + ); + + let caller = self.fuzz_accounts.caller.insert(&mut self.trident, None); + self.trident.airdrop( + &caller, + LAMPORTS_PER_SOL.checked_mul(2).expect("Airdrop amount"), + ); + let caller_token_account = self.trident.get_associated_token_address( + &collateral_token_mint, + &caller, + &TOKEN_PROGRAM, + ); + self.trident + .initialize_associated_token_account(&caller, &collateral_token_mint, &caller); + + LiquidateInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + collateral_vault, + caller_token_account, + collateral_token_mint, + user_position_account_data.owner, + caller, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_liquidate_invariants( + &mut self, + accounts: &LiquidateInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_caller_balance: u64, + ) { + // Fetch final state + let final_caller_balance = self + .trident + .get_token_account(accounts.caller_token_account) + .expect("Caller token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is collateral and which is debt + let collateral_vault = self + .trident + .get_token_account(accounts.collateral_vault) + .expect("Collateral vault should exist") + .account; + let is_collateral_token0 = collateral_vault.mint == final_pair.token0; + + // Calculate initial debt + let (_, initial_debt_shares) = if is_collateral_token0 { + // Collateral is token0, debt is token1 + let debt = if initial_pair.total_debt1_shares == 0 { + 0 + } else { + ((initial_position.debt1_shares as u128) + .checked_mul(initial_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, initial_position.debt1_shares) + } else { + // Collateral is token1, debt is token0 + let debt = if initial_pair.total_debt0_shares == 0 { + 0 + } else { + ((initial_position.debt0_shares as u128) + .checked_mul(initial_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, initial_position.debt0_shares) + }; + + // INVARIANT 1: Caller received liquidation incentive (should be > 0) + let caller_incentive = final_caller_balance + .checked_sub(initial_caller_balance) + .expect("Caller balance should increase"); + assert!( + caller_incentive > 0, + "Liquidator should receive incentive for liquidating" + ); + + // INVARIANT 2: User position collateral decreased + let (initial_collateral, final_collateral) = if is_collateral_token0 { + (initial_position.collateral0, final_position.collateral0) + } else { + (initial_position.collateral1, final_position.collateral1) + }; + let collateral_seized = initial_collateral + .checked_sub(final_collateral) + .expect("Collateral should decrease"); + assert!( + collateral_seized > 0, + "Position collateral should be seized during liquidation" + ); + + // INVARIANT 3: Pair total collateral decreased by the seized amount + let (initial_total_collateral, final_total_collateral) = if is_collateral_token0 { + (initial_pair.total_collateral0, final_pair.total_collateral0) + } else { + (initial_pair.total_collateral1, final_pair.total_collateral1) + }; + assert_eq!( + final_total_collateral, + initial_total_collateral + .checked_sub(collateral_seized) + .expect("Total collateral decrease"), + "Pair total collateral should decrease by seized amount" + ); + + // INVARIANT 4: Collateral seized >= caller incentive (caller gets incentive, rest to reserves) + assert!( + collateral_seized >= caller_incentive, + "Collateral seized must cover at least the liquidation incentive" + ); + + // INVARIANT 5: Debt decreased or was written off (final debt <= initial debt) + let (_, final_debt_shares) = if is_collateral_token0 { + let debt = if final_pair.total_debt1_shares == 0 { + 0 + } else { + ((final_position.debt1_shares as u128) + .checked_mul(final_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(final_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, final_position.debt1_shares) + } else { + let debt = if final_pair.total_debt0_shares == 0 { + 0 + } else { + ((final_position.debt0_shares as u128) + .checked_mul(final_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(final_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, final_position.debt0_shares) + }; + + // Note: final_debt may not be less than initial_debt if significant interest accrued during update() + // The authoritative measure is debt_shares, which must decrease during liquidation + assert!( + final_debt_shares < initial_debt_shares, + "Debt shares must decrease during liquidation (was {}, now {})", + initial_debt_shares, final_debt_shares + ); + + // INVARIANT 6: Collateral reserve increased (by collateral_seized - caller_incentive) + let (initial_collateral_reserve, final_collateral_reserve) = if is_collateral_token0 { + (initial_pair.reserve0, final_pair.reserve0) + } else { + (initial_pair.reserve1, final_pair.reserve1) + }; + + // Note: Due to pair.update(), reserves may have interest accrued, so we can't do exact check + // But collateral reserve should have increased + assert!( + final_collateral_reserve >= initial_collateral_reserve, + "Collateral reserve should increase (seized collateral - incentive goes to reserves)" + ); + + // INVARIANT 7: Vault solvency check + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 8: Position ownership unchanged + assert_eq!( + final_position.owner, initial_position.owner, + "Position owner should not change" + ); + assert_eq!( + final_position.pair, initial_position.pair, + "Position pair should not change" + ); + + // INVARIANT 9: Non-seized collateral unchanged + if is_collateral_token0 { + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Non-seized collateral (token1) should not change" + ); + } else { + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Non-seized collateral (token0) should not change" + ); + } + } +} diff --git a/trident-tests/fuzz/lending/mod.rs b/trident-tests/fuzz/lending/mod.rs new file mode 100644 index 0000000..4e2896b --- /dev/null +++ b/trident-tests/fuzz/lending/mod.rs @@ -0,0 +1,6 @@ +mod add_collateral; +mod borrow; +mod flashloan; +mod liquidate; +mod remove_collateral; +mod repay; diff --git a/trident-tests/fuzz/lending/remove_collateral.rs b/trident-tests/fuzz/lending/remove_collateral.rs new file mode 100644 index 0000000..265ce12 --- /dev/null +++ b/trident-tests/fuzz/lending/remove_collateral.rs @@ -0,0 +1,309 @@ + + +use crate::{ + types::{ + omnipair::{ + self, RemoveCollateralInstruction, RemoveCollateralInstructionAccounts, + RemoveCollateralInstructionData, + }, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn remove_collateral(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_remove_collateral(); + let accounts = self.get_accounts_remove_collateral(); + + // Check if user position exists + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if initial_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let ix = + RemoveCollateralInstruction::data(RemoveCollateralInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Remove Collateral")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientCollateral, BorrowingPowerExceeded) + if res.is_success() { + self.verify_remove_collateral_invariants( + &data, + &accounts, + &initial_pair, + &initial_position.unwrap(), + initial_user_balance, + ); + } + } + + fn get_data_remove_collateral(&mut self) -> AdjustPositionArgs { + // Use smaller amounts more likely to match actual collateral amounts + let amount = self.trident.random_from_range(100..=1_000_000); + self.trident + .record_histogram("REMOVE_COLLATERAL_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn get_accounts_remove_collateral(&mut self) -> RemoveCollateralInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .unwrap(); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + RemoveCollateralInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_remove_collateral_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &RemoveCollateralInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being withdrawn + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Get initial collateral amount + let initial_collateral = if is_token0 { + initial_position.collateral0 + } else { + initial_position.collateral1 + }; + + // Calculate actual withdraw amount + // If args.amount == u64::MAX, the program withdraws all available collateral + let is_withdraw_all = args.amount == u64::MAX; + let actual_withdraw_amount = if is_withdraw_all { + initial_collateral + } else { + args.amount + }; + + // INVARIANT 1: User token balance should increase by exactly the withdraw amount + let amount_transferred = final_user_balance + .checked_sub(initial_user_balance) + .expect("User balance should increase"); + assert_eq!( + amount_transferred, actual_withdraw_amount, + "User should receive exactly the withdrawn amount" + ); + + // INVARIANT 2: User position collateral should decrease by exactly the withdraw amount + let final_collateral = if is_token0 { + final_position.collateral0 + } else { + final_position.collateral1 + }; + assert_eq!( + final_collateral, + initial_collateral + .checked_sub(actual_withdraw_amount) + .expect("Collateral decrease calculation"), + "User position collateral should decrease by withdraw amount" + ); + + // INVARIANT 3: Pair total collateral should decrease by exactly the withdraw amount + let (initial_total_collateral, final_total_collateral) = if is_token0 { + (initial_pair.total_collateral0, final_pair.total_collateral0) + } else { + (initial_pair.total_collateral1, final_pair.total_collateral1) + }; + assert_eq!( + final_total_collateral, + initial_total_collateral + .checked_sub(actual_withdraw_amount) + .expect("Total collateral decrease calculation"), + "Pair total collateral should decrease by withdraw amount" + ); + + // INVARIANT 4: If withdraw_all was requested, verify final collateral is 0 + // (this assumes no debt that would prevent full withdrawal - which is validated in the program) + if is_withdraw_all && actual_withdraw_amount == initial_collateral { + assert_eq!( + final_collateral, 0, + "When withdrawing all available collateral, final collateral should be 0" + ); + } + + // INVARIANT 5: Vault solvency - vaults must hold at least reserves + total_collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 6: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 7: Debt amounts should not change during collateral removal + assert_eq!( + final_position.debt0_shares, initial_position.debt0_shares, + "Debt0 shares should not change when removing collateral" + ); + assert_eq!( + final_position.debt1_shares, initial_position.debt1_shares, + "Debt1 shares should not change when removing collateral" + ); + + // INVARIANT 8: Non-withdrawn collateral unchanged + if is_token0 { + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Non-withdrawn collateral (token1) should not change" + ); + } else { + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Non-withdrawn collateral (token0) should not change" + ); + } + + // INVARIANT 9: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "RemoveCollateral accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz/lending/repay.rs b/trident-tests/fuzz/lending/repay.rs new file mode 100644 index 0000000..e3580b4 --- /dev/null +++ b/trident-tests/fuzz/lending/repay.rs @@ -0,0 +1,350 @@ + + +use crate::{ + types::{ + omnipair::{self, RepayInstruction, RepayInstructionAccounts, RepayInstructionData}, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn repay(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_repay(); + let accounts = self.get_accounts_repay(); + + // Check if user position exists + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if user_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = user_position.unwrap(); + + let ix = RepayInstruction::data(RepayInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Repay")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientDebt, InsufficientAmount) + if res.is_success() { + self.verify_repay_invariants( + &data, + &accounts, + &initial_pair, + &initial_position, + initial_user_balance, + ); + } + } + + fn get_data_repay(&mut self) -> AdjustPositionArgs { + // Use small amounts more likely to match actual debt amounts + // Weighted distribution: favor smaller repayments + let amount = if self.trident.random_from_range(0..=9) < 7 { + self.trident.random_from_range(10..=10_000) + } else { + self.trident.random_from_range(10_000..=100_000) + }; + self.trident.record_histogram("REPAY_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn get_accounts_repay(&mut self) -> RepayInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + RepayInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_repay_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &RepayInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being repaid + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Calculate initial debt manually: debt = (user_shares * total_debt) / total_shares + let initial_debt = if is_token0 { + if initial_pair.total_debt0_shares == 0 { + 0 + } else { + ((initial_position.debt0_shares as u128) + .checked_mul(initial_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + } + } else if initial_pair.total_debt1_shares == 0 { + 0 + } else { + ((initial_position.debt1_shares as u128) + .checked_mul(initial_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + + // Calculate actual repay amount (may differ from args.amount if u64::MAX was used) + let is_repay_all = args.amount == u64::MAX; + let actual_repay_amount = initial_user_balance + .checked_sub(final_user_balance) + .expect("User balance should decrease"); + + // INVARIANT 1: User should have paid tokens for repayment + assert!( + actual_repay_amount > 0, + "User should have paid tokens for repayment" + ); + + // INVARIANT 2: If repaying all, verify the repay amount equals initial debt + if is_repay_all { + assert_eq!( + actual_repay_amount, initial_debt, + "Repay all should repay exactly the full debt amount" + ); + } + + // INVARIANT 3: Pair's total debt accounting + // Note: update() accrues interest before repayment, so we can't predict exact final debt + // The repayment reduces debt, but interest may have accrued first + // We verify the shares decrease correctly instead (which is the authoritative measure) + let (initial_total_debt, _) = if is_token0 { + (initial_pair.total_debt0, final_pair.total_debt0) + } else { + (initial_pair.total_debt1, final_pair.total_debt1) + }; + + // The final debt should be reasonable - not more than initial + potential interest + // and definitely less than initial if significant time hasn't passed + // Main check: verify shares decreased correctly (below) + + // INVARIANT 4: User position debt shares should decrease correctly + let (initial_debt_shares, final_debt_shares, initial_total_shares, final_total_shares) = + if is_token0 { + ( + initial_position.debt0_shares, + final_position.debt0_shares, + initial_pair.total_debt0_shares, + final_pair.total_debt0_shares, + ) + } else { + ( + initial_position.debt1_shares, + final_position.debt1_shares, + initial_pair.total_debt1_shares, + final_pair.total_debt1_shares, + ) + }; + + // Calculate expected shares decrease + let expected_shares = if is_repay_all { + // Repay all: use user's debt shares + initial_debt_shares + } else { + // shares = amount * total_shares / total_debt + ((actual_repay_amount as u128) + .checked_mul(initial_total_shares as u128) + .expect("Shares calculation") + .checked_div(initial_total_debt as u128) + .expect("Shares division")) as u64 + }; + + // Allow for 1-unit rounding tolerance in shares calculation + let expected_final_debt_shares = initial_debt_shares + .checked_sub(expected_shares) + .expect("User debt shares decrease"); + + // INVARIANT 5: If repay all, verify final debt shares are 0 + if is_repay_all { + assert_eq!( + final_debt_shares, 0, + "Repay all should result in zero debt shares" + ); + } else { + assert!( + final_debt_shares.abs_diff(expected_final_debt_shares) <= 1, + "User debt shares should decrease correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_debt_shares, + final_debt_shares + ); + } + + let expected_final_total_shares = initial_total_shares + .checked_sub(expected_shares) + .expect("Total debt shares decrease"); + assert!( + final_total_shares.abs_diff(expected_final_total_shares) <= 1, + "Pair total debt shares should decrease correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_total_shares, + final_total_shares + ); + + // INVARIANT 6: Vault solvency check - vault balance >= reserves + collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 7: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 8: Collateral amounts should not change during repay + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Collateral0 should not change during repay" + ); + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Collateral1 should not change during repay" + ); + + // INVARIANT 9: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "Repay accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz/liquidity/add_liquidity.rs b/trident-tests/fuzz/liquidity/add_liquidity.rs new file mode 100644 index 0000000..22a619b --- /dev/null +++ b/trident-tests/fuzz/liquidity/add_liquidity.rs @@ -0,0 +1,367 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{ + self, AddLiquidityInstruction, AddLiquidityInstructionAccounts, + AddLiquidityInstructionData, + }, + AddLiquidityArgs, Pair, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn add_liquidity(&mut self) { + // Check if any pairs exist before trying to add liquidity + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let accounts = self.get_accounts_add_liquidity(); + let data = self.get_data_add_liquidity(accounts.pair); + + let ix = AddLiquidityInstruction::data(AddLiquidityInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + // Store initial state + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + let initial_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let res = self + .trident + .process_transaction(&[ix], Some("Add Liquidity")); + + if res.is_success() { + self.verify_add_liquidity_invariants(&data, &accounts, &initial_pair, initial_user_lp); + } + } + + fn get_data_add_liquidity(&mut self, pair_pubkey: Pubkey) -> AddLiquidityArgs { + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + let total_supply = pair.total_supply; + + // Strategy: Test different liquidity addition scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Proportional liquidity (maintains price ratio) + 0..=39 => { + // Add liquidity proportional to current reserves + let proportion = self.trident.random_from_range(1..=1000); // 0.1% to 100% of reserves + let amount0_in = reserve0.saturating_mul(proportion) / 1000; + let amount1_in = reserve1.saturating_mul(proportion) / 1000; + + // Calculate expected liquidity: both ratios should be equal + let expected_liquidity = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap() as u64; + + // Set min_liquidity with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let min_liquidity_out = + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out, + } + } + + // 30% - Imbalanced liquidity (one side larger) + 40..=69 => { + // This will result in min(liquidity0, liquidity1) + let base_amount = self.trident.random_from_range(100..=100_000_000); + let imbalance_factor = self.trident.random_from_range(50..=200); // 0.5x to 2x + + let amount0_in: u64 = base_amount; + let amount1_in = base_amount.saturating_mul(imbalance_factor) / 100; + + // Calculate expected (will be the minimum) + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap(); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve1 as u128) + .unwrap(); + let expected = liquidity0.min(liquidity1) as u64; + + // Random slippage tolerance + let slippage_bps = self.trident.random_from_range(1..=500); + let min_liquidity_out = expected.saturating_mul(10000 - slippage_bps) / 10000; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out, + } + } + + // 15% - Small amounts (dust testing) + 70..=84 => { + let amount0_in = self.trident.random_from_range(1..=1000); + let amount1_in = self.trident.random_from_range(1..=1000); + + // Calculate expected + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap_or(1); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve1 as u128) + .unwrap_or(1); + let expected = liquidity0.min(liquidity1) as u64; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: expected.saturating_sub(1), // Allow for rounding + } + } + + // 10% - Large amounts (stress testing) + 85..=94 => { + let amount0_in = self.trident.random_from_range(1_000_000..=u64::MAX / 1000); + let amount1_in = self.trident.random_from_range(1_000_000..=u64::MAX / 1000); + + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap_or(u128::MAX) + .checked_div(reserve0 as u128) + .unwrap_or(u128::MAX); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap_or(u128::MAX) + .checked_div(reserve1 as u128) + .unwrap_or(u128::MAX); + let expected = liquidity0.min(liquidity1).min(u64::MAX as u128) as u64; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: expected / 2, // Large slippage tolerance + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero amounts (should fail with AmountZero) + 0 => AddLiquidityArgs { + amount0_in: 0, + amount1_in: self.trident.random_from_range(1..=1000), + min_liquidity_out: 0, + }, + 1 => AddLiquidityArgs { + amount0_in: self.trident.random_from_range(1..=1000), + amount1_in: 0, + min_liquidity_out: 0, + }, + // Unrealistic min_liquidity (should fail with InsufficientLiquidity) + 2 => { + let amount0_in = self.trident.random_from_range(100..=10000); + let amount1_in = self.trident.random_from_range(100..=10000); + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: u64::MAX, // Impossible to satisfy + } + } + // Overflow scenarios + 3 => AddLiquidityArgs { + amount0_in: u64::MAX, + amount1_in: u64::MAX, + min_liquidity_out: 0, + }, + // Random chaos + _ => AddLiquidityArgs { + amount0_in: self.trident.random_from_range(1..=u64::MAX), + amount1_in: self.trident.random_from_range(1..=u64::MAX), + min_liquidity_out: self.trident.random_from_range(1..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_add_liquidity(&mut self) -> AddLiquidityInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + //user token0 account + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + //user token1 account + let user_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + // user lp token account + let user_lp_token_account = + self.trident + .get_associated_token_address(&pair_account.lp_mint, &user, &TOKEN_PROGRAM); + + AddLiquidityInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token0_vault, + token1_vault, + user_token0_account, + user_token1_account, + pair_account.token0, + pair_account.token1, + pair_account.lp_mint, + user_lp_token_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_add_liquidity_invariants( + &mut self, + args: &AddLiquidityArgs, + accounts: &AddLiquidityInstructionAccounts, + initial_pair: &Pair, + initial_user_lp: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after add liquidity"); + + // Calculate expected liquidity using the same formula as the program + // liquidity = min(amount0_in * total_supply / reserve0, amount1_in * total_supply / reserve1) + let liquidity0 = (args.amount0_in as u128) + .checked_mul(initial_pair.total_supply as u128) + .unwrap() + .checked_div(initial_pair.reserve0 as u128) + .unwrap(); + let liquidity1 = (args.amount1_in as u128) + .checked_mul(initial_pair.total_supply as u128) + .unwrap() + .checked_div(initial_pair.reserve1 as u128) + .unwrap(); + let expected_liquidity = liquidity0.min(liquidity1) as u64; + + // Check pair reserves increased by at least the deposited amounts + // Note: reserves can increase more than deposited amounts due to LP share of accrued interest + let min_expected_reserve0 = initial_pair.reserve0.checked_add(args.amount0_in).unwrap(); + let min_expected_reserve1 = initial_pair.reserve1.checked_add(args.amount1_in).unwrap(); + assert!( + final_pair.reserve0 >= min_expected_reserve0, + "Pair reserve0 should be at least initial + amount0_in (was {}, expected >= {})", + final_pair.reserve0, + min_expected_reserve0 + ); + assert!( + final_pair.reserve1 >= min_expected_reserve1, + "Pair reserve1 should be at least initial + amount1_in (was {}, expected >= {})", + final_pair.reserve1, + min_expected_reserve1 + ); + + // Check pair total_supply increased by liquidity minted + let expected_total_supply = initial_pair + .total_supply + .checked_add(expected_liquidity) + .unwrap(); + assert_eq!( + final_pair.total_supply, expected_total_supply, + "Pair total_supply should increase by liquidity minted" + ); + + // Check user received the expected liquidity tokens + let final_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let expected_user_lp = initial_user_lp.checked_add(expected_liquidity).unwrap(); + assert_eq!( + final_user_lp, expected_user_lp, + "User should receive expected liquidity tokens" + ); + + // Check user received at least min_liquidity_out + let lp_received = final_user_lp.saturating_sub(initial_user_lp); + assert!( + lp_received >= args.min_liquidity_out, + "User should receive at least min_liquidity_out" + ); + + // Critical accounting invariant: vault balances should be >= pair reserves + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + assert!( + vault0_balance >= final_pair.reserve0, + "Token0 vault balance must be >= pair reserve0" + ); + assert!( + vault1_balance >= final_pair.reserve1, + "Token1 vault balance must be >= pair reserve1" + ); + } +} diff --git a/trident-tests/fuzz/liquidity/init_pair.rs b/trident-tests/fuzz/liquidity/init_pair.rs new file mode 100644 index 0000000..85916de --- /dev/null +++ b/trident-tests/fuzz/liquidity/init_pair.rs @@ -0,0 +1,361 @@ +use crate::{ + types::{ + omnipair::{ + self, InitializeInstruction, InitializeInstructionAccounts, InitializeInstructionData, + }, + InitializeAndBootstrapArgs, Pair, + }, + utils::{ + EVENT_AUTHORITY_ADDRESS, METADATA_SEED_PREFIX, + MPL_TOKEN_METADATA_ID, PAIR_SEED_PREFIX, TOKEN_PROGRAM, + }, + FuzzTest, +}; +use trident_fuzz::fuzzing::{solana_sdk::rent::Rent, *}; + +impl FuzzTest { + pub fn init_pair(&mut self) { + // Init pair and bootstrap + let data = self.get_data_init_pair(); + let accounts = self.get_accounts_init_pair(data.pair_nonce); + + let ix = InitializeInstruction::data(InitializeInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + // Store initial balances + let initial_deployer_token0 = self + .trident + .get_token_account(accounts.deployer_token0_account) + .expect("Deployer token0 account should exist") + .account + .amount; + let initial_deployer_token1 = self + .trident + .get_token_account(accounts.deployer_token1_account) + .expect("Deployer token1 account should exist") + .account + .amount; + let initial_deployer_sol = self.trident.get_account(&accounts.deployer).lamports(); + let initial_authority_wsol = self + .trident + .get_token_account(accounts.authority_wsol_account) + .expect("Authority WSOL token account should exist") + .account + .amount; + + let res = self.trident.process_transaction(&[ix], Some("Init Pair")); + + if res.is_success() { + // Initialization must NOT succeed when token0_mint == token1_mint + assert_ne!( + accounts.token0_mint, accounts.token1_mint, + "initialize succeeded with identical mints: token0_mint == token1_mint ({})", + accounts.token0_mint + ); + + self.store_accounts_init_pair(&accounts); + self.verify_init_pair_invariants( + &data, + &accounts, + initial_deployer_token0, + initial_deployer_token1, + initial_deployer_sol, + initial_authority_wsol, + ); + } + } + + fn get_data_init_pair(&mut self) -> InitializeAndBootstrapArgs { + let swap_fee_bps = self.trident.random_from_range(0..=10_000); // 0 to 100% + let half_life = self.trident.random_from_range(60..=12 * 60 * 60); // 1min to 12 hours + let fixed_cf_bps = self.trident.random_from_range(100..=10_000); // 1% to 100% + + self.trident + .record_histogram("INIT_PAIR_FIXED_CF_BPS", fixed_cf_bps as f64); + self.trident + .record_histogram("INIT_PAIR_HALF_LIFE", half_life as f64); + self.trident + .record_histogram("INIT_PAIR_SWAP_FEE_BPS", swap_fee_bps as f64); + + let fixed_cf_bps = if self.trident.random_from_range(0..=1) == 1 { + self.trident + .record_histogram("INIT_PAIR_FIXED_CF_BPS", fixed_cf_bps as f64); + Some(fixed_cf_bps) + } else { + None + }; + + let mut pair_nonce = [0u8; 16]; + self.trident.random_bytes(&mut pair_nonce); + + let amount0_in = self.trident.random_from_range(100..=100_000_000_000); + let amount1_in = self.trident.random_from_range(100..=100_000_000_000); + + self.trident + .record_histogram("INIT_PAIR_AMOUNT0_IN", amount0_in as f64); + self.trident + .record_histogram("INIT_PAIR_AMOUNT1_IN", amount1_in as f64); + + // Calculate expected liquidity using the same formula as the program + // liquidity = sqrt(amount0_in * amount1_in) - MIN_LIQUIDITY + let expected_liquidity = (amount0_in as u128) + .checked_mul(amount1_in as u128) + .map(|x| x.isqrt()) + .and_then(|x| x.checked_sub(1000)) // MIN_LIQUIDITY = 1000 + .unwrap_or(0) as u64; + + // Strategy: Test different scenarios + let min_liquidity_out = match self.trident.random_from_range(0..=100) { + // 70% - Normal case: small slippage tolerance (0-2%) + 0..=69 => { + let slippage_bps = self.trident.random_from_range(0..=200); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 15% - Tight slippage: very small tolerance (0-0.5%) + 70..=84 => { + let slippage_bps = self.trident.random_from_range(0..=50); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 10% - Large slippage: stress test (up to 50%) + 85..=94 => { + let slippage_bps = self.trident.random_from_range(0..=5000); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 5% - Edge cases (should sometimes fail) + _ => { + match self.trident.random_from_range(0..=4) { + 0 => 0, // No slippage protection + 1 => expected_liquidity + 1, // Impossible (expects more than possible) + 2 => u64::MAX, // Maximum value + 3 => expected_liquidity, // Exact match + _ => self.trident.random_from_range(1..=u64::MAX), // Random chaos + } + } + }; + + self.trident + .record_histogram("INIT_PAIR_MIN_LIQUIDITY_OUT", min_liquidity_out as f64); + + let lp_name = self.trident.random_string(10); + let lp_symbol = self.trident.random_string(10); + let lp_uri = self.trident.random_string(10); + + InitializeAndBootstrapArgs { + swap_fee_bps, + half_life, + fixed_cf_bps, + pair_nonce, + amount0_in, + amount1_in, + min_liquidity_out, + lp_name, + lp_symbol, + lp_uri: "https://".to_owned() + &lp_uri, + } + } + + fn get_accounts_init_pair(&mut self, pair_nonce: [u8; 16]) -> InitializeInstructionAccounts { + let token0_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token0 mint should exist"); + let mut token1_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token1 mint should exist"); + + while token0_mint == token1_mint { + token1_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token1 mint should exist"); + } + + // AKA deployer -> deploys pair + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let pair = self + .trident + .find_program_address( + &[ + PAIR_SEED_PREFIX, + token0_mint.as_ref(), + token1_mint.as_ref(), + pair_nonce.as_ref(), + ], + &omnipair::program_id(), + ) + .0; + + // futarchy authority PDA + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // rate model + let rate_model = self.trident.random_keypair().pubkey(); + + // lp mint + + let lp_mint = self.trident.random_keypair().pubkey(); + let rent = self.trident.get_sysvar::(); + let account_custom = AccountSharedData::new(rent.minimum_balance(82), 82, &TOKEN_PROGRAM); + self.trident.set_account_custom(&lp_mint, &account_custom); + + // lp token metadata + let lp_token_metadata = self + .trident + .find_program_address( + &[ + METADATA_SEED_PREFIX, + MPL_TOKEN_METADATA_ID.as_ref(), + lp_mint.as_ref(), + ], + &MPL_TOKEN_METADATA_ID, + ) + .0; + + // deployer lp token account + let deployer_lp_token_account = + self.trident + .get_associated_token_address(&lp_mint, &user, &TOKEN_PROGRAM); + + // token0 vault + let token0_vault = + self.trident + .get_associated_token_address(&token0_mint, &pair, &TOKEN_PROGRAM); + + // token1 vault + let token1_vault = + self.trident + .get_associated_token_address(&token1_mint, &pair, &TOKEN_PROGRAM); + + // deployer token0 account + let deployer_token0_account = + self.trident + .get_associated_token_address(&token0_mint, &user, &TOKEN_PROGRAM); + + // deployer token1 account + let deployer_token1_account = + self.trident + .get_associated_token_address(&token1_mint, &user, &TOKEN_PROGRAM); + + InitializeInstructionAccounts::new( + user, + token0_mint, + token1_mint, + pair, + futarchy_authority, + rate_model, + lp_mint, + lp_token_metadata, + deployer_lp_token_account, + token0_vault, + token1_vault, + deployer_token0_account, + deployer_token1_account, + self.fuzz_accounts + .authority_wsol_account + .get(&mut self.trident).expect("Authority WSOL account should exist"), + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn store_accounts_init_pair(&mut self, accounts: &InitializeInstructionAccounts) { + self.fuzz_accounts.pair.insert_with_address(accounts.pair); + self.fuzz_accounts + .rate_model + .insert_with_address(accounts.rate_model); + self.fuzz_accounts + .lp_mint + .insert_with_address(accounts.lp_mint); + } + + fn verify_init_pair_invariants( + &mut self, + args: &InitializeAndBootstrapArgs, + accounts: &InitializeInstructionAccounts, + _initial_deployer_token0: u64, + _initial_deployer_token1: u64, + _initial_deployer_sol: u64, + initial_authority_wsol: u64, + ) { + const PAIR_CREATION_FEE_LAMPORTS: u64 = 200_000_000; // 0.2 SOL + const MIN_LIQUIDITY: u64 = 1000; + + // Calculate expected liquidity using the same formula as the program + // liquidity = sqrt(amount0_in * amount1_in) - MIN_LIQUIDITY + let expected_liquidity = (args.amount0_in as u128) + .checked_mul(args.amount1_in as u128) + .map(|x| x.isqrt()) + .and_then(|x| x.checked_sub(MIN_LIQUIDITY as u128)) + .unwrap_or(0) as u64; + + // Check pair state (fresh for each new pair) + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair account should exist"); + + // Check pair reserves match amounts deposited + assert_eq!( + pair_account.reserve0, args.amount0_in, + "Pair reserve0 should match amount0_in" + ); + assert_eq!( + pair_account.reserve1, args.amount1_in, + "Pair reserve1 should match amount1_in" + ); + + // Check pair total_supply includes MIN_LIQUIDITY (locked/burned permanently) + assert_eq!( + pair_account.total_supply, + expected_liquidity + MIN_LIQUIDITY, + "Pair total_supply should match total liquidity (including locked MIN_LIQUIDITY)" + ); + + // Check pair parameters + assert_eq!( + pair_account.swap_fee_bps, args.swap_fee_bps, + "Pair swap_fee_bps should match args" + ); + assert_eq!( + pair_account.half_life, args.half_life, + "Pair half_life should match args" + ); + assert_eq!( + pair_account.fixed_cf_bps, args.fixed_cf_bps, + "Pair fixed_cf_bps should match args" + ); + + // Check vault balances (vaults might be reused in fuzzing, so check >= pair reserves) + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + // Critical accounting invariant: vault balances must be at least pair reserves + assert!( + vault0_balance >= pair_account.reserve0, + "Token0 vault balance must be >= pair reserve0" + ); + assert!( + vault1_balance >= pair_account.reserve1, + "Token1 vault balance must be >= pair reserve1" + ); + + // Check authority WSOL balance increased by pair creation fee + let final_authority_wsol = self + .trident + .get_token_account(accounts.authority_wsol_account) + .expect("Authority WSOL account should exist") + .account + .amount; + assert!( + final_authority_wsol >= initial_authority_wsol + PAIR_CREATION_FEE_LAMPORTS, + "Authority WSOL balance should increase by at least pair creation fee" + ); + } +} diff --git a/trident-tests/fuzz/liquidity/mod.rs b/trident-tests/fuzz/liquidity/mod.rs new file mode 100644 index 0000000..1774d85 --- /dev/null +++ b/trident-tests/fuzz/liquidity/mod.rs @@ -0,0 +1,3 @@ +mod add_liquidity; +mod init_pair; +mod remove_liquidity; diff --git a/trident-tests/fuzz/liquidity/remove_liquidity.rs b/trident-tests/fuzz/liquidity/remove_liquidity.rs new file mode 100644 index 0000000..12206fc --- /dev/null +++ b/trident-tests/fuzz/liquidity/remove_liquidity.rs @@ -0,0 +1,463 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{ + self, RemoveLiquidityInstruction, RemoveLiquidityInstructionAccounts, + RemoveLiquidityInstructionData, + }, + Pair, RemoveLiquidityArgs, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn remove_liquidity(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_remove_liquidity(); + + // Check if user has any LP token account initialized + if self + .trident + .get_token_account(accounts.user_lp_token_account) + .is_err() + { + // No LP token account found, skip + return; + } + + let data = self.get_data_remove_liquidity(accounts.pair, accounts.user_lp_token_account); + + self.trident + .record_histogram("REMOVE_LIQUIDITY_LIQUIDITY_IN", data.liquidity_in as f64); + self.trident.record_histogram( + "REMOVE_LIQUIDITY_MIN_AMOUNT0_OUT", + data.min_amount0_out as f64, + ); + self.trident.record_histogram( + "REMOVE_LIQUIDITY_MIN_AMOUNT1_OUT", + data.min_amount1_out as f64, + ); + + // Store initial state + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + let initial_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let initial_user_token0 = self + .trident + .get_token_account(accounts.user_token0_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + let initial_user_token1 = self + .trident + .get_token_account(accounts.user_token1_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = + RemoveLiquidityInstruction::data(RemoveLiquidityInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Remove Liquidity")); + + if res.is_success() { + self.verify_remove_liquidity_invariants( + &data, + &accounts, + &initial_pair, + initial_user_lp, + initial_user_token0, + initial_user_token1, + ); + } + } + + fn get_data_remove_liquidity( + &mut self, + pair_pubkey: Pubkey, + user_lp_token_account: Pubkey, + ) -> RemoveLiquidityArgs { + let user_lp_balance = self + .trident + .get_token_account(user_lp_token_account) + .expect("User LP token account should exist") + .account + .amount; + + // Early return if user has no LP tokens to avoid empty range errors + if user_lp_balance == 0 { + return RemoveLiquidityArgs { + liquidity_in: 0, + min_amount0_out: 0, + min_amount1_out: 0, + }; + } + + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + let total_supply = pair.total_supply; + + // Strategy: Test different liquidity removal scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Proportional removal (remove a percentage of user's LP tokens) + 0..=39 => { + // Remove between 1% and 100% of user's LP tokens + let percentage = self.trident.random_from_range(1..=100); + let liquidity_in = user_lp_balance + .saturating_mul(percentage) + .checked_div(100) + .unwrap_or(0); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + // Set min amounts with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let slippage_multiplier = 10000u64.checked_sub(slippage_bps).unwrap_or(10000); + let min_amount0_out = amount0_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + let min_amount1_out = amount1_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out, + min_amount1_out, + } + } + + // 30% - Small amounts (dust testing) + 40..=69 => { + let liquidity_in = self + .trident + .random_from_range(1..=1000.min(user_lp_balance.max(1))); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out: amount0_out.saturating_sub(1), // Allow for rounding + min_amount1_out: amount1_out.saturating_sub(1), + } + } + + // 15% - Remove all liquidity + 70..=84 => { + let liquidity_in = user_lp_balance; + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + // Allow for some slippage + let slippage_bps = self.trident.random_from_range(0..=500); + let slippage_multiplier = 10000u64.checked_sub(slippage_bps).unwrap_or(10000); + let min_amount0_out = amount0_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + let min_amount1_out = amount1_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out, + min_amount1_out, + } + } + + // 10% - Unrealistic expectations (should fail) + 85..=94 => { + let liquidity_in = self + .trident + .random_from_range(1..=user_lp_balance.max(1000)); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + // Set unrealistic min amounts (should fail with InsufficientOutput) + let multiplier = self.trident.random_from_range(2..=10); + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out: amount0_out.saturating_mul(multiplier), + min_amount1_out: amount1_out.saturating_mul(multiplier), + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero liquidity (should fail with AmountZero) + 0 => RemoveLiquidityArgs { + liquidity_in: 0, + min_amount0_out: 0, + min_amount1_out: 0, + }, + // More liquidity than user has (should fail with InsufficientBalance) + 1 => RemoveLiquidityArgs { + liquidity_in: user_lp_balance.saturating_add(1_000_000), + min_amount0_out: 0, + min_amount1_out: 0, + }, + // Unrealistic min amounts + 2 => RemoveLiquidityArgs { + liquidity_in: self.trident.random_from_range(1..=1000), + min_amount0_out: u64::MAX, + min_amount1_out: u64::MAX, + }, + // Overflow scenarios + 3 => RemoveLiquidityArgs { + liquidity_in: u64::MAX, + min_amount0_out: 0, + min_amount1_out: 0, + }, + // Random chaos + _ => RemoveLiquidityArgs { + liquidity_in: self.trident.random_from_range(1..=u64::MAX), + min_amount0_out: self.trident.random_from_range(0..=u64::MAX), + min_amount1_out: self.trident.random_from_range(0..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_remove_liquidity(&mut self) -> RemoveLiquidityInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // user (reuse existing user or create new one) + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + // user token0 account + let user_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + // user token1 account + let user_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + // user lp token account + let user_lp_token_account = + self.trident + .get_associated_token_address(&pair_account.lp_mint, &user, &TOKEN_PROGRAM); + + RemoveLiquidityInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token0_vault, + token1_vault, + user_token0_account, + user_token1_account, + pair_account.token0, + pair_account.token1, + pair_account.lp_mint, + user_lp_token_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + #[allow(clippy::too_many_arguments)] + fn verify_remove_liquidity_invariants( + &mut self, + args: &RemoveLiquidityArgs, + accounts: &RemoveLiquidityInstructionAccounts, + initial_pair: &Pair, + initial_user_lp: u64, + initial_user_token0: u64, + initial_user_token1: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after remove liquidity"); + + // Get actual user balances to determine what they received + let final_user_token0 = self + .trident + .get_token_account(accounts.user_token0_account) + .expect("User token0 account should exist") + .account + .amount; + let final_user_token1 = self + .trident + .get_token_account(accounts.user_token1_account) + .expect("User token1 account should exist") + .account + .amount; + + // Calculate actual amounts received by user + let actual_amount0_received = final_user_token0.checked_sub(initial_user_token0).unwrap(); + let actual_amount1_received = final_user_token1.checked_sub(initial_user_token1).unwrap(); + + // Check amounts received meet minimum requirements (slippage protection) + assert!( + actual_amount0_received >= args.min_amount0_out, + "User should receive at least min_amount0_out" + ); + assert!( + actual_amount1_received >= args.min_amount1_out, + "User should receive at least min_amount1_out" + ); + + // NOTE: We cannot directly check reserve deltas because pair.update() accrues interest + // during the transaction, which adds to reserves. This makes the net reserve decrease + // smaller than what the user received. Instead, we verify: + // 1. User received what they expected (checked above) + // 2. LP tokens were burned correctly (checked below) + // 3. Vault solvency is maintained (checked below) + + // Check pair total_supply decreased by liquidity burned + let expected_total_supply = initial_pair + .total_supply + .checked_sub(args.liquidity_in) + .unwrap(); + assert_eq!( + final_pair.total_supply, expected_total_supply, + "Pair total_supply should decrease by liquidity_in" + ); + + // Check user LP balance decreased by liquidity_in + let final_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let expected_user_lp = initial_user_lp.checked_sub(args.liquidity_in).unwrap(); + assert_eq!( + final_user_lp, expected_user_lp, + "User LP balance should decrease by liquidity_in" + ); + + // Critical accounting invariant: vault balances must be >= pair reserves + collateral + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + // Vaults must have enough for reserves + collateral combined + let required_vault0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Overflow calculating required vault0"); + let required_vault1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Overflow calculating required vault1"); + + assert!( + vault0_balance >= required_vault0, + "Token0 vault balance must be >= reserve0 + total_collateral0. Vault: {}, Required: {}", + vault0_balance, + required_vault0 + ); + assert!( + vault1_balance >= required_vault1, + "Token1 vault balance must be >= reserve1 + total_collateral1. Vault: {}, Required: {}", + vault1_balance, + required_vault1 + ); + } +} diff --git a/trident-tests/fuzz/spot/mod.rs b/trident-tests/fuzz/spot/mod.rs new file mode 100644 index 0000000..04e62b0 --- /dev/null +++ b/trident-tests/fuzz/spot/mod.rs @@ -0,0 +1 @@ +mod swap; diff --git a/trident-tests/fuzz/spot/swap.rs b/trident-tests/fuzz/spot/swap.rs new file mode 100644 index 0000000..7d622e2 --- /dev/null +++ b/trident-tests/fuzz/spot/swap.rs @@ -0,0 +1,412 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{self, SwapInstruction, SwapInstructionAccounts, SwapInstructionData}, + Pair, SwapArgs, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn swap(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_swap(); + let data = self.get_data_swap(accounts.pair, accounts.token_in_mint); + + // record histogram + self.trident + .record_histogram("SWAP_AMOUNT_IN", data.amount_in as f64); + self.trident + .record_histogram("SWAP_MIN_AMOUNT_OUT", data.min_amount_out as f64); + + // Store initial USER state only (pair state will be modified by update() during transaction) + let initial_user_token_in = self + .trident + .get_token_account(accounts.user_token_in_account) + .expect("User token in account should exist") + .account + .amount; + let initial_user_token_out = self + .trident + .get_token_account(accounts.user_token_out_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + let initial_authority_token_in = self + .trident + .get_token_account(accounts.authority_token_in_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = SwapInstruction::data(SwapInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Swap")); + + if res.is_success() { + self.verify_swap_invariants( + &data, + &accounts, + initial_user_token_in, + initial_user_token_out, + initial_authority_token_in, + ); + } + } + + fn get_data_swap(&mut self, pair_pubkey: Pubkey, token_in_mint: Pubkey) -> SwapArgs { + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .unwrap(); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + + // Determine which token we're swapping in and get the corresponding reserves + let (reserve_in, reserve_out) = if token_in_mint == pair.token0 { + (reserve0, reserve1) + } else { + (reserve1, reserve0) + }; + + // Early return if reserves are too low to avoid empty range errors + if reserve_in == 0 || reserve_out == 0 { + return SwapArgs { + amount_in: 100, + min_amount_out: 0, + }; + } + + // Strategy: Test different swap scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Small to medium swaps (0.1% to 10% of reserve) + 0..=39 => { + let percentage = self.trident.random_from_range(1..=1000); // 0.1% to 100% + let amount_in = reserve_in.saturating_mul(percentage) / 10000; // 0.01% to 10% + let amount_in = amount_in.max(100); // Minimum 100 tokens + + // Calculate expected output using constant product formula + // amount_out = (amount_in * reserve_out) / (reserve_in + amount_in) + // With fees: amount_in_after_fee = amount_in * (10000 - fee_bps) / 10000 + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Set min_amount_out with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let min_amount_out = expected_out.saturating_mul(10000 - slippage_bps) / 10000; + + SwapArgs { + amount_in, + min_amount_out, + } + } + + // 30% - Larger swaps (10% to 50% of reserve) + 40..=69 => { + let percentage = self.trident.random_from_range(1000..=5000); // 10% to 50% + let amount_in = reserve_in.saturating_mul(percentage) / 10000; + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Larger slippage tolerance for bigger swaps + let slippage_bps = self.trident.random_from_range(100..=1000); + let min_amount_out = expected_out.saturating_mul(10000 - slippage_bps) / 10000; + + SwapArgs { + amount_in, + min_amount_out, + } + } + + // 15% - Dust amounts (very small swaps) + 70..=84 => { + let amount_in = self.trident.random_from_range(1..=1000); + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + SwapArgs { + amount_in, + min_amount_out: expected_out.saturating_sub(1), // Allow for rounding + } + } + + // 10% - Unrealistic expectations (should fail) + 85..=94 => { + let amount_in = self.trident.random_from_range(100..=100_000_000); + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Set unrealistic min_amount_out (should fail with InsufficientOutput) + let multiplier = self.trident.random_from_range(2..=10); + SwapArgs { + amount_in, + min_amount_out: expected_out.saturating_mul(multiplier), + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero amount (should fail with AmountZero) + 0 => SwapArgs { + amount_in: 0, + min_amount_out: 0, + }, + // Unrealistic min_amount_out + 1 => SwapArgs { + amount_in: self.trident.random_from_range(1..=1000), + min_amount_out: u64::MAX, + }, + // Swap entire reserve (should fail or cause extreme slippage) + 2 => SwapArgs { + amount_in: reserve_in, + min_amount_out: 0, + }, + // Overflow scenarios + 3 => SwapArgs { + amount_in: u64::MAX, + min_amount_out: 0, + }, + // Random chaos + _ => SwapArgs { + amount_in: self.trident.random_from_range(1..=u64::MAX), + min_amount_out: self.trident.random_from_range(0..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_swap(&mut self) -> SwapInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .unwrap(); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Randomly choose swap direction: token0 -> token1 or token1 -> token0 + let is_token0_in = self.trident.random_from_range(0..=1) == 0; + + let (token_in_mint, token_out_mint) = if is_token0_in { + (pair_account.token0, pair_account.token1) + } else { + (pair_account.token1, pair_account.token0) + }; + + // token vaults + let token_in_vault = + self.trident + .get_associated_token_address(&token_in_mint, &pair_pubkey, &TOKEN_PROGRAM); + + let token_out_vault = self.trident.get_associated_token_address( + &token_out_mint, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // user + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + // user token account + let user_token_in_account = + self.trident + .get_associated_token_address(&token_in_mint, &user, &TOKEN_PROGRAM); + + let user_token_out_account = + self.trident + .get_associated_token_address(&token_out_mint, &user, &TOKEN_PROGRAM); + + // authority token account (for fees) + let authority_token_in_account = self.trident.get_associated_token_address( + &token_in_mint, + &futarchy_authority_pubkey, + &TOKEN_PROGRAM, + ); + + if self + .trident + .get_token_account(authority_token_in_account) + .is_err() + { + self.trident.initialize_associated_token_account( + &user, + &token_in_mint, + &futarchy_authority_pubkey, + ); + } + + SwapInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token_in_vault, + token_out_vault, + user_token_in_account, + user_token_out_account, + token_in_mint, + token_out_mint, + authority_token_in_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_swap_invariants( + &mut self, + args: &SwapArgs, + accounts: &SwapInstructionAccounts, + initial_user_token_in: u64, + initial_user_token_out: u64, + initial_authority_token_in: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after swap"); + + // Get final user balances + let final_user_token_in = self + .trident + .get_token_account(accounts.user_token_in_account) + .expect("User token in account should exist") + .account + .amount; + let final_user_token_out = self + .trident + .get_token_account(accounts.user_token_out_account) + .expect("User token out account should exist") + .account + .amount; + + // Calculate actual amounts + let actual_amount_in = initial_user_token_in + .checked_sub(final_user_token_in) + .unwrap(); + let actual_amount_out = final_user_token_out + .checked_sub(initial_user_token_out) + .unwrap(); + + // Verify user paid exactly amount_in + assert_eq!( + actual_amount_in, args.amount_in, + "User should pay exactly amount_in" + ); + + // Verify user received at least min_amount_out (slippage protection) + assert!( + actual_amount_out >= args.min_amount_out, + "User should receive at least min_amount_out" + ); + + // Verify constant product invariant: k should not decrease + // Note: k may increase slightly due to fees staying in the pool + let final_k = (final_pair.reserve0 as u128) + .checked_mul(final_pair.reserve1 as u128) + .unwrap(); + + // We can't check k against initial because update() modifies reserves with interest + // But we can verify k is reasonable and reserves are positive + assert!(final_k > 0, "Constant product k must be positive"); + assert!( + final_pair.reserve0 > 0, + "Reserve0 must be positive after swap" + ); + assert!( + final_pair.reserve1 > 0, + "Reserve1 must be positive after swap" + ); + + // Verify authority received futarchy fee (if any) + let final_authority_token_in = self + .trident + .get_token_account(accounts.authority_token_in_account) + .expect("Authority token in account should exist") + .account + .amount; + + let authority_fee_received = final_authority_token_in + .checked_sub(initial_authority_token_in) + .unwrap(); + + // Authority should receive some fee (unless swap is too small or fees are 0) + // We verify it's reasonable: futarchy fee = total_fee * revenue_share_bps / 10000 + // where total_fee = amount_in * swap_fee_bps / 10000 + if args.amount_in > 1000 && final_pair.swap_fee_bps > 0 { + assert!( + authority_fee_received > 0, + "Authority should receive futarchy fee for non-trivial swaps" + ); + } + + // Critical accounting invariant: vault balances should be >= pair reserves + let token_in_vault_balance = self + .trident + .get_token_account(accounts.token_in_vault) + .expect("Token in vault should exist") + .account + .amount; + let token_out_vault_balance = self + .trident + .get_token_account(accounts.token_out_vault) + .expect("Token out vault should exist") + .account + .amount; + + // Determine which reserve corresponds to which vault + let is_token0_in = accounts.token_in_mint == final_pair.token0; + let (reserve_in, reserve_out) = if is_token0_in { + (final_pair.reserve0, final_pair.reserve1) + } else { + (final_pair.reserve1, final_pair.reserve0) + }; + + assert!( + token_in_vault_balance >= reserve_in, + "Token in vault balance must be >= corresponding reserve" + ); + assert!( + token_out_vault_balance >= reserve_out, + "Token out vault balance must be >= corresponding reserve" + ); + } +} diff --git a/trident-tests/fuzz/test_fuzz.rs b/trident-tests/fuzz/test_fuzz.rs new file mode 100644 index 0000000..ae1fd6b --- /dev/null +++ b/trident-tests/fuzz/test_fuzz.rs @@ -0,0 +1,159 @@ +use fuzz_accounts::*; +use trident_fuzz::fuzzing::*; + +use crate::{ + types::omnipair, + utils::{DEPLOYER_ADDRESS, FUTARCHY_AUTHORITY_SEED_PREFIX, TOKEN_PROGRAM, WSOL_MINT_ADDRESS}, +}; +mod futarchy; +mod fuzz_accounts; +mod lending; +mod liquidity; +mod spot; +mod types; +mod utils; +mod view; + +const USER_COUNT: usize = 100; +const TOKEN_MINT_COUNT: usize = 5; + +#[derive(FuzzTestMethods)] +struct FuzzTest { + /// Trident client for interacting with the Solana program + trident: Trident, + /// Storage for all account addresses used in fuzz testing + fuzz_accounts: AccountAddresses, +} + +#[flow_executor] +impl FuzzTest { + fn new() -> Self { + Self { + trident: Trident::default(), + fuzz_accounts: AccountAddresses::default(), + } + } + + #[init] + fn start(&mut self) { + self.setup_accounts(); + + self.init_futarchy(); + self.init_pair(); + } + + #[flow(weight = 100)] + fn flow(&mut self) { + // Liquidity operations + self.add_liquidity(); + self.swap(); + + // Lending operations + self.add_collateral(); + self.borrow(); + self.repay(); + self.remove_collateral(); + + // Advanced operations + self.liquidate(); + self.flashloan(); + } + + #[end] + fn end(&mut self) {} + + fn setup_accounts(&mut self) { + // Airdrop DEPLOYER_ADDRESS for transaction fees + self.trident.airdrop( + &DEPLOYER_ADDRESS, + LAMPORTS_PER_SOL + .checked_mul(5) + .expect("Airdrop amount overflow"), + ); + + let mut users = Vec::new(); + + // Pre-create user accounts + for _ in 0..USER_COUNT { + let user = self.fuzz_accounts.user.insert(&mut self.trident, None); + users.push(user); + self.trident.airdrop( + &user, + LAMPORTS_PER_SOL + .checked_mul(1000) + .expect("Airdrop amount overflow"), + ); + } + + // Pre-create futarchy authority account + let futarchy_authority = self.fuzz_accounts.futarchy_authority.insert( + &mut self.trident, + Some(PdaSeeds { + seeds: &[FUTARCHY_AUTHORITY_SEED_PREFIX], + program_id: omnipair::program_id(), + }), + ); + + // Pre-create authority wsol account + let authority_wsol_account = self.trident.get_associated_token_address( + &WSOL_MINT_ADDRESS, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + let ix = self.trident.initialize_associated_token_account( + &DEPLOYER_ADDRESS, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + self.trident.process_transaction(&[ix], None); + + self.fuzz_accounts + .authority_wsol_account + .insert_with_address(authority_wsol_account); + + // Pre initialize some token mints + for _ in 0..TOKEN_MINT_COUNT { + let token_mint = self + .fuzz_accounts + .token_mint + .insert(&mut self.trident, None); + + let mint_authority = self.fuzz_accounts.user.get(&mut self.trident).expect("Mint authority should exist"); + let ix = self.trident.initialize_mint( + &mint_authority, + &token_mint, + 9, + &mint_authority, + None, + ); + let res = self.trident.process_transaction(&ix, None); + assert!(res.is_success()); + + // Generate token account for users + for user in users.iter() { + // User token account + let initialize_user_token_account_ix = self + .trident + .initialize_associated_token_account(user, &token_mint, user); + let user_token_account = + self.trident + .get_associated_token_address(&token_mint, user, &TOKEN_PROGRAM); + let mint_to_user_ix = self.trident.mint_to( + &user_token_account, + &token_mint, + &mint_authority, + 100_000_000_000_000_000, + ); + let res = self.trident.process_transaction( + &[initialize_user_token_account_ix, mint_to_user_ix], + None, + ); + assert!(res.is_success()); + } + } + } +} + +fn main() { + FuzzTest::fuzz(1000, 100); +} diff --git a/trident-tests/fuzz/types.rs b/trident-tests/fuzz/types.rs new file mode 100644 index 0000000..a6e1c16 --- /dev/null +++ b/trident-tests/fuzz/types.rs @@ -0,0 +1,4739 @@ +//! # Trident Generated Types +//! +//! This file is automatically generated by Trident. +//! **DO NOT EDIT THIS FILE MANUALLY** + +#![allow(dead_code)] +#![allow(unused_imports)] + +use borsh::BorshDeserialize; +use borsh::BorshSerialize; +use trident_fuzz::fuzzing::*; + +// ============================================================================ +// PROGRAM MODULES +// ============================================================================ + +// ---------------------------------------------------------------------------- +// Program: faucet +// ---------------------------------------------------------------------------- +pub mod faucet { + use super::*; + + // ------------------------------------------------------------------------ + // Program ID + // ------------------------------------------------------------------------ + + /// Returns the program ID for faucet + pub fn program_id() -> Pubkey { + pubkey!("3Ckfn1LMByoDfVpcDPf7nouk5nQAUm51Zkdf1oprQTAK") + } + + // ------------------------------------------------------------------------ + // Instructions + // ------------------------------------------------------------------------ + + // .................................................................... + // Instruction: FaucetMint + // .................................................................... + + /// Main instruction struct for FaucetMint + pub struct FaucetMintInstruction { + pub accounts: FaucetMintInstructionAccountMetas, + pub data: FaucetMintInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for FaucetMint instruction + #[derive(Debug, Clone, Default)] + pub struct FaucetMintInstructionAccountMetas { + pub user: AccountMeta, + + pub faucet_authority: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub system_program: AccountMeta, + + pub token_program: AccountMeta, + + pub associated_token_program: AccountMeta, + } + + /// Account pubkeys for FaucetMint instruction + #[derive(Debug, Clone)] + pub struct FaucetMintInstructionAccounts { + pub user: Pubkey, + + pub faucet_authority: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + } + + impl FaucetMintInstructionAccounts { + pub fn new( + user: Pubkey, + + faucet_authority: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + ) -> Self { + Self { + user, + + faucet_authority, + + user_token0_account, + + user_token1_account, + + token0_mint, + + token1_mint, + } + } + } + + /// Instruction data for FaucetMint + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct FaucetMintInstructionData {} + + impl FaucetMintInstructionData { + pub fn new() -> Self { + Self {} + } + } + + /// Implementation for FaucetMintInstruction + impl FaucetMintInstruction { + fn discriminator() -> [u8; 8] { + [47u8, 229u8, 221u8, 88u8, 0u8, 56u8, 156u8, 38u8] + } + + pub fn data(data: FaucetMintInstructionData) -> Self { + Self { + accounts: FaucetMintInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: FaucetMintInstructionAccounts) -> Self { + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.faucet_authority = + AccountMeta::new_readonly(accounts.faucet_authority, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_mint = AccountMeta::new(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new(accounts.token1_mint, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.faucet_authority.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // ------------------------------------------------------------------------ + // Composite Accounts + // ------------------------------------------------------------------------ +} + +// ---------------------------------------------------------------------------- +// Program: omnipair +// ---------------------------------------------------------------------------- +pub mod omnipair { + use super::*; + + // ------------------------------------------------------------------------ + // Program ID + // ------------------------------------------------------------------------ + + /// Returns the program ID for omnipair + pub fn program_id() -> Pubkey { + pubkey!("Bd9Uhf5S8yzfop8cG9oqRs6jVcLtu8B4cb2gvRmtbNzk") + } + + // ------------------------------------------------------------------------ + // Instructions + // ------------------------------------------------------------------------ + + // .................................................................... + // Instruction: AddCollateral + // .................................................................... + + /// Main instruction struct for AddCollateral + pub struct AddCollateralInstruction { + pub accounts: AddCollateralInstructionAccountMetas, + pub data: AddCollateralInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for AddCollateral instruction + #[derive(Debug, Clone, Default)] + pub struct AddCollateralInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub user_position: AccountMeta, + + pub collateral_vault: AccountMeta, + + pub user_collateral_token_account: AccountMeta, + + pub collateral_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for AddCollateral instruction + #[derive(Debug, Clone)] + pub struct AddCollateralInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub user_position: Pubkey, + + pub collateral_vault: Pubkey, + + pub user_collateral_token_account: Pubkey, + + pub collateral_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl AddCollateralInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + user_position: Pubkey, + + collateral_vault: Pubkey, + + user_collateral_token_account: Pubkey, + + collateral_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + user_position, + + collateral_vault, + + user_collateral_token_account, + + collateral_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for AddCollateral + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct AddCollateralInstructionData { + pub args: AdjustPositionArgs, + } + + impl AddCollateralInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for AddCollateralInstruction + impl AddCollateralInstruction { + fn discriminator() -> [u8; 8] { + [127u8, 82u8, 121u8, 42u8, 161u8, 176u8, 249u8, 206u8] + } + + pub fn data(data: AddCollateralInstructionData) -> Self { + Self { + accounts: AddCollateralInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: AddCollateralInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.collateral_vault = AccountMeta::new(accounts.collateral_vault, false); + + self.accounts.user_collateral_token_account = + AccountMeta::new(accounts.user_collateral_token_account, false); + + self.accounts.collateral_token_mint = + AccountMeta::new_readonly(accounts.collateral_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.collateral_vault.clone()); + + metas.push(self.accounts.user_collateral_token_account.clone()); + + metas.push(self.accounts.collateral_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: AddLiquidity + // .................................................................... + + /// Main instruction struct for AddLiquidity + pub struct AddLiquidityInstruction { + pub accounts: AddLiquidityInstructionAccountMetas, + pub data: AddLiquidityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for AddLiquidity instruction + #[derive(Debug, Clone, Default)] + pub struct AddLiquidityInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_vault_mint: AccountMeta, + + pub token1_vault_mint: AccountMeta, + + pub lp_mint: AccountMeta, + + pub user_lp_token_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for AddLiquidity instruction + #[derive(Debug, Clone)] + pub struct AddLiquidityInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_vault_mint: Pubkey, + + pub token1_vault_mint: Pubkey, + + pub lp_mint: Pubkey, + + pub user_lp_token_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl AddLiquidityInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_vault_mint: Pubkey, + + token1_vault_mint: Pubkey, + + lp_mint: Pubkey, + + user_lp_token_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + user_token0_account, + + user_token1_account, + + token0_vault_mint, + + token1_vault_mint, + + lp_mint, + + user_lp_token_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for AddLiquidity + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct AddLiquidityInstructionData { + pub args: AddLiquidityArgs, + } + + impl AddLiquidityInstructionData { + pub fn new(args: AddLiquidityArgs) -> Self { + Self { args } + } + } + + /// Implementation for AddLiquidityInstruction + impl AddLiquidityInstruction { + fn discriminator() -> [u8; 8] { + [181u8, 157u8, 89u8, 67u8, 143u8, 182u8, 52u8, 72u8] + } + + pub fn data(data: AddLiquidityInstructionData) -> Self { + Self { + accounts: AddLiquidityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: AddLiquidityInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_vault_mint = + AccountMeta::new_readonly(accounts.token0_vault_mint, false); + + self.accounts.token1_vault_mint = + AccountMeta::new_readonly(accounts.token1_vault_mint, false); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.user_lp_token_account = + AccountMeta::new(accounts.user_lp_token_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_vault_mint.clone()); + + metas.push(self.accounts.token1_vault_mint.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.user_lp_token_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Borrow + // .................................................................... + + /// Main instruction struct for Borrow + pub struct BorrowInstruction { + pub accounts: BorrowInstructionAccountMetas, + pub data: BorrowInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Borrow instruction + #[derive(Debug, Clone, Default)] + pub struct BorrowInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Borrow instruction + #[derive(Debug, Clone)] + pub struct BorrowInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl BorrowInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Borrow + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct BorrowInstructionData { + pub args: AdjustPositionArgs, + } + + impl BorrowInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for BorrowInstruction + impl BorrowInstruction { + fn discriminator() -> [u8; 8] { + [228u8, 253u8, 131u8, 202u8, 207u8, 116u8, 89u8, 18u8] + } + + pub fn data(data: BorrowInstructionData) -> Self { + Self { + accounts: BorrowInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: BorrowInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ClaimProtocolFees + // .................................................................... + + /// Main instruction struct for ClaimProtocolFees + pub struct ClaimProtocolFeesInstruction { + pub accounts: ClaimProtocolFeesInstructionAccountMetas, + pub data: ClaimProtocolFeesInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ClaimProtocolFees instruction + #[derive(Debug, Clone, Default)] + pub struct ClaimProtocolFeesInstructionAccountMetas { + pub caller: AccountMeta, + + pub pair: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub authority_token0_account: AccountMeta, + + pub authority_token1_account: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + } + + /// Account pubkeys for ClaimProtocolFees instruction + #[derive(Debug, Clone)] + pub struct ClaimProtocolFeesInstructionAccounts { + pub caller: Pubkey, + + pub pair: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub authority_token0_account: Pubkey, + + pub authority_token1_account: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + } + + impl ClaimProtocolFeesInstructionAccounts { + pub fn new( + caller: Pubkey, + + pair: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + authority_token0_account: Pubkey, + + authority_token1_account: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + ) -> Self { + Self { + caller, + + pair, + + futarchy_authority, + + token0_vault, + + token1_vault, + + authority_token0_account, + + authority_token1_account, + + token0_mint, + + token1_mint, + } + } + } + + /// Instruction data for ClaimProtocolFees + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ClaimProtocolFeesInstructionData { + pub args: ClaimProtocolFeesArgs, + } + + impl ClaimProtocolFeesInstructionData { + pub fn new(args: ClaimProtocolFeesArgs) -> Self { + Self { args } + } + } + + /// Implementation for ClaimProtocolFeesInstruction + impl ClaimProtocolFeesInstruction { + fn discriminator() -> [u8; 8] { + [34u8, 142u8, 219u8, 112u8, 109u8, 54u8, 133u8, 23u8] + } + + pub fn data(data: ClaimProtocolFeesInstructionData) -> Self { + Self { + accounts: ClaimProtocolFeesInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ClaimProtocolFeesInstructionAccounts) -> Self { + self.accounts.caller = AccountMeta::new(accounts.caller, true); + + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.authority_token0_account = + AccountMeta::new(accounts.authority_token0_account, false); + + self.accounts.authority_token1_account = + AccountMeta::new(accounts.authority_token1_account, false); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.caller.clone()); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.authority_token0_account.clone()); + + metas.push(self.accounts.authority_token1_account.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: DistributeTokens + // .................................................................... + + /// Main instruction struct for DistributeTokens + pub struct DistributeTokensInstruction { + pub accounts: DistributeTokensInstructionAccountMetas, + pub data: DistributeTokensInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for DistributeTokens instruction + #[derive(Debug, Clone, Default)] + pub struct DistributeTokensInstructionAccountMetas { + pub futarchy_authority: AccountMeta, + + pub source_mint: AccountMeta, + + pub source_token_account: AccountMeta, + + pub futarchy_treasury_token_account: AccountMeta, + + pub buybacks_vault_token_account: AccountMeta, + + pub team_treasury_token_account: AccountMeta, + + pub token_program: AccountMeta, + } + + /// Account pubkeys for DistributeTokens instruction + #[derive(Debug, Clone)] + pub struct DistributeTokensInstructionAccounts { + pub futarchy_authority: Pubkey, + + pub source_mint: Pubkey, + + pub source_token_account: Pubkey, + + pub futarchy_treasury_token_account: Pubkey, + + pub buybacks_vault_token_account: Pubkey, + + pub team_treasury_token_account: Pubkey, + } + + impl DistributeTokensInstructionAccounts { + pub fn new( + futarchy_authority: Pubkey, + + source_mint: Pubkey, + + source_token_account: Pubkey, + + futarchy_treasury_token_account: Pubkey, + + buybacks_vault_token_account: Pubkey, + + team_treasury_token_account: Pubkey, + ) -> Self { + Self { + futarchy_authority, + + source_mint, + + source_token_account, + + futarchy_treasury_token_account, + + buybacks_vault_token_account, + + team_treasury_token_account, + } + } + } + + /// Instruction data for DistributeTokens + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct DistributeTokensInstructionData { + pub args: DistributeTokensArgs, + } + + impl DistributeTokensInstructionData { + pub fn new(args: DistributeTokensArgs) -> Self { + Self { args } + } + } + + /// Implementation for DistributeTokensInstruction + impl DistributeTokensInstruction { + fn discriminator() -> [u8; 8] { + [105u8, 69u8, 130u8, 52u8, 196u8, 28u8, 176u8, 120u8] + } + + pub fn data(data: DistributeTokensInstructionData) -> Self { + Self { + accounts: DistributeTokensInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: DistributeTokensInstructionAccounts) -> Self { + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.source_mint = AccountMeta::new_readonly(accounts.source_mint, false); + + self.accounts.source_token_account = + AccountMeta::new(accounts.source_token_account, false); + + self.accounts.futarchy_treasury_token_account = + AccountMeta::new(accounts.futarchy_treasury_token_account, false); + + self.accounts.buybacks_vault_token_account = + AccountMeta::new(accounts.buybacks_vault_token_account, false); + + self.accounts.team_treasury_token_account = + AccountMeta::new(accounts.team_treasury_token_account, false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.source_mint.clone()); + + metas.push(self.accounts.source_token_account.clone()); + + metas.push(self.accounts.futarchy_treasury_token_account.clone()); + + metas.push(self.accounts.buybacks_vault_token_account.clone()); + + metas.push(self.accounts.team_treasury_token_account.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Flashloan + // .................................................................... + + /// Main instruction struct for Flashloan + pub struct FlashloanInstruction { + pub accounts: FlashloanInstructionAccountMetas, + pub data: FlashloanInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Flashloan instruction + #[derive(Debug, Clone, Default)] + pub struct FlashloanInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub receiver_token0_account: AccountMeta, + + pub receiver_token1_account: AccountMeta, + + pub receiver_program: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Flashloan instruction + #[derive(Debug, Clone)] + pub struct FlashloanInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + + pub receiver_token0_account: Pubkey, + + pub receiver_token1_account: Pubkey, + + pub receiver_program: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl FlashloanInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + + receiver_token0_account: Pubkey, + + receiver_token1_account: Pubkey, + + receiver_program: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + token0_mint, + + token1_mint, + + receiver_token0_account, + + receiver_token1_account, + + receiver_program, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Flashloan + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct FlashloanInstructionData { + pub args: FlashloanArgs, + } + + impl FlashloanInstructionData { + pub fn new(args: FlashloanArgs) -> Self { + Self { args } + } + } + + /// Implementation for FlashloanInstruction + impl FlashloanInstruction { + fn discriminator() -> [u8; 8] { + [105u8, 33u8, 1u8, 3u8, 42u8, 158u8, 246u8, 67u8] + } + + pub fn data(data: FlashloanInstructionData) -> Self { + Self { + accounts: FlashloanInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: FlashloanInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.receiver_token0_account = + AccountMeta::new(accounts.receiver_token0_account, false); + + self.accounts.receiver_token1_account = + AccountMeta::new(accounts.receiver_token1_account, false); + + self.accounts.receiver_program = + AccountMeta::new_readonly(accounts.receiver_program, false); + + self.accounts.user = AccountMeta::new_readonly(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.receiver_token0_account.clone()); + + metas.push(self.accounts.receiver_token1_account.clone()); + + metas.push(self.accounts.receiver_program.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: InitFutarchyAuthority + // .................................................................... + + /// Main instruction struct for InitFutarchyAuthority + pub struct InitFutarchyAuthorityInstruction { + pub accounts: InitFutarchyAuthorityInstructionAccountMetas, + pub data: InitFutarchyAuthorityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for InitFutarchyAuthority instruction + #[derive(Debug, Clone, Default)] + pub struct InitFutarchyAuthorityInstructionAccountMetas { + pub deployer: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub system_program: AccountMeta, + } + + /// Account pubkeys for InitFutarchyAuthority instruction + #[derive(Debug, Clone)] + pub struct InitFutarchyAuthorityInstructionAccounts { + pub futarchy_authority: Pubkey, + } + + impl InitFutarchyAuthorityInstructionAccounts { + pub fn new(futarchy_authority: Pubkey) -> Self { + Self { futarchy_authority } + } + } + + /// Instruction data for InitFutarchyAuthority + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct InitFutarchyAuthorityInstructionData { + pub args: InitFutarchyAuthorityArgs, + } + + impl InitFutarchyAuthorityInstructionData { + pub fn new(args: InitFutarchyAuthorityArgs) -> Self { + Self { args } + } + } + + /// Implementation for InitFutarchyAuthorityInstruction + impl InitFutarchyAuthorityInstruction { + fn discriminator() -> [u8; 8] { + [133u8, 110u8, 154u8, 29u8, 240u8, 206u8, 71u8, 100u8] + } + + pub fn data(data: InitFutarchyAuthorityInstructionData) -> Self { + Self { + accounts: InitFutarchyAuthorityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: InitFutarchyAuthorityInstructionAccounts) -> Self { + self.accounts.deployer = AccountMeta::new( + pubkey!("C7GKpfqQyBoFR6S13DECwBjdi7aCQKbbeKjXm4Jt5Hds"), + true, + ); + + self.accounts.futarchy_authority = AccountMeta::new(accounts.futarchy_authority, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.deployer.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Initialize + // .................................................................... + + /// Main instruction struct for Initialize + pub struct InitializeInstruction { + pub accounts: InitializeInstructionAccountMetas, + pub data: InitializeInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Initialize instruction + #[derive(Debug, Clone, Default)] + pub struct InitializeInstructionAccountMetas { + pub deployer: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub pair: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub rate_model: AccountMeta, + + pub lp_mint: AccountMeta, + + pub lp_token_metadata: AccountMeta, + + pub deployer_lp_token_account: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub deployer_token0_account: AccountMeta, + + pub deployer_token1_account: AccountMeta, + + pub authority_wsol_account: AccountMeta, + + pub system_program: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub token_metadata_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub rent: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Initialize instruction + #[derive(Debug, Clone)] + pub struct InitializeInstructionAccounts { + pub deployer: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + + pub pair: Pubkey, + + pub futarchy_authority: Pubkey, + + pub rate_model: Pubkey, + + pub lp_mint: Pubkey, + + pub lp_token_metadata: Pubkey, + + pub deployer_lp_token_account: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub deployer_token0_account: Pubkey, + + pub deployer_token1_account: Pubkey, + + pub authority_wsol_account: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl InitializeInstructionAccounts { + pub fn new( + deployer: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + + pair: Pubkey, + + futarchy_authority: Pubkey, + + rate_model: Pubkey, + + lp_mint: Pubkey, + + lp_token_metadata: Pubkey, + + deployer_lp_token_account: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + deployer_token0_account: Pubkey, + + deployer_token1_account: Pubkey, + + authority_wsol_account: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + deployer, + + token0_mint, + + token1_mint, + + pair, + + futarchy_authority, + + rate_model, + + lp_mint, + + lp_token_metadata, + + deployer_lp_token_account, + + token0_vault, + + token1_vault, + + deployer_token0_account, + + deployer_token1_account, + + authority_wsol_account, + + event_authority, + + program, + } + } + } + + /// Instruction data for Initialize + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct InitializeInstructionData { + pub args: InitializeAndBootstrapArgs, + } + + impl InitializeInstructionData { + pub fn new(args: InitializeAndBootstrapArgs) -> Self { + Self { args } + } + } + + /// Implementation for InitializeInstruction + impl InitializeInstruction { + fn discriminator() -> [u8; 8] { + [175u8, 175u8, 109u8, 31u8, 13u8, 152u8, 155u8, 237u8] + } + + pub fn data(data: InitializeInstructionData) -> Self { + Self { + accounts: InitializeInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: InitializeInstructionAccounts) -> Self { + self.accounts.deployer = AccountMeta::new(accounts.deployer, true); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, true); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.lp_token_metadata = AccountMeta::new(accounts.lp_token_metadata, false); + + self.accounts.deployer_lp_token_account = + AccountMeta::new(accounts.deployer_lp_token_account, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.deployer_token0_account = + AccountMeta::new(accounts.deployer_token0_account, false); + + self.accounts.deployer_token1_account = + AccountMeta::new(accounts.deployer_token1_account, false); + + self.accounts.authority_wsol_account = + AccountMeta::new(accounts.authority_wsol_account, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.token_metadata_program = AccountMeta::new_readonly( + pubkey!("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.rent = AccountMeta::new_readonly( + pubkey!("SysvarRent111111111111111111111111111111111"), + false, + ); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.deployer.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.lp_token_metadata.clone()); + + metas.push(self.accounts.deployer_lp_token_account.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.deployer_token0_account.clone()); + + metas.push(self.accounts.deployer_token1_account.clone()); + + metas.push(self.accounts.authority_wsol_account.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.token_metadata_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.rent.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Liquidate + // .................................................................... + + /// Main instruction struct for Liquidate + pub struct LiquidateInstruction { + pub accounts: LiquidateInstructionAccountMetas, + pub data: LiquidateInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Liquidate instruction + #[derive(Debug, Clone, Default)] + pub struct LiquidateInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub collateral_vault: AccountMeta, + + pub caller_token_account: AccountMeta, + + pub collateral_token_mint: AccountMeta, + + pub position_owner: AccountMeta, + + pub payer: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Liquidate instruction + #[derive(Debug, Clone)] + pub struct LiquidateInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub collateral_vault: Pubkey, + + pub caller_token_account: Pubkey, + + pub collateral_token_mint: Pubkey, + + pub position_owner: Pubkey, + + pub payer: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl LiquidateInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + collateral_vault: Pubkey, + + caller_token_account: Pubkey, + + collateral_token_mint: Pubkey, + + position_owner: Pubkey, + + payer: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + collateral_vault, + + caller_token_account, + + collateral_token_mint, + + position_owner, + + payer, + + event_authority, + + program, + } + } + } + + /// Instruction data for Liquidate + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct LiquidateInstructionData {} + + impl LiquidateInstructionData { + pub fn new() -> Self { + Self {} + } + } + + /// Implementation for LiquidateInstruction + impl LiquidateInstruction { + fn discriminator() -> [u8; 8] { + [223u8, 179u8, 226u8, 125u8, 48u8, 46u8, 39u8, 74u8] + } + + pub fn data(data: LiquidateInstructionData) -> Self { + Self { + accounts: LiquidateInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: LiquidateInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.collateral_vault = AccountMeta::new(accounts.collateral_vault, false); + + self.accounts.caller_token_account = + AccountMeta::new(accounts.caller_token_account, false); + + self.accounts.collateral_token_mint = + AccountMeta::new_readonly(accounts.collateral_token_mint, false); + + self.accounts.position_owner = + AccountMeta::new_readonly(accounts.position_owner, false); + + self.accounts.payer = AccountMeta::new(accounts.payer, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.collateral_vault.clone()); + + metas.push(self.accounts.caller_token_account.clone()); + + metas.push(self.accounts.collateral_token_mint.clone()); + + metas.push(self.accounts.position_owner.clone()); + + metas.push(self.accounts.payer.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: RemoveCollateral + // .................................................................... + + /// Main instruction struct for RemoveCollateral + pub struct RemoveCollateralInstruction { + pub accounts: RemoveCollateralInstructionAccountMetas, + pub data: RemoveCollateralInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for RemoveCollateral instruction + #[derive(Debug, Clone, Default)] + pub struct RemoveCollateralInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for RemoveCollateral instruction + #[derive(Debug, Clone)] + pub struct RemoveCollateralInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RemoveCollateralInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for RemoveCollateral + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RemoveCollateralInstructionData { + pub args: AdjustPositionArgs, + } + + impl RemoveCollateralInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for RemoveCollateralInstruction + impl RemoveCollateralInstruction { + fn discriminator() -> [u8; 8] { + [86u8, 222u8, 130u8, 86u8, 92u8, 20u8, 72u8, 65u8] + } + + pub fn data(data: RemoveCollateralInstructionData) -> Self { + Self { + accounts: RemoveCollateralInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RemoveCollateralInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: RemoveLiquidity + // .................................................................... + + /// Main instruction struct for RemoveLiquidity + pub struct RemoveLiquidityInstruction { + pub accounts: RemoveLiquidityInstructionAccountMetas, + pub data: RemoveLiquidityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for RemoveLiquidity instruction + #[derive(Debug, Clone, Default)] + pub struct RemoveLiquidityInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_vault_mint: AccountMeta, + + pub token1_vault_mint: AccountMeta, + + pub lp_mint: AccountMeta, + + pub user_lp_token_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for RemoveLiquidity instruction + #[derive(Debug, Clone)] + pub struct RemoveLiquidityInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_vault_mint: Pubkey, + + pub token1_vault_mint: Pubkey, + + pub lp_mint: Pubkey, + + pub user_lp_token_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RemoveLiquidityInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_vault_mint: Pubkey, + + token1_vault_mint: Pubkey, + + lp_mint: Pubkey, + + user_lp_token_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + user_token0_account, + + user_token1_account, + + token0_vault_mint, + + token1_vault_mint, + + lp_mint, + + user_lp_token_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for RemoveLiquidity + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RemoveLiquidityInstructionData { + pub args: RemoveLiquidityArgs, + } + + impl RemoveLiquidityInstructionData { + pub fn new(args: RemoveLiquidityArgs) -> Self { + Self { args } + } + } + + /// Implementation for RemoveLiquidityInstruction + impl RemoveLiquidityInstruction { + fn discriminator() -> [u8; 8] { + [80u8, 85u8, 209u8, 72u8, 24u8, 206u8, 177u8, 108u8] + } + + pub fn data(data: RemoveLiquidityInstructionData) -> Self { + Self { + accounts: RemoveLiquidityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RemoveLiquidityInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_vault_mint = + AccountMeta::new_readonly(accounts.token0_vault_mint, false); + + self.accounts.token1_vault_mint = + AccountMeta::new_readonly(accounts.token1_vault_mint, false); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.user_lp_token_account = + AccountMeta::new(accounts.user_lp_token_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_vault_mint.clone()); + + metas.push(self.accounts.token1_vault_mint.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.user_lp_token_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Repay + // .................................................................... + + /// Main instruction struct for Repay + pub struct RepayInstruction { + pub accounts: RepayInstructionAccountMetas, + pub data: RepayInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Repay instruction + #[derive(Debug, Clone, Default)] + pub struct RepayInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Repay instruction + #[derive(Debug, Clone)] + pub struct RepayInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RepayInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Repay + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RepayInstructionData { + pub args: AdjustPositionArgs, + } + + impl RepayInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for RepayInstruction + impl RepayInstruction { + fn discriminator() -> [u8; 8] { + [234u8, 103u8, 67u8, 82u8, 208u8, 234u8, 219u8, 166u8] + } + + pub fn data(data: RepayInstructionData) -> Self { + Self { + accounts: RepayInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RepayInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Swap + // .................................................................... + + /// Main instruction struct for Swap + pub struct SwapInstruction { + pub accounts: SwapInstructionAccountMetas, + pub data: SwapInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Swap instruction + #[derive(Debug, Clone, Default)] + pub struct SwapInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_in_vault: AccountMeta, + + pub token_out_vault: AccountMeta, + + pub user_token_in_account: AccountMeta, + + pub user_token_out_account: AccountMeta, + + pub token_in_mint: AccountMeta, + + pub token_out_mint: AccountMeta, + + pub authority_token_in_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Swap instruction + #[derive(Debug, Clone)] + pub struct SwapInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_in_vault: Pubkey, + + pub token_out_vault: Pubkey, + + pub user_token_in_account: Pubkey, + + pub user_token_out_account: Pubkey, + + pub token_in_mint: Pubkey, + + pub token_out_mint: Pubkey, + + pub authority_token_in_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl SwapInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_in_vault: Pubkey, + + token_out_vault: Pubkey, + + user_token_in_account: Pubkey, + + user_token_out_account: Pubkey, + + token_in_mint: Pubkey, + + token_out_mint: Pubkey, + + authority_token_in_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token_in_vault, + + token_out_vault, + + user_token_in_account, + + user_token_out_account, + + token_in_mint, + + token_out_mint, + + authority_token_in_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Swap + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct SwapInstructionData { + pub args: SwapArgs, + } + + impl SwapInstructionData { + pub fn new(args: SwapArgs) -> Self { + Self { args } + } + } + + /// Implementation for SwapInstruction + impl SwapInstruction { + fn discriminator() -> [u8; 8] { + [248u8, 198u8, 158u8, 145u8, 225u8, 117u8, 135u8, 200u8] + } + + pub fn data(data: SwapInstructionData) -> Self { + Self { + accounts: SwapInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: SwapInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_in_vault = AccountMeta::new(accounts.token_in_vault, false); + + self.accounts.token_out_vault = AccountMeta::new(accounts.token_out_vault, false); + + self.accounts.user_token_in_account = + AccountMeta::new(accounts.user_token_in_account, false); + + self.accounts.user_token_out_account = + AccountMeta::new(accounts.user_token_out_account, false); + + self.accounts.token_in_mint = AccountMeta::new_readonly(accounts.token_in_mint, false); + + self.accounts.token_out_mint = + AccountMeta::new_readonly(accounts.token_out_mint, false); + + self.accounts.authority_token_in_account = + AccountMeta::new(accounts.authority_token_in_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_in_vault.clone()); + + metas.push(self.accounts.token_out_vault.clone()); + + metas.push(self.accounts.user_token_in_account.clone()); + + metas.push(self.accounts.user_token_out_account.clone()); + + metas.push(self.accounts.token_in_mint.clone()); + + metas.push(self.accounts.token_out_mint.clone()); + + metas.push(self.accounts.authority_token_in_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ViewPairData + // .................................................................... + + /// Main instruction struct for ViewPairData + pub struct ViewPairDataInstruction { + pub accounts: ViewPairDataInstructionAccountMetas, + pub data: ViewPairDataInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ViewPairData instruction + #[derive(Debug, Clone, Default)] + pub struct ViewPairDataInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + } + + /// Account pubkeys for ViewPairData instruction + #[derive(Debug, Clone)] + pub struct ViewPairDataInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + } + + impl ViewPairDataInstructionAccounts { + pub fn new(pair: Pubkey, rate_model: Pubkey, futarchy_authority: Pubkey) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + } + } + } + + /// Instruction data for ViewPairData + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ViewPairDataInstructionData { + pub getter: PairViewKind, + + pub args: EmitValueArgs, + } + + impl ViewPairDataInstructionData { + pub fn new(getter: PairViewKind, args: EmitValueArgs) -> Self { + Self { getter, args } + } + } + + /// Implementation for ViewPairDataInstruction + impl ViewPairDataInstruction { + fn discriminator() -> [u8; 8] { + [30u8, 231u8, 169u8, 73u8, 19u8, 161u8, 44u8, 252u8] + } + + pub fn data(data: ViewPairDataInstructionData) -> Self { + Self { + accounts: ViewPairDataInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ViewPairDataInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new_readonly(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ViewUserPositionData + // .................................................................... + + /// Main instruction struct for ViewUserPositionData + pub struct ViewUserPositionDataInstruction { + pub accounts: ViewUserPositionDataInstructionAccountMetas, + pub data: ViewUserPositionDataInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ViewUserPositionData instruction + #[derive(Debug, Clone, Default)] + pub struct ViewUserPositionDataInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + } + + /// Account pubkeys for ViewUserPositionData instruction + #[derive(Debug, Clone)] + pub struct ViewUserPositionDataInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + } + + impl ViewUserPositionDataInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + } + } + } + + /// Instruction data for ViewUserPositionData + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ViewUserPositionDataInstructionData { + pub getter: UserPositionViewKind, + } + + impl ViewUserPositionDataInstructionData { + pub fn new(getter: UserPositionViewKind) -> Self { + Self { getter } + } + } + + /// Implementation for ViewUserPositionDataInstruction + impl ViewUserPositionDataInstruction { + fn discriminator() -> [u8; 8] { + [203u8, 218u8, 173u8, 213u8, 43u8, 31u8, 211u8, 152u8] + } + + pub fn data(data: ViewUserPositionDataInstructionData) -> Self { + Self { + accounts: ViewUserPositionDataInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ViewUserPositionDataInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new_readonly(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // ------------------------------------------------------------------------ + // Composite Accounts + // ------------------------------------------------------------------------ +} + +// ============================================================================ +// CUSTOM TYPES +// ============================================================================ + +/// Custom struct: AddLiquidityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AddLiquidityArgs { + pub amount0_in: u64, + + pub amount1_in: u64, + + pub min_liquidity_out: u64, +} + +impl AddLiquidityArgs { + pub fn new(amount0_in: u64, amount1_in: u64, min_liquidity_out: u64) -> Self { + Self { + amount0_in, + + amount1_in, + + min_liquidity_out, + } + } +} + +/// Custom struct: AdjustCollateralEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustCollateralEvent { + pub amount0: i64, + + pub amount1: i64, + + pub metadata: EventMetadata, +} + +impl AdjustCollateralEvent { + pub fn new(amount0: i64, amount1: i64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + metadata, + } + } +} + +/// Custom struct: AdjustDebtEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustDebtEvent { + pub amount0: i64, + + pub amount1: i64, + + pub metadata: EventMetadata, +} + +impl AdjustDebtEvent { + pub fn new(amount0: i64, amount1: i64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + metadata, + } + } +} + +/// Custom struct: AdjustLiquidityEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustLiquidityEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl AdjustLiquidityEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: AdjustPositionArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustPositionArgs { + pub amount: u64, +} + +impl AdjustPositionArgs { + pub fn new(amount: u64) -> Self { + Self { amount } + } +} + +/// Custom struct: BurnEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct BurnEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl BurnEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: ClaimProtocolFeesArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct ClaimProtocolFeesArgs { + pub amount0: u64, + + pub amount1: u64, +} + +impl ClaimProtocolFeesArgs { + pub fn new(amount0: u64, amount1: u64) -> Self { + Self { amount0, amount1 } + } +} + +/// Custom struct: DistributeTokensArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct DistributeTokensArgs {} + +impl DistributeTokensArgs { + pub fn new() -> Self { + Self {} + } +} + +/// Custom struct: EmitValueArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct EmitValueArgs { + pub debt_amount: Option, + + pub collateral_amount: Option, + + pub collateral_token: Option, +} + +impl EmitValueArgs { + pub fn new( + debt_amount: Option, + + collateral_amount: Option, + + collateral_token: Option, + ) -> Self { + Self { + debt_amount, + + collateral_amount, + + collateral_token, + } + } +} + +/// Custom struct: EventMetadata +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct EventMetadata { + pub signer: Pubkey, + + pub pair: Pubkey, + + pub timestamp: i64, +} + +impl EventMetadata { + pub fn new(signer: Pubkey, pair: Pubkey, timestamp: i64) -> Self { + Self { + signer, + + pair, + + timestamp, + } + } +} + +/// Custom struct: FlashloanArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FlashloanArgs { + pub amount0: u64, + + pub amount1: u64, + + pub data: Vec, +} + +impl FlashloanArgs { + pub fn new(amount0: u64, amount1: u64, data: Vec) -> Self { + Self { + amount0, + + amount1, + + data, + } + } +} + +/// Custom struct: FlashloanEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FlashloanEvent { + pub amount0: u64, + + pub amount1: u64, + + pub fee0: u64, + + pub fee1: u64, + + pub receiver: Pubkey, + + pub metadata: EventMetadata, +} + +impl FlashloanEvent { + pub fn new( + amount0: u64, + + amount1: u64, + + fee0: u64, + + fee1: u64, + + receiver: Pubkey, + + metadata: EventMetadata, + ) -> Self { + Self { + amount0, + + amount1, + + fee0, + + fee1, + + receiver, + + metadata, + } + } +} + +/// Custom struct: FutarchyAuthority +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FutarchyAuthority { + pub version: u8, + + pub authority: Pubkey, + + pub recipients: RevenueRecipients, + + pub revenue_share: RevenueShare, + + pub revenue_distribution: RevenueDistribution, + + pub bump: u8, +} + +impl FutarchyAuthority { + pub fn new( + version: u8, + + authority: Pubkey, + + recipients: RevenueRecipients, + + revenue_share: RevenueShare, + + revenue_distribution: RevenueDistribution, + + bump: u8, + ) -> Self { + Self { + version, + + authority, + + recipients, + + revenue_share, + + revenue_distribution, + + bump, + } + } +} + +/// Custom struct: InitFutarchyAuthorityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct InitFutarchyAuthorityArgs { + pub authority: Pubkey, + + pub swap_bps: u16, + + pub interest_bps: u16, + + pub futarchy_treasury: Pubkey, + + pub futarchy_treasury_bps: u16, + + pub buybacks_vault: Pubkey, + + pub buybacks_vault_bps: u16, + + pub team_treasury: Pubkey, + + pub team_treasury_bps: u16, +} + +impl InitFutarchyAuthorityArgs { + pub fn new( + authority: Pubkey, + + swap_bps: u16, + + interest_bps: u16, + + futarchy_treasury: Pubkey, + + futarchy_treasury_bps: u16, + + buybacks_vault: Pubkey, + + buybacks_vault_bps: u16, + + team_treasury: Pubkey, + + team_treasury_bps: u16, + ) -> Self { + Self { + authority, + + swap_bps, + + interest_bps, + + futarchy_treasury, + + futarchy_treasury_bps, + + buybacks_vault, + + buybacks_vault_bps, + + team_treasury, + + team_treasury_bps, + } + } +} + +/// Custom struct: InitializeAndBootstrapArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct InitializeAndBootstrapArgs { + pub swap_fee_bps: u16, + + pub half_life: u64, + + pub fixed_cf_bps: Option, + + pub pair_nonce: [u8; 16], + + pub amount0_in: u64, + + pub amount1_in: u64, + + pub min_liquidity_out: u64, + + pub lp_name: String, + + pub lp_symbol: String, + + pub lp_uri: String, +} + +impl InitializeAndBootstrapArgs { + pub fn new( + swap_fee_bps: u16, + + half_life: u64, + + fixed_cf_bps: Option, + + pair_nonce: [u8; 16], + + amount0_in: u64, + + amount1_in: u64, + + min_liquidity_out: u64, + + lp_name: String, + + lp_symbol: String, + + lp_uri: String, + ) -> Self { + Self { + swap_fee_bps, + + half_life, + + fixed_cf_bps, + + pair_nonce, + + amount0_in, + + amount1_in, + + min_liquidity_out, + + lp_name, + + lp_symbol, + + lp_uri, + } + } +} + +/// Custom struct: MintEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct MintEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl MintEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: Pair +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct Pair { + pub token0: Pubkey, + + pub token1: Pubkey, + + pub lp_mint: Pubkey, + + pub token0_decimals: u8, + + pub token1_decimals: u8, + + pub rate_model: Pubkey, + + pub swap_fee_bps: u16, + + pub half_life: u64, + + pub fixed_cf_bps: Option, + + pub reserve0: u64, + + pub reserve1: u64, + + pub protocol_revenue_reserve0: u64, + + pub protocol_revenue_reserve1: u64, + + pub last_price0_ema: u64, + + pub last_price1_ema: u64, + + pub last_update: i64, + + pub last_rate0: u64, + + pub last_rate1: u64, + + pub total_debt0: u64, + + pub total_debt1: u64, + + pub total_debt0_shares: u64, + + pub total_debt1_shares: u64, + + pub total_supply: u64, + + pub total_collateral0: u64, + + pub total_collateral1: u64, + + pub pair_nonce: [u8; 16], + + pub bump: u8, +} + +impl Pair { + pub fn new( + token0: Pubkey, + + token1: Pubkey, + + lp_mint: Pubkey, + + token0_decimals: u8, + + token1_decimals: u8, + + rate_model: Pubkey, + + swap_fee_bps: u16, + + half_life: u64, + + fixed_cf_bps: Option, + + reserve0: u64, + + reserve1: u64, + + protocol_revenue_reserve0: u64, + + protocol_revenue_reserve1: u64, + + last_price0_ema: u64, + + last_price1_ema: u64, + + last_update: i64, + + last_rate0: u64, + + last_rate1: u64, + + total_debt0: u64, + + total_debt1: u64, + + total_debt0_shares: u64, + + total_debt1_shares: u64, + + total_supply: u64, + + total_collateral0: u64, + + total_collateral1: u64, + + pair_nonce: [u8; 16], + + bump: u8, + ) -> Self { + Self { + token0, + + token1, + + lp_mint, + + token0_decimals, + + token1_decimals, + + rate_model, + + swap_fee_bps, + + half_life, + + fixed_cf_bps, + + reserve0, + + reserve1, + + protocol_revenue_reserve0, + + protocol_revenue_reserve1, + + last_price0_ema, + + last_price1_ema, + + last_update, + + last_rate0, + + last_rate1, + + total_debt0, + + total_debt1, + + total_debt0_shares, + + total_debt1_shares, + + total_supply, + + total_collateral0, + + total_collateral1, + + pair_nonce, + + bump, + } + } +} + +/// Custom struct: PairCreatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct PairCreatedEvent { + pub token0: Pubkey, + + pub token1: Pubkey, + + pub metadata: EventMetadata, +} + +impl PairCreatedEvent { + pub fn new(token0: Pubkey, token1: Pubkey, metadata: EventMetadata) -> Self { + Self { + token0, + + token1, + + metadata, + } + } +} + +/// Custom enum: PairViewKind +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone, PartialEq)] +pub enum PairViewKind { + EmaPrice0Nad, + + EmaPrice1Nad, + + SpotPrice0Nad, + + SpotPrice1Nad, + + K, + + GetRates, + + GetBorrowLimitAndCfBpsForCollateral, +} + +/// Custom struct: RateModel +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RateModel { + pub exp_rate: u64, + + pub target_util_start: u64, + + pub target_util_end: u64, +} + +impl RateModel { + pub fn new(exp_rate: u64, target_util_start: u64, target_util_end: u64) -> Self { + Self { + exp_rate, + + target_util_start, + + target_util_end, + } + } +} + +/// Custom struct: RemoveLiquidityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RemoveLiquidityArgs { + pub liquidity_in: u64, + + pub min_amount0_out: u64, + + pub min_amount1_out: u64, +} + +impl RemoveLiquidityArgs { + pub fn new(liquidity_in: u64, min_amount0_out: u64, min_amount1_out: u64) -> Self { + Self { + liquidity_in, + + min_amount0_out, + + min_amount1_out, + } + } +} + +/// Custom struct: RevenueDistribution +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueDistribution { + pub futarchy_treasury_bps: u16, + + pub buybacks_vault_bps: u16, + + pub team_treasury_bps: u16, +} + +impl RevenueDistribution { + pub fn new( + futarchy_treasury_bps: u16, + + buybacks_vault_bps: u16, + + team_treasury_bps: u16, + ) -> Self { + Self { + futarchy_treasury_bps, + + buybacks_vault_bps, + + team_treasury_bps, + } + } +} + +/// Custom struct: RevenueRecipients +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueRecipients { + pub futarchy_treasury: Pubkey, + + pub buybacks_vault: Pubkey, + + pub team_treasury: Pubkey, +} + +impl RevenueRecipients { + pub fn new(futarchy_treasury: Pubkey, buybacks_vault: Pubkey, team_treasury: Pubkey) -> Self { + Self { + futarchy_treasury, + + buybacks_vault, + + team_treasury, + } + } +} + +/// Custom struct: RevenueShare +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueShare { + pub swap_bps: u16, + + pub interest_bps: u16, +} + +impl RevenueShare { + pub fn new(swap_bps: u16, interest_bps: u16) -> Self { + Self { + swap_bps, + + interest_bps, + } + } +} + +/// Custom struct: SwapArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct SwapArgs { + pub amount_in: u64, + + pub min_amount_out: u64, +} + +impl SwapArgs { + pub fn new(amount_in: u64, min_amount_out: u64) -> Self { + Self { + amount_in, + + min_amount_out, + } + } +} + +/// Custom struct: SwapEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct SwapEvent { + pub reserve0: u64, + + pub reserve1: u64, + + pub is_token0_in: bool, + + pub amount_in: u64, + + pub amount_out: u64, + + pub amount_in_after_fee: u64, + + pub metadata: EventMetadata, +} + +impl SwapEvent { + pub fn new( + reserve0: u64, + + reserve1: u64, + + is_token0_in: bool, + + amount_in: u64, + + amount_out: u64, + + amount_in_after_fee: u64, + + metadata: EventMetadata, + ) -> Self { + Self { + reserve0, + + reserve1, + + is_token0_in, + + amount_in, + + amount_out, + + amount_in_after_fee, + + metadata, + } + } +} + +/// Custom struct: UpdatePairEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UpdatePairEvent { + pub price0_ema: u64, + + pub price1_ema: u64, + + pub rate0: u64, + + pub rate1: u64, + + pub accrued_interest0: u128, + + pub accrued_interest1: u128, + + pub protocol_revenue_reserve0: u64, + + pub protocol_revenue_reserve1: u64, + + pub reserve0_after_interest: u64, + + pub reserve1_after_interest: u64, + + pub metadata: EventMetadata, +} + +impl UpdatePairEvent { + pub fn new( + price0_ema: u64, + + price1_ema: u64, + + rate0: u64, + + rate1: u64, + + accrued_interest0: u128, + + accrued_interest1: u128, + + protocol_revenue_reserve0: u64, + + protocol_revenue_reserve1: u64, + + reserve0_after_interest: u64, + + reserve1_after_interest: u64, + + metadata: EventMetadata, + ) -> Self { + Self { + price0_ema, + + price1_ema, + + rate0, + + rate1, + + accrued_interest0, + + accrued_interest1, + + protocol_revenue_reserve0, + + protocol_revenue_reserve1, + + reserve0_after_interest, + + reserve1_after_interest, + + metadata, + } + } +} + +/// Custom struct: UserPosition +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPosition { + pub owner: Pubkey, + + pub pair: Pubkey, + + pub collateral0_applied_min_cf_bps: u16, + + pub collateral1_applied_min_cf_bps: u16, + + pub collateral0: u64, + + pub collateral1: u64, + + pub debt0_shares: u64, + + pub debt1_shares: u64, + + pub bump: u8, +} + +impl UserPosition { + pub fn new( + owner: Pubkey, + + pair: Pubkey, + + collateral0_applied_min_cf_bps: u16, + + collateral1_applied_min_cf_bps: u16, + + collateral0: u64, + + collateral1: u64, + + debt0_shares: u64, + + debt1_shares: u64, + + bump: u8, + ) -> Self { + Self { + owner, + + pair, + + collateral0_applied_min_cf_bps, + + collateral1_applied_min_cf_bps, + + collateral0, + + collateral1, + + debt0_shares, + + debt1_shares, + + bump, + } + } +} + +/// Custom struct: UserPositionCreatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionCreatedEvent { + pub position: Pubkey, + + pub metadata: EventMetadata, +} + +impl UserPositionCreatedEvent { + pub fn new(position: Pubkey, metadata: EventMetadata) -> Self { + Self { position, metadata } + } +} + +/// Custom struct: UserPositionLiquidatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionLiquidatedEvent { + pub position: Pubkey, + + pub liquidator: Pubkey, + + pub collateral0_liquidated: u64, + + pub collateral1_liquidated: u64, + + pub debt0_liquidated: u64, + + pub debt1_liquidated: u64, + + pub collateral_price: u64, + + pub shortfall: u128, + + pub liquidation_bonus_applied: u64, + + pub k0: u128, + + pub k1: u128, + + pub metadata: EventMetadata, +} + +impl UserPositionLiquidatedEvent { + pub fn new( + position: Pubkey, + + liquidator: Pubkey, + + collateral0_liquidated: u64, + + collateral1_liquidated: u64, + + debt0_liquidated: u64, + + debt1_liquidated: u64, + + collateral_price: u64, + + shortfall: u128, + + liquidation_bonus_applied: u64, + + k0: u128, + + k1: u128, + + metadata: EventMetadata, + ) -> Self { + Self { + position, + + liquidator, + + collateral0_liquidated, + + collateral1_liquidated, + + debt0_liquidated, + + debt1_liquidated, + + collateral_price, + + shortfall, + + liquidation_bonus_applied, + + k0, + + k1, + + metadata, + } + } +} + +/// Custom struct: UserPositionUpdatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionUpdatedEvent { + pub position: Pubkey, + + pub collateral0: u64, + + pub collateral1: u64, + + pub debt0_shares: u64, + + pub debt1_shares: u64, + + pub collateral0_applied_min_cf_bps: u16, + + pub collateral1_applied_min_cf_bps: u16, + + pub metadata: EventMetadata, +} + +impl UserPositionUpdatedEvent { + pub fn new( + position: Pubkey, + + collateral0: u64, + + collateral1: u64, + + debt0_shares: u64, + + debt1_shares: u64, + + collateral0_applied_min_cf_bps: u16, + + collateral1_applied_min_cf_bps: u16, + + metadata: EventMetadata, + ) -> Self { + Self { + position, + + collateral0, + + collateral1, + + debt0_shares, + + debt1_shares, + + collateral0_applied_min_cf_bps, + + collateral1_applied_min_cf_bps, + + metadata, + } + } +} + +/// Custom enum: UserPositionViewKind +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub enum UserPositionViewKind { + UserBorrowingPower, + + UserAppliedCollateralFactorBps, + + UserLiquidationCollateralFactorBps, + + UserDebtUtilizationBps, + + UserLiquidationPrice, + + UserDebtWithInterest, +} + +// ============================================================================ +// END OF GENERATED FILE +// ============================================================================ diff --git a/trident-tests/fuzz/utils.rs b/trident-tests/fuzz/utils.rs new file mode 100644 index 0000000..9654487 --- /dev/null +++ b/trident-tests/fuzz/utils.rs @@ -0,0 +1,28 @@ +use trident_fuzz::fuzzing::{pubkey, Pubkey}; + +// DEPLOYER +pub const DEPLOYER_ADDRESS: Pubkey = pubkey!("C7GKpfqQyBoFR6S13DECwBjdi7aCQKbbeKjXm4Jt5Hds"); + +// PROGRAMS +pub const TOKEN_2022_PROGRAM: Pubkey = pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"); +pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); +pub const MPL_TOKEN_METADATA_ID: Pubkey = pubkey!("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"); +pub const FLASHLOAN_CALLBACK_RECEIVER_PROGRAM: Pubkey = + pubkey!("GmtswKBDrFZ9DfUfP7jbPFvbtuG7AJcX73SvoKWGxJbu"); + +// MINTS +pub const WSOL_MINT_ADDRESS: Pubkey = pubkey!("So11111111111111111111111111111111111111112"); + +// SEEDS +pub const FUTARCHY_AUTHORITY_SEED_PREFIX: &[u8] = b"futarchy_authority"; +pub const METADATA_SEED_PREFIX: &[u8] = b"metadata"; +pub const PAIR_SEED_PREFIX: &[u8] = b"gamm_pair"; +pub const LP_MINT_SEED_PREFIX: &[u8] = b"gamm_lp_mint"; +pub const POSITION_SEED_PREFIX: &[u8] = b"gamm_position"; + +// EVENT AUTHORITY +pub const EVENT_AUTHORITY_ADDRESS: Pubkey = pubkey!("fY27dnRLq4XVAKNRAY7nATiicimnY6mwLHq3V65uoP2"); + +pub fn get_size_with_discriminator() -> usize { + 8 + std::mem::size_of::() +} diff --git a/trident-tests/fuzz/view/mod.rs b/trident-tests/fuzz/view/mod.rs new file mode 100644 index 0000000..57ab2fb --- /dev/null +++ b/trident-tests/fuzz/view/mod.rs @@ -0,0 +1,2 @@ +mod view_pair_data; +mod view_user_position_data; diff --git a/trident-tests/fuzz/view/view_pair_data.rs b/trident-tests/fuzz/view/view_pair_data.rs new file mode 100644 index 0000000..a9fb613 --- /dev/null +++ b/trident-tests/fuzz/view/view_pair_data.rs @@ -0,0 +1,88 @@ +use trident_fuzz::fuzzing::*; + +use crate::{ + types::{ + omnipair::{ + ViewPairDataInstruction, ViewPairDataInstructionAccounts, ViewPairDataInstructionData, + }, + EmitValueArgs, Pair, PairViewKind, + }, + FuzzTest, +}; + +impl FuzzTest { + pub fn view_pair_data(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + let data = self.get_data_view_pair(); + let accounts = self.get_accounts_view_pair(); + + let ix = ViewPairDataInstruction::data(data) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("View Pair Data")); + + assert!(res.is_success()); + + // INVARIANT 1: the passed rate_model must match the pair's configured rate_model + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .unwrap(); + assert_eq!( + pair_account.rate_model, accounts.rate_model, + "ViewPairData accepted a mismatched rate_model for the given pair" + ); + } + + fn get_data_view_pair(&mut self) -> ViewPairDataInstructionData { + let getter = PairViewKind::random(&mut self.trident); + let collateral_amount = if getter == PairViewKind::GetBorrowLimitAndCfBpsForCollateral { + Some(self.trident.random_from_range(100..=100_000_000)) + } else { + None + }; + // Any token mint for collateral token + let collateral_token = if getter == PairViewKind::GetBorrowLimitAndCfBpsForCollateral { + Some(self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token mint should exist")) + } else { + None + }; + + ViewPairDataInstructionData::new( + getter, + EmitValueArgs::new( + None, // unused + collateral_amount, + collateral_token, + ), + ) + } + + fn get_accounts_view_pair(&mut self) -> ViewPairDataInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let rate_model = self.fuzz_accounts.rate_model.get(&mut self.trident).expect("Rate model should exist"); + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + ViewPairDataInstructionAccounts::new(pair, rate_model, futarchy_authority) + } +} + +impl PairViewKind { + pub fn random(trident: &mut Trident) -> Self { + match trident.random_from_range(0..=6) { + 0 => Self::EmaPrice0Nad, + 1 => Self::EmaPrice1Nad, + 2 => Self::SpotPrice0Nad, + 3 => Self::SpotPrice1Nad, + 4 => Self::K, + 5 => Self::GetRates, + 6 => Self::GetBorrowLimitAndCfBpsForCollateral, + _ => unreachable!(), + } + } +} diff --git a/trident-tests/fuzz/view/view_user_position_data.rs b/trident-tests/fuzz/view/view_user_position_data.rs new file mode 100644 index 0000000..2fad729 --- /dev/null +++ b/trident-tests/fuzz/view/view_user_position_data.rs @@ -0,0 +1,95 @@ +use trident_fuzz::trident::Trident; + +use crate::{ + types::{ + omnipair::{ + self, ViewUserPositionDataInstruction, ViewUserPositionDataInstructionAccounts, + ViewUserPositionDataInstructionData, + }, + Pair, UserPosition, UserPositionViewKind, + }, + utils::POSITION_SEED_PREFIX, + FuzzTest, +}; + +impl FuzzTest { + pub fn view_user_position_data(&mut self) { + if let Some(accounts) = self.get_accounts_view_user_position_data() { + let data = self.get_data_view_user_position_data(); + let ix = ViewUserPositionDataInstruction::data(data) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("View User Position Data")); + + assert!(res.is_success()); + + // INVARIANT 1: the user_position must belong to the passed pair + let up_account = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .unwrap(); + assert_eq!( + up_account.pair, accounts.pair, + "ViewUserPositionData accepted a user_position that does not belong to the given pair" + ); + // INVARIANT 2: the passed rate_model must match the pair's configured rate_model + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .unwrap(); + assert_eq!( + pair_account.rate_model, accounts.rate_model, + "ViewPairData accepted a mismatched rate_model for the given pair" + ); + } + } + + fn get_data_view_user_position_data(&mut self) -> ViewUserPositionDataInstructionData { + let getter = UserPositionViewKind::random(&mut self.trident); + ViewUserPositionDataInstructionData::new(getter) + } + + fn get_accounts_view_user_position_data( + &mut self, + ) -> Option { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + self.trident + .get_account_with_type::(&user_position, 8)?; + + let rate_model = self.fuzz_accounts.rate_model.get(&mut self.trident).expect("Rate model should exist"); + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + Some(ViewUserPositionDataInstructionAccounts::new( + pair, + user_position, + rate_model, + futarchy_authority, + )) + } +} + +impl UserPositionViewKind { + pub fn random(trident: &mut Trident) -> Self { + match trident.random_from_range(0..=5) { + 0 => Self::UserBorrowingPower, + 1 => Self::UserAppliedCollateralFactorBps, + 2 => Self::UserLiquidationCollateralFactorBps, + 3 => Self::UserDebtUtilizationBps, + 4 => Self::UserLiquidationPrice, + 5 => Self::UserDebtWithInterest, + _ => unreachable!(), + } + } +} diff --git a/trident-tests/fuzz_lending/futarchy/claim_protocol_fees.rs b/trident-tests/fuzz_lending/futarchy/claim_protocol_fees.rs new file mode 100644 index 0000000..87be3fb --- /dev/null +++ b/trident-tests/fuzz_lending/futarchy/claim_protocol_fees.rs @@ -0,0 +1,305 @@ +use crate::FuzzTest; +use crate::{ + types::{ + omnipair::{ + ClaimProtocolFeesInstruction, ClaimProtocolFeesInstructionAccounts, + ClaimProtocolFeesInstructionData, + }, + ClaimProtocolFeesArgs, Pair, + }, + utils::{TOKEN_PROGRAM}, +}; + +impl FuzzTest { + pub fn claim_protocol_fees(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_claim_protocol_fees(); + let data = self.get_data_claim_protocol_fees(); + + // Capture initial state before the transaction + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_authority_token0_balance = self + .trident + .get_token_account(accounts.authority_token0_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_authority_token1_balance = self + .trident + .get_token_account(accounts.authority_token1_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = + ClaimProtocolFeesInstruction::data(ClaimProtocolFeesInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Claim Protocol Fees")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientAmount) + if res.is_success() { + self.verify_claim_protocol_fees_invariants( + &data, + &accounts, + &initial_pair, + initial_authority_token0_balance, + initial_authority_token1_balance, + ); + } + } + + fn get_data_claim_protocol_fees(&mut self) -> ClaimProtocolFeesArgs { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair account should exist"); + + // Get protocol fees from the pair account + // Claim partial amounts (or 0 if no fees available) + let amount0 = if pair_account.protocol_revenue_reserve0 > 0 { + self.trident + .random_from_range(1..=pair_account.protocol_revenue_reserve0) + } else { + 0 + }; + + let amount1 = if pair_account.protocol_revenue_reserve1 > 0 { + self.trident + .random_from_range(1..=pair_account.protocol_revenue_reserve1) + } else { + 0 + }; + + self.trident + .record_histogram("CLAIM_PROTOCOL_FEES_AMOUNT0", amount0 as f64); + self.trident + .record_histogram("CLAIM_PROTOCOL_FEES_AMOUNT1", amount1 as f64); + + ClaimProtocolFeesArgs::new(amount0, amount1) + } + + fn get_accounts_claim_protocol_fees(&mut self) -> ClaimProtocolFeesInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair account should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // authority token0 account + let authority_token0_account = self.trident.get_associated_token_address( + &pair_account.token0, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + + // authority token1 account + let authority_token1_account = self.trident.get_associated_token_address( + &pair_account.token1, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + + // caller (whoever) + let caller = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + ClaimProtocolFeesInstructionAccounts::new( + caller, + pair_pubkey, + futarchy_authority, + token0_vault, + token1_vault, + authority_token0_account, + authority_token1_account, + pair_account.token0, + pair_account.token1, + ) + } + + fn verify_claim_protocol_fees_invariants( + &mut self, + args: &ClaimProtocolFeesArgs, + accounts: &ClaimProtocolFeesInstructionAccounts, + initial_pair: &Pair, + initial_authority_token0_balance: u64, + initial_authority_token1_balance: u64, + ) { + // Fetch final state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_authority_token0_balance = self + .trident + .get_token_account(accounts.authority_token0_account) + .expect("Authority token0 account should exist") + .account + .amount; + + let final_authority_token1_balance = self + .trident + .get_token_account(accounts.authority_token1_account) + .expect("Authority token1 account should exist") + .account + .amount; + + // INVARIANT 1: Authority received exactly the claimed amounts + if args.amount0 > 0 { + assert_eq!( + final_authority_token0_balance, + initial_authority_token0_balance + .checked_add(args.amount0) + .expect("Authority token0 balance increase"), + "Authority should receive exactly amount0 claimed" + ); + } + + if args.amount1 > 0 { + assert_eq!( + final_authority_token1_balance, + initial_authority_token1_balance + .checked_add(args.amount1) + .expect("Authority token1 balance increase"), + "Authority should receive exactly amount1 claimed" + ); + } + + // INVARIANT 2: Protocol revenue reserves decreased by exactly the claimed amounts + assert_eq!( + final_pair.protocol_revenue_reserve0, + initial_pair + .protocol_revenue_reserve0 + .checked_sub(args.amount0) + .expect("Protocol revenue reserve0 decrease"), + "Protocol revenue reserve0 should decrease by amount0" + ); + + assert_eq!( + final_pair.protocol_revenue_reserve1, + initial_pair + .protocol_revenue_reserve1 + .checked_sub(args.amount1) + .expect("Protocol revenue reserve1 decrease"), + "Protocol revenue reserve1 should decrease by amount1" + ); + + // INVARIANT 3: Vault solvency check - vaults must hold at least reserves + collateral - debt + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .checked_add(final_pair.protocol_revenue_reserve0) + .expect("Adding protocol revenue overflow") + .saturating_sub(final_pair.total_debt0); + + // Allow small tolerance for accumulated rounding errors (0.01% of required or claimed amount) + let tolerance0 = args.amount0.max(required0.checked_div(10000).unwrap_or(0)); + assert!( + vault0_balance >= required0.saturating_sub(tolerance0), + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .checked_add(final_pair.protocol_revenue_reserve1) + .expect("Adding protocol revenue overflow") + .saturating_sub(final_pair.total_debt1); + + // Allow small tolerance for accumulated rounding errors (0.01% of required or claimed amount) + let tolerance1 = args.amount1.max(required1.checked_div(10000).unwrap_or(0)); + assert!( + vault1_balance >= required1.saturating_sub(tolerance1), + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 4: Pair core state should not have changed + // (claiming fees doesn't affect liquidity, collateral, or debt) + assert_eq!( + final_pair.reserve0, initial_pair.reserve0, + "Reserve0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.reserve1, initial_pair.reserve1, + "Reserve1 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_supply, initial_pair.total_supply, + "Total supply should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_collateral0, initial_pair.total_collateral0, + "Total collateral0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_collateral1, initial_pair.total_collateral1, + "Total collateral1 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_debt0, initial_pair.total_debt0, + "Total debt0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_debt1, initial_pair.total_debt1, + "Total debt1 should not change when claiming protocol fees" + ); + + // INVARIANT 5: Pair identity should not have changed + assert_eq!( + final_pair.token0, initial_pair.token0, + "Pair token0 should not change" + ); + assert_eq!( + final_pair.token1, initial_pair.token1, + "Pair token1 should not change" + ); + assert_eq!( + final_pair.lp_mint, initial_pair.lp_mint, + "Pair LP mint should not change" + ); + } +} diff --git a/trident-tests/fuzz_lending/futarchy/distribute_tokens.rs b/trident-tests/fuzz_lending/futarchy/distribute_tokens.rs new file mode 100644 index 0000000..1393a26 --- /dev/null +++ b/trident-tests/fuzz_lending/futarchy/distribute_tokens.rs @@ -0,0 +1,77 @@ +use crate::types::{ + omnipair::{ + DistributeTokensInstruction, DistributeTokensInstructionAccounts, + DistributeTokensInstructionData, + }, + DistributeTokensArgs, +}; +use crate::utils::WSOL_MINT_ADDRESS; +use crate::FuzzTest; + +impl FuzzTest { + pub fn distribute_tokens(&mut self) { + let accounts = self.get_accounts_distribute_tokens(); + let data = self.get_data_distribute_tokens(); + + let pre_src_acc = self.trident.get_token_account(accounts.source_token_account); + let pre_amount = pre_src_acc.unwrap().account.amount; + + let ix = DistributeTokensInstruction::data(DistributeTokensInstructionData::new(data)) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Distribute Tokens")); + + // Verify the transaction was successful + assert!(res.is_success()); + + // INVARIANT 1: if the source had a positive balance, + // the distribution should drain the entire source (no dust left behind). + if pre_amount > 0 { + let post_src_acc = self.trident.get_token_account(accounts.source_token_account); + let post_amount = post_src_acc.unwrap().account.amount; + assert!(post_amount == 0, "DistributeTokens left dust in source token account (amount={})", post_amount); + } + } + + fn get_data_distribute_tokens(&mut self) -> DistributeTokensArgs { + DistributeTokensArgs::new() + } + + fn get_accounts_distribute_tokens(&mut self) -> DistributeTokensInstructionAccounts { + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Get the source token account - this should be an associated token account + // that holds tokens to be distributed + let source_token_account = self + .fuzz_accounts + .authority_wsol_account + .get(&mut self.trident).expect("Authority WSOl account should exist"); + + let futarchy_treasury_token_account = self + .fuzz_accounts + .futarchy_treasury_token_account + .get(&mut self.trident).expect("Futarchy treasury token account should exist"); + + let buybacks_vault_token_account = self + .fuzz_accounts + .buybacks_vault_token_account + .get(&mut self.trident).expect("Buybacks vault token account should exist"); + + let team_treasury_token_account = self + .fuzz_accounts + .team_treasury_token_account + .get(&mut self.trident).expect("Team treasury token account should exist"); + + DistributeTokensInstructionAccounts::new( + futarchy_authority, + WSOL_MINT_ADDRESS, + source_token_account, + futarchy_treasury_token_account, + buybacks_vault_token_account, + team_treasury_token_account, + ) + } +} diff --git a/trident-tests/fuzz_lending/futarchy/init_futarchy.rs b/trident-tests/fuzz_lending/futarchy/init_futarchy.rs new file mode 100644 index 0000000..7ca72c6 --- /dev/null +++ b/trident-tests/fuzz_lending/futarchy/init_futarchy.rs @@ -0,0 +1,193 @@ +use crate::utils::{WSOL_MINT_ADDRESS}; +use crate::FuzzTest; +use crate::{ + types::{ + omnipair::{ + InitFutarchyAuthorityInstruction, InitFutarchyAuthorityInstructionAccounts, + InitFutarchyAuthorityInstructionData, + }, + FutarchyAuthority, InitFutarchyAuthorityArgs, + }, + utils::DEPLOYER_ADDRESS, +}; +use trident_fuzz::fuzzing::*; + +impl FuzzTest { + pub fn init_futarchy(&mut self) { + // Init futarchy authority + let data = self.get_data_init_futarchy(); + let accounts = self.get_accounts_init_futarchy(); + + let ix = InitFutarchyAuthorityInstruction::data(InitFutarchyAuthorityInstructionData::new( + data.clone(), + )) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Init Futarchy Authority")); + + // MUST BE SUCCESSFUL + assert!(res.is_success()); + + // Verify invariants + self.verify_futarchy_invariants(&data, &accounts); + } + + fn get_data_init_futarchy(&mut self) -> InitFutarchyAuthorityArgs { + let swap_bps = self.trident.random_from_range(1..=5_000); + let interest_bps = self.trident.random_from_range(1..=5_000); + + self.trident + .record_histogram("INIT_FUTARCHY_SWAP_BPS", swap_bps as f64); + self.trident + .record_histogram("INIT_FUTARCHY_INTEREST_BPS", interest_bps as f64); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Futarchy treasury token account + let futarchy_treasury_token_account = self + .fuzz_accounts + .futarchy_treasury_token_account + .insert(&mut self.trident, None); + let init_futarchy_treasury_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &futarchy_treasury_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + + // Buybacks vault token account + let buybacks_vault_token_account = self + .fuzz_accounts + .buybacks_vault_token_account + .insert(&mut self.trident, None); + let init_buybacks_vault_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &buybacks_vault_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + // Team treasury token account + let team_treasury_token_account = self + .fuzz_accounts + .team_treasury_token_account + .insert(&mut self.trident, None); + let init_team_treasury_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &team_treasury_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + let ixs: Vec = vec![ + init_futarchy_treasury_token_account_ix, + init_buybacks_vault_token_account_ix, + init_team_treasury_token_account_ix, + ] + .into_iter() + .flatten() + .collect(); + let res = self.trident.process_transaction(&ixs, None); + assert!(res.is_success()); + + // Generate random bps values that sum to exactly 10_000 + let futarchy_treasury_bps = self.trident.random_from_range(0..=10_000); + let remaining_after_first = 10_000 - futarchy_treasury_bps; + + let buybacks_vault_bps = if remaining_after_first > 0 { + self.trident.random_from_range(0..=remaining_after_first) + } else { + 0 + }; + + // Calculate the remaining to ensure sum equals 10_000 + let team_treasury_bps = 10_000u16 + .saturating_sub(futarchy_treasury_bps) + .saturating_sub(buybacks_vault_bps); + + self.trident.record_histogram( + "INIT_FUTARCHY_FUTARCHY_TREASURY_BPS", + futarchy_treasury_bps as f64, + ); + self.trident.record_histogram( + "INIT_FUTARCHY_BUYBACKS_VAULT_BPS", + buybacks_vault_bps as f64, + ); + self.trident + .record_histogram("INIT_FUTARCHY_TEAM_TREASURY_BPS", team_treasury_bps as f64); + + InitFutarchyAuthorityArgs { + authority: DEPLOYER_ADDRESS, + swap_bps, + interest_bps, + futarchy_treasury: futarchy_treasury_token_account, + futarchy_treasury_bps, + buybacks_vault: buybacks_vault_token_account, + buybacks_vault_bps, + team_treasury: team_treasury_token_account, + team_treasury_bps, + } + } + + fn get_accounts_init_futarchy(&mut self) -> InitFutarchyAuthorityInstructionAccounts { + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + InitFutarchyAuthorityInstructionAccounts::new(futarchy_authority) + } + + fn verify_futarchy_invariants( + &mut self, + data: &InitFutarchyAuthorityArgs, + accounts: &InitFutarchyAuthorityInstructionAccounts, + ) { + let futarchy_authority_account = self + .trident + .get_account_with_type::(&accounts.futarchy_authority, 8); + + if let Some(futarchy_authority_account) = futarchy_authority_account { + // Verify recipients are set correctly + assert_eq!( + futarchy_authority_account.recipients.futarchy_treasury, data.futarchy_treasury, + "Futarchy treasury recipient mismatch" + ); + assert_eq!( + futarchy_authority_account.recipients.buybacks_vault, data.buybacks_vault, + "Buybacks vault recipient mismatch" + ); + assert_eq!( + futarchy_authority_account.recipients.team_treasury, data.team_treasury, + "Team treasury recipient mismatch" + ); + + // Verify revenue distribution BPS sum to 10_000 + let total_bps = futarchy_authority_account + .revenue_distribution + .futarchy_treasury_bps + .saturating_add( + futarchy_authority_account + .revenue_distribution + .buybacks_vault_bps, + ) + .saturating_add( + futarchy_authority_account + .revenue_distribution + .team_treasury_bps, + ); + assert_eq!( + total_bps, 10_000, + "Revenue distribution BPS must sum to 10_000" + ); + + // Verify revenue share BPS are within valid range + assert!( + futarchy_authority_account.revenue_share.swap_bps <= 10_000, + "Swap BPS must be <= 10_000" + ); + assert!( + futarchy_authority_account.revenue_share.interest_bps <= 10_000, + "Interest BPS must be <= 10_000" + ); + } + } +} diff --git a/trident-tests/fuzz_lending/futarchy/mod.rs b/trident-tests/fuzz_lending/futarchy/mod.rs new file mode 100644 index 0000000..4b1cb1a --- /dev/null +++ b/trident-tests/fuzz_lending/futarchy/mod.rs @@ -0,0 +1,3 @@ +mod claim_protocol_fees; +mod distribute_tokens; +mod init_futarchy; diff --git a/trident-tests/fuzz_lending/fuzz_accounts.rs b/trident-tests/fuzz_lending/fuzz_accounts.rs new file mode 100644 index 0000000..3f9d329 --- /dev/null +++ b/trident-tests/fuzz_lending/fuzz_accounts.rs @@ -0,0 +1,90 @@ +use trident_fuzz::fuzzing::*; + +/// Storage for all account addresses used in fuzz testing. +/// +/// This struct serves as a centralized repository for account addresses, +/// enabling their reuse across different instruction flows and test scenarios. +/// +/// Docs: https://ackee.xyz/trident/docs/latest/trident-api-macro/trident-types/fuzz-accounts/ +#[derive(Default)] +pub struct AccountAddresses { + pub user: AddressStorage, + + pub token_mint: AddressStorage, + + pub pair: AddressStorage, + + pub rate_model: AddressStorage, + + pub futarchy_authority: AddressStorage, + + pub user_position: AddressStorage, + + pub collateral_vault: AddressStorage, + + pub user_collateral_token_account: AddressStorage, + + pub collateral_token_mint: AddressStorage, + + pub token0_vault: AddressStorage, + + pub token1_vault: AddressStorage, + + pub token0_vault_mint: AddressStorage, + + pub token1_vault_mint: AddressStorage, + + pub lp_mint: AddressStorage, + + pub user_lp_token_account: AddressStorage, + + pub token_vault: AddressStorage, + + pub user_token_account: AddressStorage, + + pub vault_token_mint: AddressStorage, + + pub caller: AddressStorage, + + pub authority_token0_account: AddressStorage, + + pub authority_token1_account: AddressStorage, + + pub source_mint: AddressStorage, + + pub source_token_account: AddressStorage, + + pub futarchy_treasury_token_account: AddressStorage, + + pub buybacks_vault_token_account: AddressStorage, + + pub team_treasury_token_account: AddressStorage, + + pub receiver_token0_account: AddressStorage, + + pub receiver_token1_account: AddressStorage, + + pub deployer_token0_account: AddressStorage, + + pub deployer_token1_account: AddressStorage, + + pub authority_wsol_account: AddressStorage, + + pub caller_token_account: AddressStorage, + + pub position_owner: AddressStorage, + + pub token_in_vault: AddressStorage, + + pub token_out_vault: AddressStorage, + + pub user_token_in_account: AddressStorage, + + pub user_token_out_account: AddressStorage, + + pub token_in_mint: AddressStorage, + + pub token_out_mint: AddressStorage, + + pub authority_token_in_account: AddressStorage, +} diff --git a/trident-tests/fuzz_lending/lending/add_collateral.rs b/trident-tests/fuzz_lending/lending/add_collateral.rs new file mode 100644 index 0000000..46ca124 --- /dev/null +++ b/trident-tests/fuzz_lending/lending/add_collateral.rs @@ -0,0 +1,246 @@ +use crate::{ + types::{ + omnipair::{ + self, AddCollateralInstruction, AddCollateralInstructionAccounts, + AddCollateralInstructionData, + }, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn add_collateral(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_add_collateral(); + let accounts = self.get_accounts_add_collateral(); + + // Store initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_collateral_token_account) + .expect("User collateral account should exist") + .account + .amount; + + // Get initial pair and position state (position may not exist yet - init_if_needed) + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + let ix = AddCollateralInstruction::data(AddCollateralInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Add Collateral")); + + if res.is_success() { + self.verify_add_collateral_invariants( + &data, + &accounts, + initial_user_balance, + &initial_pair, + initial_position.as_ref(), + ); + } + } + + fn get_accounts_add_collateral(&mut self) -> AddCollateralInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // user related accounts + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let collateral_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + // let collateral_token_mint = pair_account.token0; + + let collateral_vault = self.trident.get_associated_token_address( + &collateral_token_mint, + &pair, + &TOKEN_PROGRAM, + ); + + let user_collateral_token_account = self.trident.get_associated_token_address( + &collateral_token_mint, + &user, + &TOKEN_PROGRAM, + ); + + AddCollateralInstructionAccounts::new( + pair, + pair_account.rate_model, + futarchy_authority, + user_position, + collateral_vault, + user_collateral_token_account, + collateral_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn get_data_add_collateral(&mut self) -> AdjustPositionArgs { + // Use reasonable collateral amounts that match typical use cases + let amount = self.trident.random_from_range(100_000..=100_000_000); + + self.trident + .record_histogram("ADD_COLLATERAL_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn verify_add_collateral_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &AddCollateralInstructionAccounts, + initial_user_balance: u64, + initial_pair: &Pair, + + initial_position: Option<&UserPosition>, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after add collateral"); + + // Get user position + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist after add collateral"); + + // Determine which token is being used as collateral + let is_token0 = accounts.collateral_token_mint == final_pair.token0; + + // Verify user balance decreased by exactly the amount + let final_user_balance = self + .trident + .get_token_account(accounts.user_collateral_token_account) + .expect("User collateral account should exist") + .account + .amount; + + let amount_transferred = initial_user_balance + .checked_sub(final_user_balance) + .expect("User balance should decrease"); + assert_eq!( + amount_transferred, args.amount, + "User balance should decrease by exactly the collateral amount" + ); + + // Verify user position collateral increased by EXACTLY the amount + // (position may be newly created, so initial collateral is 0 if it didn't exist) + if is_token0 { + let initial_collateral = initial_position.map(|p| p.collateral0).unwrap_or(0); + let expected_collateral = initial_collateral + .checked_add(args.amount) + .expect("User position collateral0 should not overflow"); + assert_eq!( + user_position.collateral0, expected_collateral, + "User position collateral0 should increase by exactly the amount added" + ); + } else { + let initial_collateral = initial_position.map(|p| p.collateral1).unwrap_or(0); + let expected_collateral = initial_collateral + .checked_add(args.amount) + .expect("User position collateral1 should not overflow"); + assert_eq!( + user_position.collateral1, expected_collateral, + "User position collateral1 should increase by exactly the amount added" + ); + } + + // Verify pair total_collateral increased by EXACTLY the amount (atomic transaction) + if is_token0 { + let expected_total = initial_pair + .total_collateral0 + .checked_add(args.amount) + .expect("Total collateral0 should not overflow"); + assert_eq!( + final_pair.total_collateral0, expected_total, + "Pair total_collateral0 should increase by exactly the amount added" + ); + } else { + let expected_total = initial_pair + .total_collateral1 + .checked_add(args.amount) + .expect("Total collateral1 should not overflow"); + assert_eq!( + final_pair.total_collateral1, expected_total, + "Pair total_collateral1 should increase by exactly the amount added" + ); + } + + // Critical accounting invariant: vault balance should be >= reserves + collateral - debt + // (vaults hold LP liquidity + borrower collateral, minus what was borrowed out) + let vault_balance = self + .trident + .get_token_account(accounts.collateral_vault) + .expect("Collateral vault should exist") + .account + .amount; + + if is_token0 { + let total_required = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); // Subtract debt (borrowed tokens are out of vault) + assert!( + vault_balance >= total_required, + "Vault balance must be >= reserve0 + total_collateral0 - total_debt0" + ); + } else { + let total_required = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); // Subtract debt (borrowed tokens are out of vault) + assert!( + vault_balance >= total_required, + "Vault balance must be >= reserve1 + total_collateral1 - total_debt1" + ); + } + + // Verify user position is properly initialized (always, whether new or existing) + assert_eq!( + user_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + user_position.pair, accounts.pair, + "User position pair should match pair" + ); + } +} diff --git a/trident-tests/fuzz_lending/lending/borrow.rs b/trident-tests/fuzz_lending/lending/borrow.rs new file mode 100644 index 0000000..8962bc4 --- /dev/null +++ b/trident-tests/fuzz_lending/lending/borrow.rs @@ -0,0 +1,334 @@ +use crate::{ + types::{ + omnipair::{self, BorrowInstruction, BorrowInstructionAccounts, BorrowInstructionData}, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn borrow(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_borrow(); + let accounts = self.get_accounts_borrow(); + + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if user_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = user_position.expect("User position should exist"); + + let ix = BorrowInstruction::data(BorrowInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Borrow")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., BorrowingPowerExceeded) + if res.is_success() { + self.verify_borrow_invariants( + &accounts, + &initial_pair, + &initial_position, + initial_user_balance, + ); + } + } + + fn get_data_borrow(&mut self) -> AdjustPositionArgs { + // Use very small amounts to increase success rate + // Borrowing power depends on collateral, which may be limited + // Use weighted distribution: 80% small amounts, 20% larger amounts + let amount = if self.trident.random_from_range(0..=9) < 8 { + // 80% chance: very small borrow (10 to 10,000) + self.trident.random_from_range(10..=10_000) + } else { + // 20% chance: larger borrow (10,000 to 100,000) + self.trident.random_from_range(10_000..=100_000) + }; + + self.trident + .record_histogram("BORROW_AMOUNT", amount as f64); + + AdjustPositionArgs::new(amount) + } + + fn get_accounts_borrow(&mut self) -> BorrowInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + // let vault_token_mint = pair_account.token1; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + BorrowInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_borrow_invariants( + &mut self, + accounts: &BorrowInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being borrowed + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Calculate actual borrow amount (may differ from args.amount if u64::MAX was used) + let actual_borrow_amount = final_user_balance + .checked_sub(initial_user_balance) + .expect("User balance should increase"); + + // INVARIANT 1: User should have received borrowed tokens + assert!( + actual_borrow_amount > 0, + "User should receive borrowed tokens" + ); + + // INVARIANT 2: Pair's total debt should increase by at least the borrow amount + // Note: update() accrues interest before borrowing, so total_debt may increase more than just the borrow amount + let (initial_total_debt, final_total_debt) = if is_token0 { + (initial_pair.total_debt0, final_pair.total_debt0) + } else { + (initial_pair.total_debt1, final_pair.total_debt1) + }; + let min_expected_debt = initial_total_debt + .checked_add(actual_borrow_amount) + .expect("Total debt increase calculation"); + assert!( + final_total_debt >= min_expected_debt, + "Pair total debt should be at least initial + borrow amount (was {}, expected >= {})", + final_total_debt, + min_expected_debt + ); + + // INVARIANT 3: User position debt shares should increase correctly + let (initial_debt_shares, final_debt_shares, initial_total_shares, final_total_shares) = + if is_token0 { + ( + initial_position.debt0_shares, + final_position.debt0_shares, + initial_pair.total_debt0_shares, + final_pair.total_debt0_shares, + ) + } else { + ( + initial_position.debt1_shares, + final_position.debt1_shares, + initial_pair.total_debt1_shares, + final_pair.total_debt1_shares, + ) + }; + + // Calculate expected shares increase + let expected_shares = if initial_total_shares == 0 { + // First debt: shares = amount (1:1 ratio) + actual_borrow_amount + } else { + // Subsequent debt: shares = amount * total_shares / total_debt + ((actual_borrow_amount as u128) + .checked_mul(initial_total_shares as u128) + .expect("Shares calculation") + .checked_div(initial_total_debt as u128) + .expect("Shares division")) as u64 + }; + + // Allow for 1-unit rounding tolerance in shares calculation + let expected_final_debt_shares = initial_debt_shares + .checked_add(expected_shares) + .expect("User debt shares increase"); + assert!( + final_debt_shares.abs_diff(expected_final_debt_shares) <= 1, + "User debt shares should increase correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_debt_shares, + final_debt_shares + ); + + let expected_final_total_shares = initial_total_shares + .checked_add(expected_shares) + .expect("Total debt shares increase"); + assert!( + final_total_shares.abs_diff(expected_final_total_shares) <= 1, + "Pair total debt shares should increase correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_total_shares, + final_total_shares + ); + + if is_token0 { + assert!( + final_position.debt0_shares > initial_position.debt0_shares, + "Debt0 shares should increase {:?} > {:?}", + final_position.debt0_shares, + initial_position.debt0_shares + ); + } else { + assert!( + final_position.debt1_shares > initial_position.debt1_shares, + "Debt1 shares should increase {:?} > {:?}", + final_position.debt1_shares, + initial_position.debt1_shares + ); + } + + // INVARIANT 4: Vault solvency check - vault balance >= reserves + collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 5: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 6: Collateral amounts should not change during borrow + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Collateral0 should not change during borrow" + ); + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Collateral1 should not change during borrow" + ); + + // INVARIANT 7: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "Borrow accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz_lending/lending/flashloan.rs b/trident-tests/fuzz_lending/lending/flashloan.rs new file mode 100644 index 0000000..8629c50 --- /dev/null +++ b/trident-tests/fuzz_lending/lending/flashloan.rs @@ -0,0 +1,292 @@ +use trident_fuzz::fuzzing::AccountMeta; + +use crate::{ + types::{ + omnipair::{ + self, FlashloanInstruction, FlashloanInstructionAccounts, FlashloanInstructionData, + }, + FlashloanArgs, Pair, + }, + utils::{EVENT_AUTHORITY_ADDRESS, FLASHLOAN_CALLBACK_RECEIVER_PROGRAM, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn flashloan(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + // NOTE: Flashloan failures (InsufficientAmount0/1) are expected and normal during fuzzing + // The callback receiver program needs to repay the loan + fees, which may not always succeed + let data = self.get_data_flashloan(); + let accounts = self.get_accounts_flashloan(); + + // Capture initial state before the transaction + let initial_vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let initial_vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let ix = FlashloanInstruction::data(data.clone()) + .accounts(accounts.clone()) + .remaining_accounts(vec![ + AccountMeta::new(accounts.token0_vault, false), + AccountMeta::new(accounts.token1_vault, false), + ]) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Flashloan")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., BorrowExceedsReserve, InsufficientAmount) + if res.is_success() { + self.verify_flashloan_invariants( + &data.args, + &accounts, + &initial_pair, + initial_vault0_balance, + initial_vault1_balance, + ); + } + } + + pub fn get_accounts_flashloan(&mut self) -> FlashloanInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let token0_vault = + self.trident + .get_associated_token_address(&pair_account.token0, &pair, &TOKEN_PROGRAM); + + let token1_vault = + self.trident + .get_associated_token_address(&pair_account.token1, &pair, &TOKEN_PROGRAM); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let receiver_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + let receiver_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + FlashloanInstructionAccounts::new( + pair, + pair_account.rate_model, + futarchy_authority, + token0_vault, + token1_vault, + pair_account.token0, + pair_account.token1, + receiver_token0_account, + receiver_token1_account, + FLASHLOAN_CALLBACK_RECEIVER_PROGRAM, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + pub fn get_data_flashloan(&mut self) -> FlashloanInstructionData { + // Strategy: Use very small amounts to maximize success rate + // Flashloans require the callback receiver to repay loan + fees, which often fails in fuzzing + let (amount0, amount1) = match self.trident.random_from_range(0..=100) { + // 40% - Flash only token0 (very small amount) + 0..=39 => (self.trident.random_from_range(100..=1_000), 0), + // 40% - Flash only token1 (very small amount) + 40..=79 => (0, self.trident.random_from_range(100..=1_000)), + // 15% - Flash both tokens (very small amounts) + 80..=94 => ( + self.trident.random_from_range(100..=500), + self.trident.random_from_range(100..=500), + ), + // 5% - Slightly larger amounts (stress test) + _ => ( + self.trident.random_from_range(1_000..=5_000), + self.trident.random_from_range(1_000..=5_000), + ), + }; + + self.trident + .record_histogram("FLASHLOAN_AMOUNT0", amount0 as f64); + self.trident + .record_histogram("FLASHLOAN_AMOUNT1", amount1 as f64); + + FlashloanInstructionData::new(FlashloanArgs::new(amount0, amount1, vec![])) + } + + fn verify_flashloan_invariants( + &mut self, + args: &FlashloanArgs, + accounts: &FlashloanInstructionAccounts, + initial_pair: &Pair, + initial_vault0_balance: u64, + initial_vault1_balance: u64, + ) { + // Fetch final state + let final_vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let final_vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + // Calculate expected fees (FLASHLOAN_FEE_BPS = 5 bps = 0.05%) + const FLASHLOAN_FEE_BPS: u64 = 5; + const BPS_DENOMINATOR: u64 = 10_000; + + let fee0 = (args.amount0 as u128) + .checked_mul(FLASHLOAN_FEE_BPS as u128) + .expect("Fee0 calculation") + .checked_div(BPS_DENOMINATOR as u128) + .expect("Fee0 division") as u64; + + let fee1 = (args.amount1 as u128) + .checked_mul(FLASHLOAN_FEE_BPS as u128) + .expect("Fee1 calculation") + .checked_div(BPS_DENOMINATOR as u128) + .expect("Fee1 division") as u64; + + // INVARIANT 1: Vault balances must have increased by at least the fee + // Critical: This ensures flashloan was repaid with fee + let required_balance0 = initial_vault0_balance + .checked_add(fee0) + .expect("Required balance0 calculation"); + let required_balance1 = initial_vault1_balance + .checked_add(fee1) + .expect("Required balance1 calculation"); + + assert!( + final_vault0_balance >= required_balance0, + "Token0 vault must have at least initial balance + fee after flashloan (repayment check)" + ); + assert!( + final_vault1_balance >= required_balance1, + "Token1 vault must have at least initial balance + fee after flashloan (repayment check)" + ); + + // INVARIANT 2: Net profit check - vault balances increased (fees were collected) + // Only check if fee > 0 (small amounts can round to 0 fee) + if fee0 > 0 { + assert!( + final_vault0_balance > initial_vault0_balance, + "Token0 vault should have more tokens after flashloan (fee collected)" + ); + } + if fee1 > 0 { + assert!( + final_vault1_balance > initial_vault1_balance, + "Token1 vault should have more tokens after flashloan (fee collected)" + ); + } + + // INVARIANT 3: Vault solvency check - vaults must hold at least reserves + collateral - debt + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + final_vault0_balance >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + final_vault1_balance >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 4: Pair state should not have fundamentally changed + // (reserves may have changed due to update() interest accrual, but core structure remains) + assert_eq!( + final_pair.token0, initial_pair.token0, + "Pair token0 should not change" + ); + assert_eq!( + final_pair.token1, initial_pair.token1, + "Pair token1 should not change" + ); + assert_eq!( + final_pair.lp_mint, initial_pair.lp_mint, + "Pair LP mint should not change" + ); + + // INVARIANT 5: Collateral and debt shares should not have changed + // (flashloans shouldn't affect lending positions) + // Note: total_debt can increase due to interest accrual, but debt_shares should remain constant + assert_eq!( + final_pair.total_collateral0, initial_pair.total_collateral0, + "Total collateral0 should not change during flashloan" + ); + assert_eq!( + final_pair.total_collateral1, initial_pair.total_collateral1, + "Total collateral1 should not change during flashloan" + ); + assert!( + final_pair.total_debt0 >= initial_pair.total_debt0, + "Total debt0 should be at least initial (interest can accrue). Was {}, initial {}", + final_pair.total_debt0, + initial_pair.total_debt0 + ); + assert!( + final_pair.total_debt1 >= initial_pair.total_debt1, + "Total debt1 should be at least initial (interest can accrue). Was {}, initial {}", + final_pair.total_debt1, + initial_pair.total_debt1 + ); + assert_eq!( + final_pair.total_debt0_shares, initial_pair.total_debt0_shares, + "Total debt0 shares should not change during flashloan" + ); + assert_eq!( + final_pair.total_debt1_shares, initial_pair.total_debt1_shares, + "Total debt1 shares should not change during flashloan" + ); + + // INVARIANT 6: Total supply should not have changed + // (flashloans don't mint or burn LP tokens) + assert_eq!( + final_pair.total_supply, initial_pair.total_supply, + "Total supply should not change during flashloan" + ); + } +} diff --git a/trident-tests/fuzz_lending/lending/liquidate.rs b/trident-tests/fuzz_lending/lending/liquidate.rs new file mode 100644 index 0000000..3afe70b --- /dev/null +++ b/trident-tests/fuzz_lending/lending/liquidate.rs @@ -0,0 +1,331 @@ +use trident_fuzz::fuzzing::LAMPORTS_PER_SOL; + +use crate::{ + types::{ + omnipair::{ + self, LiquidateInstruction, LiquidateInstructionAccounts, LiquidateInstructionData, + }, + Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn liquidate(&mut self) { + if self.fuzz_accounts.pair.is_empty() || self.fuzz_accounts.user_position.is_empty() { + return; + } + + let accounts = self.get_accounts_liquidate(); + + // Capture initial state before the transaction + let initial_caller_balance = self + .trident + .get_token_account(accounts.caller_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + let ix = LiquidateInstruction::data(LiquidateInstructionData::new()) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Liquidate")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., NotUndercollateralized, no debt) + if res.is_success() { + self.verify_liquidate_invariants( + &accounts, + &initial_pair, + &initial_position, + initial_caller_balance, + ); + } + } + + fn get_accounts_liquidate(&mut self) -> LiquidateInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user_position = self.fuzz_accounts.user_position.get(&mut self.trident).expect("User position should exist"); + let user_position_account_data = self + .trident + .get_account_with_type::(&user_position, 8) + .expect("User position should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let collateral_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let collateral_vault = self.trident.get_associated_token_address( + &collateral_token_mint, + &pair, + &TOKEN_PROGRAM, + ); + + let caller = self.fuzz_accounts.caller.insert(&mut self.trident, None); + self.trident.airdrop( + &caller, + LAMPORTS_PER_SOL.checked_mul(2).expect("Airdrop amount"), + ); + let caller_token_account = self.trident.get_associated_token_address( + &collateral_token_mint, + &caller, + &TOKEN_PROGRAM, + ); + self.trident + .initialize_associated_token_account(&caller, &collateral_token_mint, &caller); + + LiquidateInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + collateral_vault, + caller_token_account, + collateral_token_mint, + user_position_account_data.owner, + caller, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_liquidate_invariants( + &mut self, + accounts: &LiquidateInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_caller_balance: u64, + ) { + // Fetch final state + let final_caller_balance = self + .trident + .get_token_account(accounts.caller_token_account) + .expect("Caller token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is collateral and which is debt + let collateral_vault = self + .trident + .get_token_account(accounts.collateral_vault) + .expect("Collateral vault should exist") + .account; + let is_collateral_token0 = collateral_vault.mint == final_pair.token0; + + // Calculate initial debt + let (_, initial_debt_shares) = if is_collateral_token0 { + // Collateral is token0, debt is token1 + let debt = if initial_pair.total_debt1_shares == 0 { + 0 + } else { + ((initial_position.debt1_shares as u128) + .checked_mul(initial_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, initial_position.debt1_shares) + } else { + // Collateral is token1, debt is token0 + let debt = if initial_pair.total_debt0_shares == 0 { + 0 + } else { + ((initial_position.debt0_shares as u128) + .checked_mul(initial_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, initial_position.debt0_shares) + }; + + // INVARIANT 1: Caller received liquidation incentive (should be > 0) + let caller_incentive = final_caller_balance + .checked_sub(initial_caller_balance) + .expect("Caller balance should increase"); + assert!( + caller_incentive > 0, + "Liquidator should receive incentive for liquidating" + ); + + // INVARIANT 2: User position collateral decreased + let (initial_collateral, final_collateral) = if is_collateral_token0 { + (initial_position.collateral0, final_position.collateral0) + } else { + (initial_position.collateral1, final_position.collateral1) + }; + let collateral_seized = initial_collateral + .checked_sub(final_collateral) + .expect("Collateral should decrease"); + assert!( + collateral_seized > 0, + "Position collateral should be seized during liquidation" + ); + + // INVARIANT 3: Pair total collateral decreased by the seized amount + let (initial_total_collateral, final_total_collateral) = if is_collateral_token0 { + (initial_pair.total_collateral0, final_pair.total_collateral0) + } else { + (initial_pair.total_collateral1, final_pair.total_collateral1) + }; + assert_eq!( + final_total_collateral, + initial_total_collateral + .checked_sub(collateral_seized) + .expect("Total collateral decrease"), + "Pair total collateral should decrease by seized amount" + ); + + // INVARIANT 4: Collateral seized >= caller incentive (caller gets incentive, rest to reserves) + assert!( + collateral_seized >= caller_incentive, + "Collateral seized must cover at least the liquidation incentive" + ); + + // INVARIANT 5: Debt decreased or was written off (final debt <= initial debt) + let (_, final_debt_shares) = if is_collateral_token0 { + let debt = if final_pair.total_debt1_shares == 0 { + 0 + } else { + ((final_position.debt1_shares as u128) + .checked_mul(final_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(final_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, final_position.debt1_shares) + } else { + let debt = if final_pair.total_debt0_shares == 0 { + 0 + } else { + ((final_position.debt0_shares as u128) + .checked_mul(final_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(final_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, final_position.debt0_shares) + }; + + // Note: final_debt may not be less than initial_debt if significant interest accrued during update() + // The authoritative measure is debt_shares, which must decrease during liquidation + assert!( + final_debt_shares < initial_debt_shares, + "Debt shares must decrease during liquidation (was {}, now {})", + initial_debt_shares, final_debt_shares + ); + + // INVARIANT 6: Collateral reserve increased (by collateral_seized - caller_incentive) + let (initial_collateral_reserve, final_collateral_reserve) = if is_collateral_token0 { + (initial_pair.reserve0, final_pair.reserve0) + } else { + (initial_pair.reserve1, final_pair.reserve1) + }; + + // Note: Due to pair.update(), reserves may have interest accrued, so we can't do exact check + // But collateral reserve should have increased + assert!( + final_collateral_reserve >= initial_collateral_reserve, + "Collateral reserve should increase (seized collateral - incentive goes to reserves)" + ); + + // INVARIANT 7: Vault solvency check + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 8: Position ownership unchanged + assert_eq!( + final_position.owner, initial_position.owner, + "Position owner should not change" + ); + assert_eq!( + final_position.pair, initial_position.pair, + "Position pair should not change" + ); + + // INVARIANT 9: Non-seized collateral unchanged + if is_collateral_token0 { + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Non-seized collateral (token1) should not change" + ); + } else { + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Non-seized collateral (token0) should not change" + ); + } + } +} diff --git a/trident-tests/fuzz_lending/lending/mod.rs b/trident-tests/fuzz_lending/lending/mod.rs new file mode 100644 index 0000000..4e2896b --- /dev/null +++ b/trident-tests/fuzz_lending/lending/mod.rs @@ -0,0 +1,6 @@ +mod add_collateral; +mod borrow; +mod flashloan; +mod liquidate; +mod remove_collateral; +mod repay; diff --git a/trident-tests/fuzz_lending/lending/remove_collateral.rs b/trident-tests/fuzz_lending/lending/remove_collateral.rs new file mode 100644 index 0000000..265ce12 --- /dev/null +++ b/trident-tests/fuzz_lending/lending/remove_collateral.rs @@ -0,0 +1,309 @@ + + +use crate::{ + types::{ + omnipair::{ + self, RemoveCollateralInstruction, RemoveCollateralInstructionAccounts, + RemoveCollateralInstructionData, + }, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn remove_collateral(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_remove_collateral(); + let accounts = self.get_accounts_remove_collateral(); + + // Check if user position exists + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if initial_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let ix = + RemoveCollateralInstruction::data(RemoveCollateralInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Remove Collateral")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientCollateral, BorrowingPowerExceeded) + if res.is_success() { + self.verify_remove_collateral_invariants( + &data, + &accounts, + &initial_pair, + &initial_position.unwrap(), + initial_user_balance, + ); + } + } + + fn get_data_remove_collateral(&mut self) -> AdjustPositionArgs { + // Use smaller amounts more likely to match actual collateral amounts + let amount = self.trident.random_from_range(100..=1_000_000); + self.trident + .record_histogram("REMOVE_COLLATERAL_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn get_accounts_remove_collateral(&mut self) -> RemoveCollateralInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .unwrap(); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + RemoveCollateralInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_remove_collateral_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &RemoveCollateralInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being withdrawn + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Get initial collateral amount + let initial_collateral = if is_token0 { + initial_position.collateral0 + } else { + initial_position.collateral1 + }; + + // Calculate actual withdraw amount + // If args.amount == u64::MAX, the program withdraws all available collateral + let is_withdraw_all = args.amount == u64::MAX; + let actual_withdraw_amount = if is_withdraw_all { + initial_collateral + } else { + args.amount + }; + + // INVARIANT 1: User token balance should increase by exactly the withdraw amount + let amount_transferred = final_user_balance + .checked_sub(initial_user_balance) + .expect("User balance should increase"); + assert_eq!( + amount_transferred, actual_withdraw_amount, + "User should receive exactly the withdrawn amount" + ); + + // INVARIANT 2: User position collateral should decrease by exactly the withdraw amount + let final_collateral = if is_token0 { + final_position.collateral0 + } else { + final_position.collateral1 + }; + assert_eq!( + final_collateral, + initial_collateral + .checked_sub(actual_withdraw_amount) + .expect("Collateral decrease calculation"), + "User position collateral should decrease by withdraw amount" + ); + + // INVARIANT 3: Pair total collateral should decrease by exactly the withdraw amount + let (initial_total_collateral, final_total_collateral) = if is_token0 { + (initial_pair.total_collateral0, final_pair.total_collateral0) + } else { + (initial_pair.total_collateral1, final_pair.total_collateral1) + }; + assert_eq!( + final_total_collateral, + initial_total_collateral + .checked_sub(actual_withdraw_amount) + .expect("Total collateral decrease calculation"), + "Pair total collateral should decrease by withdraw amount" + ); + + // INVARIANT 4: If withdraw_all was requested, verify final collateral is 0 + // (this assumes no debt that would prevent full withdrawal - which is validated in the program) + if is_withdraw_all && actual_withdraw_amount == initial_collateral { + assert_eq!( + final_collateral, 0, + "When withdrawing all available collateral, final collateral should be 0" + ); + } + + // INVARIANT 5: Vault solvency - vaults must hold at least reserves + total_collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 6: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 7: Debt amounts should not change during collateral removal + assert_eq!( + final_position.debt0_shares, initial_position.debt0_shares, + "Debt0 shares should not change when removing collateral" + ); + assert_eq!( + final_position.debt1_shares, initial_position.debt1_shares, + "Debt1 shares should not change when removing collateral" + ); + + // INVARIANT 8: Non-withdrawn collateral unchanged + if is_token0 { + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Non-withdrawn collateral (token1) should not change" + ); + } else { + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Non-withdrawn collateral (token0) should not change" + ); + } + + // INVARIANT 9: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "RemoveCollateral accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz_lending/lending/repay.rs b/trident-tests/fuzz_lending/lending/repay.rs new file mode 100644 index 0000000..e3580b4 --- /dev/null +++ b/trident-tests/fuzz_lending/lending/repay.rs @@ -0,0 +1,350 @@ + + +use crate::{ + types::{ + omnipair::{self, RepayInstruction, RepayInstructionAccounts, RepayInstructionData}, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn repay(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_repay(); + let accounts = self.get_accounts_repay(); + + // Check if user position exists + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if user_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = user_position.unwrap(); + + let ix = RepayInstruction::data(RepayInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Repay")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientDebt, InsufficientAmount) + if res.is_success() { + self.verify_repay_invariants( + &data, + &accounts, + &initial_pair, + &initial_position, + initial_user_balance, + ); + } + } + + fn get_data_repay(&mut self) -> AdjustPositionArgs { + // Use small amounts more likely to match actual debt amounts + // Weighted distribution: favor smaller repayments + let amount = if self.trident.random_from_range(0..=9) < 7 { + self.trident.random_from_range(10..=10_000) + } else { + self.trident.random_from_range(10_000..=100_000) + }; + self.trident.record_histogram("REPAY_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn get_accounts_repay(&mut self) -> RepayInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + RepayInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_repay_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &RepayInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being repaid + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Calculate initial debt manually: debt = (user_shares * total_debt) / total_shares + let initial_debt = if is_token0 { + if initial_pair.total_debt0_shares == 0 { + 0 + } else { + ((initial_position.debt0_shares as u128) + .checked_mul(initial_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + } + } else if initial_pair.total_debt1_shares == 0 { + 0 + } else { + ((initial_position.debt1_shares as u128) + .checked_mul(initial_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + + // Calculate actual repay amount (may differ from args.amount if u64::MAX was used) + let is_repay_all = args.amount == u64::MAX; + let actual_repay_amount = initial_user_balance + .checked_sub(final_user_balance) + .expect("User balance should decrease"); + + // INVARIANT 1: User should have paid tokens for repayment + assert!( + actual_repay_amount > 0, + "User should have paid tokens for repayment" + ); + + // INVARIANT 2: If repaying all, verify the repay amount equals initial debt + if is_repay_all { + assert_eq!( + actual_repay_amount, initial_debt, + "Repay all should repay exactly the full debt amount" + ); + } + + // INVARIANT 3: Pair's total debt accounting + // Note: update() accrues interest before repayment, so we can't predict exact final debt + // The repayment reduces debt, but interest may have accrued first + // We verify the shares decrease correctly instead (which is the authoritative measure) + let (initial_total_debt, _) = if is_token0 { + (initial_pair.total_debt0, final_pair.total_debt0) + } else { + (initial_pair.total_debt1, final_pair.total_debt1) + }; + + // The final debt should be reasonable - not more than initial + potential interest + // and definitely less than initial if significant time hasn't passed + // Main check: verify shares decreased correctly (below) + + // INVARIANT 4: User position debt shares should decrease correctly + let (initial_debt_shares, final_debt_shares, initial_total_shares, final_total_shares) = + if is_token0 { + ( + initial_position.debt0_shares, + final_position.debt0_shares, + initial_pair.total_debt0_shares, + final_pair.total_debt0_shares, + ) + } else { + ( + initial_position.debt1_shares, + final_position.debt1_shares, + initial_pair.total_debt1_shares, + final_pair.total_debt1_shares, + ) + }; + + // Calculate expected shares decrease + let expected_shares = if is_repay_all { + // Repay all: use user's debt shares + initial_debt_shares + } else { + // shares = amount * total_shares / total_debt + ((actual_repay_amount as u128) + .checked_mul(initial_total_shares as u128) + .expect("Shares calculation") + .checked_div(initial_total_debt as u128) + .expect("Shares division")) as u64 + }; + + // Allow for 1-unit rounding tolerance in shares calculation + let expected_final_debt_shares = initial_debt_shares + .checked_sub(expected_shares) + .expect("User debt shares decrease"); + + // INVARIANT 5: If repay all, verify final debt shares are 0 + if is_repay_all { + assert_eq!( + final_debt_shares, 0, + "Repay all should result in zero debt shares" + ); + } else { + assert!( + final_debt_shares.abs_diff(expected_final_debt_shares) <= 1, + "User debt shares should decrease correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_debt_shares, + final_debt_shares + ); + } + + let expected_final_total_shares = initial_total_shares + .checked_sub(expected_shares) + .expect("Total debt shares decrease"); + assert!( + final_total_shares.abs_diff(expected_final_total_shares) <= 1, + "Pair total debt shares should decrease correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_total_shares, + final_total_shares + ); + + // INVARIANT 6: Vault solvency check - vault balance >= reserves + collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 7: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 8: Collateral amounts should not change during repay + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Collateral0 should not change during repay" + ); + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Collateral1 should not change during repay" + ); + + // INVARIANT 9: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "Repay accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz_lending/liquidity/add_liquidity.rs b/trident-tests/fuzz_lending/liquidity/add_liquidity.rs new file mode 100644 index 0000000..22a619b --- /dev/null +++ b/trident-tests/fuzz_lending/liquidity/add_liquidity.rs @@ -0,0 +1,367 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{ + self, AddLiquidityInstruction, AddLiquidityInstructionAccounts, + AddLiquidityInstructionData, + }, + AddLiquidityArgs, Pair, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn add_liquidity(&mut self) { + // Check if any pairs exist before trying to add liquidity + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let accounts = self.get_accounts_add_liquidity(); + let data = self.get_data_add_liquidity(accounts.pair); + + let ix = AddLiquidityInstruction::data(AddLiquidityInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + // Store initial state + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + let initial_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let res = self + .trident + .process_transaction(&[ix], Some("Add Liquidity")); + + if res.is_success() { + self.verify_add_liquidity_invariants(&data, &accounts, &initial_pair, initial_user_lp); + } + } + + fn get_data_add_liquidity(&mut self, pair_pubkey: Pubkey) -> AddLiquidityArgs { + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + let total_supply = pair.total_supply; + + // Strategy: Test different liquidity addition scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Proportional liquidity (maintains price ratio) + 0..=39 => { + // Add liquidity proportional to current reserves + let proportion = self.trident.random_from_range(1..=1000); // 0.1% to 100% of reserves + let amount0_in = reserve0.saturating_mul(proportion) / 1000; + let amount1_in = reserve1.saturating_mul(proportion) / 1000; + + // Calculate expected liquidity: both ratios should be equal + let expected_liquidity = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap() as u64; + + // Set min_liquidity with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let min_liquidity_out = + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out, + } + } + + // 30% - Imbalanced liquidity (one side larger) + 40..=69 => { + // This will result in min(liquidity0, liquidity1) + let base_amount = self.trident.random_from_range(100..=100_000_000); + let imbalance_factor = self.trident.random_from_range(50..=200); // 0.5x to 2x + + let amount0_in: u64 = base_amount; + let amount1_in = base_amount.saturating_mul(imbalance_factor) / 100; + + // Calculate expected (will be the minimum) + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap(); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve1 as u128) + .unwrap(); + let expected = liquidity0.min(liquidity1) as u64; + + // Random slippage tolerance + let slippage_bps = self.trident.random_from_range(1..=500); + let min_liquidity_out = expected.saturating_mul(10000 - slippage_bps) / 10000; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out, + } + } + + // 15% - Small amounts (dust testing) + 70..=84 => { + let amount0_in = self.trident.random_from_range(1..=1000); + let amount1_in = self.trident.random_from_range(1..=1000); + + // Calculate expected + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap_or(1); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve1 as u128) + .unwrap_or(1); + let expected = liquidity0.min(liquidity1) as u64; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: expected.saturating_sub(1), // Allow for rounding + } + } + + // 10% - Large amounts (stress testing) + 85..=94 => { + let amount0_in = self.trident.random_from_range(1_000_000..=u64::MAX / 1000); + let amount1_in = self.trident.random_from_range(1_000_000..=u64::MAX / 1000); + + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap_or(u128::MAX) + .checked_div(reserve0 as u128) + .unwrap_or(u128::MAX); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap_or(u128::MAX) + .checked_div(reserve1 as u128) + .unwrap_or(u128::MAX); + let expected = liquidity0.min(liquidity1).min(u64::MAX as u128) as u64; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: expected / 2, // Large slippage tolerance + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero amounts (should fail with AmountZero) + 0 => AddLiquidityArgs { + amount0_in: 0, + amount1_in: self.trident.random_from_range(1..=1000), + min_liquidity_out: 0, + }, + 1 => AddLiquidityArgs { + amount0_in: self.trident.random_from_range(1..=1000), + amount1_in: 0, + min_liquidity_out: 0, + }, + // Unrealistic min_liquidity (should fail with InsufficientLiquidity) + 2 => { + let amount0_in = self.trident.random_from_range(100..=10000); + let amount1_in = self.trident.random_from_range(100..=10000); + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: u64::MAX, // Impossible to satisfy + } + } + // Overflow scenarios + 3 => AddLiquidityArgs { + amount0_in: u64::MAX, + amount1_in: u64::MAX, + min_liquidity_out: 0, + }, + // Random chaos + _ => AddLiquidityArgs { + amount0_in: self.trident.random_from_range(1..=u64::MAX), + amount1_in: self.trident.random_from_range(1..=u64::MAX), + min_liquidity_out: self.trident.random_from_range(1..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_add_liquidity(&mut self) -> AddLiquidityInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + //user token0 account + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + //user token1 account + let user_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + // user lp token account + let user_lp_token_account = + self.trident + .get_associated_token_address(&pair_account.lp_mint, &user, &TOKEN_PROGRAM); + + AddLiquidityInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token0_vault, + token1_vault, + user_token0_account, + user_token1_account, + pair_account.token0, + pair_account.token1, + pair_account.lp_mint, + user_lp_token_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_add_liquidity_invariants( + &mut self, + args: &AddLiquidityArgs, + accounts: &AddLiquidityInstructionAccounts, + initial_pair: &Pair, + initial_user_lp: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after add liquidity"); + + // Calculate expected liquidity using the same formula as the program + // liquidity = min(amount0_in * total_supply / reserve0, amount1_in * total_supply / reserve1) + let liquidity0 = (args.amount0_in as u128) + .checked_mul(initial_pair.total_supply as u128) + .unwrap() + .checked_div(initial_pair.reserve0 as u128) + .unwrap(); + let liquidity1 = (args.amount1_in as u128) + .checked_mul(initial_pair.total_supply as u128) + .unwrap() + .checked_div(initial_pair.reserve1 as u128) + .unwrap(); + let expected_liquidity = liquidity0.min(liquidity1) as u64; + + // Check pair reserves increased by at least the deposited amounts + // Note: reserves can increase more than deposited amounts due to LP share of accrued interest + let min_expected_reserve0 = initial_pair.reserve0.checked_add(args.amount0_in).unwrap(); + let min_expected_reserve1 = initial_pair.reserve1.checked_add(args.amount1_in).unwrap(); + assert!( + final_pair.reserve0 >= min_expected_reserve0, + "Pair reserve0 should be at least initial + amount0_in (was {}, expected >= {})", + final_pair.reserve0, + min_expected_reserve0 + ); + assert!( + final_pair.reserve1 >= min_expected_reserve1, + "Pair reserve1 should be at least initial + amount1_in (was {}, expected >= {})", + final_pair.reserve1, + min_expected_reserve1 + ); + + // Check pair total_supply increased by liquidity minted + let expected_total_supply = initial_pair + .total_supply + .checked_add(expected_liquidity) + .unwrap(); + assert_eq!( + final_pair.total_supply, expected_total_supply, + "Pair total_supply should increase by liquidity minted" + ); + + // Check user received the expected liquidity tokens + let final_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let expected_user_lp = initial_user_lp.checked_add(expected_liquidity).unwrap(); + assert_eq!( + final_user_lp, expected_user_lp, + "User should receive expected liquidity tokens" + ); + + // Check user received at least min_liquidity_out + let lp_received = final_user_lp.saturating_sub(initial_user_lp); + assert!( + lp_received >= args.min_liquidity_out, + "User should receive at least min_liquidity_out" + ); + + // Critical accounting invariant: vault balances should be >= pair reserves + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + assert!( + vault0_balance >= final_pair.reserve0, + "Token0 vault balance must be >= pair reserve0" + ); + assert!( + vault1_balance >= final_pair.reserve1, + "Token1 vault balance must be >= pair reserve1" + ); + } +} diff --git a/trident-tests/fuzz_lending/liquidity/init_pair.rs b/trident-tests/fuzz_lending/liquidity/init_pair.rs new file mode 100644 index 0000000..85916de --- /dev/null +++ b/trident-tests/fuzz_lending/liquidity/init_pair.rs @@ -0,0 +1,361 @@ +use crate::{ + types::{ + omnipair::{ + self, InitializeInstruction, InitializeInstructionAccounts, InitializeInstructionData, + }, + InitializeAndBootstrapArgs, Pair, + }, + utils::{ + EVENT_AUTHORITY_ADDRESS, METADATA_SEED_PREFIX, + MPL_TOKEN_METADATA_ID, PAIR_SEED_PREFIX, TOKEN_PROGRAM, + }, + FuzzTest, +}; +use trident_fuzz::fuzzing::{solana_sdk::rent::Rent, *}; + +impl FuzzTest { + pub fn init_pair(&mut self) { + // Init pair and bootstrap + let data = self.get_data_init_pair(); + let accounts = self.get_accounts_init_pair(data.pair_nonce); + + let ix = InitializeInstruction::data(InitializeInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + // Store initial balances + let initial_deployer_token0 = self + .trident + .get_token_account(accounts.deployer_token0_account) + .expect("Deployer token0 account should exist") + .account + .amount; + let initial_deployer_token1 = self + .trident + .get_token_account(accounts.deployer_token1_account) + .expect("Deployer token1 account should exist") + .account + .amount; + let initial_deployer_sol = self.trident.get_account(&accounts.deployer).lamports(); + let initial_authority_wsol = self + .trident + .get_token_account(accounts.authority_wsol_account) + .expect("Authority WSOL token account should exist") + .account + .amount; + + let res = self.trident.process_transaction(&[ix], Some("Init Pair")); + + if res.is_success() { + // Initialization must NOT succeed when token0_mint == token1_mint + assert_ne!( + accounts.token0_mint, accounts.token1_mint, + "initialize succeeded with identical mints: token0_mint == token1_mint ({})", + accounts.token0_mint + ); + + self.store_accounts_init_pair(&accounts); + self.verify_init_pair_invariants( + &data, + &accounts, + initial_deployer_token0, + initial_deployer_token1, + initial_deployer_sol, + initial_authority_wsol, + ); + } + } + + fn get_data_init_pair(&mut self) -> InitializeAndBootstrapArgs { + let swap_fee_bps = self.trident.random_from_range(0..=10_000); // 0 to 100% + let half_life = self.trident.random_from_range(60..=12 * 60 * 60); // 1min to 12 hours + let fixed_cf_bps = self.trident.random_from_range(100..=10_000); // 1% to 100% + + self.trident + .record_histogram("INIT_PAIR_FIXED_CF_BPS", fixed_cf_bps as f64); + self.trident + .record_histogram("INIT_PAIR_HALF_LIFE", half_life as f64); + self.trident + .record_histogram("INIT_PAIR_SWAP_FEE_BPS", swap_fee_bps as f64); + + let fixed_cf_bps = if self.trident.random_from_range(0..=1) == 1 { + self.trident + .record_histogram("INIT_PAIR_FIXED_CF_BPS", fixed_cf_bps as f64); + Some(fixed_cf_bps) + } else { + None + }; + + let mut pair_nonce = [0u8; 16]; + self.trident.random_bytes(&mut pair_nonce); + + let amount0_in = self.trident.random_from_range(100..=100_000_000_000); + let amount1_in = self.trident.random_from_range(100..=100_000_000_000); + + self.trident + .record_histogram("INIT_PAIR_AMOUNT0_IN", amount0_in as f64); + self.trident + .record_histogram("INIT_PAIR_AMOUNT1_IN", amount1_in as f64); + + // Calculate expected liquidity using the same formula as the program + // liquidity = sqrt(amount0_in * amount1_in) - MIN_LIQUIDITY + let expected_liquidity = (amount0_in as u128) + .checked_mul(amount1_in as u128) + .map(|x| x.isqrt()) + .and_then(|x| x.checked_sub(1000)) // MIN_LIQUIDITY = 1000 + .unwrap_or(0) as u64; + + // Strategy: Test different scenarios + let min_liquidity_out = match self.trident.random_from_range(0..=100) { + // 70% - Normal case: small slippage tolerance (0-2%) + 0..=69 => { + let slippage_bps = self.trident.random_from_range(0..=200); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 15% - Tight slippage: very small tolerance (0-0.5%) + 70..=84 => { + let slippage_bps = self.trident.random_from_range(0..=50); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 10% - Large slippage: stress test (up to 50%) + 85..=94 => { + let slippage_bps = self.trident.random_from_range(0..=5000); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 5% - Edge cases (should sometimes fail) + _ => { + match self.trident.random_from_range(0..=4) { + 0 => 0, // No slippage protection + 1 => expected_liquidity + 1, // Impossible (expects more than possible) + 2 => u64::MAX, // Maximum value + 3 => expected_liquidity, // Exact match + _ => self.trident.random_from_range(1..=u64::MAX), // Random chaos + } + } + }; + + self.trident + .record_histogram("INIT_PAIR_MIN_LIQUIDITY_OUT", min_liquidity_out as f64); + + let lp_name = self.trident.random_string(10); + let lp_symbol = self.trident.random_string(10); + let lp_uri = self.trident.random_string(10); + + InitializeAndBootstrapArgs { + swap_fee_bps, + half_life, + fixed_cf_bps, + pair_nonce, + amount0_in, + amount1_in, + min_liquidity_out, + lp_name, + lp_symbol, + lp_uri: "https://".to_owned() + &lp_uri, + } + } + + fn get_accounts_init_pair(&mut self, pair_nonce: [u8; 16]) -> InitializeInstructionAccounts { + let token0_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token0 mint should exist"); + let mut token1_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token1 mint should exist"); + + while token0_mint == token1_mint { + token1_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token1 mint should exist"); + } + + // AKA deployer -> deploys pair + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let pair = self + .trident + .find_program_address( + &[ + PAIR_SEED_PREFIX, + token0_mint.as_ref(), + token1_mint.as_ref(), + pair_nonce.as_ref(), + ], + &omnipair::program_id(), + ) + .0; + + // futarchy authority PDA + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // rate model + let rate_model = self.trident.random_keypair().pubkey(); + + // lp mint + + let lp_mint = self.trident.random_keypair().pubkey(); + let rent = self.trident.get_sysvar::(); + let account_custom = AccountSharedData::new(rent.minimum_balance(82), 82, &TOKEN_PROGRAM); + self.trident.set_account_custom(&lp_mint, &account_custom); + + // lp token metadata + let lp_token_metadata = self + .trident + .find_program_address( + &[ + METADATA_SEED_PREFIX, + MPL_TOKEN_METADATA_ID.as_ref(), + lp_mint.as_ref(), + ], + &MPL_TOKEN_METADATA_ID, + ) + .0; + + // deployer lp token account + let deployer_lp_token_account = + self.trident + .get_associated_token_address(&lp_mint, &user, &TOKEN_PROGRAM); + + // token0 vault + let token0_vault = + self.trident + .get_associated_token_address(&token0_mint, &pair, &TOKEN_PROGRAM); + + // token1 vault + let token1_vault = + self.trident + .get_associated_token_address(&token1_mint, &pair, &TOKEN_PROGRAM); + + // deployer token0 account + let deployer_token0_account = + self.trident + .get_associated_token_address(&token0_mint, &user, &TOKEN_PROGRAM); + + // deployer token1 account + let deployer_token1_account = + self.trident + .get_associated_token_address(&token1_mint, &user, &TOKEN_PROGRAM); + + InitializeInstructionAccounts::new( + user, + token0_mint, + token1_mint, + pair, + futarchy_authority, + rate_model, + lp_mint, + lp_token_metadata, + deployer_lp_token_account, + token0_vault, + token1_vault, + deployer_token0_account, + deployer_token1_account, + self.fuzz_accounts + .authority_wsol_account + .get(&mut self.trident).expect("Authority WSOL account should exist"), + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn store_accounts_init_pair(&mut self, accounts: &InitializeInstructionAccounts) { + self.fuzz_accounts.pair.insert_with_address(accounts.pair); + self.fuzz_accounts + .rate_model + .insert_with_address(accounts.rate_model); + self.fuzz_accounts + .lp_mint + .insert_with_address(accounts.lp_mint); + } + + fn verify_init_pair_invariants( + &mut self, + args: &InitializeAndBootstrapArgs, + accounts: &InitializeInstructionAccounts, + _initial_deployer_token0: u64, + _initial_deployer_token1: u64, + _initial_deployer_sol: u64, + initial_authority_wsol: u64, + ) { + const PAIR_CREATION_FEE_LAMPORTS: u64 = 200_000_000; // 0.2 SOL + const MIN_LIQUIDITY: u64 = 1000; + + // Calculate expected liquidity using the same formula as the program + // liquidity = sqrt(amount0_in * amount1_in) - MIN_LIQUIDITY + let expected_liquidity = (args.amount0_in as u128) + .checked_mul(args.amount1_in as u128) + .map(|x| x.isqrt()) + .and_then(|x| x.checked_sub(MIN_LIQUIDITY as u128)) + .unwrap_or(0) as u64; + + // Check pair state (fresh for each new pair) + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair account should exist"); + + // Check pair reserves match amounts deposited + assert_eq!( + pair_account.reserve0, args.amount0_in, + "Pair reserve0 should match amount0_in" + ); + assert_eq!( + pair_account.reserve1, args.amount1_in, + "Pair reserve1 should match amount1_in" + ); + + // Check pair total_supply includes MIN_LIQUIDITY (locked/burned permanently) + assert_eq!( + pair_account.total_supply, + expected_liquidity + MIN_LIQUIDITY, + "Pair total_supply should match total liquidity (including locked MIN_LIQUIDITY)" + ); + + // Check pair parameters + assert_eq!( + pair_account.swap_fee_bps, args.swap_fee_bps, + "Pair swap_fee_bps should match args" + ); + assert_eq!( + pair_account.half_life, args.half_life, + "Pair half_life should match args" + ); + assert_eq!( + pair_account.fixed_cf_bps, args.fixed_cf_bps, + "Pair fixed_cf_bps should match args" + ); + + // Check vault balances (vaults might be reused in fuzzing, so check >= pair reserves) + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + // Critical accounting invariant: vault balances must be at least pair reserves + assert!( + vault0_balance >= pair_account.reserve0, + "Token0 vault balance must be >= pair reserve0" + ); + assert!( + vault1_balance >= pair_account.reserve1, + "Token1 vault balance must be >= pair reserve1" + ); + + // Check authority WSOL balance increased by pair creation fee + let final_authority_wsol = self + .trident + .get_token_account(accounts.authority_wsol_account) + .expect("Authority WSOL account should exist") + .account + .amount; + assert!( + final_authority_wsol >= initial_authority_wsol + PAIR_CREATION_FEE_LAMPORTS, + "Authority WSOL balance should increase by at least pair creation fee" + ); + } +} diff --git a/trident-tests/fuzz_lending/liquidity/mod.rs b/trident-tests/fuzz_lending/liquidity/mod.rs new file mode 100644 index 0000000..1774d85 --- /dev/null +++ b/trident-tests/fuzz_lending/liquidity/mod.rs @@ -0,0 +1,3 @@ +mod add_liquidity; +mod init_pair; +mod remove_liquidity; diff --git a/trident-tests/fuzz_lending/liquidity/remove_liquidity.rs b/trident-tests/fuzz_lending/liquidity/remove_liquidity.rs new file mode 100644 index 0000000..12206fc --- /dev/null +++ b/trident-tests/fuzz_lending/liquidity/remove_liquidity.rs @@ -0,0 +1,463 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{ + self, RemoveLiquidityInstruction, RemoveLiquidityInstructionAccounts, + RemoveLiquidityInstructionData, + }, + Pair, RemoveLiquidityArgs, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn remove_liquidity(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_remove_liquidity(); + + // Check if user has any LP token account initialized + if self + .trident + .get_token_account(accounts.user_lp_token_account) + .is_err() + { + // No LP token account found, skip + return; + } + + let data = self.get_data_remove_liquidity(accounts.pair, accounts.user_lp_token_account); + + self.trident + .record_histogram("REMOVE_LIQUIDITY_LIQUIDITY_IN", data.liquidity_in as f64); + self.trident.record_histogram( + "REMOVE_LIQUIDITY_MIN_AMOUNT0_OUT", + data.min_amount0_out as f64, + ); + self.trident.record_histogram( + "REMOVE_LIQUIDITY_MIN_AMOUNT1_OUT", + data.min_amount1_out as f64, + ); + + // Store initial state + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + let initial_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let initial_user_token0 = self + .trident + .get_token_account(accounts.user_token0_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + let initial_user_token1 = self + .trident + .get_token_account(accounts.user_token1_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = + RemoveLiquidityInstruction::data(RemoveLiquidityInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Remove Liquidity")); + + if res.is_success() { + self.verify_remove_liquidity_invariants( + &data, + &accounts, + &initial_pair, + initial_user_lp, + initial_user_token0, + initial_user_token1, + ); + } + } + + fn get_data_remove_liquidity( + &mut self, + pair_pubkey: Pubkey, + user_lp_token_account: Pubkey, + ) -> RemoveLiquidityArgs { + let user_lp_balance = self + .trident + .get_token_account(user_lp_token_account) + .expect("User LP token account should exist") + .account + .amount; + + // Early return if user has no LP tokens to avoid empty range errors + if user_lp_balance == 0 { + return RemoveLiquidityArgs { + liquidity_in: 0, + min_amount0_out: 0, + min_amount1_out: 0, + }; + } + + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + let total_supply = pair.total_supply; + + // Strategy: Test different liquidity removal scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Proportional removal (remove a percentage of user's LP tokens) + 0..=39 => { + // Remove between 1% and 100% of user's LP tokens + let percentage = self.trident.random_from_range(1..=100); + let liquidity_in = user_lp_balance + .saturating_mul(percentage) + .checked_div(100) + .unwrap_or(0); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + // Set min amounts with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let slippage_multiplier = 10000u64.checked_sub(slippage_bps).unwrap_or(10000); + let min_amount0_out = amount0_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + let min_amount1_out = amount1_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out, + min_amount1_out, + } + } + + // 30% - Small amounts (dust testing) + 40..=69 => { + let liquidity_in = self + .trident + .random_from_range(1..=1000.min(user_lp_balance.max(1))); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out: amount0_out.saturating_sub(1), // Allow for rounding + min_amount1_out: amount1_out.saturating_sub(1), + } + } + + // 15% - Remove all liquidity + 70..=84 => { + let liquidity_in = user_lp_balance; + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + // Allow for some slippage + let slippage_bps = self.trident.random_from_range(0..=500); + let slippage_multiplier = 10000u64.checked_sub(slippage_bps).unwrap_or(10000); + let min_amount0_out = amount0_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + let min_amount1_out = amount1_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out, + min_amount1_out, + } + } + + // 10% - Unrealistic expectations (should fail) + 85..=94 => { + let liquidity_in = self + .trident + .random_from_range(1..=user_lp_balance.max(1000)); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + // Set unrealistic min amounts (should fail with InsufficientOutput) + let multiplier = self.trident.random_from_range(2..=10); + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out: amount0_out.saturating_mul(multiplier), + min_amount1_out: amount1_out.saturating_mul(multiplier), + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero liquidity (should fail with AmountZero) + 0 => RemoveLiquidityArgs { + liquidity_in: 0, + min_amount0_out: 0, + min_amount1_out: 0, + }, + // More liquidity than user has (should fail with InsufficientBalance) + 1 => RemoveLiquidityArgs { + liquidity_in: user_lp_balance.saturating_add(1_000_000), + min_amount0_out: 0, + min_amount1_out: 0, + }, + // Unrealistic min amounts + 2 => RemoveLiquidityArgs { + liquidity_in: self.trident.random_from_range(1..=1000), + min_amount0_out: u64::MAX, + min_amount1_out: u64::MAX, + }, + // Overflow scenarios + 3 => RemoveLiquidityArgs { + liquidity_in: u64::MAX, + min_amount0_out: 0, + min_amount1_out: 0, + }, + // Random chaos + _ => RemoveLiquidityArgs { + liquidity_in: self.trident.random_from_range(1..=u64::MAX), + min_amount0_out: self.trident.random_from_range(0..=u64::MAX), + min_amount1_out: self.trident.random_from_range(0..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_remove_liquidity(&mut self) -> RemoveLiquidityInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // user (reuse existing user or create new one) + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + // user token0 account + let user_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + // user token1 account + let user_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + // user lp token account + let user_lp_token_account = + self.trident + .get_associated_token_address(&pair_account.lp_mint, &user, &TOKEN_PROGRAM); + + RemoveLiquidityInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token0_vault, + token1_vault, + user_token0_account, + user_token1_account, + pair_account.token0, + pair_account.token1, + pair_account.lp_mint, + user_lp_token_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + #[allow(clippy::too_many_arguments)] + fn verify_remove_liquidity_invariants( + &mut self, + args: &RemoveLiquidityArgs, + accounts: &RemoveLiquidityInstructionAccounts, + initial_pair: &Pair, + initial_user_lp: u64, + initial_user_token0: u64, + initial_user_token1: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after remove liquidity"); + + // Get actual user balances to determine what they received + let final_user_token0 = self + .trident + .get_token_account(accounts.user_token0_account) + .expect("User token0 account should exist") + .account + .amount; + let final_user_token1 = self + .trident + .get_token_account(accounts.user_token1_account) + .expect("User token1 account should exist") + .account + .amount; + + // Calculate actual amounts received by user + let actual_amount0_received = final_user_token0.checked_sub(initial_user_token0).unwrap(); + let actual_amount1_received = final_user_token1.checked_sub(initial_user_token1).unwrap(); + + // Check amounts received meet minimum requirements (slippage protection) + assert!( + actual_amount0_received >= args.min_amount0_out, + "User should receive at least min_amount0_out" + ); + assert!( + actual_amount1_received >= args.min_amount1_out, + "User should receive at least min_amount1_out" + ); + + // NOTE: We cannot directly check reserve deltas because pair.update() accrues interest + // during the transaction, which adds to reserves. This makes the net reserve decrease + // smaller than what the user received. Instead, we verify: + // 1. User received what they expected (checked above) + // 2. LP tokens were burned correctly (checked below) + // 3. Vault solvency is maintained (checked below) + + // Check pair total_supply decreased by liquidity burned + let expected_total_supply = initial_pair + .total_supply + .checked_sub(args.liquidity_in) + .unwrap(); + assert_eq!( + final_pair.total_supply, expected_total_supply, + "Pair total_supply should decrease by liquidity_in" + ); + + // Check user LP balance decreased by liquidity_in + let final_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let expected_user_lp = initial_user_lp.checked_sub(args.liquidity_in).unwrap(); + assert_eq!( + final_user_lp, expected_user_lp, + "User LP balance should decrease by liquidity_in" + ); + + // Critical accounting invariant: vault balances must be >= pair reserves + collateral + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + // Vaults must have enough for reserves + collateral combined + let required_vault0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Overflow calculating required vault0"); + let required_vault1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Overflow calculating required vault1"); + + assert!( + vault0_balance >= required_vault0, + "Token0 vault balance must be >= reserve0 + total_collateral0. Vault: {}, Required: {}", + vault0_balance, + required_vault0 + ); + assert!( + vault1_balance >= required_vault1, + "Token1 vault balance must be >= reserve1 + total_collateral1. Vault: {}, Required: {}", + vault1_balance, + required_vault1 + ); + } +} diff --git a/trident-tests/fuzz_lending/spot/mod.rs b/trident-tests/fuzz_lending/spot/mod.rs new file mode 100644 index 0000000..04e62b0 --- /dev/null +++ b/trident-tests/fuzz_lending/spot/mod.rs @@ -0,0 +1 @@ +mod swap; diff --git a/trident-tests/fuzz_lending/spot/swap.rs b/trident-tests/fuzz_lending/spot/swap.rs new file mode 100644 index 0000000..7d622e2 --- /dev/null +++ b/trident-tests/fuzz_lending/spot/swap.rs @@ -0,0 +1,412 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{self, SwapInstruction, SwapInstructionAccounts, SwapInstructionData}, + Pair, SwapArgs, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn swap(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_swap(); + let data = self.get_data_swap(accounts.pair, accounts.token_in_mint); + + // record histogram + self.trident + .record_histogram("SWAP_AMOUNT_IN", data.amount_in as f64); + self.trident + .record_histogram("SWAP_MIN_AMOUNT_OUT", data.min_amount_out as f64); + + // Store initial USER state only (pair state will be modified by update() during transaction) + let initial_user_token_in = self + .trident + .get_token_account(accounts.user_token_in_account) + .expect("User token in account should exist") + .account + .amount; + let initial_user_token_out = self + .trident + .get_token_account(accounts.user_token_out_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + let initial_authority_token_in = self + .trident + .get_token_account(accounts.authority_token_in_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = SwapInstruction::data(SwapInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Swap")); + + if res.is_success() { + self.verify_swap_invariants( + &data, + &accounts, + initial_user_token_in, + initial_user_token_out, + initial_authority_token_in, + ); + } + } + + fn get_data_swap(&mut self, pair_pubkey: Pubkey, token_in_mint: Pubkey) -> SwapArgs { + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .unwrap(); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + + // Determine which token we're swapping in and get the corresponding reserves + let (reserve_in, reserve_out) = if token_in_mint == pair.token0 { + (reserve0, reserve1) + } else { + (reserve1, reserve0) + }; + + // Early return if reserves are too low to avoid empty range errors + if reserve_in == 0 || reserve_out == 0 { + return SwapArgs { + amount_in: 100, + min_amount_out: 0, + }; + } + + // Strategy: Test different swap scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Small to medium swaps (0.1% to 10% of reserve) + 0..=39 => { + let percentage = self.trident.random_from_range(1..=1000); // 0.1% to 100% + let amount_in = reserve_in.saturating_mul(percentage) / 10000; // 0.01% to 10% + let amount_in = amount_in.max(100); // Minimum 100 tokens + + // Calculate expected output using constant product formula + // amount_out = (amount_in * reserve_out) / (reserve_in + amount_in) + // With fees: amount_in_after_fee = amount_in * (10000 - fee_bps) / 10000 + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Set min_amount_out with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let min_amount_out = expected_out.saturating_mul(10000 - slippage_bps) / 10000; + + SwapArgs { + amount_in, + min_amount_out, + } + } + + // 30% - Larger swaps (10% to 50% of reserve) + 40..=69 => { + let percentage = self.trident.random_from_range(1000..=5000); // 10% to 50% + let amount_in = reserve_in.saturating_mul(percentage) / 10000; + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Larger slippage tolerance for bigger swaps + let slippage_bps = self.trident.random_from_range(100..=1000); + let min_amount_out = expected_out.saturating_mul(10000 - slippage_bps) / 10000; + + SwapArgs { + amount_in, + min_amount_out, + } + } + + // 15% - Dust amounts (very small swaps) + 70..=84 => { + let amount_in = self.trident.random_from_range(1..=1000); + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + SwapArgs { + amount_in, + min_amount_out: expected_out.saturating_sub(1), // Allow for rounding + } + } + + // 10% - Unrealistic expectations (should fail) + 85..=94 => { + let amount_in = self.trident.random_from_range(100..=100_000_000); + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Set unrealistic min_amount_out (should fail with InsufficientOutput) + let multiplier = self.trident.random_from_range(2..=10); + SwapArgs { + amount_in, + min_amount_out: expected_out.saturating_mul(multiplier), + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero amount (should fail with AmountZero) + 0 => SwapArgs { + amount_in: 0, + min_amount_out: 0, + }, + // Unrealistic min_amount_out + 1 => SwapArgs { + amount_in: self.trident.random_from_range(1..=1000), + min_amount_out: u64::MAX, + }, + // Swap entire reserve (should fail or cause extreme slippage) + 2 => SwapArgs { + amount_in: reserve_in, + min_amount_out: 0, + }, + // Overflow scenarios + 3 => SwapArgs { + amount_in: u64::MAX, + min_amount_out: 0, + }, + // Random chaos + _ => SwapArgs { + amount_in: self.trident.random_from_range(1..=u64::MAX), + min_amount_out: self.trident.random_from_range(0..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_swap(&mut self) -> SwapInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .unwrap(); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Randomly choose swap direction: token0 -> token1 or token1 -> token0 + let is_token0_in = self.trident.random_from_range(0..=1) == 0; + + let (token_in_mint, token_out_mint) = if is_token0_in { + (pair_account.token0, pair_account.token1) + } else { + (pair_account.token1, pair_account.token0) + }; + + // token vaults + let token_in_vault = + self.trident + .get_associated_token_address(&token_in_mint, &pair_pubkey, &TOKEN_PROGRAM); + + let token_out_vault = self.trident.get_associated_token_address( + &token_out_mint, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // user + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + // user token account + let user_token_in_account = + self.trident + .get_associated_token_address(&token_in_mint, &user, &TOKEN_PROGRAM); + + let user_token_out_account = + self.trident + .get_associated_token_address(&token_out_mint, &user, &TOKEN_PROGRAM); + + // authority token account (for fees) + let authority_token_in_account = self.trident.get_associated_token_address( + &token_in_mint, + &futarchy_authority_pubkey, + &TOKEN_PROGRAM, + ); + + if self + .trident + .get_token_account(authority_token_in_account) + .is_err() + { + self.trident.initialize_associated_token_account( + &user, + &token_in_mint, + &futarchy_authority_pubkey, + ); + } + + SwapInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token_in_vault, + token_out_vault, + user_token_in_account, + user_token_out_account, + token_in_mint, + token_out_mint, + authority_token_in_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_swap_invariants( + &mut self, + args: &SwapArgs, + accounts: &SwapInstructionAccounts, + initial_user_token_in: u64, + initial_user_token_out: u64, + initial_authority_token_in: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after swap"); + + // Get final user balances + let final_user_token_in = self + .trident + .get_token_account(accounts.user_token_in_account) + .expect("User token in account should exist") + .account + .amount; + let final_user_token_out = self + .trident + .get_token_account(accounts.user_token_out_account) + .expect("User token out account should exist") + .account + .amount; + + // Calculate actual amounts + let actual_amount_in = initial_user_token_in + .checked_sub(final_user_token_in) + .unwrap(); + let actual_amount_out = final_user_token_out + .checked_sub(initial_user_token_out) + .unwrap(); + + // Verify user paid exactly amount_in + assert_eq!( + actual_amount_in, args.amount_in, + "User should pay exactly amount_in" + ); + + // Verify user received at least min_amount_out (slippage protection) + assert!( + actual_amount_out >= args.min_amount_out, + "User should receive at least min_amount_out" + ); + + // Verify constant product invariant: k should not decrease + // Note: k may increase slightly due to fees staying in the pool + let final_k = (final_pair.reserve0 as u128) + .checked_mul(final_pair.reserve1 as u128) + .unwrap(); + + // We can't check k against initial because update() modifies reserves with interest + // But we can verify k is reasonable and reserves are positive + assert!(final_k > 0, "Constant product k must be positive"); + assert!( + final_pair.reserve0 > 0, + "Reserve0 must be positive after swap" + ); + assert!( + final_pair.reserve1 > 0, + "Reserve1 must be positive after swap" + ); + + // Verify authority received futarchy fee (if any) + let final_authority_token_in = self + .trident + .get_token_account(accounts.authority_token_in_account) + .expect("Authority token in account should exist") + .account + .amount; + + let authority_fee_received = final_authority_token_in + .checked_sub(initial_authority_token_in) + .unwrap(); + + // Authority should receive some fee (unless swap is too small or fees are 0) + // We verify it's reasonable: futarchy fee = total_fee * revenue_share_bps / 10000 + // where total_fee = amount_in * swap_fee_bps / 10000 + if args.amount_in > 1000 && final_pair.swap_fee_bps > 0 { + assert!( + authority_fee_received > 0, + "Authority should receive futarchy fee for non-trivial swaps" + ); + } + + // Critical accounting invariant: vault balances should be >= pair reserves + let token_in_vault_balance = self + .trident + .get_token_account(accounts.token_in_vault) + .expect("Token in vault should exist") + .account + .amount; + let token_out_vault_balance = self + .trident + .get_token_account(accounts.token_out_vault) + .expect("Token out vault should exist") + .account + .amount; + + // Determine which reserve corresponds to which vault + let is_token0_in = accounts.token_in_mint == final_pair.token0; + let (reserve_in, reserve_out) = if is_token0_in { + (final_pair.reserve0, final_pair.reserve1) + } else { + (final_pair.reserve1, final_pair.reserve0) + }; + + assert!( + token_in_vault_balance >= reserve_in, + "Token in vault balance must be >= corresponding reserve" + ); + assert!( + token_out_vault_balance >= reserve_out, + "Token out vault balance must be >= corresponding reserve" + ); + } +} diff --git a/trident-tests/fuzz_lending/test_fuzz.rs b/trident-tests/fuzz_lending/test_fuzz.rs new file mode 100644 index 0000000..f75d409 --- /dev/null +++ b/trident-tests/fuzz_lending/test_fuzz.rs @@ -0,0 +1,188 @@ +use fuzz_accounts::*; +use trident_fuzz::fuzzing::*; + +use crate::{ + types::omnipair, + utils::{DEPLOYER_ADDRESS, FUTARCHY_AUTHORITY_SEED_PREFIX, TOKEN_PROGRAM, WSOL_MINT_ADDRESS}, +}; +mod futarchy; +mod fuzz_accounts; +mod lending; +mod liquidity; +mod spot; +mod types; +mod utils; +mod view; + +const USER_COUNT: usize = 20; +const TOKEN_MINT_COUNT: usize = 2; + +#[derive(FuzzTestMethods)] +struct FuzzTest { + /// Trident client for interacting with the Solana program + trident: Trident, + /// Storage for all account addresses used in fuzz testing + fuzz_accounts: AccountAddresses, +} + +#[flow_executor] +impl FuzzTest { + fn new() -> Self { + Self { + trident: Trident::default(), + fuzz_accounts: AccountAddresses::default(), + } + } + + #[init] + fn start(&mut self) { + self.setup_accounts(); + + self.init_futarchy(); + self.init_pair(); + self.add_liquidity(); + } + + // Main lending flow: add collateral -> borrow + #[flow(weight = 30)] + fn lending_flow_borrow(&mut self) { + self.add_collateral(); + self.trident.forward_in_time(60 * 60); + self.borrow(); + } + + // Lending flow: add collateral -> borrow -> repay + #[flow(weight = 25)] + fn lending_flow_repay(&mut self) { + self.add_collateral(); + self.trident.forward_in_time(60 * 60); + self.borrow(); + self.trident.forward_in_time(60 * 60); + self.repay(); + } + + // Lending flow: add collateral -> remove collateral + #[flow(weight = 20)] + fn lending_flow_collateral(&mut self) { + self.add_collateral(); + self.trident.forward_in_time(60 * 60); + self.remove_collateral(); + } + + // Lending flow: flashloan + #[flow(weight = 15)] + fn lending_flow_flashloan(&mut self) { + self.flashloan(); + } + + // Lending flow: add collateral -> borrow -> liquidate + #[flow(weight = 10)] + fn lending_flow_liquidate(&mut self) { + self.add_collateral(); + self.trident.forward_in_time(60 * 60); + self.borrow(); + self.trident.forward_in_time(60 * 60 * 24 * 30); // Forward time significantly + self.liquidate(); + } + + #[end] + fn end(&mut self) { + self.trident.forward_in_time(60 * 60 * 24); + self.claim_protocol_fees(); + } + + fn setup_accounts(&mut self) { + // Airdrop DEPLOYER_ADDRESS for transaction fees + self.trident.airdrop( + &DEPLOYER_ADDRESS, + LAMPORTS_PER_SOL + .checked_mul(5) + .expect("Airdrop amount overflow"), + ); + + let mut users = Vec::new(); + + // Pre-create user accounts + for _ in 0..USER_COUNT { + let user = self.fuzz_accounts.user.insert(&mut self.trident, None); + users.push(user); + self.trident.airdrop( + &user, + LAMPORTS_PER_SOL + .checked_mul(1000) + .expect("Airdrop amount overflow"), + ); + } + + // Pre-create futarchy authority account + let futarchy_authority = self.fuzz_accounts.futarchy_authority.insert( + &mut self.trident, + Some(PdaSeeds { + seeds: &[FUTARCHY_AUTHORITY_SEED_PREFIX], + program_id: omnipair::program_id(), + }), + ); + + // Pre-create authority wsol account + let authority_wsol_account = self.trident.get_associated_token_address( + &WSOL_MINT_ADDRESS, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + let ix = self.trident.initialize_associated_token_account( + &DEPLOYER_ADDRESS, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + self.trident.process_transaction(&[ix], None); + + self.fuzz_accounts + .authority_wsol_account + .insert_with_address(authority_wsol_account); + + // Pre initialize some token mints + for _ in 0..TOKEN_MINT_COUNT { + let token_mint = self + .fuzz_accounts + .token_mint + .insert(&mut self.trident, None); + + let mint_authority = self.fuzz_accounts.user.get(&mut self.trident).expect("Mint authority should exist"); + let ix = self.trident.initialize_mint( + &mint_authority, + &token_mint, + 9, + &mint_authority, + None, + ); + let res = self.trident.process_transaction(&ix, None); + assert!(res.is_success()); + + // Generate token account for users + for user in users.iter() { + // User token account + let initialize_user_token_account_ix = self + .trident + .initialize_associated_token_account(user, &token_mint, user); + let user_token_account = + self.trident + .get_associated_token_address(&token_mint, user, &TOKEN_PROGRAM); + let mint_to_user_ix = self.trident.mint_to( + &user_token_account, + &token_mint, + &mint_authority, + 100_000_000_000_000_000, + ); + let res = self.trident.process_transaction( + &[initialize_user_token_account_ix, mint_to_user_ix], + None, + ); + assert!(res.is_success()); + } + } + } +} + +fn main() { + FuzzTest::fuzz(1000, 100); +} diff --git a/trident-tests/fuzz_lending/types.rs b/trident-tests/fuzz_lending/types.rs new file mode 100644 index 0000000..a6e1c16 --- /dev/null +++ b/trident-tests/fuzz_lending/types.rs @@ -0,0 +1,4739 @@ +//! # Trident Generated Types +//! +//! This file is automatically generated by Trident. +//! **DO NOT EDIT THIS FILE MANUALLY** + +#![allow(dead_code)] +#![allow(unused_imports)] + +use borsh::BorshDeserialize; +use borsh::BorshSerialize; +use trident_fuzz::fuzzing::*; + +// ============================================================================ +// PROGRAM MODULES +// ============================================================================ + +// ---------------------------------------------------------------------------- +// Program: faucet +// ---------------------------------------------------------------------------- +pub mod faucet { + use super::*; + + // ------------------------------------------------------------------------ + // Program ID + // ------------------------------------------------------------------------ + + /// Returns the program ID for faucet + pub fn program_id() -> Pubkey { + pubkey!("3Ckfn1LMByoDfVpcDPf7nouk5nQAUm51Zkdf1oprQTAK") + } + + // ------------------------------------------------------------------------ + // Instructions + // ------------------------------------------------------------------------ + + // .................................................................... + // Instruction: FaucetMint + // .................................................................... + + /// Main instruction struct for FaucetMint + pub struct FaucetMintInstruction { + pub accounts: FaucetMintInstructionAccountMetas, + pub data: FaucetMintInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for FaucetMint instruction + #[derive(Debug, Clone, Default)] + pub struct FaucetMintInstructionAccountMetas { + pub user: AccountMeta, + + pub faucet_authority: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub system_program: AccountMeta, + + pub token_program: AccountMeta, + + pub associated_token_program: AccountMeta, + } + + /// Account pubkeys for FaucetMint instruction + #[derive(Debug, Clone)] + pub struct FaucetMintInstructionAccounts { + pub user: Pubkey, + + pub faucet_authority: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + } + + impl FaucetMintInstructionAccounts { + pub fn new( + user: Pubkey, + + faucet_authority: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + ) -> Self { + Self { + user, + + faucet_authority, + + user_token0_account, + + user_token1_account, + + token0_mint, + + token1_mint, + } + } + } + + /// Instruction data for FaucetMint + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct FaucetMintInstructionData {} + + impl FaucetMintInstructionData { + pub fn new() -> Self { + Self {} + } + } + + /// Implementation for FaucetMintInstruction + impl FaucetMintInstruction { + fn discriminator() -> [u8; 8] { + [47u8, 229u8, 221u8, 88u8, 0u8, 56u8, 156u8, 38u8] + } + + pub fn data(data: FaucetMintInstructionData) -> Self { + Self { + accounts: FaucetMintInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: FaucetMintInstructionAccounts) -> Self { + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.faucet_authority = + AccountMeta::new_readonly(accounts.faucet_authority, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_mint = AccountMeta::new(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new(accounts.token1_mint, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.faucet_authority.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // ------------------------------------------------------------------------ + // Composite Accounts + // ------------------------------------------------------------------------ +} + +// ---------------------------------------------------------------------------- +// Program: omnipair +// ---------------------------------------------------------------------------- +pub mod omnipair { + use super::*; + + // ------------------------------------------------------------------------ + // Program ID + // ------------------------------------------------------------------------ + + /// Returns the program ID for omnipair + pub fn program_id() -> Pubkey { + pubkey!("Bd9Uhf5S8yzfop8cG9oqRs6jVcLtu8B4cb2gvRmtbNzk") + } + + // ------------------------------------------------------------------------ + // Instructions + // ------------------------------------------------------------------------ + + // .................................................................... + // Instruction: AddCollateral + // .................................................................... + + /// Main instruction struct for AddCollateral + pub struct AddCollateralInstruction { + pub accounts: AddCollateralInstructionAccountMetas, + pub data: AddCollateralInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for AddCollateral instruction + #[derive(Debug, Clone, Default)] + pub struct AddCollateralInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub user_position: AccountMeta, + + pub collateral_vault: AccountMeta, + + pub user_collateral_token_account: AccountMeta, + + pub collateral_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for AddCollateral instruction + #[derive(Debug, Clone)] + pub struct AddCollateralInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub user_position: Pubkey, + + pub collateral_vault: Pubkey, + + pub user_collateral_token_account: Pubkey, + + pub collateral_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl AddCollateralInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + user_position: Pubkey, + + collateral_vault: Pubkey, + + user_collateral_token_account: Pubkey, + + collateral_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + user_position, + + collateral_vault, + + user_collateral_token_account, + + collateral_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for AddCollateral + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct AddCollateralInstructionData { + pub args: AdjustPositionArgs, + } + + impl AddCollateralInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for AddCollateralInstruction + impl AddCollateralInstruction { + fn discriminator() -> [u8; 8] { + [127u8, 82u8, 121u8, 42u8, 161u8, 176u8, 249u8, 206u8] + } + + pub fn data(data: AddCollateralInstructionData) -> Self { + Self { + accounts: AddCollateralInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: AddCollateralInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.collateral_vault = AccountMeta::new(accounts.collateral_vault, false); + + self.accounts.user_collateral_token_account = + AccountMeta::new(accounts.user_collateral_token_account, false); + + self.accounts.collateral_token_mint = + AccountMeta::new_readonly(accounts.collateral_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.collateral_vault.clone()); + + metas.push(self.accounts.user_collateral_token_account.clone()); + + metas.push(self.accounts.collateral_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: AddLiquidity + // .................................................................... + + /// Main instruction struct for AddLiquidity + pub struct AddLiquidityInstruction { + pub accounts: AddLiquidityInstructionAccountMetas, + pub data: AddLiquidityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for AddLiquidity instruction + #[derive(Debug, Clone, Default)] + pub struct AddLiquidityInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_vault_mint: AccountMeta, + + pub token1_vault_mint: AccountMeta, + + pub lp_mint: AccountMeta, + + pub user_lp_token_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for AddLiquidity instruction + #[derive(Debug, Clone)] + pub struct AddLiquidityInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_vault_mint: Pubkey, + + pub token1_vault_mint: Pubkey, + + pub lp_mint: Pubkey, + + pub user_lp_token_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl AddLiquidityInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_vault_mint: Pubkey, + + token1_vault_mint: Pubkey, + + lp_mint: Pubkey, + + user_lp_token_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + user_token0_account, + + user_token1_account, + + token0_vault_mint, + + token1_vault_mint, + + lp_mint, + + user_lp_token_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for AddLiquidity + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct AddLiquidityInstructionData { + pub args: AddLiquidityArgs, + } + + impl AddLiquidityInstructionData { + pub fn new(args: AddLiquidityArgs) -> Self { + Self { args } + } + } + + /// Implementation for AddLiquidityInstruction + impl AddLiquidityInstruction { + fn discriminator() -> [u8; 8] { + [181u8, 157u8, 89u8, 67u8, 143u8, 182u8, 52u8, 72u8] + } + + pub fn data(data: AddLiquidityInstructionData) -> Self { + Self { + accounts: AddLiquidityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: AddLiquidityInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_vault_mint = + AccountMeta::new_readonly(accounts.token0_vault_mint, false); + + self.accounts.token1_vault_mint = + AccountMeta::new_readonly(accounts.token1_vault_mint, false); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.user_lp_token_account = + AccountMeta::new(accounts.user_lp_token_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_vault_mint.clone()); + + metas.push(self.accounts.token1_vault_mint.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.user_lp_token_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Borrow + // .................................................................... + + /// Main instruction struct for Borrow + pub struct BorrowInstruction { + pub accounts: BorrowInstructionAccountMetas, + pub data: BorrowInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Borrow instruction + #[derive(Debug, Clone, Default)] + pub struct BorrowInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Borrow instruction + #[derive(Debug, Clone)] + pub struct BorrowInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl BorrowInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Borrow + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct BorrowInstructionData { + pub args: AdjustPositionArgs, + } + + impl BorrowInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for BorrowInstruction + impl BorrowInstruction { + fn discriminator() -> [u8; 8] { + [228u8, 253u8, 131u8, 202u8, 207u8, 116u8, 89u8, 18u8] + } + + pub fn data(data: BorrowInstructionData) -> Self { + Self { + accounts: BorrowInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: BorrowInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ClaimProtocolFees + // .................................................................... + + /// Main instruction struct for ClaimProtocolFees + pub struct ClaimProtocolFeesInstruction { + pub accounts: ClaimProtocolFeesInstructionAccountMetas, + pub data: ClaimProtocolFeesInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ClaimProtocolFees instruction + #[derive(Debug, Clone, Default)] + pub struct ClaimProtocolFeesInstructionAccountMetas { + pub caller: AccountMeta, + + pub pair: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub authority_token0_account: AccountMeta, + + pub authority_token1_account: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + } + + /// Account pubkeys for ClaimProtocolFees instruction + #[derive(Debug, Clone)] + pub struct ClaimProtocolFeesInstructionAccounts { + pub caller: Pubkey, + + pub pair: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub authority_token0_account: Pubkey, + + pub authority_token1_account: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + } + + impl ClaimProtocolFeesInstructionAccounts { + pub fn new( + caller: Pubkey, + + pair: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + authority_token0_account: Pubkey, + + authority_token1_account: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + ) -> Self { + Self { + caller, + + pair, + + futarchy_authority, + + token0_vault, + + token1_vault, + + authority_token0_account, + + authority_token1_account, + + token0_mint, + + token1_mint, + } + } + } + + /// Instruction data for ClaimProtocolFees + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ClaimProtocolFeesInstructionData { + pub args: ClaimProtocolFeesArgs, + } + + impl ClaimProtocolFeesInstructionData { + pub fn new(args: ClaimProtocolFeesArgs) -> Self { + Self { args } + } + } + + /// Implementation for ClaimProtocolFeesInstruction + impl ClaimProtocolFeesInstruction { + fn discriminator() -> [u8; 8] { + [34u8, 142u8, 219u8, 112u8, 109u8, 54u8, 133u8, 23u8] + } + + pub fn data(data: ClaimProtocolFeesInstructionData) -> Self { + Self { + accounts: ClaimProtocolFeesInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ClaimProtocolFeesInstructionAccounts) -> Self { + self.accounts.caller = AccountMeta::new(accounts.caller, true); + + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.authority_token0_account = + AccountMeta::new(accounts.authority_token0_account, false); + + self.accounts.authority_token1_account = + AccountMeta::new(accounts.authority_token1_account, false); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.caller.clone()); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.authority_token0_account.clone()); + + metas.push(self.accounts.authority_token1_account.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: DistributeTokens + // .................................................................... + + /// Main instruction struct for DistributeTokens + pub struct DistributeTokensInstruction { + pub accounts: DistributeTokensInstructionAccountMetas, + pub data: DistributeTokensInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for DistributeTokens instruction + #[derive(Debug, Clone, Default)] + pub struct DistributeTokensInstructionAccountMetas { + pub futarchy_authority: AccountMeta, + + pub source_mint: AccountMeta, + + pub source_token_account: AccountMeta, + + pub futarchy_treasury_token_account: AccountMeta, + + pub buybacks_vault_token_account: AccountMeta, + + pub team_treasury_token_account: AccountMeta, + + pub token_program: AccountMeta, + } + + /// Account pubkeys for DistributeTokens instruction + #[derive(Debug, Clone)] + pub struct DistributeTokensInstructionAccounts { + pub futarchy_authority: Pubkey, + + pub source_mint: Pubkey, + + pub source_token_account: Pubkey, + + pub futarchy_treasury_token_account: Pubkey, + + pub buybacks_vault_token_account: Pubkey, + + pub team_treasury_token_account: Pubkey, + } + + impl DistributeTokensInstructionAccounts { + pub fn new( + futarchy_authority: Pubkey, + + source_mint: Pubkey, + + source_token_account: Pubkey, + + futarchy_treasury_token_account: Pubkey, + + buybacks_vault_token_account: Pubkey, + + team_treasury_token_account: Pubkey, + ) -> Self { + Self { + futarchy_authority, + + source_mint, + + source_token_account, + + futarchy_treasury_token_account, + + buybacks_vault_token_account, + + team_treasury_token_account, + } + } + } + + /// Instruction data for DistributeTokens + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct DistributeTokensInstructionData { + pub args: DistributeTokensArgs, + } + + impl DistributeTokensInstructionData { + pub fn new(args: DistributeTokensArgs) -> Self { + Self { args } + } + } + + /// Implementation for DistributeTokensInstruction + impl DistributeTokensInstruction { + fn discriminator() -> [u8; 8] { + [105u8, 69u8, 130u8, 52u8, 196u8, 28u8, 176u8, 120u8] + } + + pub fn data(data: DistributeTokensInstructionData) -> Self { + Self { + accounts: DistributeTokensInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: DistributeTokensInstructionAccounts) -> Self { + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.source_mint = AccountMeta::new_readonly(accounts.source_mint, false); + + self.accounts.source_token_account = + AccountMeta::new(accounts.source_token_account, false); + + self.accounts.futarchy_treasury_token_account = + AccountMeta::new(accounts.futarchy_treasury_token_account, false); + + self.accounts.buybacks_vault_token_account = + AccountMeta::new(accounts.buybacks_vault_token_account, false); + + self.accounts.team_treasury_token_account = + AccountMeta::new(accounts.team_treasury_token_account, false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.source_mint.clone()); + + metas.push(self.accounts.source_token_account.clone()); + + metas.push(self.accounts.futarchy_treasury_token_account.clone()); + + metas.push(self.accounts.buybacks_vault_token_account.clone()); + + metas.push(self.accounts.team_treasury_token_account.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Flashloan + // .................................................................... + + /// Main instruction struct for Flashloan + pub struct FlashloanInstruction { + pub accounts: FlashloanInstructionAccountMetas, + pub data: FlashloanInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Flashloan instruction + #[derive(Debug, Clone, Default)] + pub struct FlashloanInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub receiver_token0_account: AccountMeta, + + pub receiver_token1_account: AccountMeta, + + pub receiver_program: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Flashloan instruction + #[derive(Debug, Clone)] + pub struct FlashloanInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + + pub receiver_token0_account: Pubkey, + + pub receiver_token1_account: Pubkey, + + pub receiver_program: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl FlashloanInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + + receiver_token0_account: Pubkey, + + receiver_token1_account: Pubkey, + + receiver_program: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + token0_mint, + + token1_mint, + + receiver_token0_account, + + receiver_token1_account, + + receiver_program, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Flashloan + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct FlashloanInstructionData { + pub args: FlashloanArgs, + } + + impl FlashloanInstructionData { + pub fn new(args: FlashloanArgs) -> Self { + Self { args } + } + } + + /// Implementation for FlashloanInstruction + impl FlashloanInstruction { + fn discriminator() -> [u8; 8] { + [105u8, 33u8, 1u8, 3u8, 42u8, 158u8, 246u8, 67u8] + } + + pub fn data(data: FlashloanInstructionData) -> Self { + Self { + accounts: FlashloanInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: FlashloanInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.receiver_token0_account = + AccountMeta::new(accounts.receiver_token0_account, false); + + self.accounts.receiver_token1_account = + AccountMeta::new(accounts.receiver_token1_account, false); + + self.accounts.receiver_program = + AccountMeta::new_readonly(accounts.receiver_program, false); + + self.accounts.user = AccountMeta::new_readonly(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.receiver_token0_account.clone()); + + metas.push(self.accounts.receiver_token1_account.clone()); + + metas.push(self.accounts.receiver_program.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: InitFutarchyAuthority + // .................................................................... + + /// Main instruction struct for InitFutarchyAuthority + pub struct InitFutarchyAuthorityInstruction { + pub accounts: InitFutarchyAuthorityInstructionAccountMetas, + pub data: InitFutarchyAuthorityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for InitFutarchyAuthority instruction + #[derive(Debug, Clone, Default)] + pub struct InitFutarchyAuthorityInstructionAccountMetas { + pub deployer: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub system_program: AccountMeta, + } + + /// Account pubkeys for InitFutarchyAuthority instruction + #[derive(Debug, Clone)] + pub struct InitFutarchyAuthorityInstructionAccounts { + pub futarchy_authority: Pubkey, + } + + impl InitFutarchyAuthorityInstructionAccounts { + pub fn new(futarchy_authority: Pubkey) -> Self { + Self { futarchy_authority } + } + } + + /// Instruction data for InitFutarchyAuthority + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct InitFutarchyAuthorityInstructionData { + pub args: InitFutarchyAuthorityArgs, + } + + impl InitFutarchyAuthorityInstructionData { + pub fn new(args: InitFutarchyAuthorityArgs) -> Self { + Self { args } + } + } + + /// Implementation for InitFutarchyAuthorityInstruction + impl InitFutarchyAuthorityInstruction { + fn discriminator() -> [u8; 8] { + [133u8, 110u8, 154u8, 29u8, 240u8, 206u8, 71u8, 100u8] + } + + pub fn data(data: InitFutarchyAuthorityInstructionData) -> Self { + Self { + accounts: InitFutarchyAuthorityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: InitFutarchyAuthorityInstructionAccounts) -> Self { + self.accounts.deployer = AccountMeta::new( + pubkey!("C7GKpfqQyBoFR6S13DECwBjdi7aCQKbbeKjXm4Jt5Hds"), + true, + ); + + self.accounts.futarchy_authority = AccountMeta::new(accounts.futarchy_authority, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.deployer.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Initialize + // .................................................................... + + /// Main instruction struct for Initialize + pub struct InitializeInstruction { + pub accounts: InitializeInstructionAccountMetas, + pub data: InitializeInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Initialize instruction + #[derive(Debug, Clone, Default)] + pub struct InitializeInstructionAccountMetas { + pub deployer: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub pair: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub rate_model: AccountMeta, + + pub lp_mint: AccountMeta, + + pub lp_token_metadata: AccountMeta, + + pub deployer_lp_token_account: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub deployer_token0_account: AccountMeta, + + pub deployer_token1_account: AccountMeta, + + pub authority_wsol_account: AccountMeta, + + pub system_program: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub token_metadata_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub rent: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Initialize instruction + #[derive(Debug, Clone)] + pub struct InitializeInstructionAccounts { + pub deployer: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + + pub pair: Pubkey, + + pub futarchy_authority: Pubkey, + + pub rate_model: Pubkey, + + pub lp_mint: Pubkey, + + pub lp_token_metadata: Pubkey, + + pub deployer_lp_token_account: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub deployer_token0_account: Pubkey, + + pub deployer_token1_account: Pubkey, + + pub authority_wsol_account: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl InitializeInstructionAccounts { + pub fn new( + deployer: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + + pair: Pubkey, + + futarchy_authority: Pubkey, + + rate_model: Pubkey, + + lp_mint: Pubkey, + + lp_token_metadata: Pubkey, + + deployer_lp_token_account: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + deployer_token0_account: Pubkey, + + deployer_token1_account: Pubkey, + + authority_wsol_account: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + deployer, + + token0_mint, + + token1_mint, + + pair, + + futarchy_authority, + + rate_model, + + lp_mint, + + lp_token_metadata, + + deployer_lp_token_account, + + token0_vault, + + token1_vault, + + deployer_token0_account, + + deployer_token1_account, + + authority_wsol_account, + + event_authority, + + program, + } + } + } + + /// Instruction data for Initialize + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct InitializeInstructionData { + pub args: InitializeAndBootstrapArgs, + } + + impl InitializeInstructionData { + pub fn new(args: InitializeAndBootstrapArgs) -> Self { + Self { args } + } + } + + /// Implementation for InitializeInstruction + impl InitializeInstruction { + fn discriminator() -> [u8; 8] { + [175u8, 175u8, 109u8, 31u8, 13u8, 152u8, 155u8, 237u8] + } + + pub fn data(data: InitializeInstructionData) -> Self { + Self { + accounts: InitializeInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: InitializeInstructionAccounts) -> Self { + self.accounts.deployer = AccountMeta::new(accounts.deployer, true); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, true); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.lp_token_metadata = AccountMeta::new(accounts.lp_token_metadata, false); + + self.accounts.deployer_lp_token_account = + AccountMeta::new(accounts.deployer_lp_token_account, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.deployer_token0_account = + AccountMeta::new(accounts.deployer_token0_account, false); + + self.accounts.deployer_token1_account = + AccountMeta::new(accounts.deployer_token1_account, false); + + self.accounts.authority_wsol_account = + AccountMeta::new(accounts.authority_wsol_account, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.token_metadata_program = AccountMeta::new_readonly( + pubkey!("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.rent = AccountMeta::new_readonly( + pubkey!("SysvarRent111111111111111111111111111111111"), + false, + ); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.deployer.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.lp_token_metadata.clone()); + + metas.push(self.accounts.deployer_lp_token_account.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.deployer_token0_account.clone()); + + metas.push(self.accounts.deployer_token1_account.clone()); + + metas.push(self.accounts.authority_wsol_account.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.token_metadata_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.rent.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Liquidate + // .................................................................... + + /// Main instruction struct for Liquidate + pub struct LiquidateInstruction { + pub accounts: LiquidateInstructionAccountMetas, + pub data: LiquidateInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Liquidate instruction + #[derive(Debug, Clone, Default)] + pub struct LiquidateInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub collateral_vault: AccountMeta, + + pub caller_token_account: AccountMeta, + + pub collateral_token_mint: AccountMeta, + + pub position_owner: AccountMeta, + + pub payer: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Liquidate instruction + #[derive(Debug, Clone)] + pub struct LiquidateInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub collateral_vault: Pubkey, + + pub caller_token_account: Pubkey, + + pub collateral_token_mint: Pubkey, + + pub position_owner: Pubkey, + + pub payer: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl LiquidateInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + collateral_vault: Pubkey, + + caller_token_account: Pubkey, + + collateral_token_mint: Pubkey, + + position_owner: Pubkey, + + payer: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + collateral_vault, + + caller_token_account, + + collateral_token_mint, + + position_owner, + + payer, + + event_authority, + + program, + } + } + } + + /// Instruction data for Liquidate + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct LiquidateInstructionData {} + + impl LiquidateInstructionData { + pub fn new() -> Self { + Self {} + } + } + + /// Implementation for LiquidateInstruction + impl LiquidateInstruction { + fn discriminator() -> [u8; 8] { + [223u8, 179u8, 226u8, 125u8, 48u8, 46u8, 39u8, 74u8] + } + + pub fn data(data: LiquidateInstructionData) -> Self { + Self { + accounts: LiquidateInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: LiquidateInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.collateral_vault = AccountMeta::new(accounts.collateral_vault, false); + + self.accounts.caller_token_account = + AccountMeta::new(accounts.caller_token_account, false); + + self.accounts.collateral_token_mint = + AccountMeta::new_readonly(accounts.collateral_token_mint, false); + + self.accounts.position_owner = + AccountMeta::new_readonly(accounts.position_owner, false); + + self.accounts.payer = AccountMeta::new(accounts.payer, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.collateral_vault.clone()); + + metas.push(self.accounts.caller_token_account.clone()); + + metas.push(self.accounts.collateral_token_mint.clone()); + + metas.push(self.accounts.position_owner.clone()); + + metas.push(self.accounts.payer.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: RemoveCollateral + // .................................................................... + + /// Main instruction struct for RemoveCollateral + pub struct RemoveCollateralInstruction { + pub accounts: RemoveCollateralInstructionAccountMetas, + pub data: RemoveCollateralInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for RemoveCollateral instruction + #[derive(Debug, Clone, Default)] + pub struct RemoveCollateralInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for RemoveCollateral instruction + #[derive(Debug, Clone)] + pub struct RemoveCollateralInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RemoveCollateralInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for RemoveCollateral + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RemoveCollateralInstructionData { + pub args: AdjustPositionArgs, + } + + impl RemoveCollateralInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for RemoveCollateralInstruction + impl RemoveCollateralInstruction { + fn discriminator() -> [u8; 8] { + [86u8, 222u8, 130u8, 86u8, 92u8, 20u8, 72u8, 65u8] + } + + pub fn data(data: RemoveCollateralInstructionData) -> Self { + Self { + accounts: RemoveCollateralInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RemoveCollateralInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: RemoveLiquidity + // .................................................................... + + /// Main instruction struct for RemoveLiquidity + pub struct RemoveLiquidityInstruction { + pub accounts: RemoveLiquidityInstructionAccountMetas, + pub data: RemoveLiquidityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for RemoveLiquidity instruction + #[derive(Debug, Clone, Default)] + pub struct RemoveLiquidityInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_vault_mint: AccountMeta, + + pub token1_vault_mint: AccountMeta, + + pub lp_mint: AccountMeta, + + pub user_lp_token_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for RemoveLiquidity instruction + #[derive(Debug, Clone)] + pub struct RemoveLiquidityInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_vault_mint: Pubkey, + + pub token1_vault_mint: Pubkey, + + pub lp_mint: Pubkey, + + pub user_lp_token_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RemoveLiquidityInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_vault_mint: Pubkey, + + token1_vault_mint: Pubkey, + + lp_mint: Pubkey, + + user_lp_token_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + user_token0_account, + + user_token1_account, + + token0_vault_mint, + + token1_vault_mint, + + lp_mint, + + user_lp_token_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for RemoveLiquidity + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RemoveLiquidityInstructionData { + pub args: RemoveLiquidityArgs, + } + + impl RemoveLiquidityInstructionData { + pub fn new(args: RemoveLiquidityArgs) -> Self { + Self { args } + } + } + + /// Implementation for RemoveLiquidityInstruction + impl RemoveLiquidityInstruction { + fn discriminator() -> [u8; 8] { + [80u8, 85u8, 209u8, 72u8, 24u8, 206u8, 177u8, 108u8] + } + + pub fn data(data: RemoveLiquidityInstructionData) -> Self { + Self { + accounts: RemoveLiquidityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RemoveLiquidityInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_vault_mint = + AccountMeta::new_readonly(accounts.token0_vault_mint, false); + + self.accounts.token1_vault_mint = + AccountMeta::new_readonly(accounts.token1_vault_mint, false); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.user_lp_token_account = + AccountMeta::new(accounts.user_lp_token_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_vault_mint.clone()); + + metas.push(self.accounts.token1_vault_mint.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.user_lp_token_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Repay + // .................................................................... + + /// Main instruction struct for Repay + pub struct RepayInstruction { + pub accounts: RepayInstructionAccountMetas, + pub data: RepayInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Repay instruction + #[derive(Debug, Clone, Default)] + pub struct RepayInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Repay instruction + #[derive(Debug, Clone)] + pub struct RepayInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RepayInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Repay + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RepayInstructionData { + pub args: AdjustPositionArgs, + } + + impl RepayInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for RepayInstruction + impl RepayInstruction { + fn discriminator() -> [u8; 8] { + [234u8, 103u8, 67u8, 82u8, 208u8, 234u8, 219u8, 166u8] + } + + pub fn data(data: RepayInstructionData) -> Self { + Self { + accounts: RepayInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RepayInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Swap + // .................................................................... + + /// Main instruction struct for Swap + pub struct SwapInstruction { + pub accounts: SwapInstructionAccountMetas, + pub data: SwapInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Swap instruction + #[derive(Debug, Clone, Default)] + pub struct SwapInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_in_vault: AccountMeta, + + pub token_out_vault: AccountMeta, + + pub user_token_in_account: AccountMeta, + + pub user_token_out_account: AccountMeta, + + pub token_in_mint: AccountMeta, + + pub token_out_mint: AccountMeta, + + pub authority_token_in_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Swap instruction + #[derive(Debug, Clone)] + pub struct SwapInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_in_vault: Pubkey, + + pub token_out_vault: Pubkey, + + pub user_token_in_account: Pubkey, + + pub user_token_out_account: Pubkey, + + pub token_in_mint: Pubkey, + + pub token_out_mint: Pubkey, + + pub authority_token_in_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl SwapInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_in_vault: Pubkey, + + token_out_vault: Pubkey, + + user_token_in_account: Pubkey, + + user_token_out_account: Pubkey, + + token_in_mint: Pubkey, + + token_out_mint: Pubkey, + + authority_token_in_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token_in_vault, + + token_out_vault, + + user_token_in_account, + + user_token_out_account, + + token_in_mint, + + token_out_mint, + + authority_token_in_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Swap + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct SwapInstructionData { + pub args: SwapArgs, + } + + impl SwapInstructionData { + pub fn new(args: SwapArgs) -> Self { + Self { args } + } + } + + /// Implementation for SwapInstruction + impl SwapInstruction { + fn discriminator() -> [u8; 8] { + [248u8, 198u8, 158u8, 145u8, 225u8, 117u8, 135u8, 200u8] + } + + pub fn data(data: SwapInstructionData) -> Self { + Self { + accounts: SwapInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: SwapInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_in_vault = AccountMeta::new(accounts.token_in_vault, false); + + self.accounts.token_out_vault = AccountMeta::new(accounts.token_out_vault, false); + + self.accounts.user_token_in_account = + AccountMeta::new(accounts.user_token_in_account, false); + + self.accounts.user_token_out_account = + AccountMeta::new(accounts.user_token_out_account, false); + + self.accounts.token_in_mint = AccountMeta::new_readonly(accounts.token_in_mint, false); + + self.accounts.token_out_mint = + AccountMeta::new_readonly(accounts.token_out_mint, false); + + self.accounts.authority_token_in_account = + AccountMeta::new(accounts.authority_token_in_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_in_vault.clone()); + + metas.push(self.accounts.token_out_vault.clone()); + + metas.push(self.accounts.user_token_in_account.clone()); + + metas.push(self.accounts.user_token_out_account.clone()); + + metas.push(self.accounts.token_in_mint.clone()); + + metas.push(self.accounts.token_out_mint.clone()); + + metas.push(self.accounts.authority_token_in_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ViewPairData + // .................................................................... + + /// Main instruction struct for ViewPairData + pub struct ViewPairDataInstruction { + pub accounts: ViewPairDataInstructionAccountMetas, + pub data: ViewPairDataInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ViewPairData instruction + #[derive(Debug, Clone, Default)] + pub struct ViewPairDataInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + } + + /// Account pubkeys for ViewPairData instruction + #[derive(Debug, Clone)] + pub struct ViewPairDataInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + } + + impl ViewPairDataInstructionAccounts { + pub fn new(pair: Pubkey, rate_model: Pubkey, futarchy_authority: Pubkey) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + } + } + } + + /// Instruction data for ViewPairData + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ViewPairDataInstructionData { + pub getter: PairViewKind, + + pub args: EmitValueArgs, + } + + impl ViewPairDataInstructionData { + pub fn new(getter: PairViewKind, args: EmitValueArgs) -> Self { + Self { getter, args } + } + } + + /// Implementation for ViewPairDataInstruction + impl ViewPairDataInstruction { + fn discriminator() -> [u8; 8] { + [30u8, 231u8, 169u8, 73u8, 19u8, 161u8, 44u8, 252u8] + } + + pub fn data(data: ViewPairDataInstructionData) -> Self { + Self { + accounts: ViewPairDataInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ViewPairDataInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new_readonly(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ViewUserPositionData + // .................................................................... + + /// Main instruction struct for ViewUserPositionData + pub struct ViewUserPositionDataInstruction { + pub accounts: ViewUserPositionDataInstructionAccountMetas, + pub data: ViewUserPositionDataInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ViewUserPositionData instruction + #[derive(Debug, Clone, Default)] + pub struct ViewUserPositionDataInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + } + + /// Account pubkeys for ViewUserPositionData instruction + #[derive(Debug, Clone)] + pub struct ViewUserPositionDataInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + } + + impl ViewUserPositionDataInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + } + } + } + + /// Instruction data for ViewUserPositionData + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ViewUserPositionDataInstructionData { + pub getter: UserPositionViewKind, + } + + impl ViewUserPositionDataInstructionData { + pub fn new(getter: UserPositionViewKind) -> Self { + Self { getter } + } + } + + /// Implementation for ViewUserPositionDataInstruction + impl ViewUserPositionDataInstruction { + fn discriminator() -> [u8; 8] { + [203u8, 218u8, 173u8, 213u8, 43u8, 31u8, 211u8, 152u8] + } + + pub fn data(data: ViewUserPositionDataInstructionData) -> Self { + Self { + accounts: ViewUserPositionDataInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ViewUserPositionDataInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new_readonly(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // ------------------------------------------------------------------------ + // Composite Accounts + // ------------------------------------------------------------------------ +} + +// ============================================================================ +// CUSTOM TYPES +// ============================================================================ + +/// Custom struct: AddLiquidityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AddLiquidityArgs { + pub amount0_in: u64, + + pub amount1_in: u64, + + pub min_liquidity_out: u64, +} + +impl AddLiquidityArgs { + pub fn new(amount0_in: u64, amount1_in: u64, min_liquidity_out: u64) -> Self { + Self { + amount0_in, + + amount1_in, + + min_liquidity_out, + } + } +} + +/// Custom struct: AdjustCollateralEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustCollateralEvent { + pub amount0: i64, + + pub amount1: i64, + + pub metadata: EventMetadata, +} + +impl AdjustCollateralEvent { + pub fn new(amount0: i64, amount1: i64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + metadata, + } + } +} + +/// Custom struct: AdjustDebtEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustDebtEvent { + pub amount0: i64, + + pub amount1: i64, + + pub metadata: EventMetadata, +} + +impl AdjustDebtEvent { + pub fn new(amount0: i64, amount1: i64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + metadata, + } + } +} + +/// Custom struct: AdjustLiquidityEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustLiquidityEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl AdjustLiquidityEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: AdjustPositionArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustPositionArgs { + pub amount: u64, +} + +impl AdjustPositionArgs { + pub fn new(amount: u64) -> Self { + Self { amount } + } +} + +/// Custom struct: BurnEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct BurnEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl BurnEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: ClaimProtocolFeesArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct ClaimProtocolFeesArgs { + pub amount0: u64, + + pub amount1: u64, +} + +impl ClaimProtocolFeesArgs { + pub fn new(amount0: u64, amount1: u64) -> Self { + Self { amount0, amount1 } + } +} + +/// Custom struct: DistributeTokensArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct DistributeTokensArgs {} + +impl DistributeTokensArgs { + pub fn new() -> Self { + Self {} + } +} + +/// Custom struct: EmitValueArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct EmitValueArgs { + pub debt_amount: Option, + + pub collateral_amount: Option, + + pub collateral_token: Option, +} + +impl EmitValueArgs { + pub fn new( + debt_amount: Option, + + collateral_amount: Option, + + collateral_token: Option, + ) -> Self { + Self { + debt_amount, + + collateral_amount, + + collateral_token, + } + } +} + +/// Custom struct: EventMetadata +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct EventMetadata { + pub signer: Pubkey, + + pub pair: Pubkey, + + pub timestamp: i64, +} + +impl EventMetadata { + pub fn new(signer: Pubkey, pair: Pubkey, timestamp: i64) -> Self { + Self { + signer, + + pair, + + timestamp, + } + } +} + +/// Custom struct: FlashloanArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FlashloanArgs { + pub amount0: u64, + + pub amount1: u64, + + pub data: Vec, +} + +impl FlashloanArgs { + pub fn new(amount0: u64, amount1: u64, data: Vec) -> Self { + Self { + amount0, + + amount1, + + data, + } + } +} + +/// Custom struct: FlashloanEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FlashloanEvent { + pub amount0: u64, + + pub amount1: u64, + + pub fee0: u64, + + pub fee1: u64, + + pub receiver: Pubkey, + + pub metadata: EventMetadata, +} + +impl FlashloanEvent { + pub fn new( + amount0: u64, + + amount1: u64, + + fee0: u64, + + fee1: u64, + + receiver: Pubkey, + + metadata: EventMetadata, + ) -> Self { + Self { + amount0, + + amount1, + + fee0, + + fee1, + + receiver, + + metadata, + } + } +} + +/// Custom struct: FutarchyAuthority +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FutarchyAuthority { + pub version: u8, + + pub authority: Pubkey, + + pub recipients: RevenueRecipients, + + pub revenue_share: RevenueShare, + + pub revenue_distribution: RevenueDistribution, + + pub bump: u8, +} + +impl FutarchyAuthority { + pub fn new( + version: u8, + + authority: Pubkey, + + recipients: RevenueRecipients, + + revenue_share: RevenueShare, + + revenue_distribution: RevenueDistribution, + + bump: u8, + ) -> Self { + Self { + version, + + authority, + + recipients, + + revenue_share, + + revenue_distribution, + + bump, + } + } +} + +/// Custom struct: InitFutarchyAuthorityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct InitFutarchyAuthorityArgs { + pub authority: Pubkey, + + pub swap_bps: u16, + + pub interest_bps: u16, + + pub futarchy_treasury: Pubkey, + + pub futarchy_treasury_bps: u16, + + pub buybacks_vault: Pubkey, + + pub buybacks_vault_bps: u16, + + pub team_treasury: Pubkey, + + pub team_treasury_bps: u16, +} + +impl InitFutarchyAuthorityArgs { + pub fn new( + authority: Pubkey, + + swap_bps: u16, + + interest_bps: u16, + + futarchy_treasury: Pubkey, + + futarchy_treasury_bps: u16, + + buybacks_vault: Pubkey, + + buybacks_vault_bps: u16, + + team_treasury: Pubkey, + + team_treasury_bps: u16, + ) -> Self { + Self { + authority, + + swap_bps, + + interest_bps, + + futarchy_treasury, + + futarchy_treasury_bps, + + buybacks_vault, + + buybacks_vault_bps, + + team_treasury, + + team_treasury_bps, + } + } +} + +/// Custom struct: InitializeAndBootstrapArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct InitializeAndBootstrapArgs { + pub swap_fee_bps: u16, + + pub half_life: u64, + + pub fixed_cf_bps: Option, + + pub pair_nonce: [u8; 16], + + pub amount0_in: u64, + + pub amount1_in: u64, + + pub min_liquidity_out: u64, + + pub lp_name: String, + + pub lp_symbol: String, + + pub lp_uri: String, +} + +impl InitializeAndBootstrapArgs { + pub fn new( + swap_fee_bps: u16, + + half_life: u64, + + fixed_cf_bps: Option, + + pair_nonce: [u8; 16], + + amount0_in: u64, + + amount1_in: u64, + + min_liquidity_out: u64, + + lp_name: String, + + lp_symbol: String, + + lp_uri: String, + ) -> Self { + Self { + swap_fee_bps, + + half_life, + + fixed_cf_bps, + + pair_nonce, + + amount0_in, + + amount1_in, + + min_liquidity_out, + + lp_name, + + lp_symbol, + + lp_uri, + } + } +} + +/// Custom struct: MintEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct MintEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl MintEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: Pair +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct Pair { + pub token0: Pubkey, + + pub token1: Pubkey, + + pub lp_mint: Pubkey, + + pub token0_decimals: u8, + + pub token1_decimals: u8, + + pub rate_model: Pubkey, + + pub swap_fee_bps: u16, + + pub half_life: u64, + + pub fixed_cf_bps: Option, + + pub reserve0: u64, + + pub reserve1: u64, + + pub protocol_revenue_reserve0: u64, + + pub protocol_revenue_reserve1: u64, + + pub last_price0_ema: u64, + + pub last_price1_ema: u64, + + pub last_update: i64, + + pub last_rate0: u64, + + pub last_rate1: u64, + + pub total_debt0: u64, + + pub total_debt1: u64, + + pub total_debt0_shares: u64, + + pub total_debt1_shares: u64, + + pub total_supply: u64, + + pub total_collateral0: u64, + + pub total_collateral1: u64, + + pub pair_nonce: [u8; 16], + + pub bump: u8, +} + +impl Pair { + pub fn new( + token0: Pubkey, + + token1: Pubkey, + + lp_mint: Pubkey, + + token0_decimals: u8, + + token1_decimals: u8, + + rate_model: Pubkey, + + swap_fee_bps: u16, + + half_life: u64, + + fixed_cf_bps: Option, + + reserve0: u64, + + reserve1: u64, + + protocol_revenue_reserve0: u64, + + protocol_revenue_reserve1: u64, + + last_price0_ema: u64, + + last_price1_ema: u64, + + last_update: i64, + + last_rate0: u64, + + last_rate1: u64, + + total_debt0: u64, + + total_debt1: u64, + + total_debt0_shares: u64, + + total_debt1_shares: u64, + + total_supply: u64, + + total_collateral0: u64, + + total_collateral1: u64, + + pair_nonce: [u8; 16], + + bump: u8, + ) -> Self { + Self { + token0, + + token1, + + lp_mint, + + token0_decimals, + + token1_decimals, + + rate_model, + + swap_fee_bps, + + half_life, + + fixed_cf_bps, + + reserve0, + + reserve1, + + protocol_revenue_reserve0, + + protocol_revenue_reserve1, + + last_price0_ema, + + last_price1_ema, + + last_update, + + last_rate0, + + last_rate1, + + total_debt0, + + total_debt1, + + total_debt0_shares, + + total_debt1_shares, + + total_supply, + + total_collateral0, + + total_collateral1, + + pair_nonce, + + bump, + } + } +} + +/// Custom struct: PairCreatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct PairCreatedEvent { + pub token0: Pubkey, + + pub token1: Pubkey, + + pub metadata: EventMetadata, +} + +impl PairCreatedEvent { + pub fn new(token0: Pubkey, token1: Pubkey, metadata: EventMetadata) -> Self { + Self { + token0, + + token1, + + metadata, + } + } +} + +/// Custom enum: PairViewKind +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone, PartialEq)] +pub enum PairViewKind { + EmaPrice0Nad, + + EmaPrice1Nad, + + SpotPrice0Nad, + + SpotPrice1Nad, + + K, + + GetRates, + + GetBorrowLimitAndCfBpsForCollateral, +} + +/// Custom struct: RateModel +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RateModel { + pub exp_rate: u64, + + pub target_util_start: u64, + + pub target_util_end: u64, +} + +impl RateModel { + pub fn new(exp_rate: u64, target_util_start: u64, target_util_end: u64) -> Self { + Self { + exp_rate, + + target_util_start, + + target_util_end, + } + } +} + +/// Custom struct: RemoveLiquidityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RemoveLiquidityArgs { + pub liquidity_in: u64, + + pub min_amount0_out: u64, + + pub min_amount1_out: u64, +} + +impl RemoveLiquidityArgs { + pub fn new(liquidity_in: u64, min_amount0_out: u64, min_amount1_out: u64) -> Self { + Self { + liquidity_in, + + min_amount0_out, + + min_amount1_out, + } + } +} + +/// Custom struct: RevenueDistribution +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueDistribution { + pub futarchy_treasury_bps: u16, + + pub buybacks_vault_bps: u16, + + pub team_treasury_bps: u16, +} + +impl RevenueDistribution { + pub fn new( + futarchy_treasury_bps: u16, + + buybacks_vault_bps: u16, + + team_treasury_bps: u16, + ) -> Self { + Self { + futarchy_treasury_bps, + + buybacks_vault_bps, + + team_treasury_bps, + } + } +} + +/// Custom struct: RevenueRecipients +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueRecipients { + pub futarchy_treasury: Pubkey, + + pub buybacks_vault: Pubkey, + + pub team_treasury: Pubkey, +} + +impl RevenueRecipients { + pub fn new(futarchy_treasury: Pubkey, buybacks_vault: Pubkey, team_treasury: Pubkey) -> Self { + Self { + futarchy_treasury, + + buybacks_vault, + + team_treasury, + } + } +} + +/// Custom struct: RevenueShare +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueShare { + pub swap_bps: u16, + + pub interest_bps: u16, +} + +impl RevenueShare { + pub fn new(swap_bps: u16, interest_bps: u16) -> Self { + Self { + swap_bps, + + interest_bps, + } + } +} + +/// Custom struct: SwapArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct SwapArgs { + pub amount_in: u64, + + pub min_amount_out: u64, +} + +impl SwapArgs { + pub fn new(amount_in: u64, min_amount_out: u64) -> Self { + Self { + amount_in, + + min_amount_out, + } + } +} + +/// Custom struct: SwapEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct SwapEvent { + pub reserve0: u64, + + pub reserve1: u64, + + pub is_token0_in: bool, + + pub amount_in: u64, + + pub amount_out: u64, + + pub amount_in_after_fee: u64, + + pub metadata: EventMetadata, +} + +impl SwapEvent { + pub fn new( + reserve0: u64, + + reserve1: u64, + + is_token0_in: bool, + + amount_in: u64, + + amount_out: u64, + + amount_in_after_fee: u64, + + metadata: EventMetadata, + ) -> Self { + Self { + reserve0, + + reserve1, + + is_token0_in, + + amount_in, + + amount_out, + + amount_in_after_fee, + + metadata, + } + } +} + +/// Custom struct: UpdatePairEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UpdatePairEvent { + pub price0_ema: u64, + + pub price1_ema: u64, + + pub rate0: u64, + + pub rate1: u64, + + pub accrued_interest0: u128, + + pub accrued_interest1: u128, + + pub protocol_revenue_reserve0: u64, + + pub protocol_revenue_reserve1: u64, + + pub reserve0_after_interest: u64, + + pub reserve1_after_interest: u64, + + pub metadata: EventMetadata, +} + +impl UpdatePairEvent { + pub fn new( + price0_ema: u64, + + price1_ema: u64, + + rate0: u64, + + rate1: u64, + + accrued_interest0: u128, + + accrued_interest1: u128, + + protocol_revenue_reserve0: u64, + + protocol_revenue_reserve1: u64, + + reserve0_after_interest: u64, + + reserve1_after_interest: u64, + + metadata: EventMetadata, + ) -> Self { + Self { + price0_ema, + + price1_ema, + + rate0, + + rate1, + + accrued_interest0, + + accrued_interest1, + + protocol_revenue_reserve0, + + protocol_revenue_reserve1, + + reserve0_after_interest, + + reserve1_after_interest, + + metadata, + } + } +} + +/// Custom struct: UserPosition +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPosition { + pub owner: Pubkey, + + pub pair: Pubkey, + + pub collateral0_applied_min_cf_bps: u16, + + pub collateral1_applied_min_cf_bps: u16, + + pub collateral0: u64, + + pub collateral1: u64, + + pub debt0_shares: u64, + + pub debt1_shares: u64, + + pub bump: u8, +} + +impl UserPosition { + pub fn new( + owner: Pubkey, + + pair: Pubkey, + + collateral0_applied_min_cf_bps: u16, + + collateral1_applied_min_cf_bps: u16, + + collateral0: u64, + + collateral1: u64, + + debt0_shares: u64, + + debt1_shares: u64, + + bump: u8, + ) -> Self { + Self { + owner, + + pair, + + collateral0_applied_min_cf_bps, + + collateral1_applied_min_cf_bps, + + collateral0, + + collateral1, + + debt0_shares, + + debt1_shares, + + bump, + } + } +} + +/// Custom struct: UserPositionCreatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionCreatedEvent { + pub position: Pubkey, + + pub metadata: EventMetadata, +} + +impl UserPositionCreatedEvent { + pub fn new(position: Pubkey, metadata: EventMetadata) -> Self { + Self { position, metadata } + } +} + +/// Custom struct: UserPositionLiquidatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionLiquidatedEvent { + pub position: Pubkey, + + pub liquidator: Pubkey, + + pub collateral0_liquidated: u64, + + pub collateral1_liquidated: u64, + + pub debt0_liquidated: u64, + + pub debt1_liquidated: u64, + + pub collateral_price: u64, + + pub shortfall: u128, + + pub liquidation_bonus_applied: u64, + + pub k0: u128, + + pub k1: u128, + + pub metadata: EventMetadata, +} + +impl UserPositionLiquidatedEvent { + pub fn new( + position: Pubkey, + + liquidator: Pubkey, + + collateral0_liquidated: u64, + + collateral1_liquidated: u64, + + debt0_liquidated: u64, + + debt1_liquidated: u64, + + collateral_price: u64, + + shortfall: u128, + + liquidation_bonus_applied: u64, + + k0: u128, + + k1: u128, + + metadata: EventMetadata, + ) -> Self { + Self { + position, + + liquidator, + + collateral0_liquidated, + + collateral1_liquidated, + + debt0_liquidated, + + debt1_liquidated, + + collateral_price, + + shortfall, + + liquidation_bonus_applied, + + k0, + + k1, + + metadata, + } + } +} + +/// Custom struct: UserPositionUpdatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionUpdatedEvent { + pub position: Pubkey, + + pub collateral0: u64, + + pub collateral1: u64, + + pub debt0_shares: u64, + + pub debt1_shares: u64, + + pub collateral0_applied_min_cf_bps: u16, + + pub collateral1_applied_min_cf_bps: u16, + + pub metadata: EventMetadata, +} + +impl UserPositionUpdatedEvent { + pub fn new( + position: Pubkey, + + collateral0: u64, + + collateral1: u64, + + debt0_shares: u64, + + debt1_shares: u64, + + collateral0_applied_min_cf_bps: u16, + + collateral1_applied_min_cf_bps: u16, + + metadata: EventMetadata, + ) -> Self { + Self { + position, + + collateral0, + + collateral1, + + debt0_shares, + + debt1_shares, + + collateral0_applied_min_cf_bps, + + collateral1_applied_min_cf_bps, + + metadata, + } + } +} + +/// Custom enum: UserPositionViewKind +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub enum UserPositionViewKind { + UserBorrowingPower, + + UserAppliedCollateralFactorBps, + + UserLiquidationCollateralFactorBps, + + UserDebtUtilizationBps, + + UserLiquidationPrice, + + UserDebtWithInterest, +} + +// ============================================================================ +// END OF GENERATED FILE +// ============================================================================ diff --git a/trident-tests/fuzz_lending/utils.rs b/trident-tests/fuzz_lending/utils.rs new file mode 100644 index 0000000..9654487 --- /dev/null +++ b/trident-tests/fuzz_lending/utils.rs @@ -0,0 +1,28 @@ +use trident_fuzz::fuzzing::{pubkey, Pubkey}; + +// DEPLOYER +pub const DEPLOYER_ADDRESS: Pubkey = pubkey!("C7GKpfqQyBoFR6S13DECwBjdi7aCQKbbeKjXm4Jt5Hds"); + +// PROGRAMS +pub const TOKEN_2022_PROGRAM: Pubkey = pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"); +pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); +pub const MPL_TOKEN_METADATA_ID: Pubkey = pubkey!("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"); +pub const FLASHLOAN_CALLBACK_RECEIVER_PROGRAM: Pubkey = + pubkey!("GmtswKBDrFZ9DfUfP7jbPFvbtuG7AJcX73SvoKWGxJbu"); + +// MINTS +pub const WSOL_MINT_ADDRESS: Pubkey = pubkey!("So11111111111111111111111111111111111111112"); + +// SEEDS +pub const FUTARCHY_AUTHORITY_SEED_PREFIX: &[u8] = b"futarchy_authority"; +pub const METADATA_SEED_PREFIX: &[u8] = b"metadata"; +pub const PAIR_SEED_PREFIX: &[u8] = b"gamm_pair"; +pub const LP_MINT_SEED_PREFIX: &[u8] = b"gamm_lp_mint"; +pub const POSITION_SEED_PREFIX: &[u8] = b"gamm_position"; + +// EVENT AUTHORITY +pub const EVENT_AUTHORITY_ADDRESS: Pubkey = pubkey!("fY27dnRLq4XVAKNRAY7nATiicimnY6mwLHq3V65uoP2"); + +pub fn get_size_with_discriminator() -> usize { + 8 + std::mem::size_of::() +} diff --git a/trident-tests/fuzz_lending/view/mod.rs b/trident-tests/fuzz_lending/view/mod.rs new file mode 100644 index 0000000..57ab2fb --- /dev/null +++ b/trident-tests/fuzz_lending/view/mod.rs @@ -0,0 +1,2 @@ +mod view_pair_data; +mod view_user_position_data; diff --git a/trident-tests/fuzz_lending/view/view_pair_data.rs b/trident-tests/fuzz_lending/view/view_pair_data.rs new file mode 100644 index 0000000..a9fb613 --- /dev/null +++ b/trident-tests/fuzz_lending/view/view_pair_data.rs @@ -0,0 +1,88 @@ +use trident_fuzz::fuzzing::*; + +use crate::{ + types::{ + omnipair::{ + ViewPairDataInstruction, ViewPairDataInstructionAccounts, ViewPairDataInstructionData, + }, + EmitValueArgs, Pair, PairViewKind, + }, + FuzzTest, +}; + +impl FuzzTest { + pub fn view_pair_data(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + let data = self.get_data_view_pair(); + let accounts = self.get_accounts_view_pair(); + + let ix = ViewPairDataInstruction::data(data) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("View Pair Data")); + + assert!(res.is_success()); + + // INVARIANT 1: the passed rate_model must match the pair's configured rate_model + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .unwrap(); + assert_eq!( + pair_account.rate_model, accounts.rate_model, + "ViewPairData accepted a mismatched rate_model for the given pair" + ); + } + + fn get_data_view_pair(&mut self) -> ViewPairDataInstructionData { + let getter = PairViewKind::random(&mut self.trident); + let collateral_amount = if getter == PairViewKind::GetBorrowLimitAndCfBpsForCollateral { + Some(self.trident.random_from_range(100..=100_000_000)) + } else { + None + }; + // Any token mint for collateral token + let collateral_token = if getter == PairViewKind::GetBorrowLimitAndCfBpsForCollateral { + Some(self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token mint should exist")) + } else { + None + }; + + ViewPairDataInstructionData::new( + getter, + EmitValueArgs::new( + None, // unused + collateral_amount, + collateral_token, + ), + ) + } + + fn get_accounts_view_pair(&mut self) -> ViewPairDataInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let rate_model = self.fuzz_accounts.rate_model.get(&mut self.trident).expect("Rate model should exist"); + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + ViewPairDataInstructionAccounts::new(pair, rate_model, futarchy_authority) + } +} + +impl PairViewKind { + pub fn random(trident: &mut Trident) -> Self { + match trident.random_from_range(0..=6) { + 0 => Self::EmaPrice0Nad, + 1 => Self::EmaPrice1Nad, + 2 => Self::SpotPrice0Nad, + 3 => Self::SpotPrice1Nad, + 4 => Self::K, + 5 => Self::GetRates, + 6 => Self::GetBorrowLimitAndCfBpsForCollateral, + _ => unreachable!(), + } + } +} diff --git a/trident-tests/fuzz_lending/view/view_user_position_data.rs b/trident-tests/fuzz_lending/view/view_user_position_data.rs new file mode 100644 index 0000000..2fad729 --- /dev/null +++ b/trident-tests/fuzz_lending/view/view_user_position_data.rs @@ -0,0 +1,95 @@ +use trident_fuzz::trident::Trident; + +use crate::{ + types::{ + omnipair::{ + self, ViewUserPositionDataInstruction, ViewUserPositionDataInstructionAccounts, + ViewUserPositionDataInstructionData, + }, + Pair, UserPosition, UserPositionViewKind, + }, + utils::POSITION_SEED_PREFIX, + FuzzTest, +}; + +impl FuzzTest { + pub fn view_user_position_data(&mut self) { + if let Some(accounts) = self.get_accounts_view_user_position_data() { + let data = self.get_data_view_user_position_data(); + let ix = ViewUserPositionDataInstruction::data(data) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("View User Position Data")); + + assert!(res.is_success()); + + // INVARIANT 1: the user_position must belong to the passed pair + let up_account = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .unwrap(); + assert_eq!( + up_account.pair, accounts.pair, + "ViewUserPositionData accepted a user_position that does not belong to the given pair" + ); + // INVARIANT 2: the passed rate_model must match the pair's configured rate_model + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .unwrap(); + assert_eq!( + pair_account.rate_model, accounts.rate_model, + "ViewPairData accepted a mismatched rate_model for the given pair" + ); + } + } + + fn get_data_view_user_position_data(&mut self) -> ViewUserPositionDataInstructionData { + let getter = UserPositionViewKind::random(&mut self.trident); + ViewUserPositionDataInstructionData::new(getter) + } + + fn get_accounts_view_user_position_data( + &mut self, + ) -> Option { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + self.trident + .get_account_with_type::(&user_position, 8)?; + + let rate_model = self.fuzz_accounts.rate_model.get(&mut self.trident).expect("Rate model should exist"); + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + Some(ViewUserPositionDataInstructionAccounts::new( + pair, + user_position, + rate_model, + futarchy_authority, + )) + } +} + +impl UserPositionViewKind { + pub fn random(trident: &mut Trident) -> Self { + match trident.random_from_range(0..=5) { + 0 => Self::UserBorrowingPower, + 1 => Self::UserAppliedCollateralFactorBps, + 2 => Self::UserLiquidationCollateralFactorBps, + 3 => Self::UserDebtUtilizationBps, + 4 => Self::UserLiquidationPrice, + 5 => Self::UserDebtWithInterest, + _ => unreachable!(), + } + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/futarchy/claim_protocol_fees.rs b/trident-tests/fuzz_liquidity_swaps/futarchy/claim_protocol_fees.rs new file mode 100644 index 0000000..87be3fb --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/futarchy/claim_protocol_fees.rs @@ -0,0 +1,305 @@ +use crate::FuzzTest; +use crate::{ + types::{ + omnipair::{ + ClaimProtocolFeesInstruction, ClaimProtocolFeesInstructionAccounts, + ClaimProtocolFeesInstructionData, + }, + ClaimProtocolFeesArgs, Pair, + }, + utils::{TOKEN_PROGRAM}, +}; + +impl FuzzTest { + pub fn claim_protocol_fees(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_claim_protocol_fees(); + let data = self.get_data_claim_protocol_fees(); + + // Capture initial state before the transaction + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_authority_token0_balance = self + .trident + .get_token_account(accounts.authority_token0_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_authority_token1_balance = self + .trident + .get_token_account(accounts.authority_token1_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = + ClaimProtocolFeesInstruction::data(ClaimProtocolFeesInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Claim Protocol Fees")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientAmount) + if res.is_success() { + self.verify_claim_protocol_fees_invariants( + &data, + &accounts, + &initial_pair, + initial_authority_token0_balance, + initial_authority_token1_balance, + ); + } + } + + fn get_data_claim_protocol_fees(&mut self) -> ClaimProtocolFeesArgs { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair account should exist"); + + // Get protocol fees from the pair account + // Claim partial amounts (or 0 if no fees available) + let amount0 = if pair_account.protocol_revenue_reserve0 > 0 { + self.trident + .random_from_range(1..=pair_account.protocol_revenue_reserve0) + } else { + 0 + }; + + let amount1 = if pair_account.protocol_revenue_reserve1 > 0 { + self.trident + .random_from_range(1..=pair_account.protocol_revenue_reserve1) + } else { + 0 + }; + + self.trident + .record_histogram("CLAIM_PROTOCOL_FEES_AMOUNT0", amount0 as f64); + self.trident + .record_histogram("CLAIM_PROTOCOL_FEES_AMOUNT1", amount1 as f64); + + ClaimProtocolFeesArgs::new(amount0, amount1) + } + + fn get_accounts_claim_protocol_fees(&mut self) -> ClaimProtocolFeesInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair account should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // authority token0 account + let authority_token0_account = self.trident.get_associated_token_address( + &pair_account.token0, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + + // authority token1 account + let authority_token1_account = self.trident.get_associated_token_address( + &pair_account.token1, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + + // caller (whoever) + let caller = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + ClaimProtocolFeesInstructionAccounts::new( + caller, + pair_pubkey, + futarchy_authority, + token0_vault, + token1_vault, + authority_token0_account, + authority_token1_account, + pair_account.token0, + pair_account.token1, + ) + } + + fn verify_claim_protocol_fees_invariants( + &mut self, + args: &ClaimProtocolFeesArgs, + accounts: &ClaimProtocolFeesInstructionAccounts, + initial_pair: &Pair, + initial_authority_token0_balance: u64, + initial_authority_token1_balance: u64, + ) { + // Fetch final state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_authority_token0_balance = self + .trident + .get_token_account(accounts.authority_token0_account) + .expect("Authority token0 account should exist") + .account + .amount; + + let final_authority_token1_balance = self + .trident + .get_token_account(accounts.authority_token1_account) + .expect("Authority token1 account should exist") + .account + .amount; + + // INVARIANT 1: Authority received exactly the claimed amounts + if args.amount0 > 0 { + assert_eq!( + final_authority_token0_balance, + initial_authority_token0_balance + .checked_add(args.amount0) + .expect("Authority token0 balance increase"), + "Authority should receive exactly amount0 claimed" + ); + } + + if args.amount1 > 0 { + assert_eq!( + final_authority_token1_balance, + initial_authority_token1_balance + .checked_add(args.amount1) + .expect("Authority token1 balance increase"), + "Authority should receive exactly amount1 claimed" + ); + } + + // INVARIANT 2: Protocol revenue reserves decreased by exactly the claimed amounts + assert_eq!( + final_pair.protocol_revenue_reserve0, + initial_pair + .protocol_revenue_reserve0 + .checked_sub(args.amount0) + .expect("Protocol revenue reserve0 decrease"), + "Protocol revenue reserve0 should decrease by amount0" + ); + + assert_eq!( + final_pair.protocol_revenue_reserve1, + initial_pair + .protocol_revenue_reserve1 + .checked_sub(args.amount1) + .expect("Protocol revenue reserve1 decrease"), + "Protocol revenue reserve1 should decrease by amount1" + ); + + // INVARIANT 3: Vault solvency check - vaults must hold at least reserves + collateral - debt + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .checked_add(final_pair.protocol_revenue_reserve0) + .expect("Adding protocol revenue overflow") + .saturating_sub(final_pair.total_debt0); + + // Allow small tolerance for accumulated rounding errors (0.01% of required or claimed amount) + let tolerance0 = args.amount0.max(required0.checked_div(10000).unwrap_or(0)); + assert!( + vault0_balance >= required0.saturating_sub(tolerance0), + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .checked_add(final_pair.protocol_revenue_reserve1) + .expect("Adding protocol revenue overflow") + .saturating_sub(final_pair.total_debt1); + + // Allow small tolerance for accumulated rounding errors (0.01% of required or claimed amount) + let tolerance1 = args.amount1.max(required1.checked_div(10000).unwrap_or(0)); + assert!( + vault1_balance >= required1.saturating_sub(tolerance1), + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 4: Pair core state should not have changed + // (claiming fees doesn't affect liquidity, collateral, or debt) + assert_eq!( + final_pair.reserve0, initial_pair.reserve0, + "Reserve0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.reserve1, initial_pair.reserve1, + "Reserve1 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_supply, initial_pair.total_supply, + "Total supply should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_collateral0, initial_pair.total_collateral0, + "Total collateral0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_collateral1, initial_pair.total_collateral1, + "Total collateral1 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_debt0, initial_pair.total_debt0, + "Total debt0 should not change when claiming protocol fees" + ); + assert_eq!( + final_pair.total_debt1, initial_pair.total_debt1, + "Total debt1 should not change when claiming protocol fees" + ); + + // INVARIANT 5: Pair identity should not have changed + assert_eq!( + final_pair.token0, initial_pair.token0, + "Pair token0 should not change" + ); + assert_eq!( + final_pair.token1, initial_pair.token1, + "Pair token1 should not change" + ); + assert_eq!( + final_pair.lp_mint, initial_pair.lp_mint, + "Pair LP mint should not change" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/futarchy/distribute_tokens.rs b/trident-tests/fuzz_liquidity_swaps/futarchy/distribute_tokens.rs new file mode 100644 index 0000000..1393a26 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/futarchy/distribute_tokens.rs @@ -0,0 +1,77 @@ +use crate::types::{ + omnipair::{ + DistributeTokensInstruction, DistributeTokensInstructionAccounts, + DistributeTokensInstructionData, + }, + DistributeTokensArgs, +}; +use crate::utils::WSOL_MINT_ADDRESS; +use crate::FuzzTest; + +impl FuzzTest { + pub fn distribute_tokens(&mut self) { + let accounts = self.get_accounts_distribute_tokens(); + let data = self.get_data_distribute_tokens(); + + let pre_src_acc = self.trident.get_token_account(accounts.source_token_account); + let pre_amount = pre_src_acc.unwrap().account.amount; + + let ix = DistributeTokensInstruction::data(DistributeTokensInstructionData::new(data)) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Distribute Tokens")); + + // Verify the transaction was successful + assert!(res.is_success()); + + // INVARIANT 1: if the source had a positive balance, + // the distribution should drain the entire source (no dust left behind). + if pre_amount > 0 { + let post_src_acc = self.trident.get_token_account(accounts.source_token_account); + let post_amount = post_src_acc.unwrap().account.amount; + assert!(post_amount == 0, "DistributeTokens left dust in source token account (amount={})", post_amount); + } + } + + fn get_data_distribute_tokens(&mut self) -> DistributeTokensArgs { + DistributeTokensArgs::new() + } + + fn get_accounts_distribute_tokens(&mut self) -> DistributeTokensInstructionAccounts { + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Get the source token account - this should be an associated token account + // that holds tokens to be distributed + let source_token_account = self + .fuzz_accounts + .authority_wsol_account + .get(&mut self.trident).expect("Authority WSOl account should exist"); + + let futarchy_treasury_token_account = self + .fuzz_accounts + .futarchy_treasury_token_account + .get(&mut self.trident).expect("Futarchy treasury token account should exist"); + + let buybacks_vault_token_account = self + .fuzz_accounts + .buybacks_vault_token_account + .get(&mut self.trident).expect("Buybacks vault token account should exist"); + + let team_treasury_token_account = self + .fuzz_accounts + .team_treasury_token_account + .get(&mut self.trident).expect("Team treasury token account should exist"); + + DistributeTokensInstructionAccounts::new( + futarchy_authority, + WSOL_MINT_ADDRESS, + source_token_account, + futarchy_treasury_token_account, + buybacks_vault_token_account, + team_treasury_token_account, + ) + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/futarchy/init_futarchy.rs b/trident-tests/fuzz_liquidity_swaps/futarchy/init_futarchy.rs new file mode 100644 index 0000000..7ca72c6 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/futarchy/init_futarchy.rs @@ -0,0 +1,193 @@ +use crate::utils::{WSOL_MINT_ADDRESS}; +use crate::FuzzTest; +use crate::{ + types::{ + omnipair::{ + InitFutarchyAuthorityInstruction, InitFutarchyAuthorityInstructionAccounts, + InitFutarchyAuthorityInstructionData, + }, + FutarchyAuthority, InitFutarchyAuthorityArgs, + }, + utils::DEPLOYER_ADDRESS, +}; +use trident_fuzz::fuzzing::*; + +impl FuzzTest { + pub fn init_futarchy(&mut self) { + // Init futarchy authority + let data = self.get_data_init_futarchy(); + let accounts = self.get_accounts_init_futarchy(); + + let ix = InitFutarchyAuthorityInstruction::data(InitFutarchyAuthorityInstructionData::new( + data.clone(), + )) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Init Futarchy Authority")); + + // MUST BE SUCCESSFUL + assert!(res.is_success()); + + // Verify invariants + self.verify_futarchy_invariants(&data, &accounts); + } + + fn get_data_init_futarchy(&mut self) -> InitFutarchyAuthorityArgs { + let swap_bps = self.trident.random_from_range(1..=5_000); + let interest_bps = self.trident.random_from_range(1..=5_000); + + self.trident + .record_histogram("INIT_FUTARCHY_SWAP_BPS", swap_bps as f64); + self.trident + .record_histogram("INIT_FUTARCHY_INTEREST_BPS", interest_bps as f64); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Futarchy treasury token account + let futarchy_treasury_token_account = self + .fuzz_accounts + .futarchy_treasury_token_account + .insert(&mut self.trident, None); + let init_futarchy_treasury_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &futarchy_treasury_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + + // Buybacks vault token account + let buybacks_vault_token_account = self + .fuzz_accounts + .buybacks_vault_token_account + .insert(&mut self.trident, None); + let init_buybacks_vault_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &buybacks_vault_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + // Team treasury token account + let team_treasury_token_account = self + .fuzz_accounts + .team_treasury_token_account + .insert(&mut self.trident, None); + let init_team_treasury_token_account_ix = self.trident.initialize_token_account( + &DEPLOYER_ADDRESS, + &team_treasury_token_account, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + let ixs: Vec = vec![ + init_futarchy_treasury_token_account_ix, + init_buybacks_vault_token_account_ix, + init_team_treasury_token_account_ix, + ] + .into_iter() + .flatten() + .collect(); + let res = self.trident.process_transaction(&ixs, None); + assert!(res.is_success()); + + // Generate random bps values that sum to exactly 10_000 + let futarchy_treasury_bps = self.trident.random_from_range(0..=10_000); + let remaining_after_first = 10_000 - futarchy_treasury_bps; + + let buybacks_vault_bps = if remaining_after_first > 0 { + self.trident.random_from_range(0..=remaining_after_first) + } else { + 0 + }; + + // Calculate the remaining to ensure sum equals 10_000 + let team_treasury_bps = 10_000u16 + .saturating_sub(futarchy_treasury_bps) + .saturating_sub(buybacks_vault_bps); + + self.trident.record_histogram( + "INIT_FUTARCHY_FUTARCHY_TREASURY_BPS", + futarchy_treasury_bps as f64, + ); + self.trident.record_histogram( + "INIT_FUTARCHY_BUYBACKS_VAULT_BPS", + buybacks_vault_bps as f64, + ); + self.trident + .record_histogram("INIT_FUTARCHY_TEAM_TREASURY_BPS", team_treasury_bps as f64); + + InitFutarchyAuthorityArgs { + authority: DEPLOYER_ADDRESS, + swap_bps, + interest_bps, + futarchy_treasury: futarchy_treasury_token_account, + futarchy_treasury_bps, + buybacks_vault: buybacks_vault_token_account, + buybacks_vault_bps, + team_treasury: team_treasury_token_account, + team_treasury_bps, + } + } + + fn get_accounts_init_futarchy(&mut self) -> InitFutarchyAuthorityInstructionAccounts { + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + InitFutarchyAuthorityInstructionAccounts::new(futarchy_authority) + } + + fn verify_futarchy_invariants( + &mut self, + data: &InitFutarchyAuthorityArgs, + accounts: &InitFutarchyAuthorityInstructionAccounts, + ) { + let futarchy_authority_account = self + .trident + .get_account_with_type::(&accounts.futarchy_authority, 8); + + if let Some(futarchy_authority_account) = futarchy_authority_account { + // Verify recipients are set correctly + assert_eq!( + futarchy_authority_account.recipients.futarchy_treasury, data.futarchy_treasury, + "Futarchy treasury recipient mismatch" + ); + assert_eq!( + futarchy_authority_account.recipients.buybacks_vault, data.buybacks_vault, + "Buybacks vault recipient mismatch" + ); + assert_eq!( + futarchy_authority_account.recipients.team_treasury, data.team_treasury, + "Team treasury recipient mismatch" + ); + + // Verify revenue distribution BPS sum to 10_000 + let total_bps = futarchy_authority_account + .revenue_distribution + .futarchy_treasury_bps + .saturating_add( + futarchy_authority_account + .revenue_distribution + .buybacks_vault_bps, + ) + .saturating_add( + futarchy_authority_account + .revenue_distribution + .team_treasury_bps, + ); + assert_eq!( + total_bps, 10_000, + "Revenue distribution BPS must sum to 10_000" + ); + + // Verify revenue share BPS are within valid range + assert!( + futarchy_authority_account.revenue_share.swap_bps <= 10_000, + "Swap BPS must be <= 10_000" + ); + assert!( + futarchy_authority_account.revenue_share.interest_bps <= 10_000, + "Interest BPS must be <= 10_000" + ); + } + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/futarchy/mod.rs b/trident-tests/fuzz_liquidity_swaps/futarchy/mod.rs new file mode 100644 index 0000000..4b1cb1a --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/futarchy/mod.rs @@ -0,0 +1,3 @@ +mod claim_protocol_fees; +mod distribute_tokens; +mod init_futarchy; diff --git a/trident-tests/fuzz_liquidity_swaps/fuzz_accounts.rs b/trident-tests/fuzz_liquidity_swaps/fuzz_accounts.rs new file mode 100644 index 0000000..1b999c9 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/fuzz_accounts.rs @@ -0,0 +1,34 @@ +use trident_fuzz::fuzzing::*; + +/// Storage for all account addresses used in fuzz testing. +/// +/// This struct serves as a centralized repository for account addresses, +/// enabling their reuse across different instruction flows and test scenarios. +/// +/// Docs: https://ackee.xyz/trident/docs/latest/trident-api-macro/trident-types/fuzz-accounts/ +#[derive(Default)] +pub struct AccountAddresses { + pub user: AddressStorage, + + pub token_mint: AddressStorage, + + pub pair: AddressStorage, + + pub rate_model: AddressStorage, + + pub futarchy_authority: AddressStorage, + + pub user_position: AddressStorage, + + pub lp_mint: AddressStorage, + + pub caller: AddressStorage, + + pub futarchy_treasury_token_account: AddressStorage, + + pub buybacks_vault_token_account: AddressStorage, + + pub team_treasury_token_account: AddressStorage, + + pub authority_wsol_account: AddressStorage, +} diff --git a/trident-tests/fuzz_liquidity_swaps/lending/add_collateral.rs b/trident-tests/fuzz_liquidity_swaps/lending/add_collateral.rs new file mode 100644 index 0000000..46ca124 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/add_collateral.rs @@ -0,0 +1,246 @@ +use crate::{ + types::{ + omnipair::{ + self, AddCollateralInstruction, AddCollateralInstructionAccounts, + AddCollateralInstructionData, + }, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn add_collateral(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_add_collateral(); + let accounts = self.get_accounts_add_collateral(); + + // Store initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_collateral_token_account) + .expect("User collateral account should exist") + .account + .amount; + + // Get initial pair and position state (position may not exist yet - init_if_needed) + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + let ix = AddCollateralInstruction::data(AddCollateralInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Add Collateral")); + + if res.is_success() { + self.verify_add_collateral_invariants( + &data, + &accounts, + initial_user_balance, + &initial_pair, + initial_position.as_ref(), + ); + } + } + + fn get_accounts_add_collateral(&mut self) -> AddCollateralInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // user related accounts + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let collateral_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + // let collateral_token_mint = pair_account.token0; + + let collateral_vault = self.trident.get_associated_token_address( + &collateral_token_mint, + &pair, + &TOKEN_PROGRAM, + ); + + let user_collateral_token_account = self.trident.get_associated_token_address( + &collateral_token_mint, + &user, + &TOKEN_PROGRAM, + ); + + AddCollateralInstructionAccounts::new( + pair, + pair_account.rate_model, + futarchy_authority, + user_position, + collateral_vault, + user_collateral_token_account, + collateral_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn get_data_add_collateral(&mut self) -> AdjustPositionArgs { + // Use reasonable collateral amounts that match typical use cases + let amount = self.trident.random_from_range(100_000..=100_000_000); + + self.trident + .record_histogram("ADD_COLLATERAL_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn verify_add_collateral_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &AddCollateralInstructionAccounts, + initial_user_balance: u64, + initial_pair: &Pair, + + initial_position: Option<&UserPosition>, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after add collateral"); + + // Get user position + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist after add collateral"); + + // Determine which token is being used as collateral + let is_token0 = accounts.collateral_token_mint == final_pair.token0; + + // Verify user balance decreased by exactly the amount + let final_user_balance = self + .trident + .get_token_account(accounts.user_collateral_token_account) + .expect("User collateral account should exist") + .account + .amount; + + let amount_transferred = initial_user_balance + .checked_sub(final_user_balance) + .expect("User balance should decrease"); + assert_eq!( + amount_transferred, args.amount, + "User balance should decrease by exactly the collateral amount" + ); + + // Verify user position collateral increased by EXACTLY the amount + // (position may be newly created, so initial collateral is 0 if it didn't exist) + if is_token0 { + let initial_collateral = initial_position.map(|p| p.collateral0).unwrap_or(0); + let expected_collateral = initial_collateral + .checked_add(args.amount) + .expect("User position collateral0 should not overflow"); + assert_eq!( + user_position.collateral0, expected_collateral, + "User position collateral0 should increase by exactly the amount added" + ); + } else { + let initial_collateral = initial_position.map(|p| p.collateral1).unwrap_or(0); + let expected_collateral = initial_collateral + .checked_add(args.amount) + .expect("User position collateral1 should not overflow"); + assert_eq!( + user_position.collateral1, expected_collateral, + "User position collateral1 should increase by exactly the amount added" + ); + } + + // Verify pair total_collateral increased by EXACTLY the amount (atomic transaction) + if is_token0 { + let expected_total = initial_pair + .total_collateral0 + .checked_add(args.amount) + .expect("Total collateral0 should not overflow"); + assert_eq!( + final_pair.total_collateral0, expected_total, + "Pair total_collateral0 should increase by exactly the amount added" + ); + } else { + let expected_total = initial_pair + .total_collateral1 + .checked_add(args.amount) + .expect("Total collateral1 should not overflow"); + assert_eq!( + final_pair.total_collateral1, expected_total, + "Pair total_collateral1 should increase by exactly the amount added" + ); + } + + // Critical accounting invariant: vault balance should be >= reserves + collateral - debt + // (vaults hold LP liquidity + borrower collateral, minus what was borrowed out) + let vault_balance = self + .trident + .get_token_account(accounts.collateral_vault) + .expect("Collateral vault should exist") + .account + .amount; + + if is_token0 { + let total_required = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); // Subtract debt (borrowed tokens are out of vault) + assert!( + vault_balance >= total_required, + "Vault balance must be >= reserve0 + total_collateral0 - total_debt0" + ); + } else { + let total_required = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); // Subtract debt (borrowed tokens are out of vault) + assert!( + vault_balance >= total_required, + "Vault balance must be >= reserve1 + total_collateral1 - total_debt1" + ); + } + + // Verify user position is properly initialized (always, whether new or existing) + assert_eq!( + user_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + user_position.pair, accounts.pair, + "User position pair should match pair" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/lending/borrow.rs b/trident-tests/fuzz_liquidity_swaps/lending/borrow.rs new file mode 100644 index 0000000..8962bc4 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/borrow.rs @@ -0,0 +1,334 @@ +use crate::{ + types::{ + omnipair::{self, BorrowInstruction, BorrowInstructionAccounts, BorrowInstructionData}, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn borrow(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_borrow(); + let accounts = self.get_accounts_borrow(); + + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if user_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = user_position.expect("User position should exist"); + + let ix = BorrowInstruction::data(BorrowInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Borrow")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., BorrowingPowerExceeded) + if res.is_success() { + self.verify_borrow_invariants( + &accounts, + &initial_pair, + &initial_position, + initial_user_balance, + ); + } + } + + fn get_data_borrow(&mut self) -> AdjustPositionArgs { + // Use very small amounts to increase success rate + // Borrowing power depends on collateral, which may be limited + // Use weighted distribution: 80% small amounts, 20% larger amounts + let amount = if self.trident.random_from_range(0..=9) < 8 { + // 80% chance: very small borrow (10 to 10,000) + self.trident.random_from_range(10..=10_000) + } else { + // 20% chance: larger borrow (10,000 to 100,000) + self.trident.random_from_range(10_000..=100_000) + }; + + self.trident + .record_histogram("BORROW_AMOUNT", amount as f64); + + AdjustPositionArgs::new(amount) + } + + fn get_accounts_borrow(&mut self) -> BorrowInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + // let vault_token_mint = pair_account.token1; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + BorrowInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_borrow_invariants( + &mut self, + accounts: &BorrowInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being borrowed + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Calculate actual borrow amount (may differ from args.amount if u64::MAX was used) + let actual_borrow_amount = final_user_balance + .checked_sub(initial_user_balance) + .expect("User balance should increase"); + + // INVARIANT 1: User should have received borrowed tokens + assert!( + actual_borrow_amount > 0, + "User should receive borrowed tokens" + ); + + // INVARIANT 2: Pair's total debt should increase by at least the borrow amount + // Note: update() accrues interest before borrowing, so total_debt may increase more than just the borrow amount + let (initial_total_debt, final_total_debt) = if is_token0 { + (initial_pair.total_debt0, final_pair.total_debt0) + } else { + (initial_pair.total_debt1, final_pair.total_debt1) + }; + let min_expected_debt = initial_total_debt + .checked_add(actual_borrow_amount) + .expect("Total debt increase calculation"); + assert!( + final_total_debt >= min_expected_debt, + "Pair total debt should be at least initial + borrow amount (was {}, expected >= {})", + final_total_debt, + min_expected_debt + ); + + // INVARIANT 3: User position debt shares should increase correctly + let (initial_debt_shares, final_debt_shares, initial_total_shares, final_total_shares) = + if is_token0 { + ( + initial_position.debt0_shares, + final_position.debt0_shares, + initial_pair.total_debt0_shares, + final_pair.total_debt0_shares, + ) + } else { + ( + initial_position.debt1_shares, + final_position.debt1_shares, + initial_pair.total_debt1_shares, + final_pair.total_debt1_shares, + ) + }; + + // Calculate expected shares increase + let expected_shares = if initial_total_shares == 0 { + // First debt: shares = amount (1:1 ratio) + actual_borrow_amount + } else { + // Subsequent debt: shares = amount * total_shares / total_debt + ((actual_borrow_amount as u128) + .checked_mul(initial_total_shares as u128) + .expect("Shares calculation") + .checked_div(initial_total_debt as u128) + .expect("Shares division")) as u64 + }; + + // Allow for 1-unit rounding tolerance in shares calculation + let expected_final_debt_shares = initial_debt_shares + .checked_add(expected_shares) + .expect("User debt shares increase"); + assert!( + final_debt_shares.abs_diff(expected_final_debt_shares) <= 1, + "User debt shares should increase correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_debt_shares, + final_debt_shares + ); + + let expected_final_total_shares = initial_total_shares + .checked_add(expected_shares) + .expect("Total debt shares increase"); + assert!( + final_total_shares.abs_diff(expected_final_total_shares) <= 1, + "Pair total debt shares should increase correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_total_shares, + final_total_shares + ); + + if is_token0 { + assert!( + final_position.debt0_shares > initial_position.debt0_shares, + "Debt0 shares should increase {:?} > {:?}", + final_position.debt0_shares, + initial_position.debt0_shares + ); + } else { + assert!( + final_position.debt1_shares > initial_position.debt1_shares, + "Debt1 shares should increase {:?} > {:?}", + final_position.debt1_shares, + initial_position.debt1_shares + ); + } + + // INVARIANT 4: Vault solvency check - vault balance >= reserves + collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 5: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 6: Collateral amounts should not change during borrow + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Collateral0 should not change during borrow" + ); + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Collateral1 should not change during borrow" + ); + + // INVARIANT 7: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "Borrow accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/lending/flashloan.rs b/trident-tests/fuzz_liquidity_swaps/lending/flashloan.rs new file mode 100644 index 0000000..8629c50 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/flashloan.rs @@ -0,0 +1,292 @@ +use trident_fuzz::fuzzing::AccountMeta; + +use crate::{ + types::{ + omnipair::{ + self, FlashloanInstruction, FlashloanInstructionAccounts, FlashloanInstructionData, + }, + FlashloanArgs, Pair, + }, + utils::{EVENT_AUTHORITY_ADDRESS, FLASHLOAN_CALLBACK_RECEIVER_PROGRAM, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn flashloan(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + // NOTE: Flashloan failures (InsufficientAmount0/1) are expected and normal during fuzzing + // The callback receiver program needs to repay the loan + fees, which may not always succeed + let data = self.get_data_flashloan(); + let accounts = self.get_accounts_flashloan(); + + // Capture initial state before the transaction + let initial_vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let initial_vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let ix = FlashloanInstruction::data(data.clone()) + .accounts(accounts.clone()) + .remaining_accounts(vec![ + AccountMeta::new(accounts.token0_vault, false), + AccountMeta::new(accounts.token1_vault, false), + ]) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Flashloan")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., BorrowExceedsReserve, InsufficientAmount) + if res.is_success() { + self.verify_flashloan_invariants( + &data.args, + &accounts, + &initial_pair, + initial_vault0_balance, + initial_vault1_balance, + ); + } + } + + pub fn get_accounts_flashloan(&mut self) -> FlashloanInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let token0_vault = + self.trident + .get_associated_token_address(&pair_account.token0, &pair, &TOKEN_PROGRAM); + + let token1_vault = + self.trident + .get_associated_token_address(&pair_account.token1, &pair, &TOKEN_PROGRAM); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let receiver_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + let receiver_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + FlashloanInstructionAccounts::new( + pair, + pair_account.rate_model, + futarchy_authority, + token0_vault, + token1_vault, + pair_account.token0, + pair_account.token1, + receiver_token0_account, + receiver_token1_account, + FLASHLOAN_CALLBACK_RECEIVER_PROGRAM, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + pub fn get_data_flashloan(&mut self) -> FlashloanInstructionData { + // Strategy: Use very small amounts to maximize success rate + // Flashloans require the callback receiver to repay loan + fees, which often fails in fuzzing + let (amount0, amount1) = match self.trident.random_from_range(0..=100) { + // 40% - Flash only token0 (very small amount) + 0..=39 => (self.trident.random_from_range(100..=1_000), 0), + // 40% - Flash only token1 (very small amount) + 40..=79 => (0, self.trident.random_from_range(100..=1_000)), + // 15% - Flash both tokens (very small amounts) + 80..=94 => ( + self.trident.random_from_range(100..=500), + self.trident.random_from_range(100..=500), + ), + // 5% - Slightly larger amounts (stress test) + _ => ( + self.trident.random_from_range(1_000..=5_000), + self.trident.random_from_range(1_000..=5_000), + ), + }; + + self.trident + .record_histogram("FLASHLOAN_AMOUNT0", amount0 as f64); + self.trident + .record_histogram("FLASHLOAN_AMOUNT1", amount1 as f64); + + FlashloanInstructionData::new(FlashloanArgs::new(amount0, amount1, vec![])) + } + + fn verify_flashloan_invariants( + &mut self, + args: &FlashloanArgs, + accounts: &FlashloanInstructionAccounts, + initial_pair: &Pair, + initial_vault0_balance: u64, + initial_vault1_balance: u64, + ) { + // Fetch final state + let final_vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + + let final_vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + // Calculate expected fees (FLASHLOAN_FEE_BPS = 5 bps = 0.05%) + const FLASHLOAN_FEE_BPS: u64 = 5; + const BPS_DENOMINATOR: u64 = 10_000; + + let fee0 = (args.amount0 as u128) + .checked_mul(FLASHLOAN_FEE_BPS as u128) + .expect("Fee0 calculation") + .checked_div(BPS_DENOMINATOR as u128) + .expect("Fee0 division") as u64; + + let fee1 = (args.amount1 as u128) + .checked_mul(FLASHLOAN_FEE_BPS as u128) + .expect("Fee1 calculation") + .checked_div(BPS_DENOMINATOR as u128) + .expect("Fee1 division") as u64; + + // INVARIANT 1: Vault balances must have increased by at least the fee + // Critical: This ensures flashloan was repaid with fee + let required_balance0 = initial_vault0_balance + .checked_add(fee0) + .expect("Required balance0 calculation"); + let required_balance1 = initial_vault1_balance + .checked_add(fee1) + .expect("Required balance1 calculation"); + + assert!( + final_vault0_balance >= required_balance0, + "Token0 vault must have at least initial balance + fee after flashloan (repayment check)" + ); + assert!( + final_vault1_balance >= required_balance1, + "Token1 vault must have at least initial balance + fee after flashloan (repayment check)" + ); + + // INVARIANT 2: Net profit check - vault balances increased (fees were collected) + // Only check if fee > 0 (small amounts can round to 0 fee) + if fee0 > 0 { + assert!( + final_vault0_balance > initial_vault0_balance, + "Token0 vault should have more tokens after flashloan (fee collected)" + ); + } + if fee1 > 0 { + assert!( + final_vault1_balance > initial_vault1_balance, + "Token1 vault should have more tokens after flashloan (fee collected)" + ); + } + + // INVARIANT 3: Vault solvency check - vaults must hold at least reserves + collateral - debt + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + final_vault0_balance >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + final_vault1_balance >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 4: Pair state should not have fundamentally changed + // (reserves may have changed due to update() interest accrual, but core structure remains) + assert_eq!( + final_pair.token0, initial_pair.token0, + "Pair token0 should not change" + ); + assert_eq!( + final_pair.token1, initial_pair.token1, + "Pair token1 should not change" + ); + assert_eq!( + final_pair.lp_mint, initial_pair.lp_mint, + "Pair LP mint should not change" + ); + + // INVARIANT 5: Collateral and debt shares should not have changed + // (flashloans shouldn't affect lending positions) + // Note: total_debt can increase due to interest accrual, but debt_shares should remain constant + assert_eq!( + final_pair.total_collateral0, initial_pair.total_collateral0, + "Total collateral0 should not change during flashloan" + ); + assert_eq!( + final_pair.total_collateral1, initial_pair.total_collateral1, + "Total collateral1 should not change during flashloan" + ); + assert!( + final_pair.total_debt0 >= initial_pair.total_debt0, + "Total debt0 should be at least initial (interest can accrue). Was {}, initial {}", + final_pair.total_debt0, + initial_pair.total_debt0 + ); + assert!( + final_pair.total_debt1 >= initial_pair.total_debt1, + "Total debt1 should be at least initial (interest can accrue). Was {}, initial {}", + final_pair.total_debt1, + initial_pair.total_debt1 + ); + assert_eq!( + final_pair.total_debt0_shares, initial_pair.total_debt0_shares, + "Total debt0 shares should not change during flashloan" + ); + assert_eq!( + final_pair.total_debt1_shares, initial_pair.total_debt1_shares, + "Total debt1 shares should not change during flashloan" + ); + + // INVARIANT 6: Total supply should not have changed + // (flashloans don't mint or burn LP tokens) + assert_eq!( + final_pair.total_supply, initial_pair.total_supply, + "Total supply should not change during flashloan" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/lending/liquidate.rs b/trident-tests/fuzz_liquidity_swaps/lending/liquidate.rs new file mode 100644 index 0000000..3afe70b --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/liquidate.rs @@ -0,0 +1,331 @@ +use trident_fuzz::fuzzing::LAMPORTS_PER_SOL; + +use crate::{ + types::{ + omnipair::{ + self, LiquidateInstruction, LiquidateInstructionAccounts, LiquidateInstructionData, + }, + Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn liquidate(&mut self) { + if self.fuzz_accounts.pair.is_empty() || self.fuzz_accounts.user_position.is_empty() { + return; + } + + let accounts = self.get_accounts_liquidate(); + + // Capture initial state before the transaction + let initial_caller_balance = self + .trident + .get_token_account(accounts.caller_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + let ix = LiquidateInstruction::data(LiquidateInstructionData::new()) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Liquidate")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., NotUndercollateralized, no debt) + if res.is_success() { + self.verify_liquidate_invariants( + &accounts, + &initial_pair, + &initial_position, + initial_caller_balance, + ); + } + } + + fn get_accounts_liquidate(&mut self) -> LiquidateInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user_position = self.fuzz_accounts.user_position.get(&mut self.trident).expect("User position should exist"); + let user_position_account_data = self + .trident + .get_account_with_type::(&user_position, 8) + .expect("User position should exist"); + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let collateral_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let collateral_vault = self.trident.get_associated_token_address( + &collateral_token_mint, + &pair, + &TOKEN_PROGRAM, + ); + + let caller = self.fuzz_accounts.caller.insert(&mut self.trident, None); + self.trident.airdrop( + &caller, + LAMPORTS_PER_SOL.checked_mul(2).expect("Airdrop amount"), + ); + let caller_token_account = self.trident.get_associated_token_address( + &collateral_token_mint, + &caller, + &TOKEN_PROGRAM, + ); + self.trident + .initialize_associated_token_account(&caller, &collateral_token_mint, &caller); + + LiquidateInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + collateral_vault, + caller_token_account, + collateral_token_mint, + user_position_account_data.owner, + caller, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_liquidate_invariants( + &mut self, + accounts: &LiquidateInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_caller_balance: u64, + ) { + // Fetch final state + let final_caller_balance = self + .trident + .get_token_account(accounts.caller_token_account) + .expect("Caller token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is collateral and which is debt + let collateral_vault = self + .trident + .get_token_account(accounts.collateral_vault) + .expect("Collateral vault should exist") + .account; + let is_collateral_token0 = collateral_vault.mint == final_pair.token0; + + // Calculate initial debt + let (_, initial_debt_shares) = if is_collateral_token0 { + // Collateral is token0, debt is token1 + let debt = if initial_pair.total_debt1_shares == 0 { + 0 + } else { + ((initial_position.debt1_shares as u128) + .checked_mul(initial_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, initial_position.debt1_shares) + } else { + // Collateral is token1, debt is token0 + let debt = if initial_pair.total_debt0_shares == 0 { + 0 + } else { + ((initial_position.debt0_shares as u128) + .checked_mul(initial_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, initial_position.debt0_shares) + }; + + // INVARIANT 1: Caller received liquidation incentive (should be > 0) + let caller_incentive = final_caller_balance + .checked_sub(initial_caller_balance) + .expect("Caller balance should increase"); + assert!( + caller_incentive > 0, + "Liquidator should receive incentive for liquidating" + ); + + // INVARIANT 2: User position collateral decreased + let (initial_collateral, final_collateral) = if is_collateral_token0 { + (initial_position.collateral0, final_position.collateral0) + } else { + (initial_position.collateral1, final_position.collateral1) + }; + let collateral_seized = initial_collateral + .checked_sub(final_collateral) + .expect("Collateral should decrease"); + assert!( + collateral_seized > 0, + "Position collateral should be seized during liquidation" + ); + + // INVARIANT 3: Pair total collateral decreased by the seized amount + let (initial_total_collateral, final_total_collateral) = if is_collateral_token0 { + (initial_pair.total_collateral0, final_pair.total_collateral0) + } else { + (initial_pair.total_collateral1, final_pair.total_collateral1) + }; + assert_eq!( + final_total_collateral, + initial_total_collateral + .checked_sub(collateral_seized) + .expect("Total collateral decrease"), + "Pair total collateral should decrease by seized amount" + ); + + // INVARIANT 4: Collateral seized >= caller incentive (caller gets incentive, rest to reserves) + assert!( + collateral_seized >= caller_incentive, + "Collateral seized must cover at least the liquidation incentive" + ); + + // INVARIANT 5: Debt decreased or was written off (final debt <= initial debt) + let (_, final_debt_shares) = if is_collateral_token0 { + let debt = if final_pair.total_debt1_shares == 0 { + 0 + } else { + ((final_position.debt1_shares as u128) + .checked_mul(final_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(final_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, final_position.debt1_shares) + } else { + let debt = if final_pair.total_debt0_shares == 0 { + 0 + } else { + ((final_position.debt0_shares as u128) + .checked_mul(final_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(final_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + }; + (debt, final_position.debt0_shares) + }; + + // Note: final_debt may not be less than initial_debt if significant interest accrued during update() + // The authoritative measure is debt_shares, which must decrease during liquidation + assert!( + final_debt_shares < initial_debt_shares, + "Debt shares must decrease during liquidation (was {}, now {})", + initial_debt_shares, final_debt_shares + ); + + // INVARIANT 6: Collateral reserve increased (by collateral_seized - caller_incentive) + let (initial_collateral_reserve, final_collateral_reserve) = if is_collateral_token0 { + (initial_pair.reserve0, final_pair.reserve0) + } else { + (initial_pair.reserve1, final_pair.reserve1) + }; + + // Note: Due to pair.update(), reserves may have interest accrued, so we can't do exact check + // But collateral reserve should have increased + assert!( + final_collateral_reserve >= initial_collateral_reserve, + "Collateral reserve should increase (seized collateral - incentive goes to reserves)" + ); + + // INVARIANT 7: Vault solvency check + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 8: Position ownership unchanged + assert_eq!( + final_position.owner, initial_position.owner, + "Position owner should not change" + ); + assert_eq!( + final_position.pair, initial_position.pair, + "Position pair should not change" + ); + + // INVARIANT 9: Non-seized collateral unchanged + if is_collateral_token0 { + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Non-seized collateral (token1) should not change" + ); + } else { + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Non-seized collateral (token0) should not change" + ); + } + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/lending/mod.rs b/trident-tests/fuzz_liquidity_swaps/lending/mod.rs new file mode 100644 index 0000000..4e2896b --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/mod.rs @@ -0,0 +1,6 @@ +mod add_collateral; +mod borrow; +mod flashloan; +mod liquidate; +mod remove_collateral; +mod repay; diff --git a/trident-tests/fuzz_liquidity_swaps/lending/remove_collateral.rs b/trident-tests/fuzz_liquidity_swaps/lending/remove_collateral.rs new file mode 100644 index 0000000..265ce12 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/remove_collateral.rs @@ -0,0 +1,309 @@ + + +use crate::{ + types::{ + omnipair::{ + self, RemoveCollateralInstruction, RemoveCollateralInstructionAccounts, + RemoveCollateralInstructionData, + }, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn remove_collateral(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_remove_collateral(); + let accounts = self.get_accounts_remove_collateral(); + + // Check if user position exists + let initial_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if initial_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let ix = + RemoveCollateralInstruction::data(RemoveCollateralInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Remove Collateral")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientCollateral, BorrowingPowerExceeded) + if res.is_success() { + self.verify_remove_collateral_invariants( + &data, + &accounts, + &initial_pair, + &initial_position.unwrap(), + initial_user_balance, + ); + } + } + + fn get_data_remove_collateral(&mut self) -> AdjustPositionArgs { + // Use smaller amounts more likely to match actual collateral amounts + let amount = self.trident.random_from_range(100..=1_000_000); + self.trident + .record_histogram("REMOVE_COLLATERAL_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn get_accounts_remove_collateral(&mut self) -> RemoveCollateralInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .unwrap(); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + RemoveCollateralInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_remove_collateral_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &RemoveCollateralInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being withdrawn + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Get initial collateral amount + let initial_collateral = if is_token0 { + initial_position.collateral0 + } else { + initial_position.collateral1 + }; + + // Calculate actual withdraw amount + // If args.amount == u64::MAX, the program withdraws all available collateral + let is_withdraw_all = args.amount == u64::MAX; + let actual_withdraw_amount = if is_withdraw_all { + initial_collateral + } else { + args.amount + }; + + // INVARIANT 1: User token balance should increase by exactly the withdraw amount + let amount_transferred = final_user_balance + .checked_sub(initial_user_balance) + .expect("User balance should increase"); + assert_eq!( + amount_transferred, actual_withdraw_amount, + "User should receive exactly the withdrawn amount" + ); + + // INVARIANT 2: User position collateral should decrease by exactly the withdraw amount + let final_collateral = if is_token0 { + final_position.collateral0 + } else { + final_position.collateral1 + }; + assert_eq!( + final_collateral, + initial_collateral + .checked_sub(actual_withdraw_amount) + .expect("Collateral decrease calculation"), + "User position collateral should decrease by withdraw amount" + ); + + // INVARIANT 3: Pair total collateral should decrease by exactly the withdraw amount + let (initial_total_collateral, final_total_collateral) = if is_token0 { + (initial_pair.total_collateral0, final_pair.total_collateral0) + } else { + (initial_pair.total_collateral1, final_pair.total_collateral1) + }; + assert_eq!( + final_total_collateral, + initial_total_collateral + .checked_sub(actual_withdraw_amount) + .expect("Total collateral decrease calculation"), + "Pair total collateral should decrease by withdraw amount" + ); + + // INVARIANT 4: If withdraw_all was requested, verify final collateral is 0 + // (this assumes no debt that would prevent full withdrawal - which is validated in the program) + if is_withdraw_all && actual_withdraw_amount == initial_collateral { + assert_eq!( + final_collateral, 0, + "When withdrawing all available collateral, final collateral should be 0" + ); + } + + // INVARIANT 5: Vault solvency - vaults must hold at least reserves + total_collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 6: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 7: Debt amounts should not change during collateral removal + assert_eq!( + final_position.debt0_shares, initial_position.debt0_shares, + "Debt0 shares should not change when removing collateral" + ); + assert_eq!( + final_position.debt1_shares, initial_position.debt1_shares, + "Debt1 shares should not change when removing collateral" + ); + + // INVARIANT 8: Non-withdrawn collateral unchanged + if is_token0 { + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Non-withdrawn collateral (token1) should not change" + ); + } else { + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Non-withdrawn collateral (token0) should not change" + ); + } + + // INVARIANT 9: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "RemoveCollateral accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/lending/repay.rs b/trident-tests/fuzz_liquidity_swaps/lending/repay.rs new file mode 100644 index 0000000..e3580b4 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/lending/repay.rs @@ -0,0 +1,350 @@ + + +use crate::{ + types::{ + omnipair::{self, RepayInstruction, RepayInstructionAccounts, RepayInstructionData}, + AdjustPositionArgs, Pair, UserPosition, + }, + utils::{EVENT_AUTHORITY_ADDRESS, POSITION_SEED_PREFIX, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn repay(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let data = self.get_data_repay(); + let accounts = self.get_accounts_repay(); + + // Check if user position exists + let user_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8); + + if user_position.is_none() { + // No position found, skip + return; + } + + // Capture initial state + let initial_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let initial_position = user_position.unwrap(); + + let ix = RepayInstruction::data(RepayInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Repay")); + + // Only verify invariants if transaction succeeded + // Transaction may fail with expected errors (e.g., InsufficientDebt, InsufficientAmount) + if res.is_success() { + self.verify_repay_invariants( + &data, + &accounts, + &initial_pair, + &initial_position, + initial_user_balance, + ); + } + } + + fn get_data_repay(&mut self) -> AdjustPositionArgs { + // Use small amounts more likely to match actual debt amounts + // Weighted distribution: favor smaller repayments + let amount = if self.trident.random_from_range(0..=9) < 7 { + self.trident.random_from_range(10..=10_000) + } else { + self.trident.random_from_range(10_000..=100_000) + }; + self.trident.record_histogram("REPAY_AMOUNT", amount as f64); + AdjustPositionArgs::new(amount) + } + + fn get_accounts_repay(&mut self) -> RepayInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + + let pair_account = self + .trident + .get_account_with_type::(&pair, 8) + .expect("Pair should exist"); + + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + let vault_token_mint = if self.trident.random_from_range(0..=1) == 0 { + pair_account.token0 + } else { + pair_account.token1 + }; + + let token_vault = + self.trident + .get_associated_token_address(&vault_token_mint, &pair, &TOKEN_PROGRAM); + + let user_token_account = + self.trident + .get_associated_token_address(&vault_token_mint, &user, &TOKEN_PROGRAM); + + RepayInstructionAccounts::new( + pair, + user_position, + pair_account.rate_model, + futarchy_authority, + token_vault, + user_token_account, + vault_token_mint, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_repay_invariants( + &mut self, + args: &AdjustPositionArgs, + accounts: &RepayInstructionAccounts, + initial_pair: &Pair, + initial_position: &UserPosition, + initial_user_balance: u64, + ) { + // Fetch final state + let final_user_balance = self + .trident + .get_token_account(accounts.user_token_account) + .expect("User token account should exist") + .account + .amount; + + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + + let final_position = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .expect("User position should exist"); + + // Determine which token is being repaid + let vault_token_account = self + .trident + .get_token_account(accounts.token_vault) + .expect("Token vault should exist") + .account; + let is_token0 = vault_token_account.mint == final_pair.token0; + + // Calculate initial debt manually: debt = (user_shares * total_debt) / total_shares + let initial_debt = if is_token0 { + if initial_pair.total_debt0_shares == 0 { + 0 + } else { + ((initial_position.debt0_shares as u128) + .checked_mul(initial_pair.total_debt0 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt0_shares as u128) + .expect("Debt division")) as u64 + } + } else if initial_pair.total_debt1_shares == 0 { + 0 + } else { + ((initial_position.debt1_shares as u128) + .checked_mul(initial_pair.total_debt1 as u128) + .expect("Debt calculation") + .checked_div(initial_pair.total_debt1_shares as u128) + .expect("Debt division")) as u64 + }; + + // Calculate actual repay amount (may differ from args.amount if u64::MAX was used) + let is_repay_all = args.amount == u64::MAX; + let actual_repay_amount = initial_user_balance + .checked_sub(final_user_balance) + .expect("User balance should decrease"); + + // INVARIANT 1: User should have paid tokens for repayment + assert!( + actual_repay_amount > 0, + "User should have paid tokens for repayment" + ); + + // INVARIANT 2: If repaying all, verify the repay amount equals initial debt + if is_repay_all { + assert_eq!( + actual_repay_amount, initial_debt, + "Repay all should repay exactly the full debt amount" + ); + } + + // INVARIANT 3: Pair's total debt accounting + // Note: update() accrues interest before repayment, so we can't predict exact final debt + // The repayment reduces debt, but interest may have accrued first + // We verify the shares decrease correctly instead (which is the authoritative measure) + let (initial_total_debt, _) = if is_token0 { + (initial_pair.total_debt0, final_pair.total_debt0) + } else { + (initial_pair.total_debt1, final_pair.total_debt1) + }; + + // The final debt should be reasonable - not more than initial + potential interest + // and definitely less than initial if significant time hasn't passed + // Main check: verify shares decreased correctly (below) + + // INVARIANT 4: User position debt shares should decrease correctly + let (initial_debt_shares, final_debt_shares, initial_total_shares, final_total_shares) = + if is_token0 { + ( + initial_position.debt0_shares, + final_position.debt0_shares, + initial_pair.total_debt0_shares, + final_pair.total_debt0_shares, + ) + } else { + ( + initial_position.debt1_shares, + final_position.debt1_shares, + initial_pair.total_debt1_shares, + final_pair.total_debt1_shares, + ) + }; + + // Calculate expected shares decrease + let expected_shares = if is_repay_all { + // Repay all: use user's debt shares + initial_debt_shares + } else { + // shares = amount * total_shares / total_debt + ((actual_repay_amount as u128) + .checked_mul(initial_total_shares as u128) + .expect("Shares calculation") + .checked_div(initial_total_debt as u128) + .expect("Shares division")) as u64 + }; + + // Allow for 1-unit rounding tolerance in shares calculation + let expected_final_debt_shares = initial_debt_shares + .checked_sub(expected_shares) + .expect("User debt shares decrease"); + + // INVARIANT 5: If repay all, verify final debt shares are 0 + if is_repay_all { + assert_eq!( + final_debt_shares, 0, + "Repay all should result in zero debt shares" + ); + } else { + assert!( + final_debt_shares.abs_diff(expected_final_debt_shares) <= 1, + "User debt shares should decrease correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_debt_shares, + final_debt_shares + ); + } + + let expected_final_total_shares = initial_total_shares + .checked_sub(expected_shares) + .expect("Total debt shares decrease"); + assert!( + final_total_shares.abs_diff(expected_final_total_shares) <= 1, + "Pair total debt shares should decrease correctly (with 1-unit rounding tolerance). Expected: {}, Got: {}", + expected_final_total_shares, + final_total_shares + ); + + // INVARIANT 6: Vault solvency check - vault balance >= reserves + collateral - debt + let vault0_balance = self.trident.get_associated_token_address( + &final_pair.token0, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault0_amount = self + .trident + .get_token_account(vault0_balance) + .expect("Token0 vault should exist") + .account + .amount; + + let vault1_balance = self.trident.get_associated_token_address( + &final_pair.token1, + &accounts.pair, + &TOKEN_PROGRAM, + ); + let vault1_amount = self + .trident + .get_token_account(vault1_balance) + .expect("Token1 vault should exist") + .account + .amount; + + let required0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt0); + assert!( + vault0_amount >= required0, + "Token0 vault balance must be >= reserve0 + collateral0 - debt0" + ); + + let required1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Reserve + collateral overflow") + .saturating_sub(final_pair.total_debt1); + assert!( + vault1_amount >= required1, + "Token1 vault balance must be >= reserve1 + collateral1 - debt1" + ); + + // INVARIANT 7: Verify user position ownership hasn't changed + assert_eq!( + final_position.owner, accounts.user, + "User position owner should match user" + ); + assert_eq!( + final_position.pair, accounts.pair, + "User position pair should match pair" + ); + + // INVARIANT 8: Collateral amounts should not change during repay + assert_eq!( + final_position.collateral0, initial_position.collateral0, + "Collateral0 should not change during repay" + ); + assert_eq!( + final_position.collateral1, initial_position.collateral1, + "Collateral1 should not change during repay" + ); + + // INVARIANT 9: on success, token_vault must be the canonical ATA for (pair, vault_token_mint) + let canonical_vault = self.trident.get_associated_token_address( + &accounts.vault_token_mint, + &accounts.pair, + &TOKEN_PROGRAM, + ); + assert_eq!( + accounts.token_vault, canonical_vault, + "Repay accepted a non-canonical token_vault for the pair and mint" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/liquidity/add_liquidity.rs b/trident-tests/fuzz_liquidity_swaps/liquidity/add_liquidity.rs new file mode 100644 index 0000000..22a619b --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/liquidity/add_liquidity.rs @@ -0,0 +1,367 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{ + self, AddLiquidityInstruction, AddLiquidityInstructionAccounts, + AddLiquidityInstructionData, + }, + AddLiquidityArgs, Pair, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn add_liquidity(&mut self) { + // Check if any pairs exist before trying to add liquidity + if self.fuzz_accounts.pair.is_empty() { + return; + } + + let accounts = self.get_accounts_add_liquidity(); + let data = self.get_data_add_liquidity(accounts.pair); + + let ix = AddLiquidityInstruction::data(AddLiquidityInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + // Store initial state + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + let initial_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let res = self + .trident + .process_transaction(&[ix], Some("Add Liquidity")); + + if res.is_success() { + self.verify_add_liquidity_invariants(&data, &accounts, &initial_pair, initial_user_lp); + } + } + + fn get_data_add_liquidity(&mut self, pair_pubkey: Pubkey) -> AddLiquidityArgs { + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + let total_supply = pair.total_supply; + + // Strategy: Test different liquidity addition scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Proportional liquidity (maintains price ratio) + 0..=39 => { + // Add liquidity proportional to current reserves + let proportion = self.trident.random_from_range(1..=1000); // 0.1% to 100% of reserves + let amount0_in = reserve0.saturating_mul(proportion) / 1000; + let amount1_in = reserve1.saturating_mul(proportion) / 1000; + + // Calculate expected liquidity: both ratios should be equal + let expected_liquidity = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap() as u64; + + // Set min_liquidity with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let min_liquidity_out = + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out, + } + } + + // 30% - Imbalanced liquidity (one side larger) + 40..=69 => { + // This will result in min(liquidity0, liquidity1) + let base_amount = self.trident.random_from_range(100..=100_000_000); + let imbalance_factor = self.trident.random_from_range(50..=200); // 0.5x to 2x + + let amount0_in: u64 = base_amount; + let amount1_in = base_amount.saturating_mul(imbalance_factor) / 100; + + // Calculate expected (will be the minimum) + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap(); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve1 as u128) + .unwrap(); + let expected = liquidity0.min(liquidity1) as u64; + + // Random slippage tolerance + let slippage_bps = self.trident.random_from_range(1..=500); + let min_liquidity_out = expected.saturating_mul(10000 - slippage_bps) / 10000; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out, + } + } + + // 15% - Small amounts (dust testing) + 70..=84 => { + let amount0_in = self.trident.random_from_range(1..=1000); + let amount1_in = self.trident.random_from_range(1..=1000); + + // Calculate expected + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve0 as u128) + .unwrap_or(1); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap() + .checked_div(reserve1 as u128) + .unwrap_or(1); + let expected = liquidity0.min(liquidity1) as u64; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: expected.saturating_sub(1), // Allow for rounding + } + } + + // 10% - Large amounts (stress testing) + 85..=94 => { + let amount0_in = self.trident.random_from_range(1_000_000..=u64::MAX / 1000); + let amount1_in = self.trident.random_from_range(1_000_000..=u64::MAX / 1000); + + let liquidity0 = (amount0_in as u128) + .checked_mul(total_supply as u128) + .unwrap_or(u128::MAX) + .checked_div(reserve0 as u128) + .unwrap_or(u128::MAX); + let liquidity1 = (amount1_in as u128) + .checked_mul(total_supply as u128) + .unwrap_or(u128::MAX) + .checked_div(reserve1 as u128) + .unwrap_or(u128::MAX); + let expected = liquidity0.min(liquidity1).min(u64::MAX as u128) as u64; + + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: expected / 2, // Large slippage tolerance + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero amounts (should fail with AmountZero) + 0 => AddLiquidityArgs { + amount0_in: 0, + amount1_in: self.trident.random_from_range(1..=1000), + min_liquidity_out: 0, + }, + 1 => AddLiquidityArgs { + amount0_in: self.trident.random_from_range(1..=1000), + amount1_in: 0, + min_liquidity_out: 0, + }, + // Unrealistic min_liquidity (should fail with InsufficientLiquidity) + 2 => { + let amount0_in = self.trident.random_from_range(100..=10000); + let amount1_in = self.trident.random_from_range(100..=10000); + AddLiquidityArgs { + amount0_in, + amount1_in, + min_liquidity_out: u64::MAX, // Impossible to satisfy + } + } + // Overflow scenarios + 3 => AddLiquidityArgs { + amount0_in: u64::MAX, + amount1_in: u64::MAX, + min_liquidity_out: 0, + }, + // Random chaos + _ => AddLiquidityArgs { + amount0_in: self.trident.random_from_range(1..=u64::MAX), + amount1_in: self.trident.random_from_range(1..=u64::MAX), + min_liquidity_out: self.trident.random_from_range(1..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_add_liquidity(&mut self) -> AddLiquidityInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + //user token0 account + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let user_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + //user token1 account + let user_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + // user lp token account + let user_lp_token_account = + self.trident + .get_associated_token_address(&pair_account.lp_mint, &user, &TOKEN_PROGRAM); + + AddLiquidityInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token0_vault, + token1_vault, + user_token0_account, + user_token1_account, + pair_account.token0, + pair_account.token1, + pair_account.lp_mint, + user_lp_token_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_add_liquidity_invariants( + &mut self, + args: &AddLiquidityArgs, + accounts: &AddLiquidityInstructionAccounts, + initial_pair: &Pair, + initial_user_lp: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after add liquidity"); + + // Calculate expected liquidity using the same formula as the program + // liquidity = min(amount0_in * total_supply / reserve0, amount1_in * total_supply / reserve1) + let liquidity0 = (args.amount0_in as u128) + .checked_mul(initial_pair.total_supply as u128) + .unwrap() + .checked_div(initial_pair.reserve0 as u128) + .unwrap(); + let liquidity1 = (args.amount1_in as u128) + .checked_mul(initial_pair.total_supply as u128) + .unwrap() + .checked_div(initial_pair.reserve1 as u128) + .unwrap(); + let expected_liquidity = liquidity0.min(liquidity1) as u64; + + // Check pair reserves increased by at least the deposited amounts + // Note: reserves can increase more than deposited amounts due to LP share of accrued interest + let min_expected_reserve0 = initial_pair.reserve0.checked_add(args.amount0_in).unwrap(); + let min_expected_reserve1 = initial_pair.reserve1.checked_add(args.amount1_in).unwrap(); + assert!( + final_pair.reserve0 >= min_expected_reserve0, + "Pair reserve0 should be at least initial + amount0_in (was {}, expected >= {})", + final_pair.reserve0, + min_expected_reserve0 + ); + assert!( + final_pair.reserve1 >= min_expected_reserve1, + "Pair reserve1 should be at least initial + amount1_in (was {}, expected >= {})", + final_pair.reserve1, + min_expected_reserve1 + ); + + // Check pair total_supply increased by liquidity minted + let expected_total_supply = initial_pair + .total_supply + .checked_add(expected_liquidity) + .unwrap(); + assert_eq!( + final_pair.total_supply, expected_total_supply, + "Pair total_supply should increase by liquidity minted" + ); + + // Check user received the expected liquidity tokens + let final_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let expected_user_lp = initial_user_lp.checked_add(expected_liquidity).unwrap(); + assert_eq!( + final_user_lp, expected_user_lp, + "User should receive expected liquidity tokens" + ); + + // Check user received at least min_liquidity_out + let lp_received = final_user_lp.saturating_sub(initial_user_lp); + assert!( + lp_received >= args.min_liquidity_out, + "User should receive at least min_liquidity_out" + ); + + // Critical accounting invariant: vault balances should be >= pair reserves + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + assert!( + vault0_balance >= final_pair.reserve0, + "Token0 vault balance must be >= pair reserve0" + ); + assert!( + vault1_balance >= final_pair.reserve1, + "Token1 vault balance must be >= pair reserve1" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/liquidity/init_pair.rs b/trident-tests/fuzz_liquidity_swaps/liquidity/init_pair.rs new file mode 100644 index 0000000..85916de --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/liquidity/init_pair.rs @@ -0,0 +1,361 @@ +use crate::{ + types::{ + omnipair::{ + self, InitializeInstruction, InitializeInstructionAccounts, InitializeInstructionData, + }, + InitializeAndBootstrapArgs, Pair, + }, + utils::{ + EVENT_AUTHORITY_ADDRESS, METADATA_SEED_PREFIX, + MPL_TOKEN_METADATA_ID, PAIR_SEED_PREFIX, TOKEN_PROGRAM, + }, + FuzzTest, +}; +use trident_fuzz::fuzzing::{solana_sdk::rent::Rent, *}; + +impl FuzzTest { + pub fn init_pair(&mut self) { + // Init pair and bootstrap + let data = self.get_data_init_pair(); + let accounts = self.get_accounts_init_pair(data.pair_nonce); + + let ix = InitializeInstruction::data(InitializeInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + // Store initial balances + let initial_deployer_token0 = self + .trident + .get_token_account(accounts.deployer_token0_account) + .expect("Deployer token0 account should exist") + .account + .amount; + let initial_deployer_token1 = self + .trident + .get_token_account(accounts.deployer_token1_account) + .expect("Deployer token1 account should exist") + .account + .amount; + let initial_deployer_sol = self.trident.get_account(&accounts.deployer).lamports(); + let initial_authority_wsol = self + .trident + .get_token_account(accounts.authority_wsol_account) + .expect("Authority WSOL token account should exist") + .account + .amount; + + let res = self.trident.process_transaction(&[ix], Some("Init Pair")); + + if res.is_success() { + // Initialization must NOT succeed when token0_mint == token1_mint + assert_ne!( + accounts.token0_mint, accounts.token1_mint, + "initialize succeeded with identical mints: token0_mint == token1_mint ({})", + accounts.token0_mint + ); + + self.store_accounts_init_pair(&accounts); + self.verify_init_pair_invariants( + &data, + &accounts, + initial_deployer_token0, + initial_deployer_token1, + initial_deployer_sol, + initial_authority_wsol, + ); + } + } + + fn get_data_init_pair(&mut self) -> InitializeAndBootstrapArgs { + let swap_fee_bps = self.trident.random_from_range(0..=10_000); // 0 to 100% + let half_life = self.trident.random_from_range(60..=12 * 60 * 60); // 1min to 12 hours + let fixed_cf_bps = self.trident.random_from_range(100..=10_000); // 1% to 100% + + self.trident + .record_histogram("INIT_PAIR_FIXED_CF_BPS", fixed_cf_bps as f64); + self.trident + .record_histogram("INIT_PAIR_HALF_LIFE", half_life as f64); + self.trident + .record_histogram("INIT_PAIR_SWAP_FEE_BPS", swap_fee_bps as f64); + + let fixed_cf_bps = if self.trident.random_from_range(0..=1) == 1 { + self.trident + .record_histogram("INIT_PAIR_FIXED_CF_BPS", fixed_cf_bps as f64); + Some(fixed_cf_bps) + } else { + None + }; + + let mut pair_nonce = [0u8; 16]; + self.trident.random_bytes(&mut pair_nonce); + + let amount0_in = self.trident.random_from_range(100..=100_000_000_000); + let amount1_in = self.trident.random_from_range(100..=100_000_000_000); + + self.trident + .record_histogram("INIT_PAIR_AMOUNT0_IN", amount0_in as f64); + self.trident + .record_histogram("INIT_PAIR_AMOUNT1_IN", amount1_in as f64); + + // Calculate expected liquidity using the same formula as the program + // liquidity = sqrt(amount0_in * amount1_in) - MIN_LIQUIDITY + let expected_liquidity = (amount0_in as u128) + .checked_mul(amount1_in as u128) + .map(|x| x.isqrt()) + .and_then(|x| x.checked_sub(1000)) // MIN_LIQUIDITY = 1000 + .unwrap_or(0) as u64; + + // Strategy: Test different scenarios + let min_liquidity_out = match self.trident.random_from_range(0..=100) { + // 70% - Normal case: small slippage tolerance (0-2%) + 0..=69 => { + let slippage_bps = self.trident.random_from_range(0..=200); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 15% - Tight slippage: very small tolerance (0-0.5%) + 70..=84 => { + let slippage_bps = self.trident.random_from_range(0..=50); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 10% - Large slippage: stress test (up to 50%) + 85..=94 => { + let slippage_bps = self.trident.random_from_range(0..=5000); + expected_liquidity.saturating_mul(10000 - slippage_bps) / 10000 + } + + // 5% - Edge cases (should sometimes fail) + _ => { + match self.trident.random_from_range(0..=4) { + 0 => 0, // No slippage protection + 1 => expected_liquidity + 1, // Impossible (expects more than possible) + 2 => u64::MAX, // Maximum value + 3 => expected_liquidity, // Exact match + _ => self.trident.random_from_range(1..=u64::MAX), // Random chaos + } + } + }; + + self.trident + .record_histogram("INIT_PAIR_MIN_LIQUIDITY_OUT", min_liquidity_out as f64); + + let lp_name = self.trident.random_string(10); + let lp_symbol = self.trident.random_string(10); + let lp_uri = self.trident.random_string(10); + + InitializeAndBootstrapArgs { + swap_fee_bps, + half_life, + fixed_cf_bps, + pair_nonce, + amount0_in, + amount1_in, + min_liquidity_out, + lp_name, + lp_symbol, + lp_uri: "https://".to_owned() + &lp_uri, + } + } + + fn get_accounts_init_pair(&mut self, pair_nonce: [u8; 16]) -> InitializeInstructionAccounts { + let token0_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token0 mint should exist"); + let mut token1_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token1 mint should exist"); + + while token0_mint == token1_mint { + token1_mint = self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token1 mint should exist"); + } + + // AKA deployer -> deploys pair + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + let pair = self + .trident + .find_program_address( + &[ + PAIR_SEED_PREFIX, + token0_mint.as_ref(), + token1_mint.as_ref(), + pair_nonce.as_ref(), + ], + &omnipair::program_id(), + ) + .0; + + // futarchy authority PDA + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // rate model + let rate_model = self.trident.random_keypair().pubkey(); + + // lp mint + + let lp_mint = self.trident.random_keypair().pubkey(); + let rent = self.trident.get_sysvar::(); + let account_custom = AccountSharedData::new(rent.minimum_balance(82), 82, &TOKEN_PROGRAM); + self.trident.set_account_custom(&lp_mint, &account_custom); + + // lp token metadata + let lp_token_metadata = self + .trident + .find_program_address( + &[ + METADATA_SEED_PREFIX, + MPL_TOKEN_METADATA_ID.as_ref(), + lp_mint.as_ref(), + ], + &MPL_TOKEN_METADATA_ID, + ) + .0; + + // deployer lp token account + let deployer_lp_token_account = + self.trident + .get_associated_token_address(&lp_mint, &user, &TOKEN_PROGRAM); + + // token0 vault + let token0_vault = + self.trident + .get_associated_token_address(&token0_mint, &pair, &TOKEN_PROGRAM); + + // token1 vault + let token1_vault = + self.trident + .get_associated_token_address(&token1_mint, &pair, &TOKEN_PROGRAM); + + // deployer token0 account + let deployer_token0_account = + self.trident + .get_associated_token_address(&token0_mint, &user, &TOKEN_PROGRAM); + + // deployer token1 account + let deployer_token1_account = + self.trident + .get_associated_token_address(&token1_mint, &user, &TOKEN_PROGRAM); + + InitializeInstructionAccounts::new( + user, + token0_mint, + token1_mint, + pair, + futarchy_authority, + rate_model, + lp_mint, + lp_token_metadata, + deployer_lp_token_account, + token0_vault, + token1_vault, + deployer_token0_account, + deployer_token1_account, + self.fuzz_accounts + .authority_wsol_account + .get(&mut self.trident).expect("Authority WSOL account should exist"), + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn store_accounts_init_pair(&mut self, accounts: &InitializeInstructionAccounts) { + self.fuzz_accounts.pair.insert_with_address(accounts.pair); + self.fuzz_accounts + .rate_model + .insert_with_address(accounts.rate_model); + self.fuzz_accounts + .lp_mint + .insert_with_address(accounts.lp_mint); + } + + fn verify_init_pair_invariants( + &mut self, + args: &InitializeAndBootstrapArgs, + accounts: &InitializeInstructionAccounts, + _initial_deployer_token0: u64, + _initial_deployer_token1: u64, + _initial_deployer_sol: u64, + initial_authority_wsol: u64, + ) { + const PAIR_CREATION_FEE_LAMPORTS: u64 = 200_000_000; // 0.2 SOL + const MIN_LIQUIDITY: u64 = 1000; + + // Calculate expected liquidity using the same formula as the program + // liquidity = sqrt(amount0_in * amount1_in) - MIN_LIQUIDITY + let expected_liquidity = (args.amount0_in as u128) + .checked_mul(args.amount1_in as u128) + .map(|x| x.isqrt()) + .and_then(|x| x.checked_sub(MIN_LIQUIDITY as u128)) + .unwrap_or(0) as u64; + + // Check pair state (fresh for each new pair) + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair account should exist"); + + // Check pair reserves match amounts deposited + assert_eq!( + pair_account.reserve0, args.amount0_in, + "Pair reserve0 should match amount0_in" + ); + assert_eq!( + pair_account.reserve1, args.amount1_in, + "Pair reserve1 should match amount1_in" + ); + + // Check pair total_supply includes MIN_LIQUIDITY (locked/burned permanently) + assert_eq!( + pair_account.total_supply, + expected_liquidity + MIN_LIQUIDITY, + "Pair total_supply should match total liquidity (including locked MIN_LIQUIDITY)" + ); + + // Check pair parameters + assert_eq!( + pair_account.swap_fee_bps, args.swap_fee_bps, + "Pair swap_fee_bps should match args" + ); + assert_eq!( + pair_account.half_life, args.half_life, + "Pair half_life should match args" + ); + assert_eq!( + pair_account.fixed_cf_bps, args.fixed_cf_bps, + "Pair fixed_cf_bps should match args" + ); + + // Check vault balances (vaults might be reused in fuzzing, so check >= pair reserves) + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + // Critical accounting invariant: vault balances must be at least pair reserves + assert!( + vault0_balance >= pair_account.reserve0, + "Token0 vault balance must be >= pair reserve0" + ); + assert!( + vault1_balance >= pair_account.reserve1, + "Token1 vault balance must be >= pair reserve1" + ); + + // Check authority WSOL balance increased by pair creation fee + let final_authority_wsol = self + .trident + .get_token_account(accounts.authority_wsol_account) + .expect("Authority WSOL account should exist") + .account + .amount; + assert!( + final_authority_wsol >= initial_authority_wsol + PAIR_CREATION_FEE_LAMPORTS, + "Authority WSOL balance should increase by at least pair creation fee" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/liquidity/mod.rs b/trident-tests/fuzz_liquidity_swaps/liquidity/mod.rs new file mode 100644 index 0000000..1774d85 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/liquidity/mod.rs @@ -0,0 +1,3 @@ +mod add_liquidity; +mod init_pair; +mod remove_liquidity; diff --git a/trident-tests/fuzz_liquidity_swaps/liquidity/remove_liquidity.rs b/trident-tests/fuzz_liquidity_swaps/liquidity/remove_liquidity.rs new file mode 100644 index 0000000..12206fc --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/liquidity/remove_liquidity.rs @@ -0,0 +1,463 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{ + self, RemoveLiquidityInstruction, RemoveLiquidityInstructionAccounts, + RemoveLiquidityInstructionData, + }, + Pair, RemoveLiquidityArgs, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn remove_liquidity(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_remove_liquidity(); + + // Check if user has any LP token account initialized + if self + .trident + .get_token_account(accounts.user_lp_token_account) + .is_err() + { + // No LP token account found, skip + return; + } + + let data = self.get_data_remove_liquidity(accounts.pair, accounts.user_lp_token_account); + + self.trident + .record_histogram("REMOVE_LIQUIDITY_LIQUIDITY_IN", data.liquidity_in as f64); + self.trident.record_histogram( + "REMOVE_LIQUIDITY_MIN_AMOUNT0_OUT", + data.min_amount0_out as f64, + ); + self.trident.record_histogram( + "REMOVE_LIQUIDITY_MIN_AMOUNT1_OUT", + data.min_amount1_out as f64, + ); + + // Store initial state + let initial_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist"); + let initial_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let initial_user_token0 = self + .trident + .get_token_account(accounts.user_token0_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + let initial_user_token1 = self + .trident + .get_token_account(accounts.user_token1_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = + RemoveLiquidityInstruction::data(RemoveLiquidityInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("Remove Liquidity")); + + if res.is_success() { + self.verify_remove_liquidity_invariants( + &data, + &accounts, + &initial_pair, + initial_user_lp, + initial_user_token0, + initial_user_token1, + ); + } + } + + fn get_data_remove_liquidity( + &mut self, + pair_pubkey: Pubkey, + user_lp_token_account: Pubkey, + ) -> RemoveLiquidityArgs { + let user_lp_balance = self + .trident + .get_token_account(user_lp_token_account) + .expect("User LP token account should exist") + .account + .amount; + + // Early return if user has no LP tokens to avoid empty range errors + if user_lp_balance == 0 { + return RemoveLiquidityArgs { + liquidity_in: 0, + min_amount0_out: 0, + min_amount1_out: 0, + }; + } + + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + let total_supply = pair.total_supply; + + // Strategy: Test different liquidity removal scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Proportional removal (remove a percentage of user's LP tokens) + 0..=39 => { + // Remove between 1% and 100% of user's LP tokens + let percentage = self.trident.random_from_range(1..=100); + let liquidity_in = user_lp_balance + .saturating_mul(percentage) + .checked_div(100) + .unwrap_or(0); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + // Set min amounts with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let slippage_multiplier = 10000u64.checked_sub(slippage_bps).unwrap_or(10000); + let min_amount0_out = amount0_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + let min_amount1_out = amount1_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out, + min_amount1_out, + } + } + + // 30% - Small amounts (dust testing) + 40..=69 => { + let liquidity_in = self + .trident + .random_from_range(1..=1000.min(user_lp_balance.max(1))); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out: amount0_out.saturating_sub(1), // Allow for rounding + min_amount1_out: amount1_out.saturating_sub(1), + } + } + + // 15% - Remove all liquidity + 70..=84 => { + let liquidity_in = user_lp_balance; + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap() + .checked_div(total_supply as u128) + .unwrap() as u64; + + // Allow for some slippage + let slippage_bps = self.trident.random_from_range(0..=500); + let slippage_multiplier = 10000u64.checked_sub(slippage_bps).unwrap_or(10000); + let min_amount0_out = amount0_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + let min_amount1_out = amount1_out + .saturating_mul(slippage_multiplier) + .checked_div(10000) + .unwrap_or(0); + + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out, + min_amount1_out, + } + } + + // 10% - Unrealistic expectations (should fail) + 85..=94 => { + let liquidity_in = self + .trident + .random_from_range(1..=user_lp_balance.max(1000)); + + // Calculate expected amounts + let amount0_out = (liquidity_in as u128) + .checked_mul(reserve0 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + let amount1_out = (liquidity_in as u128) + .checked_mul(reserve1 as u128) + .unwrap_or(0) + .checked_div(total_supply as u128) + .unwrap_or(0) as u64; + + // Set unrealistic min amounts (should fail with InsufficientOutput) + let multiplier = self.trident.random_from_range(2..=10); + RemoveLiquidityArgs { + liquidity_in, + min_amount0_out: amount0_out.saturating_mul(multiplier), + min_amount1_out: amount1_out.saturating_mul(multiplier), + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero liquidity (should fail with AmountZero) + 0 => RemoveLiquidityArgs { + liquidity_in: 0, + min_amount0_out: 0, + min_amount1_out: 0, + }, + // More liquidity than user has (should fail with InsufficientBalance) + 1 => RemoveLiquidityArgs { + liquidity_in: user_lp_balance.saturating_add(1_000_000), + min_amount0_out: 0, + min_amount1_out: 0, + }, + // Unrealistic min amounts + 2 => RemoveLiquidityArgs { + liquidity_in: self.trident.random_from_range(1..=1000), + min_amount0_out: u64::MAX, + min_amount1_out: u64::MAX, + }, + // Overflow scenarios + 3 => RemoveLiquidityArgs { + liquidity_in: u64::MAX, + min_amount0_out: 0, + min_amount1_out: 0, + }, + // Random chaos + _ => RemoveLiquidityArgs { + liquidity_in: self.trident.random_from_range(1..=u64::MAX), + min_amount0_out: self.trident.random_from_range(0..=u64::MAX), + min_amount1_out: self.trident.random_from_range(0..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_remove_liquidity(&mut self) -> RemoveLiquidityInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .expect("Pair should exist"); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // token0 vault + let token0_vault = self.trident.get_associated_token_address( + &pair_account.token0, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // token1 vault + let token1_vault = self.trident.get_associated_token_address( + &pair_account.token1, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // user (reuse existing user or create new one) + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + // user token0 account + let user_token0_account = + self.trident + .get_associated_token_address(&pair_account.token0, &user, &TOKEN_PROGRAM); + + // user token1 account + let user_token1_account = + self.trident + .get_associated_token_address(&pair_account.token1, &user, &TOKEN_PROGRAM); + + // user lp token account + let user_lp_token_account = + self.trident + .get_associated_token_address(&pair_account.lp_mint, &user, &TOKEN_PROGRAM); + + RemoveLiquidityInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token0_vault, + token1_vault, + user_token0_account, + user_token1_account, + pair_account.token0, + pair_account.token1, + pair_account.lp_mint, + user_lp_token_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + #[allow(clippy::too_many_arguments)] + fn verify_remove_liquidity_invariants( + &mut self, + args: &RemoveLiquidityArgs, + accounts: &RemoveLiquidityInstructionAccounts, + initial_pair: &Pair, + initial_user_lp: u64, + initial_user_token0: u64, + initial_user_token1: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after remove liquidity"); + + // Get actual user balances to determine what they received + let final_user_token0 = self + .trident + .get_token_account(accounts.user_token0_account) + .expect("User token0 account should exist") + .account + .amount; + let final_user_token1 = self + .trident + .get_token_account(accounts.user_token1_account) + .expect("User token1 account should exist") + .account + .amount; + + // Calculate actual amounts received by user + let actual_amount0_received = final_user_token0.checked_sub(initial_user_token0).unwrap(); + let actual_amount1_received = final_user_token1.checked_sub(initial_user_token1).unwrap(); + + // Check amounts received meet minimum requirements (slippage protection) + assert!( + actual_amount0_received >= args.min_amount0_out, + "User should receive at least min_amount0_out" + ); + assert!( + actual_amount1_received >= args.min_amount1_out, + "User should receive at least min_amount1_out" + ); + + // NOTE: We cannot directly check reserve deltas because pair.update() accrues interest + // during the transaction, which adds to reserves. This makes the net reserve decrease + // smaller than what the user received. Instead, we verify: + // 1. User received what they expected (checked above) + // 2. LP tokens were burned correctly (checked below) + // 3. Vault solvency is maintained (checked below) + + // Check pair total_supply decreased by liquidity burned + let expected_total_supply = initial_pair + .total_supply + .checked_sub(args.liquidity_in) + .unwrap(); + assert_eq!( + final_pair.total_supply, expected_total_supply, + "Pair total_supply should decrease by liquidity_in" + ); + + // Check user LP balance decreased by liquidity_in + let final_user_lp = self + .trident + .get_token_account(accounts.user_lp_token_account) + .expect("User LP account should exist") + .account + .amount; + let expected_user_lp = initial_user_lp.checked_sub(args.liquidity_in).unwrap(); + assert_eq!( + final_user_lp, expected_user_lp, + "User LP balance should decrease by liquidity_in" + ); + + // Critical accounting invariant: vault balances must be >= pair reserves + collateral + let vault0_balance = self + .trident + .get_token_account(accounts.token0_vault) + .expect("Token0 vault should exist") + .account + .amount; + let vault1_balance = self + .trident + .get_token_account(accounts.token1_vault) + .expect("Token1 vault should exist") + .account + .amount; + + // Vaults must have enough for reserves + collateral combined + let required_vault0 = final_pair + .reserve0 + .checked_add(final_pair.total_collateral0) + .expect("Overflow calculating required vault0"); + let required_vault1 = final_pair + .reserve1 + .checked_add(final_pair.total_collateral1) + .expect("Overflow calculating required vault1"); + + assert!( + vault0_balance >= required_vault0, + "Token0 vault balance must be >= reserve0 + total_collateral0. Vault: {}, Required: {}", + vault0_balance, + required_vault0 + ); + assert!( + vault1_balance >= required_vault1, + "Token1 vault balance must be >= reserve1 + total_collateral1. Vault: {}, Required: {}", + vault1_balance, + required_vault1 + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/spot/mod.rs b/trident-tests/fuzz_liquidity_swaps/spot/mod.rs new file mode 100644 index 0000000..04e62b0 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/spot/mod.rs @@ -0,0 +1 @@ +mod swap; diff --git a/trident-tests/fuzz_liquidity_swaps/spot/swap.rs b/trident-tests/fuzz_liquidity_swaps/spot/swap.rs new file mode 100644 index 0000000..7d622e2 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/spot/swap.rs @@ -0,0 +1,412 @@ +use trident_fuzz::fuzzing::{Pubkey}; + +use crate::{ + types::{ + omnipair::{self, SwapInstruction, SwapInstructionAccounts, SwapInstructionData}, + Pair, SwapArgs, + }, + utils::{EVENT_AUTHORITY_ADDRESS, TOKEN_PROGRAM}, + FuzzTest, +}; + +impl FuzzTest { + pub fn swap(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + // No pairs found, skip + return; + } + + let accounts = self.get_accounts_swap(); + let data = self.get_data_swap(accounts.pair, accounts.token_in_mint); + + // record histogram + self.trident + .record_histogram("SWAP_AMOUNT_IN", data.amount_in as f64); + self.trident + .record_histogram("SWAP_MIN_AMOUNT_OUT", data.min_amount_out as f64); + + // Store initial USER state only (pair state will be modified by update() during transaction) + let initial_user_token_in = self + .trident + .get_token_account(accounts.user_token_in_account) + .expect("User token in account should exist") + .account + .amount; + let initial_user_token_out = self + .trident + .get_token_account(accounts.user_token_out_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + let initial_authority_token_in = self + .trident + .get_token_account(accounts.authority_token_in_account) + .map(|acc| acc.account.amount) + .unwrap_or(0); + + let ix = SwapInstruction::data(SwapInstructionData::new(data.clone())) + .accounts(accounts.clone()) + .instruction(); + + let res = self.trident.process_transaction(&[ix], Some("Swap")); + + if res.is_success() { + self.verify_swap_invariants( + &data, + &accounts, + initial_user_token_in, + initial_user_token_out, + initial_authority_token_in, + ); + } + } + + fn get_data_swap(&mut self, pair_pubkey: Pubkey, token_in_mint: Pubkey) -> SwapArgs { + // First, get the current pair state + let pair = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .unwrap(); + + let reserve0 = pair.reserve0; + let reserve1 = pair.reserve1; + + // Determine which token we're swapping in and get the corresponding reserves + let (reserve_in, reserve_out) = if token_in_mint == pair.token0 { + (reserve0, reserve1) + } else { + (reserve1, reserve0) + }; + + // Early return if reserves are too low to avoid empty range errors + if reserve_in == 0 || reserve_out == 0 { + return SwapArgs { + amount_in: 100, + min_amount_out: 0, + }; + } + + // Strategy: Test different swap scenarios + match self.trident.random_from_range(0..=100) { + // 40% - Small to medium swaps (0.1% to 10% of reserve) + 0..=39 => { + let percentage = self.trident.random_from_range(1..=1000); // 0.1% to 100% + let amount_in = reserve_in.saturating_mul(percentage) / 10000; // 0.01% to 10% + let amount_in = amount_in.max(100); // Minimum 100 tokens + + // Calculate expected output using constant product formula + // amount_out = (amount_in * reserve_out) / (reserve_in + amount_in) + // With fees: amount_in_after_fee = amount_in * (10000 - fee_bps) / 10000 + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Set min_amount_out with small slippage tolerance (0-2%) + let slippage_bps = self.trident.random_from_range(0..=200); + let min_amount_out = expected_out.saturating_mul(10000 - slippage_bps) / 10000; + + SwapArgs { + amount_in, + min_amount_out, + } + } + + // 30% - Larger swaps (10% to 50% of reserve) + 40..=69 => { + let percentage = self.trident.random_from_range(1000..=5000); // 10% to 50% + let amount_in = reserve_in.saturating_mul(percentage) / 10000; + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Larger slippage tolerance for bigger swaps + let slippage_bps = self.trident.random_from_range(100..=1000); + let min_amount_out = expected_out.saturating_mul(10000 - slippage_bps) / 10000; + + SwapArgs { + amount_in, + min_amount_out, + } + } + + // 15% - Dust amounts (very small swaps) + 70..=84 => { + let amount_in = self.trident.random_from_range(1..=1000); + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + SwapArgs { + amount_in, + min_amount_out: expected_out.saturating_sub(1), // Allow for rounding + } + } + + // 10% - Unrealistic expectations (should fail) + 85..=94 => { + let amount_in = self.trident.random_from_range(100..=100_000_000); + + let swap_fee_bps = pair.swap_fee_bps; + let amount_in_after_fee = + (amount_in as u128).saturating_mul(10000 - swap_fee_bps as u128) / 10000; + + let expected_out = amount_in_after_fee + .saturating_mul(reserve_out as u128) + .checked_div((reserve_in as u128).saturating_add(amount_in_after_fee)) + .unwrap_or(0) as u64; + + // Set unrealistic min_amount_out (should fail with InsufficientOutput) + let multiplier = self.trident.random_from_range(2..=10); + SwapArgs { + amount_in, + min_amount_out: expected_out.saturating_mul(multiplier), + } + } + + // 5% - Edge cases (should fail) + _ => { + match self.trident.random_from_range(0..=4) { + // Zero amount (should fail with AmountZero) + 0 => SwapArgs { + amount_in: 0, + min_amount_out: 0, + }, + // Unrealistic min_amount_out + 1 => SwapArgs { + amount_in: self.trident.random_from_range(1..=1000), + min_amount_out: u64::MAX, + }, + // Swap entire reserve (should fail or cause extreme slippage) + 2 => SwapArgs { + amount_in: reserve_in, + min_amount_out: 0, + }, + // Overflow scenarios + 3 => SwapArgs { + amount_in: u64::MAX, + min_amount_out: 0, + }, + // Random chaos + _ => SwapArgs { + amount_in: self.trident.random_from_range(1..=u64::MAX), + min_amount_out: self.trident.random_from_range(0..=u64::MAX), + }, + } + } + } + } + + fn get_accounts_swap(&mut self) -> SwapInstructionAccounts { + let pair_pubkey = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let pair_account = self + .trident + .get_account_with_type::(&pair_pubkey, 8) + .unwrap(); + + let futarchy_authority_pubkey = + self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + // Randomly choose swap direction: token0 -> token1 or token1 -> token0 + let is_token0_in = self.trident.random_from_range(0..=1) == 0; + + let (token_in_mint, token_out_mint) = if is_token0_in { + (pair_account.token0, pair_account.token1) + } else { + (pair_account.token1, pair_account.token0) + }; + + // token vaults + let token_in_vault = + self.trident + .get_associated_token_address(&token_in_mint, &pair_pubkey, &TOKEN_PROGRAM); + + let token_out_vault = self.trident.get_associated_token_address( + &token_out_mint, + &pair_pubkey, + &TOKEN_PROGRAM, + ); + + // user + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + + // user token account + let user_token_in_account = + self.trident + .get_associated_token_address(&token_in_mint, &user, &TOKEN_PROGRAM); + + let user_token_out_account = + self.trident + .get_associated_token_address(&token_out_mint, &user, &TOKEN_PROGRAM); + + // authority token account (for fees) + let authority_token_in_account = self.trident.get_associated_token_address( + &token_in_mint, + &futarchy_authority_pubkey, + &TOKEN_PROGRAM, + ); + + if self + .trident + .get_token_account(authority_token_in_account) + .is_err() + { + self.trident.initialize_associated_token_account( + &user, + &token_in_mint, + &futarchy_authority_pubkey, + ); + } + + SwapInstructionAccounts::new( + pair_pubkey, + pair_account.rate_model, + futarchy_authority_pubkey, + token_in_vault, + token_out_vault, + user_token_in_account, + user_token_out_account, + token_in_mint, + token_out_mint, + authority_token_in_account, + user, + EVENT_AUTHORITY_ADDRESS, + omnipair::program_id(), + ) + } + + fn verify_swap_invariants( + &mut self, + args: &SwapArgs, + accounts: &SwapInstructionAccounts, + initial_user_token_in: u64, + initial_user_token_out: u64, + initial_authority_token_in: u64, + ) { + // Get final pair state + let final_pair = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .expect("Pair should exist after swap"); + + // Get final user balances + let final_user_token_in = self + .trident + .get_token_account(accounts.user_token_in_account) + .expect("User token in account should exist") + .account + .amount; + let final_user_token_out = self + .trident + .get_token_account(accounts.user_token_out_account) + .expect("User token out account should exist") + .account + .amount; + + // Calculate actual amounts + let actual_amount_in = initial_user_token_in + .checked_sub(final_user_token_in) + .unwrap(); + let actual_amount_out = final_user_token_out + .checked_sub(initial_user_token_out) + .unwrap(); + + // Verify user paid exactly amount_in + assert_eq!( + actual_amount_in, args.amount_in, + "User should pay exactly amount_in" + ); + + // Verify user received at least min_amount_out (slippage protection) + assert!( + actual_amount_out >= args.min_amount_out, + "User should receive at least min_amount_out" + ); + + // Verify constant product invariant: k should not decrease + // Note: k may increase slightly due to fees staying in the pool + let final_k = (final_pair.reserve0 as u128) + .checked_mul(final_pair.reserve1 as u128) + .unwrap(); + + // We can't check k against initial because update() modifies reserves with interest + // But we can verify k is reasonable and reserves are positive + assert!(final_k > 0, "Constant product k must be positive"); + assert!( + final_pair.reserve0 > 0, + "Reserve0 must be positive after swap" + ); + assert!( + final_pair.reserve1 > 0, + "Reserve1 must be positive after swap" + ); + + // Verify authority received futarchy fee (if any) + let final_authority_token_in = self + .trident + .get_token_account(accounts.authority_token_in_account) + .expect("Authority token in account should exist") + .account + .amount; + + let authority_fee_received = final_authority_token_in + .checked_sub(initial_authority_token_in) + .unwrap(); + + // Authority should receive some fee (unless swap is too small or fees are 0) + // We verify it's reasonable: futarchy fee = total_fee * revenue_share_bps / 10000 + // where total_fee = amount_in * swap_fee_bps / 10000 + if args.amount_in > 1000 && final_pair.swap_fee_bps > 0 { + assert!( + authority_fee_received > 0, + "Authority should receive futarchy fee for non-trivial swaps" + ); + } + + // Critical accounting invariant: vault balances should be >= pair reserves + let token_in_vault_balance = self + .trident + .get_token_account(accounts.token_in_vault) + .expect("Token in vault should exist") + .account + .amount; + let token_out_vault_balance = self + .trident + .get_token_account(accounts.token_out_vault) + .expect("Token out vault should exist") + .account + .amount; + + // Determine which reserve corresponds to which vault + let is_token0_in = accounts.token_in_mint == final_pair.token0; + let (reserve_in, reserve_out) = if is_token0_in { + (final_pair.reserve0, final_pair.reserve1) + } else { + (final_pair.reserve1, final_pair.reserve0) + }; + + assert!( + token_in_vault_balance >= reserve_in, + "Token in vault balance must be >= corresponding reserve" + ); + assert!( + token_out_vault_balance >= reserve_out, + "Token out vault balance must be >= corresponding reserve" + ); + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/test_fuzz.rs b/trident-tests/fuzz_liquidity_swaps/test_fuzz.rs new file mode 100644 index 0000000..1955e52b --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/test_fuzz.rs @@ -0,0 +1,182 @@ +use fuzz_accounts::*; +use trident_fuzz::fuzzing::*; + +use crate::{ + types::omnipair, + utils::{DEPLOYER_ADDRESS, FUTARCHY_AUTHORITY_SEED_PREFIX, TOKEN_PROGRAM, WSOL_MINT_ADDRESS}, +}; +mod futarchy; +mod fuzz_accounts; +mod lending; +mod liquidity; +mod spot; +mod types; +mod utils; +mod view; + +const USER_COUNT: usize = 20; +const TOKEN_MINT_COUNT: usize = 2; + +#[derive(FuzzTestMethods)] +struct FuzzTest { + /// Trident client for interacting with the Solana program + trident: Trident, + /// Storage for all account addresses used in fuzz testing + fuzz_accounts: AccountAddresses, +} + +#[flow_executor] +impl FuzzTest { + fn new() -> Self { + Self { + trident: Trident::default(), + fuzz_accounts: AccountAddresses::default(), + } + } + + #[init] + fn start(&mut self) { + self.setup_accounts(); + + self.init_futarchy(); + self.init_pair(); + self.add_liquidity(); + } + + // Liquidity flow: add more liquidity + #[flow(weight = 20)] + fn liquidity_flow_add(&mut self) { + self.add_liquidity(); + } + + // Liquidity flow: add liquidity -> remove liquidity + #[flow(weight = 25)] + fn liquidity_flow_remove(&mut self) { + self.add_liquidity(); + self.trident.forward_in_time(60 * 60); + self.remove_liquidity(); + } + + // Swap flow: perform swaps + #[flow(weight = 35)] + fn swap_flow(&mut self) { + self.swap(); + } + + // Combined flow: add liquidity -> swap -> remove liquidity + #[flow(weight = 15)] + fn combined_flow(&mut self) { + self.add_liquidity(); + self.trident.forward_in_time(60); + self.swap(); + self.trident.forward_in_time(60 * 60); + self.remove_liquidity(); + } + + // View data flow + #[flow(weight = 5)] + fn view_flow(&mut self) { + self.add_collateral(); + self.borrow(); + self.view_pair_data(); + self.view_user_position_data(); + } + + #[end] + fn end(&mut self) {} + + fn setup_accounts(&mut self) { + // Airdrop DEPLOYER_ADDRESS for transaction fees + self.trident.airdrop( + &DEPLOYER_ADDRESS, + LAMPORTS_PER_SOL + .checked_mul(5) + .expect("Airdrop amount overflow"), + ); + + let mut users = Vec::new(); + + // Pre-create user accounts + for _ in 0..USER_COUNT { + let user = self.fuzz_accounts.user.insert(&mut self.trident, None); + users.push(user); + self.trident.airdrop( + &user, + LAMPORTS_PER_SOL + .checked_mul(1000) + .expect("Airdrop amount overflow"), + ); + } + + // Pre-create futarchy authority account + let futarchy_authority = self.fuzz_accounts.futarchy_authority.insert( + &mut self.trident, + Some(PdaSeeds { + seeds: &[FUTARCHY_AUTHORITY_SEED_PREFIX], + program_id: omnipair::program_id(), + }), + ); + + // Pre-create authority wsol account + let authority_wsol_account = self.trident.get_associated_token_address( + &WSOL_MINT_ADDRESS, + &futarchy_authority, + &TOKEN_PROGRAM, + ); + let ix = self.trident.initialize_associated_token_account( + &DEPLOYER_ADDRESS, + &WSOL_MINT_ADDRESS, + &futarchy_authority, + ); + self.trident.process_transaction(&[ix], None); + + self.fuzz_accounts + .authority_wsol_account + .insert_with_address(authority_wsol_account); + + // Pre initialize some token mints + for _ in 0..TOKEN_MINT_COUNT { + let token_mint = self + .fuzz_accounts + .token_mint + .insert(&mut self.trident, None); + + let mint_authority = self.fuzz_accounts.user.get(&mut self.trident).expect("Mint authority should exist"); + let ix = self.trident.initialize_mint( + &mint_authority, + &token_mint, + 9, + &mint_authority, + None, + ); + let res = self.trident.process_transaction(&ix, None); + assert!(res.is_success()); + + // Generate token account for users + for user in users.iter() { + // User token account + let initialize_user_token_account_ix = self + .trident + .initialize_associated_token_account(user, &token_mint, user); + let user_token_account = + self.trident + .get_associated_token_address(&token_mint, user, &TOKEN_PROGRAM); + let mint_to_user_ix = self.trident.mint_to( + &user_token_account, + &token_mint, + &mint_authority, + 100_000_000_000_000_000, + ); + let res = self.trident.process_transaction( + &[initialize_user_token_account_ix, mint_to_user_ix], + None, + ); + assert!(res.is_success()); + } + } + } +} + +fn main() { + FuzzTest::fuzz(1000, 100); +} diff --git a/trident-tests/fuzz_liquidity_swaps/types.rs b/trident-tests/fuzz_liquidity_swaps/types.rs new file mode 100644 index 0000000..a6e1c16 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/types.rs @@ -0,0 +1,4739 @@ +//! # Trident Generated Types +//! +//! This file is automatically generated by Trident. +//! **DO NOT EDIT THIS FILE MANUALLY** + +#![allow(dead_code)] +#![allow(unused_imports)] + +use borsh::BorshDeserialize; +use borsh::BorshSerialize; +use trident_fuzz::fuzzing::*; + +// ============================================================================ +// PROGRAM MODULES +// ============================================================================ + +// ---------------------------------------------------------------------------- +// Program: faucet +// ---------------------------------------------------------------------------- +pub mod faucet { + use super::*; + + // ------------------------------------------------------------------------ + // Program ID + // ------------------------------------------------------------------------ + + /// Returns the program ID for faucet + pub fn program_id() -> Pubkey { + pubkey!("3Ckfn1LMByoDfVpcDPf7nouk5nQAUm51Zkdf1oprQTAK") + } + + // ------------------------------------------------------------------------ + // Instructions + // ------------------------------------------------------------------------ + + // .................................................................... + // Instruction: FaucetMint + // .................................................................... + + /// Main instruction struct for FaucetMint + pub struct FaucetMintInstruction { + pub accounts: FaucetMintInstructionAccountMetas, + pub data: FaucetMintInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for FaucetMint instruction + #[derive(Debug, Clone, Default)] + pub struct FaucetMintInstructionAccountMetas { + pub user: AccountMeta, + + pub faucet_authority: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub system_program: AccountMeta, + + pub token_program: AccountMeta, + + pub associated_token_program: AccountMeta, + } + + /// Account pubkeys for FaucetMint instruction + #[derive(Debug, Clone)] + pub struct FaucetMintInstructionAccounts { + pub user: Pubkey, + + pub faucet_authority: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + } + + impl FaucetMintInstructionAccounts { + pub fn new( + user: Pubkey, + + faucet_authority: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + ) -> Self { + Self { + user, + + faucet_authority, + + user_token0_account, + + user_token1_account, + + token0_mint, + + token1_mint, + } + } + } + + /// Instruction data for FaucetMint + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct FaucetMintInstructionData {} + + impl FaucetMintInstructionData { + pub fn new() -> Self { + Self {} + } + } + + /// Implementation for FaucetMintInstruction + impl FaucetMintInstruction { + fn discriminator() -> [u8; 8] { + [47u8, 229u8, 221u8, 88u8, 0u8, 56u8, 156u8, 38u8] + } + + pub fn data(data: FaucetMintInstructionData) -> Self { + Self { + accounts: FaucetMintInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: FaucetMintInstructionAccounts) -> Self { + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.faucet_authority = + AccountMeta::new_readonly(accounts.faucet_authority, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_mint = AccountMeta::new(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new(accounts.token1_mint, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.faucet_authority.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // ------------------------------------------------------------------------ + // Composite Accounts + // ------------------------------------------------------------------------ +} + +// ---------------------------------------------------------------------------- +// Program: omnipair +// ---------------------------------------------------------------------------- +pub mod omnipair { + use super::*; + + // ------------------------------------------------------------------------ + // Program ID + // ------------------------------------------------------------------------ + + /// Returns the program ID for omnipair + pub fn program_id() -> Pubkey { + pubkey!("Bd9Uhf5S8yzfop8cG9oqRs6jVcLtu8B4cb2gvRmtbNzk") + } + + // ------------------------------------------------------------------------ + // Instructions + // ------------------------------------------------------------------------ + + // .................................................................... + // Instruction: AddCollateral + // .................................................................... + + /// Main instruction struct for AddCollateral + pub struct AddCollateralInstruction { + pub accounts: AddCollateralInstructionAccountMetas, + pub data: AddCollateralInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for AddCollateral instruction + #[derive(Debug, Clone, Default)] + pub struct AddCollateralInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub user_position: AccountMeta, + + pub collateral_vault: AccountMeta, + + pub user_collateral_token_account: AccountMeta, + + pub collateral_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for AddCollateral instruction + #[derive(Debug, Clone)] + pub struct AddCollateralInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub user_position: Pubkey, + + pub collateral_vault: Pubkey, + + pub user_collateral_token_account: Pubkey, + + pub collateral_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl AddCollateralInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + user_position: Pubkey, + + collateral_vault: Pubkey, + + user_collateral_token_account: Pubkey, + + collateral_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + user_position, + + collateral_vault, + + user_collateral_token_account, + + collateral_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for AddCollateral + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct AddCollateralInstructionData { + pub args: AdjustPositionArgs, + } + + impl AddCollateralInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for AddCollateralInstruction + impl AddCollateralInstruction { + fn discriminator() -> [u8; 8] { + [127u8, 82u8, 121u8, 42u8, 161u8, 176u8, 249u8, 206u8] + } + + pub fn data(data: AddCollateralInstructionData) -> Self { + Self { + accounts: AddCollateralInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: AddCollateralInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.collateral_vault = AccountMeta::new(accounts.collateral_vault, false); + + self.accounts.user_collateral_token_account = + AccountMeta::new(accounts.user_collateral_token_account, false); + + self.accounts.collateral_token_mint = + AccountMeta::new_readonly(accounts.collateral_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.collateral_vault.clone()); + + metas.push(self.accounts.user_collateral_token_account.clone()); + + metas.push(self.accounts.collateral_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: AddLiquidity + // .................................................................... + + /// Main instruction struct for AddLiquidity + pub struct AddLiquidityInstruction { + pub accounts: AddLiquidityInstructionAccountMetas, + pub data: AddLiquidityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for AddLiquidity instruction + #[derive(Debug, Clone, Default)] + pub struct AddLiquidityInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_vault_mint: AccountMeta, + + pub token1_vault_mint: AccountMeta, + + pub lp_mint: AccountMeta, + + pub user_lp_token_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for AddLiquidity instruction + #[derive(Debug, Clone)] + pub struct AddLiquidityInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_vault_mint: Pubkey, + + pub token1_vault_mint: Pubkey, + + pub lp_mint: Pubkey, + + pub user_lp_token_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl AddLiquidityInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_vault_mint: Pubkey, + + token1_vault_mint: Pubkey, + + lp_mint: Pubkey, + + user_lp_token_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + user_token0_account, + + user_token1_account, + + token0_vault_mint, + + token1_vault_mint, + + lp_mint, + + user_lp_token_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for AddLiquidity + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct AddLiquidityInstructionData { + pub args: AddLiquidityArgs, + } + + impl AddLiquidityInstructionData { + pub fn new(args: AddLiquidityArgs) -> Self { + Self { args } + } + } + + /// Implementation for AddLiquidityInstruction + impl AddLiquidityInstruction { + fn discriminator() -> [u8; 8] { + [181u8, 157u8, 89u8, 67u8, 143u8, 182u8, 52u8, 72u8] + } + + pub fn data(data: AddLiquidityInstructionData) -> Self { + Self { + accounts: AddLiquidityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: AddLiquidityInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_vault_mint = + AccountMeta::new_readonly(accounts.token0_vault_mint, false); + + self.accounts.token1_vault_mint = + AccountMeta::new_readonly(accounts.token1_vault_mint, false); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.user_lp_token_account = + AccountMeta::new(accounts.user_lp_token_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_vault_mint.clone()); + + metas.push(self.accounts.token1_vault_mint.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.user_lp_token_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Borrow + // .................................................................... + + /// Main instruction struct for Borrow + pub struct BorrowInstruction { + pub accounts: BorrowInstructionAccountMetas, + pub data: BorrowInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Borrow instruction + #[derive(Debug, Clone, Default)] + pub struct BorrowInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Borrow instruction + #[derive(Debug, Clone)] + pub struct BorrowInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl BorrowInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Borrow + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct BorrowInstructionData { + pub args: AdjustPositionArgs, + } + + impl BorrowInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for BorrowInstruction + impl BorrowInstruction { + fn discriminator() -> [u8; 8] { + [228u8, 253u8, 131u8, 202u8, 207u8, 116u8, 89u8, 18u8] + } + + pub fn data(data: BorrowInstructionData) -> Self { + Self { + accounts: BorrowInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: BorrowInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ClaimProtocolFees + // .................................................................... + + /// Main instruction struct for ClaimProtocolFees + pub struct ClaimProtocolFeesInstruction { + pub accounts: ClaimProtocolFeesInstructionAccountMetas, + pub data: ClaimProtocolFeesInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ClaimProtocolFees instruction + #[derive(Debug, Clone, Default)] + pub struct ClaimProtocolFeesInstructionAccountMetas { + pub caller: AccountMeta, + + pub pair: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub authority_token0_account: AccountMeta, + + pub authority_token1_account: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + } + + /// Account pubkeys for ClaimProtocolFees instruction + #[derive(Debug, Clone)] + pub struct ClaimProtocolFeesInstructionAccounts { + pub caller: Pubkey, + + pub pair: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub authority_token0_account: Pubkey, + + pub authority_token1_account: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + } + + impl ClaimProtocolFeesInstructionAccounts { + pub fn new( + caller: Pubkey, + + pair: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + authority_token0_account: Pubkey, + + authority_token1_account: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + ) -> Self { + Self { + caller, + + pair, + + futarchy_authority, + + token0_vault, + + token1_vault, + + authority_token0_account, + + authority_token1_account, + + token0_mint, + + token1_mint, + } + } + } + + /// Instruction data for ClaimProtocolFees + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ClaimProtocolFeesInstructionData { + pub args: ClaimProtocolFeesArgs, + } + + impl ClaimProtocolFeesInstructionData { + pub fn new(args: ClaimProtocolFeesArgs) -> Self { + Self { args } + } + } + + /// Implementation for ClaimProtocolFeesInstruction + impl ClaimProtocolFeesInstruction { + fn discriminator() -> [u8; 8] { + [34u8, 142u8, 219u8, 112u8, 109u8, 54u8, 133u8, 23u8] + } + + pub fn data(data: ClaimProtocolFeesInstructionData) -> Self { + Self { + accounts: ClaimProtocolFeesInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ClaimProtocolFeesInstructionAccounts) -> Self { + self.accounts.caller = AccountMeta::new(accounts.caller, true); + + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.authority_token0_account = + AccountMeta::new(accounts.authority_token0_account, false); + + self.accounts.authority_token1_account = + AccountMeta::new(accounts.authority_token1_account, false); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.caller.clone()); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.authority_token0_account.clone()); + + metas.push(self.accounts.authority_token1_account.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: DistributeTokens + // .................................................................... + + /// Main instruction struct for DistributeTokens + pub struct DistributeTokensInstruction { + pub accounts: DistributeTokensInstructionAccountMetas, + pub data: DistributeTokensInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for DistributeTokens instruction + #[derive(Debug, Clone, Default)] + pub struct DistributeTokensInstructionAccountMetas { + pub futarchy_authority: AccountMeta, + + pub source_mint: AccountMeta, + + pub source_token_account: AccountMeta, + + pub futarchy_treasury_token_account: AccountMeta, + + pub buybacks_vault_token_account: AccountMeta, + + pub team_treasury_token_account: AccountMeta, + + pub token_program: AccountMeta, + } + + /// Account pubkeys for DistributeTokens instruction + #[derive(Debug, Clone)] + pub struct DistributeTokensInstructionAccounts { + pub futarchy_authority: Pubkey, + + pub source_mint: Pubkey, + + pub source_token_account: Pubkey, + + pub futarchy_treasury_token_account: Pubkey, + + pub buybacks_vault_token_account: Pubkey, + + pub team_treasury_token_account: Pubkey, + } + + impl DistributeTokensInstructionAccounts { + pub fn new( + futarchy_authority: Pubkey, + + source_mint: Pubkey, + + source_token_account: Pubkey, + + futarchy_treasury_token_account: Pubkey, + + buybacks_vault_token_account: Pubkey, + + team_treasury_token_account: Pubkey, + ) -> Self { + Self { + futarchy_authority, + + source_mint, + + source_token_account, + + futarchy_treasury_token_account, + + buybacks_vault_token_account, + + team_treasury_token_account, + } + } + } + + /// Instruction data for DistributeTokens + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct DistributeTokensInstructionData { + pub args: DistributeTokensArgs, + } + + impl DistributeTokensInstructionData { + pub fn new(args: DistributeTokensArgs) -> Self { + Self { args } + } + } + + /// Implementation for DistributeTokensInstruction + impl DistributeTokensInstruction { + fn discriminator() -> [u8; 8] { + [105u8, 69u8, 130u8, 52u8, 196u8, 28u8, 176u8, 120u8] + } + + pub fn data(data: DistributeTokensInstructionData) -> Self { + Self { + accounts: DistributeTokensInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: DistributeTokensInstructionAccounts) -> Self { + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.source_mint = AccountMeta::new_readonly(accounts.source_mint, false); + + self.accounts.source_token_account = + AccountMeta::new(accounts.source_token_account, false); + + self.accounts.futarchy_treasury_token_account = + AccountMeta::new(accounts.futarchy_treasury_token_account, false); + + self.accounts.buybacks_vault_token_account = + AccountMeta::new(accounts.buybacks_vault_token_account, false); + + self.accounts.team_treasury_token_account = + AccountMeta::new(accounts.team_treasury_token_account, false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.source_mint.clone()); + + metas.push(self.accounts.source_token_account.clone()); + + metas.push(self.accounts.futarchy_treasury_token_account.clone()); + + metas.push(self.accounts.buybacks_vault_token_account.clone()); + + metas.push(self.accounts.team_treasury_token_account.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Flashloan + // .................................................................... + + /// Main instruction struct for Flashloan + pub struct FlashloanInstruction { + pub accounts: FlashloanInstructionAccountMetas, + pub data: FlashloanInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Flashloan instruction + #[derive(Debug, Clone, Default)] + pub struct FlashloanInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub receiver_token0_account: AccountMeta, + + pub receiver_token1_account: AccountMeta, + + pub receiver_program: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Flashloan instruction + #[derive(Debug, Clone)] + pub struct FlashloanInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + + pub receiver_token0_account: Pubkey, + + pub receiver_token1_account: Pubkey, + + pub receiver_program: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl FlashloanInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + + receiver_token0_account: Pubkey, + + receiver_token1_account: Pubkey, + + receiver_program: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + token0_mint, + + token1_mint, + + receiver_token0_account, + + receiver_token1_account, + + receiver_program, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Flashloan + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct FlashloanInstructionData { + pub args: FlashloanArgs, + } + + impl FlashloanInstructionData { + pub fn new(args: FlashloanArgs) -> Self { + Self { args } + } + } + + /// Implementation for FlashloanInstruction + impl FlashloanInstruction { + fn discriminator() -> [u8; 8] { + [105u8, 33u8, 1u8, 3u8, 42u8, 158u8, 246u8, 67u8] + } + + pub fn data(data: FlashloanInstructionData) -> Self { + Self { + accounts: FlashloanInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: FlashloanInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.receiver_token0_account = + AccountMeta::new(accounts.receiver_token0_account, false); + + self.accounts.receiver_token1_account = + AccountMeta::new(accounts.receiver_token1_account, false); + + self.accounts.receiver_program = + AccountMeta::new_readonly(accounts.receiver_program, false); + + self.accounts.user = AccountMeta::new_readonly(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.receiver_token0_account.clone()); + + metas.push(self.accounts.receiver_token1_account.clone()); + + metas.push(self.accounts.receiver_program.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: InitFutarchyAuthority + // .................................................................... + + /// Main instruction struct for InitFutarchyAuthority + pub struct InitFutarchyAuthorityInstruction { + pub accounts: InitFutarchyAuthorityInstructionAccountMetas, + pub data: InitFutarchyAuthorityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for InitFutarchyAuthority instruction + #[derive(Debug, Clone, Default)] + pub struct InitFutarchyAuthorityInstructionAccountMetas { + pub deployer: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub system_program: AccountMeta, + } + + /// Account pubkeys for InitFutarchyAuthority instruction + #[derive(Debug, Clone)] + pub struct InitFutarchyAuthorityInstructionAccounts { + pub futarchy_authority: Pubkey, + } + + impl InitFutarchyAuthorityInstructionAccounts { + pub fn new(futarchy_authority: Pubkey) -> Self { + Self { futarchy_authority } + } + } + + /// Instruction data for InitFutarchyAuthority + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct InitFutarchyAuthorityInstructionData { + pub args: InitFutarchyAuthorityArgs, + } + + impl InitFutarchyAuthorityInstructionData { + pub fn new(args: InitFutarchyAuthorityArgs) -> Self { + Self { args } + } + } + + /// Implementation for InitFutarchyAuthorityInstruction + impl InitFutarchyAuthorityInstruction { + fn discriminator() -> [u8; 8] { + [133u8, 110u8, 154u8, 29u8, 240u8, 206u8, 71u8, 100u8] + } + + pub fn data(data: InitFutarchyAuthorityInstructionData) -> Self { + Self { + accounts: InitFutarchyAuthorityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: InitFutarchyAuthorityInstructionAccounts) -> Self { + self.accounts.deployer = AccountMeta::new( + pubkey!("C7GKpfqQyBoFR6S13DECwBjdi7aCQKbbeKjXm4Jt5Hds"), + true, + ); + + self.accounts.futarchy_authority = AccountMeta::new(accounts.futarchy_authority, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.deployer.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Initialize + // .................................................................... + + /// Main instruction struct for Initialize + pub struct InitializeInstruction { + pub accounts: InitializeInstructionAccountMetas, + pub data: InitializeInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Initialize instruction + #[derive(Debug, Clone, Default)] + pub struct InitializeInstructionAccountMetas { + pub deployer: AccountMeta, + + pub token0_mint: AccountMeta, + + pub token1_mint: AccountMeta, + + pub pair: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub rate_model: AccountMeta, + + pub lp_mint: AccountMeta, + + pub lp_token_metadata: AccountMeta, + + pub deployer_lp_token_account: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub deployer_token0_account: AccountMeta, + + pub deployer_token1_account: AccountMeta, + + pub authority_wsol_account: AccountMeta, + + pub system_program: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub token_metadata_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub rent: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Initialize instruction + #[derive(Debug, Clone)] + pub struct InitializeInstructionAccounts { + pub deployer: Pubkey, + + pub token0_mint: Pubkey, + + pub token1_mint: Pubkey, + + pub pair: Pubkey, + + pub futarchy_authority: Pubkey, + + pub rate_model: Pubkey, + + pub lp_mint: Pubkey, + + pub lp_token_metadata: Pubkey, + + pub deployer_lp_token_account: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub deployer_token0_account: Pubkey, + + pub deployer_token1_account: Pubkey, + + pub authority_wsol_account: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl InitializeInstructionAccounts { + pub fn new( + deployer: Pubkey, + + token0_mint: Pubkey, + + token1_mint: Pubkey, + + pair: Pubkey, + + futarchy_authority: Pubkey, + + rate_model: Pubkey, + + lp_mint: Pubkey, + + lp_token_metadata: Pubkey, + + deployer_lp_token_account: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + deployer_token0_account: Pubkey, + + deployer_token1_account: Pubkey, + + authority_wsol_account: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + deployer, + + token0_mint, + + token1_mint, + + pair, + + futarchy_authority, + + rate_model, + + lp_mint, + + lp_token_metadata, + + deployer_lp_token_account, + + token0_vault, + + token1_vault, + + deployer_token0_account, + + deployer_token1_account, + + authority_wsol_account, + + event_authority, + + program, + } + } + } + + /// Instruction data for Initialize + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct InitializeInstructionData { + pub args: InitializeAndBootstrapArgs, + } + + impl InitializeInstructionData { + pub fn new(args: InitializeAndBootstrapArgs) -> Self { + Self { args } + } + } + + /// Implementation for InitializeInstruction + impl InitializeInstruction { + fn discriminator() -> [u8; 8] { + [175u8, 175u8, 109u8, 31u8, 13u8, 152u8, 155u8, 237u8] + } + + pub fn data(data: InitializeInstructionData) -> Self { + Self { + accounts: InitializeInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: InitializeInstructionAccounts) -> Self { + self.accounts.deployer = AccountMeta::new(accounts.deployer, true); + + self.accounts.token0_mint = AccountMeta::new_readonly(accounts.token0_mint, false); + + self.accounts.token1_mint = AccountMeta::new_readonly(accounts.token1_mint, false); + + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, true); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.lp_token_metadata = AccountMeta::new(accounts.lp_token_metadata, false); + + self.accounts.deployer_lp_token_account = + AccountMeta::new(accounts.deployer_lp_token_account, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.deployer_token0_account = + AccountMeta::new(accounts.deployer_token0_account, false); + + self.accounts.deployer_token1_account = + AccountMeta::new(accounts.deployer_token1_account, false); + + self.accounts.authority_wsol_account = + AccountMeta::new(accounts.authority_wsol_account, false); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.token_metadata_program = AccountMeta::new_readonly( + pubkey!("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.rent = AccountMeta::new_readonly( + pubkey!("SysvarRent111111111111111111111111111111111"), + false, + ); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.deployer.clone()); + + metas.push(self.accounts.token0_mint.clone()); + + metas.push(self.accounts.token1_mint.clone()); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.lp_token_metadata.clone()); + + metas.push(self.accounts.deployer_lp_token_account.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.deployer_token0_account.clone()); + + metas.push(self.accounts.deployer_token1_account.clone()); + + metas.push(self.accounts.authority_wsol_account.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.token_metadata_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.rent.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Liquidate + // .................................................................... + + /// Main instruction struct for Liquidate + pub struct LiquidateInstruction { + pub accounts: LiquidateInstructionAccountMetas, + pub data: LiquidateInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Liquidate instruction + #[derive(Debug, Clone, Default)] + pub struct LiquidateInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub collateral_vault: AccountMeta, + + pub caller_token_account: AccountMeta, + + pub collateral_token_mint: AccountMeta, + + pub position_owner: AccountMeta, + + pub payer: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Liquidate instruction + #[derive(Debug, Clone)] + pub struct LiquidateInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub collateral_vault: Pubkey, + + pub caller_token_account: Pubkey, + + pub collateral_token_mint: Pubkey, + + pub position_owner: Pubkey, + + pub payer: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl LiquidateInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + collateral_vault: Pubkey, + + caller_token_account: Pubkey, + + collateral_token_mint: Pubkey, + + position_owner: Pubkey, + + payer: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + collateral_vault, + + caller_token_account, + + collateral_token_mint, + + position_owner, + + payer, + + event_authority, + + program, + } + } + } + + /// Instruction data for Liquidate + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct LiquidateInstructionData {} + + impl LiquidateInstructionData { + pub fn new() -> Self { + Self {} + } + } + + /// Implementation for LiquidateInstruction + impl LiquidateInstruction { + fn discriminator() -> [u8; 8] { + [223u8, 179u8, 226u8, 125u8, 48u8, 46u8, 39u8, 74u8] + } + + pub fn data(data: LiquidateInstructionData) -> Self { + Self { + accounts: LiquidateInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: LiquidateInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.collateral_vault = AccountMeta::new(accounts.collateral_vault, false); + + self.accounts.caller_token_account = + AccountMeta::new(accounts.caller_token_account, false); + + self.accounts.collateral_token_mint = + AccountMeta::new_readonly(accounts.collateral_token_mint, false); + + self.accounts.position_owner = + AccountMeta::new_readonly(accounts.position_owner, false); + + self.accounts.payer = AccountMeta::new(accounts.payer, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.collateral_vault.clone()); + + metas.push(self.accounts.caller_token_account.clone()); + + metas.push(self.accounts.collateral_token_mint.clone()); + + metas.push(self.accounts.position_owner.clone()); + + metas.push(self.accounts.payer.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: RemoveCollateral + // .................................................................... + + /// Main instruction struct for RemoveCollateral + pub struct RemoveCollateralInstruction { + pub accounts: RemoveCollateralInstructionAccountMetas, + pub data: RemoveCollateralInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for RemoveCollateral instruction + #[derive(Debug, Clone, Default)] + pub struct RemoveCollateralInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for RemoveCollateral instruction + #[derive(Debug, Clone)] + pub struct RemoveCollateralInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RemoveCollateralInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for RemoveCollateral + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RemoveCollateralInstructionData { + pub args: AdjustPositionArgs, + } + + impl RemoveCollateralInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for RemoveCollateralInstruction + impl RemoveCollateralInstruction { + fn discriminator() -> [u8; 8] { + [86u8, 222u8, 130u8, 86u8, 92u8, 20u8, 72u8, 65u8] + } + + pub fn data(data: RemoveCollateralInstructionData) -> Self { + Self { + accounts: RemoveCollateralInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RemoveCollateralInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: RemoveLiquidity + // .................................................................... + + /// Main instruction struct for RemoveLiquidity + pub struct RemoveLiquidityInstruction { + pub accounts: RemoveLiquidityInstructionAccountMetas, + pub data: RemoveLiquidityInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for RemoveLiquidity instruction + #[derive(Debug, Clone, Default)] + pub struct RemoveLiquidityInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token0_vault: AccountMeta, + + pub token1_vault: AccountMeta, + + pub user_token0_account: AccountMeta, + + pub user_token1_account: AccountMeta, + + pub token0_vault_mint: AccountMeta, + + pub token1_vault_mint: AccountMeta, + + pub lp_mint: AccountMeta, + + pub user_lp_token_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for RemoveLiquidity instruction + #[derive(Debug, Clone)] + pub struct RemoveLiquidityInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token0_vault: Pubkey, + + pub token1_vault: Pubkey, + + pub user_token0_account: Pubkey, + + pub user_token1_account: Pubkey, + + pub token0_vault_mint: Pubkey, + + pub token1_vault_mint: Pubkey, + + pub lp_mint: Pubkey, + + pub user_lp_token_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RemoveLiquidityInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token0_vault: Pubkey, + + token1_vault: Pubkey, + + user_token0_account: Pubkey, + + user_token1_account: Pubkey, + + token0_vault_mint: Pubkey, + + token1_vault_mint: Pubkey, + + lp_mint: Pubkey, + + user_lp_token_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token0_vault, + + token1_vault, + + user_token0_account, + + user_token1_account, + + token0_vault_mint, + + token1_vault_mint, + + lp_mint, + + user_lp_token_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for RemoveLiquidity + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RemoveLiquidityInstructionData { + pub args: RemoveLiquidityArgs, + } + + impl RemoveLiquidityInstructionData { + pub fn new(args: RemoveLiquidityArgs) -> Self { + Self { args } + } + } + + /// Implementation for RemoveLiquidityInstruction + impl RemoveLiquidityInstruction { + fn discriminator() -> [u8; 8] { + [80u8, 85u8, 209u8, 72u8, 24u8, 206u8, 177u8, 108u8] + } + + pub fn data(data: RemoveLiquidityInstructionData) -> Self { + Self { + accounts: RemoveLiquidityInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RemoveLiquidityInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token0_vault = AccountMeta::new(accounts.token0_vault, false); + + self.accounts.token1_vault = AccountMeta::new(accounts.token1_vault, false); + + self.accounts.user_token0_account = + AccountMeta::new(accounts.user_token0_account, false); + + self.accounts.user_token1_account = + AccountMeta::new(accounts.user_token1_account, false); + + self.accounts.token0_vault_mint = + AccountMeta::new_readonly(accounts.token0_vault_mint, false); + + self.accounts.token1_vault_mint = + AccountMeta::new_readonly(accounts.token1_vault_mint, false); + + self.accounts.lp_mint = AccountMeta::new(accounts.lp_mint, false); + + self.accounts.user_lp_token_account = + AccountMeta::new(accounts.user_lp_token_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token0_vault.clone()); + + metas.push(self.accounts.token1_vault.clone()); + + metas.push(self.accounts.user_token0_account.clone()); + + metas.push(self.accounts.user_token1_account.clone()); + + metas.push(self.accounts.token0_vault_mint.clone()); + + metas.push(self.accounts.token1_vault_mint.clone()); + + metas.push(self.accounts.lp_mint.clone()); + + metas.push(self.accounts.user_lp_token_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Repay + // .................................................................... + + /// Main instruction struct for Repay + pub struct RepayInstruction { + pub accounts: RepayInstructionAccountMetas, + pub data: RepayInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Repay instruction + #[derive(Debug, Clone, Default)] + pub struct RepayInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_vault: AccountMeta, + + pub user_token_account: AccountMeta, + + pub vault_token_mint: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Repay instruction + #[derive(Debug, Clone)] + pub struct RepayInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_vault: Pubkey, + + pub user_token_account: Pubkey, + + pub vault_token_mint: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl RepayInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_vault: Pubkey, + + user_token_account: Pubkey, + + vault_token_mint: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + + token_vault, + + user_token_account, + + vault_token_mint, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Repay + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct RepayInstructionData { + pub args: AdjustPositionArgs, + } + + impl RepayInstructionData { + pub fn new(args: AdjustPositionArgs) -> Self { + Self { args } + } + } + + /// Implementation for RepayInstruction + impl RepayInstruction { + fn discriminator() -> [u8; 8] { + [234u8, 103u8, 67u8, 82u8, 208u8, 234u8, 219u8, 166u8] + } + + pub fn data(data: RepayInstructionData) -> Self { + Self { + accounts: RepayInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: RepayInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_vault = AccountMeta::new(accounts.token_vault, false); + + self.accounts.user_token_account = AccountMeta::new(accounts.user_token_account, false); + + self.accounts.vault_token_mint = + AccountMeta::new_readonly(accounts.vault_token_mint, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_vault.clone()); + + metas.push(self.accounts.user_token_account.clone()); + + metas.push(self.accounts.vault_token_mint.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: Swap + // .................................................................... + + /// Main instruction struct for Swap + pub struct SwapInstruction { + pub accounts: SwapInstructionAccountMetas, + pub data: SwapInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for Swap instruction + #[derive(Debug, Clone, Default)] + pub struct SwapInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + + pub token_in_vault: AccountMeta, + + pub token_out_vault: AccountMeta, + + pub user_token_in_account: AccountMeta, + + pub user_token_out_account: AccountMeta, + + pub token_in_mint: AccountMeta, + + pub token_out_mint: AccountMeta, + + pub authority_token_in_account: AccountMeta, + + pub user: AccountMeta, + + pub token_program: AccountMeta, + + pub token_2022_program: AccountMeta, + + pub associated_token_program: AccountMeta, + + pub system_program: AccountMeta, + + pub event_authority: AccountMeta, + + pub program: AccountMeta, + } + + /// Account pubkeys for Swap instruction + #[derive(Debug, Clone)] + pub struct SwapInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + + pub token_in_vault: Pubkey, + + pub token_out_vault: Pubkey, + + pub user_token_in_account: Pubkey, + + pub user_token_out_account: Pubkey, + + pub token_in_mint: Pubkey, + + pub token_out_mint: Pubkey, + + pub authority_token_in_account: Pubkey, + + pub user: Pubkey, + + pub event_authority: Pubkey, + + pub program: Pubkey, + } + + impl SwapInstructionAccounts { + pub fn new( + pair: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + + token_in_vault: Pubkey, + + token_out_vault: Pubkey, + + user_token_in_account: Pubkey, + + user_token_out_account: Pubkey, + + token_in_mint: Pubkey, + + token_out_mint: Pubkey, + + authority_token_in_account: Pubkey, + + user: Pubkey, + + event_authority: Pubkey, + + program: Pubkey, + ) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + + token_in_vault, + + token_out_vault, + + user_token_in_account, + + user_token_out_account, + + token_in_mint, + + token_out_mint, + + authority_token_in_account, + + user, + + event_authority, + + program, + } + } + } + + /// Instruction data for Swap + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct SwapInstructionData { + pub args: SwapArgs, + } + + impl SwapInstructionData { + pub fn new(args: SwapArgs) -> Self { + Self { args } + } + } + + /// Implementation for SwapInstruction + impl SwapInstruction { + fn discriminator() -> [u8; 8] { + [248u8, 198u8, 158u8, 145u8, 225u8, 117u8, 135u8, 200u8] + } + + pub fn data(data: SwapInstructionData) -> Self { + Self { + accounts: SwapInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: SwapInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self.accounts.token_in_vault = AccountMeta::new(accounts.token_in_vault, false); + + self.accounts.token_out_vault = AccountMeta::new(accounts.token_out_vault, false); + + self.accounts.user_token_in_account = + AccountMeta::new(accounts.user_token_in_account, false); + + self.accounts.user_token_out_account = + AccountMeta::new(accounts.user_token_out_account, false); + + self.accounts.token_in_mint = AccountMeta::new_readonly(accounts.token_in_mint, false); + + self.accounts.token_out_mint = + AccountMeta::new_readonly(accounts.token_out_mint, false); + + self.accounts.authority_token_in_account = + AccountMeta::new(accounts.authority_token_in_account, false); + + self.accounts.user = AccountMeta::new(accounts.user, true); + + self.accounts.token_program = AccountMeta::new_readonly( + pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), + false, + ); + + self.accounts.token_2022_program = AccountMeta::new_readonly( + pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"), + false, + ); + + self.accounts.associated_token_program = AccountMeta::new_readonly( + pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"), + false, + ); + + self.accounts.system_program = + AccountMeta::new_readonly(pubkey!("11111111111111111111111111111111"), false); + + self.accounts.event_authority = + AccountMeta::new_readonly(accounts.event_authority, false); + + self.accounts.program = AccountMeta::new_readonly(accounts.program, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.push(self.accounts.token_in_vault.clone()); + + metas.push(self.accounts.token_out_vault.clone()); + + metas.push(self.accounts.user_token_in_account.clone()); + + metas.push(self.accounts.user_token_out_account.clone()); + + metas.push(self.accounts.token_in_mint.clone()); + + metas.push(self.accounts.token_out_mint.clone()); + + metas.push(self.accounts.authority_token_in_account.clone()); + + metas.push(self.accounts.user.clone()); + + metas.push(self.accounts.token_program.clone()); + + metas.push(self.accounts.token_2022_program.clone()); + + metas.push(self.accounts.associated_token_program.clone()); + + metas.push(self.accounts.system_program.clone()); + + metas.push(self.accounts.event_authority.clone()); + + metas.push(self.accounts.program.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ViewPairData + // .................................................................... + + /// Main instruction struct for ViewPairData + pub struct ViewPairDataInstruction { + pub accounts: ViewPairDataInstructionAccountMetas, + pub data: ViewPairDataInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ViewPairData instruction + #[derive(Debug, Clone, Default)] + pub struct ViewPairDataInstructionAccountMetas { + pub pair: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + } + + /// Account pubkeys for ViewPairData instruction + #[derive(Debug, Clone)] + pub struct ViewPairDataInstructionAccounts { + pub pair: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + } + + impl ViewPairDataInstructionAccounts { + pub fn new(pair: Pubkey, rate_model: Pubkey, futarchy_authority: Pubkey) -> Self { + Self { + pair, + + rate_model, + + futarchy_authority, + } + } + } + + /// Instruction data for ViewPairData + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ViewPairDataInstructionData { + pub getter: PairViewKind, + + pub args: EmitValueArgs, + } + + impl ViewPairDataInstructionData { + pub fn new(getter: PairViewKind, args: EmitValueArgs) -> Self { + Self { getter, args } + } + } + + /// Implementation for ViewPairDataInstruction + impl ViewPairDataInstruction { + fn discriminator() -> [u8; 8] { + [30u8, 231u8, 169u8, 73u8, 19u8, 161u8, 44u8, 252u8] + } + + pub fn data(data: ViewPairDataInstructionData) -> Self { + Self { + accounts: ViewPairDataInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ViewPairDataInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.rate_model = AccountMeta::new_readonly(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // .................................................................... + // Instruction: ViewUserPositionData + // .................................................................... + + /// Main instruction struct for ViewUserPositionData + pub struct ViewUserPositionDataInstruction { + pub accounts: ViewUserPositionDataInstructionAccountMetas, + pub data: ViewUserPositionDataInstructionData, + pub remaining_accounts: Vec, + } + + /// Account metadata for ViewUserPositionData instruction + #[derive(Debug, Clone, Default)] + pub struct ViewUserPositionDataInstructionAccountMetas { + pub pair: AccountMeta, + + pub user_position: AccountMeta, + + pub rate_model: AccountMeta, + + pub futarchy_authority: AccountMeta, + } + + /// Account pubkeys for ViewUserPositionData instruction + #[derive(Debug, Clone)] + pub struct ViewUserPositionDataInstructionAccounts { + pub pair: Pubkey, + + pub user_position: Pubkey, + + pub rate_model: Pubkey, + + pub futarchy_authority: Pubkey, + } + + impl ViewUserPositionDataInstructionAccounts { + pub fn new( + pair: Pubkey, + + user_position: Pubkey, + + rate_model: Pubkey, + + futarchy_authority: Pubkey, + ) -> Self { + Self { + pair, + + user_position, + + rate_model, + + futarchy_authority, + } + } + } + + /// Instruction data for ViewUserPositionData + #[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] + pub struct ViewUserPositionDataInstructionData { + pub getter: UserPositionViewKind, + } + + impl ViewUserPositionDataInstructionData { + pub fn new(getter: UserPositionViewKind) -> Self { + Self { getter } + } + } + + /// Implementation for ViewUserPositionDataInstruction + impl ViewUserPositionDataInstruction { + fn discriminator() -> [u8; 8] { + [203u8, 218u8, 173u8, 213u8, 43u8, 31u8, 211u8, 152u8] + } + + pub fn data(data: ViewUserPositionDataInstructionData) -> Self { + Self { + accounts: ViewUserPositionDataInstructionAccountMetas::default(), + data, + remaining_accounts: Vec::new(), + } + } + + pub fn accounts(mut self, accounts: ViewUserPositionDataInstructionAccounts) -> Self { + self.accounts.pair = AccountMeta::new(accounts.pair, false); + + self.accounts.user_position = AccountMeta::new(accounts.user_position, false); + + self.accounts.rate_model = AccountMeta::new_readonly(accounts.rate_model, false); + + self.accounts.futarchy_authority = + AccountMeta::new_readonly(accounts.futarchy_authority, false); + + self + } + + pub fn remaining_accounts(mut self, accounts: Vec) -> Self { + self.remaining_accounts = accounts; + self + } + + fn to_account_metas(&self) -> Vec { + let mut metas = Vec::new(); + + metas.push(self.accounts.pair.clone()); + + metas.push(self.accounts.user_position.clone()); + + metas.push(self.accounts.rate_model.clone()); + + metas.push(self.accounts.futarchy_authority.clone()); + + metas.extend(self.remaining_accounts.clone()); + metas + } + + pub fn instruction(&self) -> Instruction { + let mut buffer: Vec = Vec::new(); + + buffer.extend_from_slice(&Self::discriminator()); + + self.data.serialize(&mut buffer).unwrap(); + + Instruction::new_with_bytes(program_id(), &buffer, self.to_account_metas()) + } + } + + // ------------------------------------------------------------------------ + // Composite Accounts + // ------------------------------------------------------------------------ +} + +// ============================================================================ +// CUSTOM TYPES +// ============================================================================ + +/// Custom struct: AddLiquidityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AddLiquidityArgs { + pub amount0_in: u64, + + pub amount1_in: u64, + + pub min_liquidity_out: u64, +} + +impl AddLiquidityArgs { + pub fn new(amount0_in: u64, amount1_in: u64, min_liquidity_out: u64) -> Self { + Self { + amount0_in, + + amount1_in, + + min_liquidity_out, + } + } +} + +/// Custom struct: AdjustCollateralEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustCollateralEvent { + pub amount0: i64, + + pub amount1: i64, + + pub metadata: EventMetadata, +} + +impl AdjustCollateralEvent { + pub fn new(amount0: i64, amount1: i64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + metadata, + } + } +} + +/// Custom struct: AdjustDebtEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustDebtEvent { + pub amount0: i64, + + pub amount1: i64, + + pub metadata: EventMetadata, +} + +impl AdjustDebtEvent { + pub fn new(amount0: i64, amount1: i64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + metadata, + } + } +} + +/// Custom struct: AdjustLiquidityEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustLiquidityEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl AdjustLiquidityEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: AdjustPositionArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct AdjustPositionArgs { + pub amount: u64, +} + +impl AdjustPositionArgs { + pub fn new(amount: u64) -> Self { + Self { amount } + } +} + +/// Custom struct: BurnEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct BurnEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl BurnEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: ClaimProtocolFeesArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct ClaimProtocolFeesArgs { + pub amount0: u64, + + pub amount1: u64, +} + +impl ClaimProtocolFeesArgs { + pub fn new(amount0: u64, amount1: u64) -> Self { + Self { amount0, amount1 } + } +} + +/// Custom struct: DistributeTokensArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct DistributeTokensArgs {} + +impl DistributeTokensArgs { + pub fn new() -> Self { + Self {} + } +} + +/// Custom struct: EmitValueArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct EmitValueArgs { + pub debt_amount: Option, + + pub collateral_amount: Option, + + pub collateral_token: Option, +} + +impl EmitValueArgs { + pub fn new( + debt_amount: Option, + + collateral_amount: Option, + + collateral_token: Option, + ) -> Self { + Self { + debt_amount, + + collateral_amount, + + collateral_token, + } + } +} + +/// Custom struct: EventMetadata +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct EventMetadata { + pub signer: Pubkey, + + pub pair: Pubkey, + + pub timestamp: i64, +} + +impl EventMetadata { + pub fn new(signer: Pubkey, pair: Pubkey, timestamp: i64) -> Self { + Self { + signer, + + pair, + + timestamp, + } + } +} + +/// Custom struct: FlashloanArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FlashloanArgs { + pub amount0: u64, + + pub amount1: u64, + + pub data: Vec, +} + +impl FlashloanArgs { + pub fn new(amount0: u64, amount1: u64, data: Vec) -> Self { + Self { + amount0, + + amount1, + + data, + } + } +} + +/// Custom struct: FlashloanEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FlashloanEvent { + pub amount0: u64, + + pub amount1: u64, + + pub fee0: u64, + + pub fee1: u64, + + pub receiver: Pubkey, + + pub metadata: EventMetadata, +} + +impl FlashloanEvent { + pub fn new( + amount0: u64, + + amount1: u64, + + fee0: u64, + + fee1: u64, + + receiver: Pubkey, + + metadata: EventMetadata, + ) -> Self { + Self { + amount0, + + amount1, + + fee0, + + fee1, + + receiver, + + metadata, + } + } +} + +/// Custom struct: FutarchyAuthority +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct FutarchyAuthority { + pub version: u8, + + pub authority: Pubkey, + + pub recipients: RevenueRecipients, + + pub revenue_share: RevenueShare, + + pub revenue_distribution: RevenueDistribution, + + pub bump: u8, +} + +impl FutarchyAuthority { + pub fn new( + version: u8, + + authority: Pubkey, + + recipients: RevenueRecipients, + + revenue_share: RevenueShare, + + revenue_distribution: RevenueDistribution, + + bump: u8, + ) -> Self { + Self { + version, + + authority, + + recipients, + + revenue_share, + + revenue_distribution, + + bump, + } + } +} + +/// Custom struct: InitFutarchyAuthorityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct InitFutarchyAuthorityArgs { + pub authority: Pubkey, + + pub swap_bps: u16, + + pub interest_bps: u16, + + pub futarchy_treasury: Pubkey, + + pub futarchy_treasury_bps: u16, + + pub buybacks_vault: Pubkey, + + pub buybacks_vault_bps: u16, + + pub team_treasury: Pubkey, + + pub team_treasury_bps: u16, +} + +impl InitFutarchyAuthorityArgs { + pub fn new( + authority: Pubkey, + + swap_bps: u16, + + interest_bps: u16, + + futarchy_treasury: Pubkey, + + futarchy_treasury_bps: u16, + + buybacks_vault: Pubkey, + + buybacks_vault_bps: u16, + + team_treasury: Pubkey, + + team_treasury_bps: u16, + ) -> Self { + Self { + authority, + + swap_bps, + + interest_bps, + + futarchy_treasury, + + futarchy_treasury_bps, + + buybacks_vault, + + buybacks_vault_bps, + + team_treasury, + + team_treasury_bps, + } + } +} + +/// Custom struct: InitializeAndBootstrapArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct InitializeAndBootstrapArgs { + pub swap_fee_bps: u16, + + pub half_life: u64, + + pub fixed_cf_bps: Option, + + pub pair_nonce: [u8; 16], + + pub amount0_in: u64, + + pub amount1_in: u64, + + pub min_liquidity_out: u64, + + pub lp_name: String, + + pub lp_symbol: String, + + pub lp_uri: String, +} + +impl InitializeAndBootstrapArgs { + pub fn new( + swap_fee_bps: u16, + + half_life: u64, + + fixed_cf_bps: Option, + + pair_nonce: [u8; 16], + + amount0_in: u64, + + amount1_in: u64, + + min_liquidity_out: u64, + + lp_name: String, + + lp_symbol: String, + + lp_uri: String, + ) -> Self { + Self { + swap_fee_bps, + + half_life, + + fixed_cf_bps, + + pair_nonce, + + amount0_in, + + amount1_in, + + min_liquidity_out, + + lp_name, + + lp_symbol, + + lp_uri, + } + } +} + +/// Custom struct: MintEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct MintEvent { + pub amount0: u64, + + pub amount1: u64, + + pub liquidity: u64, + + pub metadata: EventMetadata, +} + +impl MintEvent { + pub fn new(amount0: u64, amount1: u64, liquidity: u64, metadata: EventMetadata) -> Self { + Self { + amount0, + + amount1, + + liquidity, + + metadata, + } + } +} + +/// Custom struct: Pair +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct Pair { + pub token0: Pubkey, + + pub token1: Pubkey, + + pub lp_mint: Pubkey, + + pub token0_decimals: u8, + + pub token1_decimals: u8, + + pub rate_model: Pubkey, + + pub swap_fee_bps: u16, + + pub half_life: u64, + + pub fixed_cf_bps: Option, + + pub reserve0: u64, + + pub reserve1: u64, + + pub protocol_revenue_reserve0: u64, + + pub protocol_revenue_reserve1: u64, + + pub last_price0_ema: u64, + + pub last_price1_ema: u64, + + pub last_update: i64, + + pub last_rate0: u64, + + pub last_rate1: u64, + + pub total_debt0: u64, + + pub total_debt1: u64, + + pub total_debt0_shares: u64, + + pub total_debt1_shares: u64, + + pub total_supply: u64, + + pub total_collateral0: u64, + + pub total_collateral1: u64, + + pub pair_nonce: [u8; 16], + + pub bump: u8, +} + +impl Pair { + pub fn new( + token0: Pubkey, + + token1: Pubkey, + + lp_mint: Pubkey, + + token0_decimals: u8, + + token1_decimals: u8, + + rate_model: Pubkey, + + swap_fee_bps: u16, + + half_life: u64, + + fixed_cf_bps: Option, + + reserve0: u64, + + reserve1: u64, + + protocol_revenue_reserve0: u64, + + protocol_revenue_reserve1: u64, + + last_price0_ema: u64, + + last_price1_ema: u64, + + last_update: i64, + + last_rate0: u64, + + last_rate1: u64, + + total_debt0: u64, + + total_debt1: u64, + + total_debt0_shares: u64, + + total_debt1_shares: u64, + + total_supply: u64, + + total_collateral0: u64, + + total_collateral1: u64, + + pair_nonce: [u8; 16], + + bump: u8, + ) -> Self { + Self { + token0, + + token1, + + lp_mint, + + token0_decimals, + + token1_decimals, + + rate_model, + + swap_fee_bps, + + half_life, + + fixed_cf_bps, + + reserve0, + + reserve1, + + protocol_revenue_reserve0, + + protocol_revenue_reserve1, + + last_price0_ema, + + last_price1_ema, + + last_update, + + last_rate0, + + last_rate1, + + total_debt0, + + total_debt1, + + total_debt0_shares, + + total_debt1_shares, + + total_supply, + + total_collateral0, + + total_collateral1, + + pair_nonce, + + bump, + } + } +} + +/// Custom struct: PairCreatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct PairCreatedEvent { + pub token0: Pubkey, + + pub token1: Pubkey, + + pub metadata: EventMetadata, +} + +impl PairCreatedEvent { + pub fn new(token0: Pubkey, token1: Pubkey, metadata: EventMetadata) -> Self { + Self { + token0, + + token1, + + metadata, + } + } +} + +/// Custom enum: PairViewKind +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone, PartialEq)] +pub enum PairViewKind { + EmaPrice0Nad, + + EmaPrice1Nad, + + SpotPrice0Nad, + + SpotPrice1Nad, + + K, + + GetRates, + + GetBorrowLimitAndCfBpsForCollateral, +} + +/// Custom struct: RateModel +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RateModel { + pub exp_rate: u64, + + pub target_util_start: u64, + + pub target_util_end: u64, +} + +impl RateModel { + pub fn new(exp_rate: u64, target_util_start: u64, target_util_end: u64) -> Self { + Self { + exp_rate, + + target_util_start, + + target_util_end, + } + } +} + +/// Custom struct: RemoveLiquidityArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RemoveLiquidityArgs { + pub liquidity_in: u64, + + pub min_amount0_out: u64, + + pub min_amount1_out: u64, +} + +impl RemoveLiquidityArgs { + pub fn new(liquidity_in: u64, min_amount0_out: u64, min_amount1_out: u64) -> Self { + Self { + liquidity_in, + + min_amount0_out, + + min_amount1_out, + } + } +} + +/// Custom struct: RevenueDistribution +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueDistribution { + pub futarchy_treasury_bps: u16, + + pub buybacks_vault_bps: u16, + + pub team_treasury_bps: u16, +} + +impl RevenueDistribution { + pub fn new( + futarchy_treasury_bps: u16, + + buybacks_vault_bps: u16, + + team_treasury_bps: u16, + ) -> Self { + Self { + futarchy_treasury_bps, + + buybacks_vault_bps, + + team_treasury_bps, + } + } +} + +/// Custom struct: RevenueRecipients +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueRecipients { + pub futarchy_treasury: Pubkey, + + pub buybacks_vault: Pubkey, + + pub team_treasury: Pubkey, +} + +impl RevenueRecipients { + pub fn new(futarchy_treasury: Pubkey, buybacks_vault: Pubkey, team_treasury: Pubkey) -> Self { + Self { + futarchy_treasury, + + buybacks_vault, + + team_treasury, + } + } +} + +/// Custom struct: RevenueShare +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct RevenueShare { + pub swap_bps: u16, + + pub interest_bps: u16, +} + +impl RevenueShare { + pub fn new(swap_bps: u16, interest_bps: u16) -> Self { + Self { + swap_bps, + + interest_bps, + } + } +} + +/// Custom struct: SwapArgs +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct SwapArgs { + pub amount_in: u64, + + pub min_amount_out: u64, +} + +impl SwapArgs { + pub fn new(amount_in: u64, min_amount_out: u64) -> Self { + Self { + amount_in, + + min_amount_out, + } + } +} + +/// Custom struct: SwapEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct SwapEvent { + pub reserve0: u64, + + pub reserve1: u64, + + pub is_token0_in: bool, + + pub amount_in: u64, + + pub amount_out: u64, + + pub amount_in_after_fee: u64, + + pub metadata: EventMetadata, +} + +impl SwapEvent { + pub fn new( + reserve0: u64, + + reserve1: u64, + + is_token0_in: bool, + + amount_in: u64, + + amount_out: u64, + + amount_in_after_fee: u64, + + metadata: EventMetadata, + ) -> Self { + Self { + reserve0, + + reserve1, + + is_token0_in, + + amount_in, + + amount_out, + + amount_in_after_fee, + + metadata, + } + } +} + +/// Custom struct: UpdatePairEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UpdatePairEvent { + pub price0_ema: u64, + + pub price1_ema: u64, + + pub rate0: u64, + + pub rate1: u64, + + pub accrued_interest0: u128, + + pub accrued_interest1: u128, + + pub protocol_revenue_reserve0: u64, + + pub protocol_revenue_reserve1: u64, + + pub reserve0_after_interest: u64, + + pub reserve1_after_interest: u64, + + pub metadata: EventMetadata, +} + +impl UpdatePairEvent { + pub fn new( + price0_ema: u64, + + price1_ema: u64, + + rate0: u64, + + rate1: u64, + + accrued_interest0: u128, + + accrued_interest1: u128, + + protocol_revenue_reserve0: u64, + + protocol_revenue_reserve1: u64, + + reserve0_after_interest: u64, + + reserve1_after_interest: u64, + + metadata: EventMetadata, + ) -> Self { + Self { + price0_ema, + + price1_ema, + + rate0, + + rate1, + + accrued_interest0, + + accrued_interest1, + + protocol_revenue_reserve0, + + protocol_revenue_reserve1, + + reserve0_after_interest, + + reserve1_after_interest, + + metadata, + } + } +} + +/// Custom struct: UserPosition +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPosition { + pub owner: Pubkey, + + pub pair: Pubkey, + + pub collateral0_applied_min_cf_bps: u16, + + pub collateral1_applied_min_cf_bps: u16, + + pub collateral0: u64, + + pub collateral1: u64, + + pub debt0_shares: u64, + + pub debt1_shares: u64, + + pub bump: u8, +} + +impl UserPosition { + pub fn new( + owner: Pubkey, + + pair: Pubkey, + + collateral0_applied_min_cf_bps: u16, + + collateral1_applied_min_cf_bps: u16, + + collateral0: u64, + + collateral1: u64, + + debt0_shares: u64, + + debt1_shares: u64, + + bump: u8, + ) -> Self { + Self { + owner, + + pair, + + collateral0_applied_min_cf_bps, + + collateral1_applied_min_cf_bps, + + collateral0, + + collateral1, + + debt0_shares, + + debt1_shares, + + bump, + } + } +} + +/// Custom struct: UserPositionCreatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionCreatedEvent { + pub position: Pubkey, + + pub metadata: EventMetadata, +} + +impl UserPositionCreatedEvent { + pub fn new(position: Pubkey, metadata: EventMetadata) -> Self { + Self { position, metadata } + } +} + +/// Custom struct: UserPositionLiquidatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionLiquidatedEvent { + pub position: Pubkey, + + pub liquidator: Pubkey, + + pub collateral0_liquidated: u64, + + pub collateral1_liquidated: u64, + + pub debt0_liquidated: u64, + + pub debt1_liquidated: u64, + + pub collateral_price: u64, + + pub shortfall: u128, + + pub liquidation_bonus_applied: u64, + + pub k0: u128, + + pub k1: u128, + + pub metadata: EventMetadata, +} + +impl UserPositionLiquidatedEvent { + pub fn new( + position: Pubkey, + + liquidator: Pubkey, + + collateral0_liquidated: u64, + + collateral1_liquidated: u64, + + debt0_liquidated: u64, + + debt1_liquidated: u64, + + collateral_price: u64, + + shortfall: u128, + + liquidation_bonus_applied: u64, + + k0: u128, + + k1: u128, + + metadata: EventMetadata, + ) -> Self { + Self { + position, + + liquidator, + + collateral0_liquidated, + + collateral1_liquidated, + + debt0_liquidated, + + debt1_liquidated, + + collateral_price, + + shortfall, + + liquidation_bonus_applied, + + k0, + + k1, + + metadata, + } + } +} + +/// Custom struct: UserPositionUpdatedEvent +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub struct UserPositionUpdatedEvent { + pub position: Pubkey, + + pub collateral0: u64, + + pub collateral1: u64, + + pub debt0_shares: u64, + + pub debt1_shares: u64, + + pub collateral0_applied_min_cf_bps: u16, + + pub collateral1_applied_min_cf_bps: u16, + + pub metadata: EventMetadata, +} + +impl UserPositionUpdatedEvent { + pub fn new( + position: Pubkey, + + collateral0: u64, + + collateral1: u64, + + debt0_shares: u64, + + debt1_shares: u64, + + collateral0_applied_min_cf_bps: u16, + + collateral1_applied_min_cf_bps: u16, + + metadata: EventMetadata, + ) -> Self { + Self { + position, + + collateral0, + + collateral1, + + debt0_shares, + + debt1_shares, + + collateral0_applied_min_cf_bps, + + collateral1_applied_min_cf_bps, + + metadata, + } + } +} + +/// Custom enum: UserPositionViewKind +#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)] +pub enum UserPositionViewKind { + UserBorrowingPower, + + UserAppliedCollateralFactorBps, + + UserLiquidationCollateralFactorBps, + + UserDebtUtilizationBps, + + UserLiquidationPrice, + + UserDebtWithInterest, +} + +// ============================================================================ +// END OF GENERATED FILE +// ============================================================================ diff --git a/trident-tests/fuzz_liquidity_swaps/utils.rs b/trident-tests/fuzz_liquidity_swaps/utils.rs new file mode 100644 index 0000000..0f9f8e3 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/utils.rs @@ -0,0 +1,22 @@ +use trident_fuzz::fuzzing::{pubkey, Pubkey}; + +// DEPLOYER +pub const DEPLOYER_ADDRESS: Pubkey = pubkey!("C7GKpfqQyBoFR6S13DECwBjdi7aCQKbbeKjXm4Jt5Hds"); + +// PROGRAMS +pub const TOKEN_PROGRAM: Pubkey = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); +pub const MPL_TOKEN_METADATA_ID: Pubkey = pubkey!("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"); +pub const FLASHLOAN_CALLBACK_RECEIVER_PROGRAM: Pubkey = + pubkey!("GmtswKBDrFZ9DfUfP7jbPFvbtuG7AJcX73SvoKWGxJbu"); + +// MINTS +pub const WSOL_MINT_ADDRESS: Pubkey = pubkey!("So11111111111111111111111111111111111111112"); + +// SEEDS +pub const FUTARCHY_AUTHORITY_SEED_PREFIX: &[u8] = b"futarchy_authority"; +pub const METADATA_SEED_PREFIX: &[u8] = b"metadata"; +pub const PAIR_SEED_PREFIX: &[u8] = b"gamm_pair"; +pub const POSITION_SEED_PREFIX: &[u8] = b"gamm_position"; + +// EVENT AUTHORITY +pub const EVENT_AUTHORITY_ADDRESS: Pubkey = pubkey!("fY27dnRLq4XVAKNRAY7nATiicimnY6mwLHq3V65uoP2"); diff --git a/trident-tests/fuzz_liquidity_swaps/view/mod.rs b/trident-tests/fuzz_liquidity_swaps/view/mod.rs new file mode 100644 index 0000000..57ab2fb --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/view/mod.rs @@ -0,0 +1,2 @@ +mod view_pair_data; +mod view_user_position_data; diff --git a/trident-tests/fuzz_liquidity_swaps/view/view_pair_data.rs b/trident-tests/fuzz_liquidity_swaps/view/view_pair_data.rs new file mode 100644 index 0000000..a9fb613 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/view/view_pair_data.rs @@ -0,0 +1,88 @@ +use trident_fuzz::fuzzing::*; + +use crate::{ + types::{ + omnipair::{ + ViewPairDataInstruction, ViewPairDataInstructionAccounts, ViewPairDataInstructionData, + }, + EmitValueArgs, Pair, PairViewKind, + }, + FuzzTest, +}; + +impl FuzzTest { + pub fn view_pair_data(&mut self) { + if self.fuzz_accounts.pair.is_empty() { + return; + } + let data = self.get_data_view_pair(); + let accounts = self.get_accounts_view_pair(); + + let ix = ViewPairDataInstruction::data(data) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("View Pair Data")); + + assert!(res.is_success()); + + // INVARIANT 1: the passed rate_model must match the pair's configured rate_model + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .unwrap(); + assert_eq!( + pair_account.rate_model, accounts.rate_model, + "ViewPairData accepted a mismatched rate_model for the given pair" + ); + } + + fn get_data_view_pair(&mut self) -> ViewPairDataInstructionData { + let getter = PairViewKind::random(&mut self.trident); + let collateral_amount = if getter == PairViewKind::GetBorrowLimitAndCfBpsForCollateral { + Some(self.trident.random_from_range(100..=100_000_000)) + } else { + None + }; + // Any token mint for collateral token + let collateral_token = if getter == PairViewKind::GetBorrowLimitAndCfBpsForCollateral { + Some(self.fuzz_accounts.token_mint.get(&mut self.trident).expect("Token mint should exist")) + } else { + None + }; + + ViewPairDataInstructionData::new( + getter, + EmitValueArgs::new( + None, // unused + collateral_amount, + collateral_token, + ), + ) + } + + fn get_accounts_view_pair(&mut self) -> ViewPairDataInstructionAccounts { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let rate_model = self.fuzz_accounts.rate_model.get(&mut self.trident).expect("Rate model should exist"); + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + ViewPairDataInstructionAccounts::new(pair, rate_model, futarchy_authority) + } +} + +impl PairViewKind { + pub fn random(trident: &mut Trident) -> Self { + match trident.random_from_range(0..=6) { + 0 => Self::EmaPrice0Nad, + 1 => Self::EmaPrice1Nad, + 2 => Self::SpotPrice0Nad, + 3 => Self::SpotPrice1Nad, + 4 => Self::K, + 5 => Self::GetRates, + 6 => Self::GetBorrowLimitAndCfBpsForCollateral, + _ => unreachable!(), + } + } +} diff --git a/trident-tests/fuzz_liquidity_swaps/view/view_user_position_data.rs b/trident-tests/fuzz_liquidity_swaps/view/view_user_position_data.rs new file mode 100644 index 0000000..2fad729 --- /dev/null +++ b/trident-tests/fuzz_liquidity_swaps/view/view_user_position_data.rs @@ -0,0 +1,95 @@ +use trident_fuzz::trident::Trident; + +use crate::{ + types::{ + omnipair::{ + self, ViewUserPositionDataInstruction, ViewUserPositionDataInstructionAccounts, + ViewUserPositionDataInstructionData, + }, + Pair, UserPosition, UserPositionViewKind, + }, + utils::POSITION_SEED_PREFIX, + FuzzTest, +}; + +impl FuzzTest { + pub fn view_user_position_data(&mut self) { + if let Some(accounts) = self.get_accounts_view_user_position_data() { + let data = self.get_data_view_user_position_data(); + let ix = ViewUserPositionDataInstruction::data(data) + .accounts(accounts.clone()) + .instruction(); + + let res = self + .trident + .process_transaction(&[ix], Some("View User Position Data")); + + assert!(res.is_success()); + + // INVARIANT 1: the user_position must belong to the passed pair + let up_account = self + .trident + .get_account_with_type::(&accounts.user_position, 8) + .unwrap(); + assert_eq!( + up_account.pair, accounts.pair, + "ViewUserPositionData accepted a user_position that does not belong to the given pair" + ); + // INVARIANT 2: the passed rate_model must match the pair's configured rate_model + let pair_account = self + .trident + .get_account_with_type::(&accounts.pair, 8) + .unwrap(); + assert_eq!( + pair_account.rate_model, accounts.rate_model, + "ViewPairData accepted a mismatched rate_model for the given pair" + ); + } + } + + fn get_data_view_user_position_data(&mut self) -> ViewUserPositionDataInstructionData { + let getter = UserPositionViewKind::random(&mut self.trident); + ViewUserPositionDataInstructionData::new(getter) + } + + fn get_accounts_view_user_position_data( + &mut self, + ) -> Option { + let pair = self.fuzz_accounts.pair.get(&mut self.trident).expect("Pair should exist"); + let user = self.fuzz_accounts.user.get(&mut self.trident).expect("User should exist"); + let user_position = self + .trident + .find_program_address( + &[POSITION_SEED_PREFIX, pair.as_ref(), user.as_ref()], + &omnipair::program_id(), + ) + .0; + + self.trident + .get_account_with_type::(&user_position, 8)?; + + let rate_model = self.fuzz_accounts.rate_model.get(&mut self.trident).expect("Rate model should exist"); + let futarchy_authority = self.fuzz_accounts.futarchy_authority.get(&mut self.trident).expect("Futarchy authority should exist"); + + Some(ViewUserPositionDataInstructionAccounts::new( + pair, + user_position, + rate_model, + futarchy_authority, + )) + } +} + +impl UserPositionViewKind { + pub fn random(trident: &mut Trident) -> Self { + match trident.random_from_range(0..=5) { + 0 => Self::UserBorrowingPower, + 1 => Self::UserAppliedCollateralFactorBps, + 2 => Self::UserLiquidationCollateralFactorBps, + 3 => Self::UserDebtUtilizationBps, + 4 => Self::UserLiquidationPrice, + 5 => Self::UserDebtWithInterest, + _ => unreachable!(), + } + } +}