From 47e859296f70a0bcd12f7c8b870e602ef724cd7b Mon Sep 17 00:00:00 2001 From: Tasio Victoria Date: Fri, 11 Aug 2023 11:46:59 -0400 Subject: [PATCH] Add instantiate2 create pair message --- Cargo.lock | 930 +++++++++++++++++- Cargo.toml | 2 +- README.md | 42 +- contracts/infinity-builder/src/lib.rs | 2 +- contracts/infinity-factory/src/error.rs | 13 + contracts/infinity-factory/src/execute.rs | 71 ++ contracts/infinity-factory/src/helpers.rs | 34 + contracts/infinity-factory/src/instantiate.rs | 30 + contracts/infinity-factory/src/lib.rs | 93 +- contracts/infinity-factory/src/msg.rs | 41 + contracts/infinity-factory/src/query.rs | 31 + contracts/infinity-factory/src/state.rs | 5 + contracts/infinity-pair/src/math.rs | 4 +- justfile | 76 ++ unit-tests/Cargo.toml | 2 - unit-tests/src/helpers/pair_functions.rs | 2 +- .../nft_pair_swap_tests.rs | 3 +- .../pair_creation_tests.rs | 2 +- .../trade_pair_swap_tests.rs | 3 +- .../src/setup/setup_infinity_contracts.rs | 8 +- 20 files changed, 1228 insertions(+), 166 deletions(-) create mode 100644 contracts/infinity-factory/src/error.rs create mode 100644 contracts/infinity-factory/src/execute.rs create mode 100644 contracts/infinity-factory/src/helpers.rs create mode 100644 contracts/infinity-factory/src/instantiate.rs create mode 100644 contracts/infinity-factory/src/msg.rs create mode 100644 contracts/infinity-factory/src/query.rs create mode 100644 contracts/infinity-factory/src/state.rs create mode 100644 justfile diff --git a/Cargo.lock b/Cargo.lock index 7d6686a..237ce1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,69 @@ dependencies = [ "version_check", ] +[[package]] +name = "anyhow" +version = "1.0.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "base-factory" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c79bef6afa194d2bf474b1798a8f8ee63b05875f8a900f29b9b1fa599d06fd6" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "schemars", + "serde", + "sg-std", + "sg1", + "sg2", + "thiserror", +] + +[[package]] +name = "base-minter" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc7d05c81ea795739e58738a174356be19f48a890dcf0f94738aadfe7fa06d3" +dependencies = [ + "base-factory", + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw721", + "cw721-base", + "schemars", + "serde", + "sg-std", + "sg1", + "sg2", + "sg4", + "sg721", + "sg721-base", + "thiserror", + "url", +] + [[package]] name = "base16ct" version = "0.1.1" @@ -31,6 +94,28 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" +[[package]] +name = "bitvec" +version = "0.17.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41262f11d771fd4a61aa3ce019fca363b4b6c282fca9da2a31186d3965a47a5c" +dependencies = [ + "either", + "radium 0.3.0", +] + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium 0.7.0", + "tap", + "wyz", +] + [[package]] name = "block-buffer" version = "0.9.0" @@ -49,12 +134,85 @@ dependencies = [ "generic-array", ] +[[package]] +name = "borsh" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4114279215a005bc675e386011e594e1d9b800918cea18fcadadcce864a2046b" +dependencies = [ + "borsh-derive", + "hashbrown", +] + +[[package]] +name = "borsh-derive" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0754613691538d51f329cce9af41d7b7ca150bc973056f1156611489475f54f7" +dependencies = [ + "borsh-derive-internal", + "borsh-schema-derive-internal", + "proc-macro-crate", + "proc-macro2", + "syn", +] + +[[package]] +name = "borsh-derive-internal" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb438156919598d2c7bad7e1c0adf3d26ed3840dbc010db1a882a65583ca2fb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "borsh-schema-derive-internal" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634205cc43f74a1b9046ef87c4540ebda95696ec0f315024860cad7c5b0f5ccd" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "bytecheck" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "byteorder" version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + [[package]] name = "cfg-if" version = "1.0.0" @@ -202,6 +360,40 @@ dependencies = [ "cosmwasm-std", ] +[[package]] +name = "cw-controllers" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24bd6738c3fd59c87d2f84911c1cad1e4f2d1c58ecaa6e52549b4f78f4ed6f07" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "schemars", + "serde", + "thiserror", +] + +[[package]] +name = "cw-multi-test" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "127c7bb95853b8e828bdab97065c81cb5ddc20f7339180b61b2300565aaa99d1" +dependencies = [ + "anyhow", + "cosmwasm-std", + "cw-storage-plus 1.1.0", + "cw-utils 1.0.1", + "derivative", + "itertools", + "k256", + "prost", + "schemars", + "serde", + "thiserror", +] + [[package]] name = "cw-storage-macro" version = "0.16.0" @@ -250,7 +442,22 @@ checksum = "d6a84c6c1c0acc3616398eba50783934bd6c964bad6974241eaee3460c8f5b26" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw2", + "cw2 0.16.0", + "schemars", + "semver", + "serde", + "thiserror", +] + +[[package]] +name = "cw-utils" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c80e93d1deccb8588db03945016a292c3c631e6325d349ebb35d2db6f4f946f7" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw2 1.1.0", "schemars", "semver", "serde", @@ -270,6 +477,64 @@ dependencies = [ "serde", ] +[[package]] +name = "cw2" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ac2dc7a55ad64173ca1e0a46697c31b7a5c51342f55a1e84a724da4eb99908" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 1.1.0", + "schemars", + "serde", + "thiserror", +] + +[[package]] +name = "cw20" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a45a8794a5dd33b66af34caee52a7beceb690856adcc1682b6e3db88b2cdee62" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-utils 0.16.0", + "schemars", + "serde", +] + +[[package]] +name = "cw4" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86a93c2764ddb44038b948d9a9a8aadc9f92f5ef3e61249af2fad16091e43412" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "schemars", + "serde", +] + +[[package]] +name = "cw4-group" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dba1d15bff40b97bde05f36a0d44ac3369a085a4bda6c4673e33a9359513fdd2" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-controllers", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw4", + "schemars", + "serde", + "thiserror", +] + [[package]] name = "cw721" version = "0.16.0" @@ -278,7 +543,7 @@ checksum = "94a1ea6e6277bdd6dfc043a9b1380697fe29d6e24b072597439523658d21d791" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-utils", + "cw-utils 0.16.0", "schemars", "serde", ] @@ -292,8 +557,8 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 0.16.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "cw721", "schemars", "serde", @@ -374,6 +639,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "elliptic-curve" version = "0.12.3" @@ -394,6 +665,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ethereum-verify" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efda55d60fe325188bd04eb89ad5b0c9256e49eb00bbd810ef44e3efef9ce7d8" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "hex", + "sha2 0.10.6", + "sha3", +] + [[package]] name = "ff" version = "0.12.1" @@ -419,6 +703,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8cbd1169bd7b4a0a20d92b9af7a7e0422888bd38a6f5ec29c1fd8c1558a272e" +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "generic-array" version = "0.14.6" @@ -493,8 +783,8 @@ dependencies = [ "cosmwasm-std", "cw-storage-macro 0.16.0", "cw-storage-plus 1.1.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "infinity-factory", "infinity-global", "infinity-index", @@ -514,8 +804,8 @@ dependencies = [ "cosmwasm-std", "cw-storage-macro 0.16.0", "cw-storage-plus 1.1.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "infinity-global", "infinity-pair", "infinity-shared", @@ -532,7 +822,7 @@ dependencies = [ "cosmwasm-std", "cw-address-like", "cw-storage-plus 1.1.0", - "cw2", + "cw2 0.16.0", "sg-std", "thiserror", ] @@ -545,7 +835,7 @@ dependencies = [ "cosmwasm-std", "cw-storage-macro 0.16.0", "cw-storage-plus 1.1.0", - "cw2", + "cw2 0.16.0", "infinity-global", "infinity-shared", "sg-index-query", @@ -560,9 +850,10 @@ version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", + "cw-address-like", "cw-storage-plus 1.1.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "cw721", "cw721-base", "infinity-global", @@ -582,10 +873,11 @@ version = "0.1.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", + "cw-address-like", "cw-storage-macro 0.16.0", "cw-storage-plus 1.1.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "cw721", "cw721-base", "infinity-global", @@ -608,6 +900,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.5" @@ -626,12 +927,30 @@ dependencies = [ "sha2 0.10.6", ] +[[package]] +name = "keccak" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940" +dependencies = [ + "cpufeatures", +] + [[package]] name = "libc" version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.17.0" @@ -660,6 +979,21 @@ dependencies = [ "spki", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + [[package]] name = "proc-macro2" version = "1.0.56" @@ -670,43 +1004,182 @@ dependencies = [ ] [[package]] -name = "quote" -version = "1.0.27" +name = "prost" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +checksum = "444879275cb4fd84958b1a1d5420d15e6fcf7c235fe47f053c9c2a80aceb6001" dependencies = [ - "proc-macro2", + "bytes", + "prost-derive", ] [[package]] -name = "rand_core" -version = "0.5.1" +name = "prost-derive" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +checksum = "f9cc1a3263e07e0bf68e96268f37665207b49560d98739662cdfaae215c720fe" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn", +] [[package]] -name = "rand_core" -version = "0.6.4" +name = "ptr_meta" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" dependencies = [ - "getrandom", + "ptr_meta_derive", ] [[package]] -name = "rfc6979" -version = "0.3.1" +name = "ptr_meta_derive" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7743f17af12fa0b03b803ba12cd6a8d9483a587e89c69445e3909655c0b9fabb" +checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" dependencies = [ - "crypto-bigint", - "hmac", - "zeroize", + "proc-macro2", + "quote", + "syn", ] [[package]] -name = "ryu" -version = "1.0.12" +name = "quote" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radium" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "def50a86306165861203e7f84ecffbbdfdea79f0e51039b33de1e952358c47ac" + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core 0.6.4", +] + +[[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_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_xoshiro" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" +dependencies = [ + "rand_core 0.6.4", +] + +[[package]] +name = "rend" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581008d2099240d37fb08d77ad713bcaec2c4d89d50b5b21a8bb1996bbab68ab" +dependencies = [ + "bytecheck", +] + +[[package]] +name = "rfc6979" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7743f17af12fa0b03b803ba12cd6a8d9483a587e89c69445e3909655c0b9fabb" +dependencies = [ + "crypto-bigint", + "hmac", + "zeroize", +] + +[[package]] +name = "rkyv" +version = "0.7.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0200c8230b013893c0b2d6213d6ec64ed2b9be2e0e016682b7224ff82cff5c58" +dependencies = [ + "bitvec 1.0.1", + "bytecheck", + "hashbrown", + "ptr_meta", + "rend", + "rkyv_derive", + "seahash", + "tinyvec", + "uuid", +] + +[[package]] +name = "rkyv_derive" +version = "0.7.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2e06b915b5c230a17d7a736d1e2e63ee753c256a8614ef3f5147b13a4f5541d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "rust_decimal" +version = "1.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a2ab0025103a60ecaaf3abf24db1db240a4e1c15837090d2c32f625ac98abea" +dependencies = [ + "arrayvec", + "borsh", + "byteorder", + "bytes", + "num-traits", + "rand", + "rkyv", + "serde", + "serde_json", +] + +[[package]] +name = "ryu" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" @@ -734,6 +1207,12 @@ dependencies = [ "syn", ] +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + [[package]] name = "sec1" version = "0.3.0" @@ -805,6 +1284,48 @@ dependencies = [ "serde", ] +[[package]] +name = "sg-controllers" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d689e75e81f8725ad5a47dc9405c9551bfe56c2f0638e9331df581d8196a8029" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "schemars", + "serde", + "sg-std", + "thiserror", +] + +[[package]] +name = "sg-eth-airdrop" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3150a0949557a47e6bfe541f4d4ea07ddda94d994ad240787f20dc60805a12" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "ethereum-verify", + "hex", + "rust_decimal", + "schemars", + "serde", + "sg-std", + "sg-whitelist", + "sg1", + "sha2 0.10.6", + "sha3", + "thiserror", + "vending-minter", + "whitelist-immutable", +] + [[package]] name = "sg-index-query" version = "0.1.1" @@ -816,6 +1337,32 @@ dependencies = [ "cw-storage-plus 1.1.0", ] +[[package]] +name = "sg-marketplace" +version = "1.6.0" +source = "git+https://github.com/public-awesome/marketplace?rev=6aef3689ebf9b7d47805a40111f7d6c9dda8a6b7#6aef3689ebf9b7d47805a40111f7d6c9dda8a6b7" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cosmwasm-storage", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw20", + "cw721", + "cw721-base", + "schemars", + "semver", + "serde", + "sg-controllers", + "sg-std", + "sg1", + "sg2", + "sg721", + "sg721-base", + "thiserror", +] + [[package]] name = "sg-marketplace-common" version = "1.2.0" @@ -826,8 +1373,8 @@ dependencies = [ "cosmwasm-storage", "cw-storage-macro 0.16.0", "cw-storage-plus 0.16.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "cw721", "cw721-base", "schemars", @@ -840,6 +1387,40 @@ dependencies = [ "thiserror", ] +[[package]] +name = "sg-multi-test" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "126d5b5e90687a0937386b42595b703aa70f285fa297e24c5dd3565b47a25aa2" +dependencies = [ + "anyhow", + "cosmwasm-std", + "cw-multi-test", + "schemars", + "serde", + "sg-std", +] + +[[package]] +name = "sg-splits" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efefa4691f13adc08c4e733530b2c8c547f2bfc755eb343d19662a3c60b844bd" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-controllers", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw4", + "schemars", + "serde", + "sg-controllers", + "sg-std", + "thiserror", +] + [[package]] name = "sg-std" version = "2.3.0" @@ -848,13 +1429,32 @@ checksum = "ba6b7bed04a43538e04005584fb95674f181b2eed8132e13887ca6ad86a62cde" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-utils", + "cw-utils 0.16.0", "cw721", "schemars", "serde", "thiserror", ] +[[package]] +name = "sg-whitelist" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b20af0fd6b227c5f8e552de1a957b3e1cc923ad100b539f61181224e7d46bb1f" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "rust_decimal", + "schemars", + "serde", + "sg-std", + "sg1", + "thiserror", +] + [[package]] name = "sg1" version = "2.3.0" @@ -862,12 +1462,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "908f5487a480625b7d8056b20c7f9e8dd8792c1c12c191831552d87a5c1cc711" dependencies = [ "cosmwasm-std", - "cw-utils", + "cw-utils 0.16.0", "serde", "sg-std", "thiserror", ] +[[package]] +name = "sg2" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a421b38ccf4dc4968e7124fc7969406b61a08a2e51971cbb46fea307698e489" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "schemars", + "serde", + "sg721", + "thiserror", +] + +[[package]] +name = "sg4" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18da9e17654454380aa81f92cbc650098f28d657932770fda1c911747eb95ce4" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "schemars", + "serde", + "thiserror", +] + [[package]] name = "sg721" version = "2.3.0" @@ -876,7 +1505,7 @@ checksum = "8f284472b4f580af7b64ade14af42623581534512d8a681642d79cfbd0c3a254" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-utils", + "cw-utils 0.16.0", "cw721-base", "serde", "thiserror", @@ -891,8 +1520,8 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus 0.16.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "cw721", "cw721-base", "serde", @@ -902,6 +1531,25 @@ dependencies = [ "url", ] +[[package]] +name = "sg721-nt" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "230cb6b10992b6a018a0d0d6d768d8f60463a1e05e4e3d8d5eca63dd8eb030f2" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw721", + "cw721-base", + "schemars", + "serde", + "sg-std", + "sg721", + "sg721-base", +] + [[package]] name = "sha2" version = "0.9.9" @@ -926,6 +1574,26 @@ dependencies = [ "digest 0.10.6", ] +[[package]] +name = "sha3" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" +dependencies = [ + "digest 0.10.6", + "keccak", +] + +[[package]] +name = "shuffle" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fecbe806be0726e22a6192ce0ec2f90e0798946ae0cbc2c10c7a3c68cae4eb2" +dependencies = [ + "bitvec 0.17.4", + "rand", +] + [[package]] name = "signature" version = "1.6.4" @@ -936,6 +1604,12 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "simdutf8" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" + [[package]] name = "spki" version = "0.6.0" @@ -956,8 +1630,8 @@ dependencies = [ "cosmwasm-std", "cosmwasm-storage", "cw-storage-plus 0.16.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "schemars", "serde", "sg-std", @@ -975,8 +1649,8 @@ dependencies = [ "cosmwasm-storage", "cw-storage-macro 1.1.0", "cw-storage-plus 1.1.0", - "cw-utils", - "cw2", + "cw-utils 0.16.0", + "cw2 0.16.0", "schemars", "serde", "sg-index-query", @@ -1009,6 +1683,43 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "test-suite" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402e763b8509556295580c8f69af032a7a3cabff50b583c7603ab3b15e64c6e9" +dependencies = [ + "anyhow", + "base-factory", + "base-minter", + "cosmwasm-schema", + "cosmwasm-std", + "cw-multi-test", + "cw4", + "cw4-group", + "cw721", + "cw721-base", + "sg-controllers", + "sg-eth-airdrop", + "sg-multi-test", + "sg-splits", + "sg-std", + "sg-whitelist", + "sg2", + "sg721", + "sg721-base", + "sg721-nt", + "vending-factory", + "vending-minter", + "whitelist-immutable", +] + [[package]] name = "thiserror" version = "1.0.38" @@ -1044,6 +1755,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "typenum" version = "1.16.0" @@ -1083,6 +1803,42 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unit-tests" +version = "0.1.0" +dependencies = [ + "anyhow", + "cosmwasm-schema", + "cosmwasm-std", + "cw-multi-test", + "cw-storage-plus 1.1.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw721", + "cw721-base", + "infinity-factory", + "infinity-global", + "infinity-index", + "infinity-pair", + "infinity-router", + "infinity-shared", + "itertools", + "sg-marketplace", + "sg-marketplace-common", + "sg-multi-test", + "sg-std", + "sg1", + "sg2", + "sg721", + "sg721-base", + "stargaze-fair-burn", + "stargaze-royalty-registry", + "test-suite", + "thiserror", + "vending-factory", + "vending-minter", +] + [[package]] name = "url" version = "2.3.1" @@ -1094,6 +1850,64 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "uuid" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" + +[[package]] +name = "vending-factory" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "958cbc1b99099ab05e85f782397ef55a28ae5c29c6e22b1521ebfa3b293fa67c" +dependencies = [ + "base-factory", + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "schemars", + "serde", + "sg-std", + "sg1", + "sg2", + "sg721", + "thiserror", +] + +[[package]] +name = "vending-minter" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8316d488e3aed55ef0b4809c267f9a215ec58a2782ab425ddc7e78780c586bf4" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "cw721", + "cw721-base", + "rand_core 0.6.4", + "rand_xoshiro", + "schemars", + "semver", + "serde", + "sg-std", + "sg-whitelist", + "sg1", + "sg2", + "sg4", + "sg721", + "sha2 0.10.6", + "shuffle", + "thiserror", + "url", + "vending-factory", +] + [[package]] name = "version_check" version = "0.9.4" @@ -1106,6 +1920,34 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "whitelist-immutable" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc8e950cdec10dbf32b81b261e1457ba0f1c26adc1babbfd52477c23848632db" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-controllers", + "cw-storage-plus 0.16.0", + "cw-utils 0.16.0", + "cw2 0.16.0", + "schemars", + "serde", + "sg-std", + "sg-whitelist", + "thiserror", +] + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "zeroize" version = "1.5.7" diff --git a/Cargo.toml b/Cargo.toml index 242044c..01323d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["contracts/*", "packages/*"] +members = ["contracts/*", "packages/*", "unit-tests"] [workspace.package] version = "0.1.0" diff --git a/README.md b/README.md index acb3efc..39c2553 100644 --- a/README.md +++ b/README.md @@ -16,54 +16,54 @@ INFINITY SWAP IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES ## Overview -Infinity Swap is an automated market maker (AMM) protocol that allows for the buying and selling of NFT assets with a specified fungible token. The buy and sell price of the NFT assets are determined by the parameters of the pool, the bonding curve, and the assets custodied by the pool. +Infinity Swap is an automated market maker (AMM) protocol that allows for the buying and selling of NFT assets with a specified fungible token. The buy and sell price of the NFT assets are determined by the parameters of the pair, the bonding curve, and the assets custodied by the pair. Infinity Swap makes use of an NFT AMM design, but is written for [CosmWasm](https://github.com/CosmWasm/cosmwasm) so that it may be used on Cosmos SDK based chains. ## Features -- Pool pricing indexed within the contract for optimized price discovery +- Pair pricing indexed within the contract for optimized price discovery - Respects listing price of marketplace contract - Respects the minimum price of marketplace contract - Respects the maximum finders fee of marketplace contract - Respects the trading fee percentage of the marketplace contract -- Pool owner can set a finders fee that is paid to the finder address on a trade -- Pool owner can set a swap fee that is paid to the pool owner of a Trade pool -- Reinvestment of tokens and NFTs back into Trade pools based on parameter flags -- Flexible asset redirection for trader and pool owner +- Pair owner can set a finders fee that is paid to the finder address on a trade +- Pair owner can set a swap fee that is paid to the pair owner of a Trade pair +- Reinvestment of tokens and NFTs back into Trade pairs based on parameter flags +- Flexible asset redirection for trader and pair owner - Queries that allow for simulation of swaps - User slippage tolerance for swaps - User may decide to revert whole transaction or revert individual swaps using the robust flag ## How It Works -1. Liquidity providers begin by creating a pool with specific parameters. They define which pools the assets will hold and the bonding curve which will be used to determine the price. Once the pool is created, liquidity providers can deposit tokens and/or NFTs into the pool. Once the pool is funded, liquidity providers can activate the pool so that it can begin trading. -2. Traders can then buy and sell NFTs from the pool. The price of the NFTs is determined by the bonding curve and the assets held by the pool. The price of the NFTs will change as the pool is traded. -3. Liquidity providers can withdraw their assets from the pool at any time. +1. Liquidity providers begin by creating a pair with specific parameters. They define which pairs the assets will hold and the bonding curve which will be used to determine the price. Once the pair is created, liquidity providers can deposit tokens and/or NFTs into the pair. Once the pair is funded, liquidity providers can activate the pair so that it can begin trading. +2. Traders can then buy and sell NFTs from the pair. The price of the NFTs is determined by the bonding curve and the assets held by the pair. The price of the NFTs will change as the pair is traded. +3. Liquidity providers can withdraw their assets from the pair at any time. -### Creating Pools +### Creating Pairs -Creating pools follows a three message process where the first message creates the pool, the second message deposits assets into the pool, and the third message activates the pool. The three messages can be concatenated into a single transaction by the client. +Creating pairs follows a three message process where the first message creates the pair, the second message deposits assets into the pair, and the third message activates the pair. The three messages can be concatenated into a single transaction by the client. ![Screenshot 2023-01-31 at 10 45 07 AM](https://user-images.githubusercontent.com/6496257/215807687-3dca764a-5178-4eb9-8503-7c360c5e0954.png) -### Types of Pools +### Types of Pairs -The `pool_type` parameter refers to the asset that the pool holds: +The `pair_type` parameter refers to the asset that the pair holds: -- A `Token` pool has funglible tokens that it is willing to give to traders in exchange for NFTs. This is similar to a buy limit order. -- An `Nft` pool has NFTs that it is willing to give to traders in exchange for tokens. This is similar to a sell limit order. -- A `Trade` pool allows for both TOKEN-->NFT and NFT-->TOKEN swaps. This is similar to a double-sided order book. This type is the only type that supports swap fees. +- A `Token` pair has funglible tokens that it is willing to give to traders in exchange for NFTs. This is similar to a buy limit order. +- An `Nft` pair has NFTs that it is willing to give to traders in exchange for tokens. This is similar to a sell limit order. +- A `Trade` pair allows for both TOKEN-->NFT and NFT-->TOKEN swaps. This is similar to a double-sided order book. This type is the only type that supports swap fees. ### Types of bonding curves -- A `Linear` bonding curve has a constant slope, meaning that the price of an NFT increases or decreases by a constant amount with each NFT that is bought or sold to the pool. -- A `Exponential` bonding curve has a slope that increases or decreases by a percentage with each NFT that is bought or sold to the pool. +- A `Linear` bonding curve has a constant slope, meaning that the price of an NFT increases or decreases by a constant amount with each NFT that is bought or sold to the pair. +- A `Exponential` bonding curve has a slope that increases or decreases by a percentage with each NFT that is bought or sold to the pair. - A `Constant Product` bonding curve specifies that the product of two reserve assets remains constant after every trade. ### Performing Swaps -Traders can buy and sell NFTs from the pool. The price of the NFTs is determined by the bonding curve and the assets held by the pool. The price of the NFTs will change as the pool is traded. +Traders can buy and sell NFTs from the pair. The price of the NFTs is determined by the bonding curve and the assets held by the pair. The price of the NFTs will change as the pair is traded. The user flow for performing swaps is as follows: @@ -78,8 +78,8 @@ The user flow for performing swaps is as follows: These are the types of swaps that can be performed by the contract. Note that each swap can be be run as an ExecuteMsg or a QueryMsg. When run as an ExecuteMsg the full swap is performed and assets are transferred to their proper destination. When run as a query, the swap is performed in simulation mode and the results are returned to the client, but assets are not transferred. -- `DirectSwapNftsForTokens` - Swap NFTs for tokens directly with a specified pool +- `DirectSwapNftsForTokens` - Swap NFTs for tokens directly with a specified pair - `SwapNftsForTokens` - Swap NFTs for tokens at optimal sale prices -- `DirectSwapTokensForNfts` - Swap tokens for NFTs directly with a specified pool +- `DirectSwapTokensForNfts` - Swap tokens for NFTs directly with a specified pair - `SwapTokensForSpecificNfts` - Swap tokens for specific NFTs at optimal purchase prices - `SwapTokensForAnyNfts` - Swap tokens for any NFTs at optimal purchase prices diff --git a/contracts/infinity-builder/src/lib.rs b/contracts/infinity-builder/src/lib.rs index 7016481..18ecd34 100644 --- a/contracts/infinity-builder/src/lib.rs +++ b/contracts/infinity-builder/src/lib.rs @@ -4,7 +4,7 @@ use cosmwasm_std::{ Instantiate2AddressError, MessageInfo, StdError, StdResult, WasmMsg, }; use cw2::set_contract_version; -use infinity_factory::InstantiateMsg as InfinityFactoryInstantiateMsg; +use infinity_factory::msg::InstantiateMsg as InfinityFactoryInstantiateMsg; use infinity_global::{GlobalConfig, InstantiateMsg as InfinityGlobalInstantiateMsg}; use infinity_index::msg::InstantiateMsg as InfinityIndexInstantiateMsg; use infinity_router::msg::InstantiateMsg as InfinityRouterInstantiateMsg; diff --git a/contracts/infinity-factory/src/error.rs b/contracts/infinity-factory/src/error.rs new file mode 100644 index 0000000..de3d62f --- /dev/null +++ b/contracts/infinity-factory/src/error.rs @@ -0,0 +1,13 @@ +use cosmwasm_std::Instantiate2AddressError; +use cosmwasm_std::StdError; + +use thiserror::Error; + +#[derive(Error, Debug, PartialEq)] +pub enum ContractError { + #[error("{0}")] + Std(#[from] StdError), + + #[error("{0}")] + Instantiate2AddressError(#[from] Instantiate2AddressError), +} diff --git a/contracts/infinity-factory/src/execute.rs b/contracts/infinity-factory/src/execute.rs new file mode 100644 index 0000000..a3e4820 --- /dev/null +++ b/contracts/infinity-factory/src/execute.rs @@ -0,0 +1,71 @@ +use crate::helpers::generate_salt; +use crate::msg::ExecuteMsg; +use crate::state::{INFINITY_GLOBAL, SENDER_COUNTER}; +use crate::ContractError; + +use cosmwasm_std::{to_binary, DepsMut, Env, MessageInfo, WasmMsg}; +use infinity_global::load_global_config; +use infinity_pair::msg::InstantiateMsg as InfinityPairInstantiateMsg; +use sg_std::Response; + +#[cfg(not(feature = "library"))] +use cosmwasm_std::entry_point; + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn execute( + deps: DepsMut, + env: Env, + info: MessageInfo, + msg: ExecuteMsg, +) -> Result { + match msg { + ExecuteMsg::CreatePair { + pair_immutable, + pair_config, + } => { + let infinity_global = INFINITY_GLOBAL.load(deps.storage)?; + let global_config = load_global_config(&deps.querier, &infinity_global)?; + + let response = Response::new().add_message(WasmMsg::Instantiate { + admin: Some(env.contract.address.into()), + code_id: global_config.infinity_pair_code_id, + label: "Infinity Pair".to_string(), + msg: to_binary(&InfinityPairInstantiateMsg { + infinity_global: infinity_global.to_string(), + pair_immutable, + pair_config, + })?, + funds: info.funds, + }); + + Ok(response) + }, + ExecuteMsg::CreatePair2 { + pair_immutable, + pair_config, + } => { + let infinity_global = INFINITY_GLOBAL.load(deps.storage)?; + let global_config = load_global_config(&deps.querier, &infinity_global)?; + + let counter = + SENDER_COUNTER.may_load(deps.storage, info.sender.clone())?.unwrap_or_default(); + let salt = generate_salt(&info.sender, counter); + SENDER_COUNTER.save(deps.storage, info.sender.clone(), &(counter + 1))?; + + let response = Response::new().add_message(WasmMsg::Instantiate2 { + admin: Some(env.contract.address.into()), + code_id: global_config.infinity_pair_code_id, + label: "Infinity Pair".to_string(), + msg: to_binary(&InfinityPairInstantiateMsg { + infinity_global: infinity_global.to_string(), + pair_immutable, + pair_config, + })?, + funds: info.funds, + salt, + }); + + Ok(response) + }, + } +} diff --git a/contracts/infinity-factory/src/helpers.rs b/contracts/infinity-factory/src/helpers.rs new file mode 100644 index 0000000..24d6f8b --- /dev/null +++ b/contracts/infinity-factory/src/helpers.rs @@ -0,0 +1,34 @@ +use crate::ContractError; + +use cosmwasm_std::{instantiate2_address, Addr, Binary, Deps, Env}; +use sha2::{Digest, Sha256}; + +pub fn generate_salt(sender: &Addr, counter: u64) -> Binary { + let mut hasher = Sha256::new(); + hasher.update(sender.as_bytes()); + hasher.update(&counter.to_be_bytes()); + hasher.finalize().to_vec().into() +} + +pub fn generate_instantiate_2_addr( + deps: Deps, + env: &Env, + sender: &Addr, + counter: u64, + code_id: u64, +) -> Result<(Addr, Binary), ContractError> { + let code_res = deps.querier.query_wasm_code_info(code_id)?; + + let salt = generate_salt(&sender, counter); + + // predict the contract address + let addr_raw = instantiate2_address( + &code_res.checksum, + &deps.api.addr_canonicalize(env.contract.address.as_str())?, + &salt, + )?; + + let addr = deps.api.addr_humanize(&addr_raw)?; + + Ok((addr, salt)) +} diff --git a/contracts/infinity-factory/src/instantiate.rs b/contracts/infinity-factory/src/instantiate.rs new file mode 100644 index 0000000..b18e254 --- /dev/null +++ b/contracts/infinity-factory/src/instantiate.rs @@ -0,0 +1,30 @@ +use crate::msg::InstantiateMsg; +use crate::state::INFINITY_GLOBAL; + +use cosmwasm_std::StdError; +use cosmwasm_std::{DepsMut, Env, MessageInfo}; +use cw2::set_contract_version; +use sg_std::Response; + +#[cfg(not(feature = "library"))] +use cosmwasm_std::entry_point; + +pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); +pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn instantiate( + deps: DepsMut, + _env: Env, + _info: MessageInfo, + msg: InstantiateMsg, +) -> Result { + set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + INFINITY_GLOBAL.save(deps.storage, &deps.api.addr_validate(&msg.infinity_global)?)?; + + Ok(Response::new() + .add_attribute("action", "instantiate") + .add_attribute("contract_name", CONTRACT_NAME) + .add_attribute("contract_version", CONTRACT_VERSION)) +} diff --git a/contracts/infinity-factory/src/lib.rs b/contracts/infinity-factory/src/lib.rs index 8a1bf8f..7be080f 100644 --- a/contracts/infinity-factory/src/lib.rs +++ b/contracts/infinity-factory/src/lib.rs @@ -1,87 +1,10 @@ -use cosmwasm_schema::cw_serde; -use cosmwasm_std::{to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, StdResult}; -use cosmwasm_std::{StdError, WasmMsg}; -use cw2::set_contract_version; -use cw_storage_plus::Item; -use infinity_global::load_global_config; -use infinity_pair::msg::InstantiateMsg as InfinityPairInstantiateMsg; -use infinity_pair::state::{PairConfig, PairImmutable}; -use sg_std::Response; +pub mod execute; +pub mod helpers; +pub mod instantiate; +pub mod msg; +pub mod query; +pub mod state; -#[cfg(not(feature = "library"))] -use cosmwasm_std::entry_point; +mod error; -pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); -pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); - -pub const INFINITY_GLOBAL: Item = Item::new("g"); - -#[cw_serde] -pub struct InstantiateMsg { - /// The address of the infinity global contract - pub infinity_global: String, -} - -#[cfg_attr(not(feature = "library"), entry_point)] -pub fn instantiate( - deps: DepsMut, - _env: Env, - _info: MessageInfo, - msg: InstantiateMsg, -) -> Result { - set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; - - INFINITY_GLOBAL.save(deps.storage, &deps.api.addr_validate(&msg.infinity_global)?)?; - - Ok(Response::new() - .add_attribute("action", "instantiate") - .add_attribute("contract_name", CONTRACT_NAME) - .add_attribute("contract_version", CONTRACT_VERSION)) -} - -#[cw_serde] -pub enum ExecuteMsg { - CreatePair { - /// The immutable parameters of the pair - pair_immutable: PairImmutable, - /// The user configurable parameters of the pair - pair_config: PairConfig, - }, -} - -#[cfg_attr(not(feature = "library"), entry_point)] -pub fn execute( - deps: DepsMut, - env: Env, - info: MessageInfo, - msg: ExecuteMsg, -) -> Result { - match msg { - ExecuteMsg::CreatePair { - pair_immutable, - pair_config, - } => { - let infinity_global = INFINITY_GLOBAL.load(deps.storage)?; - let global_config = load_global_config(&deps.querier, &infinity_global)?; - - let response = Response::new().add_message(WasmMsg::Instantiate { - admin: Some(env.contract.address.into()), - code_id: global_config.infinity_pair_code_id, - label: "InfinityPair".to_string(), - msg: to_binary(&InfinityPairInstantiateMsg { - infinity_global: infinity_global.to_string(), - pair_immutable, - pair_config, - })?, - funds: info.funds, - }); - - Ok(response) - }, - } -} - -#[cfg_attr(not(feature = "library"), entry_point)] -pub fn query(_deps: Deps, _env: Env, _msg: Empty) -> StdResult { - unimplemented!() -} +pub use crate::error::ContractError; diff --git a/contracts/infinity-factory/src/msg.rs b/contracts/infinity-factory/src/msg.rs new file mode 100644 index 0000000..4f74130 --- /dev/null +++ b/contracts/infinity-factory/src/msg.rs @@ -0,0 +1,41 @@ +use cosmwasm_schema::{cw_serde, QueryResponses}; +use cosmwasm_std::{Addr, Binary}; +use infinity_pair::state::{PairConfig, PairImmutable}; + +#[cw_serde] +pub struct InstantiateMsg { + /// The address of the infinity global contract + pub infinity_global: String, +} + +#[cw_serde] +pub enum ExecuteMsg { + CreatePair { + /// The immutable parameters of the pair + pair_immutable: PairImmutable, + /// The user configurable parameters of the pair + pair_config: PairConfig, + }, + CreatePair2 { + /// The immutable parameters of the pair + pair_immutable: PairImmutable, + /// The user configurable parameters of the pair + pair_config: PairConfig, + }, +} + +#[cw_serde] +pub struct NextPairResponse { + pub sender: Addr, + pub pair: Addr, + pub salt: Binary, +} + +#[cw_serde] +#[derive(QueryResponses)] +pub enum QueryMsg { + #[returns(NextPairResponse)] + NextPair { + sender: String, + }, +} diff --git a/contracts/infinity-factory/src/query.rs b/contracts/infinity-factory/src/query.rs new file mode 100644 index 0000000..b98f9f5 --- /dev/null +++ b/contracts/infinity-factory/src/query.rs @@ -0,0 +1,31 @@ +use crate::helpers::generate_instantiate_2_addr; +use crate::msg::{NextPairResponse, QueryMsg}; +use crate::state::SENDER_COUNTER; + +use cosmwasm_std::{to_binary, Addr, Binary, Deps, Env, StdResult}; + +#[cfg(not(feature = "library"))] +use cosmwasm_std::entry_point; + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { + match msg { + QueryMsg::NextPair { + sender, + } => to_binary(&query_next_pair(deps, env, deps.api.addr_validate(&sender)?)?), + } +} + +pub fn query_next_pair(deps: Deps, env: Env, sender: Addr) -> StdResult { + let counter = SENDER_COUNTER.may_load(deps.storage, sender.clone())?.unwrap_or_default(); + + let response = deps.querier.query_wasm_contract_info(&env.contract.address)?; + let (pair, salt) = + generate_instantiate_2_addr(deps, &env, &sender, counter, response.code_id).unwrap(); + + Ok(NextPairResponse { + sender, + pair, + salt, + }) +} diff --git a/contracts/infinity-factory/src/state.rs b/contracts/infinity-factory/src/state.rs new file mode 100644 index 0000000..28c8854 --- /dev/null +++ b/contracts/infinity-factory/src/state.rs @@ -0,0 +1,5 @@ +use cosmwasm_std::Addr; +use cw_storage_plus::{Item, Map}; + +pub const INFINITY_GLOBAL: Item = Item::new("g"); +pub const SENDER_COUNTER: Map = Map::new("s"); diff --git a/contracts/infinity-pair/src/math.rs b/contracts/infinity-pair/src/math.rs index edaa6db..588de49 100644 --- a/contracts/infinity-pair/src/math.rs +++ b/contracts/infinity-pair/src/math.rs @@ -134,13 +134,13 @@ mod tests { let sell_to_pair_price = calc_cp_trade_sell_to_pair_price(Uint128::from(250_000_000u128), 20u64).unwrap(); - assert_eq!(sell_to_pair_price, Uint128::from(11_904_762u128)); + assert_eq!(sell_to_pair_price, Uint128::from(11_904_761u128)); let result = calc_cp_trade_buy_from_pair_price(Uint128::from(250_000_000u128), 1u64); assert!(result.is_err()); let buy_from_pair_price = calc_cp_trade_buy_from_pair_price(Uint128::from(250_000_000u128), 20u64).unwrap(); - assert_eq!(buy_from_pair_price, Uint128::from(13_157_894u128)); + assert_eq!(buy_from_pair_price, Uint128::from(13_157_895u128)); } } diff --git a/justfile b/justfile new file mode 100644 index 0000000..7318472 --- /dev/null +++ b/justfile @@ -0,0 +1,76 @@ +lint: + cargo clippy --all-targets -- -D warnings + +lint-fix: + cargo clippy --all-targets --fix -- -D warnings + +schema: + sh scripts/schema.sh + +# artifacts: +# mkdir -p artifacts + +# clear-artifacts: artifacts +# rm -rf artifacts +# mkdir -p artifacts + +# download-artifacts: artifacts +# scripts/download-core-artifacts.sh +# scripts/download-launchpad-artifacts.sh + +# optimize: +# sh scripts/optimize.sh + +# optimize-arm: +# sh scripts/optimize-arm.sh + +# deploy-local: +# #!/usr/bin/env bash +# TEST_ADDRS=`jq -r '.[].address' ./typescript/packages/e2e-tests/configs/test_accounts.json | tr '\n' ' '` +# docker kill stargaze || true +# docker volume rm -f stargaze_data +# docker run --rm -d --name stargaze \ +# -e DENOM=ustars \ +# -e CHAINID=testing \ +# -e GAS_LIMIT=75000000 \ +# -p 1317:1317 \ +# -p 26656:26656 \ +# -p 26657:26657 \ +# -p 9090:9090 \ +# --mount type=volume,source=stargaze_data,target=/root \ +# publicawesome/stargaze:10.0.1 /data/entry-point.sh $TEST_ADDRS + +# deploy-local-arm: +# #!/usr/bin/env bash +# TEST_ADDRS=`jq -r '.[].address' ./typescript/packages/e2e-tests/configs/test_accounts.json | tr '\n' ' '` +# docker kill stargaze || true +# docker volume rm -f stargaze_data +# docker run --rm -d --name stargaze \ +# -e DENOM=ustars \ +# -e CHAINID=testing \ +# -e GAS_LIMIT=75000000 \ +# -p 1317:1317 \ +# -p 26656:26656 \ +# -p 26657:26657 \ +# -p 9090:9090 \ +# --mount type=volume,source=stargaze_data,target=/root \ +# --platform linux/amd64 \ +# publicawesome/stargaze:10.0.1 /data/entry-point.sh $TEST_ADDRS + +# e2e-test: +# #!/usr/bin/env bash +# START_DIR=$(pwd) +# cd typescript/packages/e2e-tests +# yarn install +# yarn test + +# e2e-test-full: download-artifacts optimize deploy-local e2e-test + +# e2e-test-full-arm: download-artifacts optimize-arm deploy-local-arm e2e-test + +# e2e-watch: deploy-local-arm +# #!/usr/bin/env bash +# START_DIR=$(pwd) +# cd typescript/packages/e2e-tests +# yarn test +# yarn test:watch \ No newline at end of file diff --git a/unit-tests/Cargo.toml b/unit-tests/Cargo.toml index 9ef61cb..e7e4194 100644 --- a/unit-tests/Cargo.toml +++ b/unit-tests/Cargo.toml @@ -23,7 +23,6 @@ stargaze-fair-burn = { workspace = true } stargaze-royalty-registry = { workspace = true } cosmwasm-std = { workspace = true } cosmwasm-schema = { workspace = true } -cosmwasm-storage = { workspace = true } cw-storage-plus = { workspace = true } cw-utils = { workspace = true } cw2 = { workspace = true } @@ -40,4 +39,3 @@ sg-multi-test = { workspace = true } test-suite = { workspace = true } vending-minter = { workspace = true } vending-factory = { workspace = true } -mockall = { workspace = true } diff --git a/unit-tests/src/helpers/pair_functions.rs b/unit-tests/src/helpers/pair_functions.rs index b54fc78..c640bb2 100644 --- a/unit-tests/src/helpers/pair_functions.rs +++ b/unit-tests/src/helpers/pair_functions.rs @@ -3,7 +3,7 @@ use infinity_global::QueryMsg as InfinityGlobalQueryMsg; use cosmwasm_std::{coin, to_binary, Addr, Empty, Uint128}; use cw_multi_test::Executor; -use infinity_factory::ExecuteMsg as InfinityFactoryExecuteMsg; +use infinity_factory::msg::ExecuteMsg as InfinityFactoryExecuteMsg; use infinity_global::GlobalConfig; use infinity_pair::{ msg::{ExecuteMsg as InfinityPairExecuteMsg, QueryMsg as InfinityPairQueryMsg}, diff --git a/unit-tests/src/infinity_pair_tests/nft_pair_swap_tests.rs b/unit-tests/src/infinity_pair_tests/nft_pair_swap_tests.rs index 32a738c..de8e132 100644 --- a/unit-tests/src/infinity_pair_tests/nft_pair_swap_tests.rs +++ b/unit-tests/src/infinity_pair_tests/nft_pair_swap_tests.rs @@ -164,8 +164,7 @@ fn try_nft_pair_invalid_swaps() { ); assert_error( response, - ContractError::InvalidPairQuote("payment required is greater than max input".to_string()) - .to_string(), + InfinityError::InvalidInput("received funds does not equal quote".to_string()).to_string(), ); // Cannot swap using alt denom funds diff --git a/unit-tests/src/infinity_pair_tests/pair_creation_tests.rs b/unit-tests/src/infinity_pair_tests/pair_creation_tests.rs index 8375691..c072169 100644 --- a/unit-tests/src/infinity_pair_tests/pair_creation_tests.rs +++ b/unit-tests/src/infinity_pair_tests/pair_creation_tests.rs @@ -4,7 +4,7 @@ use crate::setup::templates::{setup_infinity_test, standard_minter_template, Inf use cosmwasm_std::{Addr, Uint128}; use cw_multi_test::Executor; -use infinity_factory::ExecuteMsg as InfinityFactoryExecuteMsg; +use infinity_factory::msg::ExecuteMsg as InfinityFactoryExecuteMsg; use infinity_global::{GlobalConfig, QueryMsg as InfinityGlobalQueryMsg}; use infinity_pair::msg::{ExecuteMsg as InfinityPairExecuteMsg, QueryMsg as InfinityPairQueryMsg}; use infinity_pair::pair::Pair; diff --git a/unit-tests/src/infinity_pair_tests/trade_pair_swap_tests.rs b/unit-tests/src/infinity_pair_tests/trade_pair_swap_tests.rs index ac027f2..317019e 100644 --- a/unit-tests/src/infinity_pair_tests/trade_pair_swap_tests.rs +++ b/unit-tests/src/infinity_pair_tests/trade_pair_swap_tests.rs @@ -226,8 +226,7 @@ fn try_trade_pair_invalid_swaps() { ); assert_error( response, - ContractError::InvalidPairQuote("payment required is greater than max input".to_string()) - .to_string(), + InfinityError::InvalidInput("received funds does not equal quote".to_string()).to_string(), ); // Cannot swap using alt denom funds diff --git a/unit-tests/src/setup/setup_infinity_contracts.rs b/unit-tests/src/setup/setup_infinity_contracts.rs index b1577d3..2b547ff 100644 --- a/unit-tests/src/setup/setup_infinity_contracts.rs +++ b/unit-tests/src/setup/setup_infinity_contracts.rs @@ -58,9 +58,9 @@ pub fn setup_infinity_global( pub fn contract_infinity_factory() -> Box> { let contract = ContractWrapper::new( - infinity_factory::execute, - infinity_factory::instantiate, - infinity_factory::query, + infinity_factory::execute::execute, + infinity_factory::instantiate::instantiate, + infinity_factory::query::query, ); Box::new(contract) } @@ -71,7 +71,7 @@ pub fn setup_infinity_factory( infinity_global: &Addr, ) -> Addr { let infinity_factory_code_id = router.store_code(contract_infinity_factory()); - let msg = infinity_factory::InstantiateMsg { + let msg = infinity_factory::msg::InstantiateMsg { infinity_global: infinity_global.to_string(), }; router