diff --git a/cosmwasm/Cargo.lock b/cosmwasm/Cargo.lock index 1799e32f53..7b8b673fcf 100644 --- a/cosmwasm/Cargo.lock +++ b/cosmwasm/Cargo.lock @@ -86,18 +86,36 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + [[package]] name = "base64" version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64ct" version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" +[[package]] +name = "bech32" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" + [[package]] name = "bitflags" version = "1.3.2" @@ -129,6 +147,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" +[[package]] +name = "bnum" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab9008b6bb9fc80b5277f2fe481c09e828743d9151203e804583eb4c9e15b31d" + [[package]] name = "bs58" version = "0.4.0" @@ -226,31 +250,32 @@ dependencies = [ [[package]] name = "cosmwasm-crypto" -version = "1.2.7" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb64554a91d6a9231127f4355d351130a0b94e663d5d9dc8b3a54ca17d83de49" +checksum = "dd50718a2b6830ce9eb5d465de5a018a12e71729d66b70807ce97e6dd14f931d" dependencies = [ "digest 0.10.6", + "ecdsa 0.16.7", "ed25519-zebra", - "k256", + "k256 0.13.1", "rand_core 0.6.4", "thiserror", ] [[package]] name = "cosmwasm-derive" -version = "1.2.7" +version = "1.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0fb2ce09f41a3dae1a234d56a9988f9aff4c76441cd50ef1ee9a4f20415b028" +checksum = "d67457e4acb04e738788d3489e343957455df2c4643f2b53050eb052ca631d19" dependencies = [ "syn 1.0.109", ] [[package]] name = "cosmwasm-schema" -version = "1.2.7" +version = "1.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "230e5d1cefae5331db8934763c81b9c871db6a2cd899056a5694fa71d292c815" +checksum = "13bf06bf1c7ea737f6b3d955d9cabeb8cbbe4dcb8dea392e30f6fab4493a4b7a" dependencies = [ "cosmwasm-schema-derive", "schemars", @@ -261,9 +286,9 @@ dependencies = [ [[package]] name = "cosmwasm-schema-derive" -version = "1.2.7" +version = "1.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43dadf7c23406cb28079d69e6cb922c9c29b9157b0fe887e3b79c783b7d4bcb8" +checksum = "077fe378f16b54e3d0a57adb3f39a65bcf7bdbda6a5eade2f8ba7755c2fb1250" dependencies = [ "proc-macro2", "quote", @@ -272,11 +297,13 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.2.7" +version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4337eef8dfaf8572fe6b6b415d6ec25f9308c7bb09f2da63789209fb131363be" +checksum = "ad011ae7447188e26e4a7dbca2fcd0fc186aa21ae5c86df0503ea44c78f9e469" dependencies = [ - "base64", + "base64 0.21.7", + "bech32", + "bnum", "cosmwasm-crypto", "cosmwasm-derive", "derivative", @@ -286,8 +313,8 @@ dependencies = [ "serde", "serde-json-wasm 0.5.1", "sha2 0.10.6", + "static_assertions", "thiserror", - "uint", ] [[package]] @@ -324,6 +351,19 @@ dependencies = [ "wasmer-middlewares", ] +[[package]] +name = "counter" +version = "0.1.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-multi-test", + "cw-storage-plus 1.1.0", + "cw20", + "serde", + "thiserror", +] + [[package]] name = "cpufeatures" version = "0.2.7" @@ -462,6 +502,18 @@ dependencies = [ "zeroize", ] +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -648,9 +700,12 @@ dependencies = [ name = "cw20-wrapped-2" version = "0.1.0" dependencies = [ + "anyhow", "cosmwasm-schema", "cosmwasm-std", "cosmwasm-storage", + "counter", + "cw-multi-test", "cw2 0.13.4", "cw20", "cw20-base", @@ -691,7 +746,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "syn 2.0.23", + "syn 2.0.90", ] [[package]] @@ -702,7 +757,7 @@ checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" dependencies = [ "darling_core", "quote", - "syn 2.0.23", + "syn 2.0.90", ] [[package]] @@ -715,6 +770,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "der" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "derivative" version = "2.2.0" @@ -742,6 +807,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" dependencies = [ "block-buffer 0.10.4", + "const-oid", "crypto-common", "subtle", ] @@ -784,10 +850,24 @@ version = "0.14.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "413301934810f597c1d19ca71c8710e99a3f1ba28a0d2ebc01551a2daeea3c5c" dependencies = [ - "der", - "elliptic-curve", - "rfc6979", - "signature", + "der 0.6.1", + "elliptic-curve 0.12.3", + "rfc6979 0.3.1", + "signature 1.6.4", +] + +[[package]] +name = "ecdsa" +version = "0.16.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0997c976637b606099b9985693efa3581e84e41f5c11ba5255f88711058ad428" +dependencies = [ + "der 0.7.9", + "digest 0.10.6", + "elliptic-curve 0.13.7", + "rfc6979 0.4.0", + "signature 2.1.0", + "spki 0.7.3", ] [[package]] @@ -817,16 +897,35 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e7bb888ab5300a19b8e5bceef25ac745ad065f3c9f7efc6de1b91958110891d3" dependencies = [ - "base16ct", - "crypto-bigint", - "der", + "base16ct 0.1.1", + "crypto-bigint 0.4.9", + "der 0.6.1", + "digest 0.10.6", + "ff 0.12.1", + "generic-array", + "group 0.12.1", + "pkcs8 0.9.0", + "rand_core 0.6.4", + "sec1 0.3.0", + "subtle", + "zeroize", +] + +[[package]] +name = "elliptic-curve" +version = "0.13.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9775b22bc152ad86a0cf23f0f348b884b26add12bf741e7ffc4d4ab2ab4d205" +dependencies = [ + "base16ct 0.2.0", + "crypto-bigint 0.5.5", "digest 0.10.6", - "ff", + "ff 0.13.0", "generic-array", - "group", - "pkcs8", + "group 0.13.0", + "pkcs8 0.10.2", "rand_core 0.6.4", - "sec1", + "sec1 0.7.3", "subtle", "zeroize", ] @@ -869,7 +968,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.23", + "syn 2.0.90", ] [[package]] @@ -918,6 +1017,16 @@ dependencies = [ "subtle", ] +[[package]] +name = "ff" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" +dependencies = [ + "rand_core 0.6.4", + "subtle", +] + [[package]] name = "fnv" version = "1.0.7" @@ -938,6 +1047,7 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", + "zeroize", ] [[package]] @@ -974,7 +1084,7 @@ version = "0.1.0" dependencies = [ "accountant", "anyhow", - "base64", + "base64 0.13.1", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test", @@ -998,7 +1108,18 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5dfbfb3a6cfbd390d5c9564ab283a0349b9b9fcd46a706c1eb10e0db70bfbac7" dependencies = [ - "ff", + "ff 0.12.1", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff 0.13.0", "rand_core 0.6.4", "subtle", ] @@ -1148,12 +1269,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72c1e0b51e7ec0a97369623508396067a486bd0cbed95a2659a4b863d28cfc8b" dependencies = [ "cfg-if", - "ecdsa", - "elliptic-curve", + "ecdsa 0.14.8", + "elliptic-curve 0.12.3", "sha2 0.10.6", "sha3 0.10.7", ] +[[package]] +name = "k256" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cadb76004ed8e97623117f3df85b17aaa6626ab0b0831e6573f104df16cd1bcc" +dependencies = [ + "cfg-if", + "ecdsa 0.16.7", + "elliptic-curve 0.13.7", + "once_cell", + "sha2 0.10.6", + "signature 2.1.0", +] + [[package]] name = "keccak" version = "0.1.3" @@ -1305,7 +1440,7 @@ version = "0.1.0" dependencies = [ "accountant", "anyhow", - "base64", + "base64 0.13.1", "byteorder", "cosmwasm-schema", "cosmwasm-std", @@ -1395,8 +1530,18 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9eca2c590a5f85da82668fa685c09ce2888b9430e83299debf1f34b65fd4a4ba" dependencies = [ - "der", - "spki", + "der 0.6.1", + "spki 0.6.0", +] + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der 0.7.9", + "spki 0.7.3", ] [[package]] @@ -1425,9 +1570,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.63" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -1509,9 +1654,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.29" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -1606,11 +1751,21 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7743f17af12fa0b03b803ba12cd6a8d9483a587e89c69445e3909655c0b9fabb" dependencies = [ - "crypto-bigint", + "crypto-bigint 0.4.9", "hmac", "zeroize", ] +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac", + "subtle", +] + [[package]] name = "rkyv" version = "0.7.41" @@ -1676,9 +1831,9 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "schemars" -version = "0.8.12" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f" +checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" dependencies = [ "dyn-clone", "schemars_derive", @@ -1688,14 +1843,14 @@ dependencies = [ [[package]] name = "schemars_derive" -version = "0.8.12" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "109da1e6b197438deb6db99952990c7f959572794b80ff93707d55a232545e7c" +checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e" dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 1.0.109", + "syn 2.0.90", ] [[package]] @@ -1716,10 +1871,24 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3be24c1842290c45df0a7bf069e0c268a747ad05a192f2fd7dcfdbc1cba40928" dependencies = [ - "base16ct", - "der", + "base16ct 0.1.1", + "der 0.6.1", + "generic-array", + "pkcs8 0.9.0", + "subtle", + "zeroize", +] + +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct 0.2.0", + "der 0.7.9", "generic-array", - "pkcs8", + "pkcs8 0.10.2", "subtle", "zeroize", ] @@ -1732,9 +1901,9 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "serde" -version = "1.0.164" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] @@ -1768,24 +1937,24 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.164" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.23", + "syn 2.0.90", ] [[package]] name = "serde_derive_internals" -version = "0.26.0" +version = "0.29.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.90", ] [[package]] @@ -1803,7 +1972,7 @@ dependencies = [ name = "serde_wormhole" version = "0.1.0" dependencies = [ - "base64", + "base64 0.13.1", "itoa", "serde", "serde_bytes", @@ -1880,6 +2049,16 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "signature" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e1788eed21689f9cf370582dfc467ef36ed9c707f073528ddafa8d83e3b8500" +dependencies = [ + "digest 0.10.6", + "rand_core 0.6.4", +] + [[package]] name = "simdutf8" version = "0.1.4" @@ -1899,7 +2078,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67cf02bbac7a337dc36e4f5a693db6c21e7863f45070f7064577eb4367a3212b" dependencies = [ "base64ct", - "der", + "der 0.6.1", +] + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der 0.7.9", ] [[package]] @@ -1933,9 +2122,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.23" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59fb7d6d8281a51045d62b8eb3a7d1ce347b76f312af50cd3dc0af39c87c1737" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -1992,7 +2181,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.23", + "syn 2.0.90", ] [[package]] @@ -2061,7 +2250,7 @@ checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74" dependencies = [ "proc-macro2", "quote", - "syn 2.0.23", + "syn 2.0.90", ] [[package]] @@ -2079,18 +2268,6 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" -[[package]] -name = "uint" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76f64bba2c53b04fcab63c01a7d7427eadc821e3bc48c34dc9ba29c501164b52" -dependencies = [ - "byteorder", - "crunchy", - "hex", - "static_assertions", -] - [[package]] name = "unicode-ident" version = "1.0.8" @@ -2665,7 +2842,7 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-multi-test", - "k256", + "k256 0.11.6", "schemars", "serde", "serde_wormhole", @@ -2682,7 +2859,7 @@ dependencies = [ "cw-multi-test", "generic-array", "hex", - "k256", + "k256 0.11.6", "schemars", "serde", "serde-json-wasm 0.4.1", diff --git a/cosmwasm/Cargo.toml b/cosmwasm/Cargo.toml index e2843522e9..6f3079e2ed 100644 --- a/cosmwasm/Cargo.toml +++ b/cosmwasm/Cargo.toml @@ -13,7 +13,8 @@ members = [ "packages/cw_transcode", "contracts/wormhole-ibc", "contracts/wormchain-ibc-receiver", - "contracts/ibc-translator" + "contracts/ibc-translator", + "contracts/mock-counter" ] # Needed to prevent unwanted feature unification between normal builds and dev builds. See diff --git a/cosmwasm/contracts/cw20-wrapped/Cargo.toml b/cosmwasm/contracts/cw20-wrapped/Cargo.toml index 13ec0b6321..499cb659f6 100644 --- a/cosmwasm/contracts/cw20-wrapped/Cargo.toml +++ b/cosmwasm/contracts/cw20-wrapped/Cargo.toml @@ -23,3 +23,9 @@ cw2 = { version = "0.13.2" } cw20 = { version = "0.13.2" } cw20-base = { version = "0.13.2", features = ["library"] } thiserror = { version = "1.0.31" } + +[dev-dependencies] +cw-multi-test = "0.14" +# this is just used for testing cw20-wrapped's hook logic +counter = { path = "../mock-counter", features = ["interface"] } +anyhow = "1.0.40" \ No newline at end of file diff --git a/cosmwasm/contracts/cw20-wrapped/src/contract.rs b/cosmwasm/contracts/cw20-wrapped/src/contract.rs index 4b4d127259..46ebfaa74a 100644 --- a/cosmwasm/contracts/cw20-wrapped/src/contract.rs +++ b/cosmwasm/contracts/cw20-wrapped/src/contract.rs @@ -1,6 +1,6 @@ use cosmwasm_std::{ - to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, - Uint128, WasmMsg, + to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdError, + StdResult, Uint128, WasmMsg, }; #[cfg(not(feature = "library"))] @@ -167,12 +167,12 @@ fn execute_update_metadata( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::WrappedAssetInfo {} => to_binary(&query_wrapped_asset_info(deps)?), + QueryMsg::WrappedAssetInfo {} => to_json_binary(&query_wrapped_asset_info(deps)?), // inherited from cw20-base - QueryMsg::TokenInfo {} => to_binary(&query_token_info(deps)?), - QueryMsg::Balance { address } => to_binary(&query_balance(deps, address)?), + QueryMsg::TokenInfo {} => to_json_binary(&query_token_info(deps)?), + QueryMsg::Balance { address } => to_json_binary(&query_balance(deps, address)?), QueryMsg::Allowance { owner, spender } => { - to_binary(&query_allowance(deps, owner, spender)?) + to_json_binary(&query_allowance(deps, owner, spender)?) } } } diff --git a/cosmwasm/contracts/cw20-wrapped/tests/integration.rs b/cosmwasm/contracts/cw20-wrapped/tests/integration.rs index a0ee368542..0931625884 100644 --- a/cosmwasm/contracts/cw20-wrapped/tests/integration.rs +++ b/cosmwasm/contracts/cw20-wrapped/tests/integration.rs @@ -1,16 +1,18 @@ use cosmwasm_std::{ - from_slice, + from_json, testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage}, - Addr, Api, OwnedDeps, Response, Storage, Uint128, + to_json_binary, Addr, Api, OwnedDeps, Response, Storage, Uint128, }; use cosmwasm_storage::to_length_prefixed; -use cw20::TokenInfoResponse; +use counter::msg as counter_msgs; +use cw20::{AllowanceResponse, BalanceResponse, Expiration, TokenInfoResponse}; use cw20_wrapped_2::{ contract::{execute, instantiate, query}, - msg::{ExecuteMsg, InstantiateMsg, QueryMsg, WrappedAssetInfoResponse}, + msg::{ExecuteMsg, InitHook, InitMint, InstantiateMsg, QueryMsg, WrappedAssetInfoResponse}, state::{WrappedAssetInfo, KEY_WRAPPED_ASSET}, ContractError, }; +use cw_multi_test::{App, AppBuilder, ContractWrapper, Executor}; static INITIALIZER: &str = "addr0000"; static RECIPIENT: &str = "addr2222"; @@ -19,7 +21,7 @@ static SENDER: &str = "addr3333"; fn get_wrapped_asset_info(storage: &S) -> WrappedAssetInfo { let key = to_length_prefixed(KEY_WRAPPED_ASSET); let data = storage.get(&key).expect("data should exist"); - from_slice(&data).expect("invalid data") + from_json(&data).expect("invalid data") } fn do_init() -> OwnedDeps { @@ -104,7 +106,7 @@ fn check_balance( fn check_token_details(deps: &OwnedDeps, supply: Uint128) { let query_response = query(deps.as_ref(), mock_env(), QueryMsg::TokenInfo {}).unwrap(); assert_eq!( - from_slice::(query_response.as_slice()).unwrap(), + from_json::(query_response.as_slice()).unwrap(), TokenInfoResponse { name: "Integers (Wormhole)".into(), symbol: "INT".into(), @@ -126,7 +128,7 @@ fn query_works() { let query_response = query(deps.as_ref(), mock_env(), QueryMsg::WrappedAssetInfo {}).unwrap(); assert_eq!( - from_slice::(query_response.as_slice()).unwrap(), + from_json::(query_response.as_slice()).unwrap(), WrappedAssetInfoResponse { asset_chain: 1, asset_address: vec![1; 32].into(), @@ -175,3 +177,1332 @@ fn transfer_works() { check_balance(&deps, &sender, &Uint128::new(123)); check_balance(&deps, &recipient, &Uint128::new(123_123_000)); } + +pub(crate) struct Cw20App { + app: App, + admin: Addr, + user: Addr, + cw20_wrapped_contract: Addr, + cw20_code_id: u64, +} + +pub(crate) fn create_cw20_wrapped_app(instantiate_msg: Option) -> Cw20App { + let instantiate_msg = instantiate_msg.unwrap_or(InstantiateMsg { + name: "Integers".into(), + symbol: "INT".into(), + asset_chain: 1, + asset_address: vec![1; 32].into(), + decimals: 10, + mint: None, + init_hook: None, + }); + + let mut app = AppBuilder::new().build(|_, _, _| {}); + + let admin = Addr::unchecked("admin"); + let user = Addr::unchecked("user"); + + let cw20_wrapped_wrapper = ContractWrapper::new_with_empty(execute, instantiate, query); + + let code_id = app.store_code(Box::new(cw20_wrapped_wrapper)); + + let contract_addr = app + .instantiate_contract( + code_id, + admin.clone(), + &instantiate_msg, + &[], + "cw20_wrapped", + Some(admin.to_string()), + ) + .unwrap(); + + Cw20App { + app, + admin, + user, + cw20_wrapped_contract: contract_addr, + cw20_code_id: code_id, + } +} + +pub(crate) struct Cw20AndCounterApp { + app: App, + admin: Addr, + user: Addr, + cw20_wrapped_contract: Addr, + cw20_code_id: u64, + counter_address: Addr, +} + +pub(crate) fn create_cw20_and_counter() -> Cw20AndCounterApp { + let Cw20App { + mut app, + admin, + user, + cw20_code_id, + cw20_wrapped_contract, + } = create_cw20_wrapped_app(None); + + let counter_code_id = app.store_code(Box::new(ContractWrapper::new( + counter::contract::execute, + counter::contract::instantiate, + counter::contract::query, + ))); + + let counter_address = app + .instantiate_contract( + counter_code_id, + user.clone(), + &counter_msgs::InstantiateMsg {}, + &[], + "counter", + None, + ) + .unwrap(); + + let initial_count: counter_msgs::CountResponse = app + .wrap() + .query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + ) + .unwrap(); + assert_eq!(initial_count.count, 0, "Initial count should be 0"); + + Cw20AndCounterApp { + app, + admin, + user, + cw20_wrapped_contract, + cw20_code_id, + counter_address, + } +} + +#[test] +pub fn instantiate_works() -> Result<(), anyhow::Error> { + let Cw20App { + cw20_wrapped_contract, + app, + admin, + user: _user, + .. + } = create_cw20_wrapped_app(None); + + let asset_info: WrappedAssetInfoResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::WrappedAssetInfo {}, + )?; + + assert_eq!( + asset_info, + WrappedAssetInfoResponse { + asset_chain: 1, + asset_address: vec![1; 32].into(), + bridge: admin, + } + ); + + let token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + + assert_eq!( + token_info, + TokenInfoResponse { + name: "Integers (Wormhole)".into(), + symbol: "INT".into(), + decimals: 10, + total_supply: Uint128::zero(), + } + ); + + Ok(()) +} + +#[test] +pub fn instantiate_with_minter() -> Result<(), anyhow::Error> { + let initial_balance_user = Addr::unchecked("user_with_initial_balance"); + + let Cw20App { + cw20_wrapped_contract, + mut app, + admin, + user, + .. + } = create_cw20_wrapped_app(Some(InstantiateMsg { + name: "Integers".into(), + symbol: "INT".into(), + asset_chain: 1, + asset_address: vec![1; 32].into(), + decimals: 6, + mint: Some(InitMint { + recipient: initial_balance_user.to_string(), + amount: Uint128::from(1_000_000u128), + }), + init_hook: None, + })); + + let init_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: initial_balance_user.to_string(), + }, + )?; + assert_eq!( + init_balance.balance, + Uint128::from(1_000_000u128), + "User should have balance from tge" + ); + + let mint_info = ExecuteMsg::Mint { + recipient: user.to_string(), + amount: 750_000u128.into(), + }; + + let unauthorized_user_mint = + app.execute_contract(user.clone(), cw20_wrapped_contract.clone(), &mint_info, &[]); + assert!( + unauthorized_user_mint.is_err(), + "Only Admin should be able to mint" + ); + + // Admin mints to user + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &mint_info, + &[], + )?; + + let user_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: user.to_string(), + }, + )?; + + assert_eq!( + user_balance.balance, + Uint128::from(750_000u128), + "User should have a balance since the admin minted to the user's address" + ); + + Ok(()) +} + +#[test] +fn instantiate_with_inithook() -> Result<(), anyhow::Error> { + let Cw20AndCounterApp { + mut app, + admin, + cw20_code_id, + counter_address, + .. + } = create_cw20_and_counter(); + + let instantiation_with_init_hook = InstantiateMsg { + name: "Floats".into(), + symbol: "FLT".into(), + asset_chain: 1, + asset_address: vec![2; 32].into(), + decimals: 6, + mint: None, + init_hook: Some(InitHook { + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + contract_addr: counter_address.to_string(), + }), + }; + + app.instantiate_contract( + cw20_code_id, + admin.clone(), + &instantiation_with_init_hook, + &[], + "floats_cw20", + None, + )?; + + let incremented_count: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + incremented_count.count, 1, + "After FLT is instantiated, the count should be incremented" + ); + + Ok(()) +} + +#[test] +fn update_metadata_functionality() -> Result<(), anyhow::Error> { + let Cw20App { + cw20_wrapped_contract, + mut app, + admin, + user, + .. + } = create_cw20_wrapped_app(None); + + // Verify initial metadata + let initial_token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + + assert_eq!( + initial_token_info, + TokenInfoResponse { + name: "Integers (Wormhole)".into(), + symbol: "INT".into(), + decimals: 10, + total_supply: Uint128::zero(), + } + ); + + // Try to update metadata as non-admin (should fail) + let unauthorized_update = app.execute_contract( + user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::UpdateMetadata { + name: "Updated Token".into(), + symbol: "UPDT".into(), + }, + &[], + ); + assert!( + unauthorized_update.is_err(), + "Non-admin should not be able to update metadata" + ); + + // Verify metadata hasn't changed after failed update + let unchanged_token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + assert_eq!(unchanged_token_info, initial_token_info); + + // Update metadata as admin + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::UpdateMetadata { + name: "Updated Integers".into(), + symbol: "UINT".into(), + }, + &[], + )?; + + // Verify metadata was updated + let updated_token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + + assert_eq!( + updated_token_info, + TokenInfoResponse { + name: "Updated Integers (Wormhole)".into(), + symbol: "UINT".into(), + decimals: 10, + total_supply: Uint128::zero(), + } + ); + + // Update metadata again to verify multiple updates work + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::UpdateMetadata { + name: "Final Name".into(), + symbol: "FINAL".into(), + }, + &[], + )?; + + // Verify final metadata update + let final_token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + + assert_eq!( + final_token_info, + TokenInfoResponse { + name: "Final Name (Wormhole)".into(), + symbol: "FINAL".into(), + decimals: 10, + total_supply: Uint128::zero(), + } + ); + + Ok(()) +} + +#[test] +fn send_tokens_functionality() -> Result<(), anyhow::Error> { + let Cw20AndCounterApp { + mut app, + admin, + user, + counter_address, + cw20_wrapped_contract: cw20_address, + .. + } = create_cw20_and_counter(); + + app.execute_contract( + admin.clone(), + cw20_address.clone(), + &ExecuteMsg::Mint { + recipient: user.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + let send_to_counter_app = ExecuteMsg::Send { + contract: counter_address.to_string(), + amount: Uint128::from(1_000_000u128), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }; + + let failing_send = app.execute_contract( + admin.clone(), + cw20_address.clone(), + &send_to_counter_app, + &[], + ); + assert!( + failing_send.is_err(), + "Admin should not be able to send to the counter contract since it has no balance" + ); + + // Now send it as user who actually has a balance + let send_resp = app.execute_contract( + user.clone(), + cw20_address.clone(), + &send_to_counter_app, + &[], + ); + println!("send response {:?}", send_resp); + send_resp?; + + let incremented_count: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + incremented_count.count, 1, + "Post-send to counter contract, the count should be incremented" + ); + + // Try to send it again as User to double check that things fail properly + let failing_send = app.execute_contract( + user.clone(), + cw20_address.clone(), + &send_to_counter_app, + &[], + ); + assert!( + failing_send.is_err(), + "User should not be able to send to the counter contract since it has no balance" + ); + + let incremented_count: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + incremented_count.count, 1, + "Count should still be 1 since it hasn't been incremented again" + ); + + Ok(()) +} + +// Test that transfer works correctly +#[test] +fn transfer_tokens_functionality() -> Result<(), anyhow::Error> { + let Cw20App { + cw20_wrapped_contract, + mut app, + admin, + user, + .. + } = create_cw20_wrapped_app(None); + + // Mint some tokens to the user + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Mint { + recipient: user.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + // Validate the user has the correct balance + let user_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: user.to_string(), + }, + )?; + assert_eq!(user_balance.balance, Uint128::from(1_000_000u128)); + + // Attempt to transfer as the admin which should fail + let admin_transfer = app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Transfer { + recipient: admin.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + ); + assert!( + admin_transfer.is_err(), + "Admin should not be able to transfer tokens since it has no balance" + ); + + // Attempt to transfer too much as the user which should fail + let user_transfer = app.execute_contract( + user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Transfer { + recipient: admin.to_string(), + amount: 1_000_001u128.into(), + }, + &[], + ); + assert!( + user_transfer.is_err(), + "User should not be able to transfer more tokens than they have" + ); + + // Transfer 500,000 tokens from the user to the admin + app.execute_contract( + user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Transfer { + recipient: admin.to_string(), + amount: 500_000u128.into(), + }, + &[], + )?; + + // Validate the user has the correct balance + let user_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: user.to_string(), + }, + )?; + assert_eq!(user_balance.balance, Uint128::from(500_000u128)); + + // Validate the admin has the correct balance + let admin_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: admin.to_string(), + }, + )?; + assert_eq!(admin_balance.balance, Uint128::from(500_000u128)); + + Ok(()) +} + +#[test] +fn burn_functionality() -> Result<(), anyhow::Error> { + let Cw20App { + cw20_wrapped_contract, + mut app, + admin, + .. + } = create_cw20_wrapped_app(None); + + let burn_user = Addr::unchecked("burner"); + let approved_burner = Addr::unchecked("approved_burner"); + + // Mint some tokens to the burn user + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Mint { + recipient: burn_user.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + // Verify initial balance + let initial_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: burn_user.to_string(), + }, + )?; + assert_eq!(initial_balance.balance, Uint128::from(1_000_000u128)); + + // Try to burn tokens without allowance (should fail) + let unauthorized_self_burn = app.execute_contract( + burn_user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Burn { + account: burn_user.to_string(), + amount: 500_000u128.into(), + }, + &[], + ); + assert!( + unauthorized_self_burn.is_err(), + "Should not be able to burn tokens without allowance from another address" + ); + + // Try to set allowance for self (should fail) + let self_allowance = app.execute_contract( + burn_user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: burn_user.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + ); + assert!( + self_allowance.is_err(), + "Should not be able to set allowance for self" + ); + + // Set up allowance for approved burner + app.execute_contract( + burn_user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: approved_burner.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + )?; + + // Try to burn more tokens than allowance (should fail) + let excessive_burn = app.execute_contract( + approved_burner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Burn { + account: burn_user.to_string(), + amount: 750_000u128.into(), + }, + &[], + ); + assert!( + excessive_burn.is_err(), + "Should not be able to burn more tokens than allowed" + ); + + // Successfully burn tokens with proper allowance + app.execute_contract( + approved_burner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Burn { + account: burn_user.to_string(), + amount: 500_000u128.into(), + }, + &[], + )?; + + // Verify remaining balance + let remaining_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: burn_user.to_string(), + }, + )?; + assert_eq!(remaining_balance.balance, Uint128::from(500_000u128)); + + // Verify total supply has decreased + let token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + assert_eq!( + token_info.total_supply, + Uint128::from(500_000u128), + "Total supply should reflect burned tokens" + ); + + // Verify allowance was properly decreased + let allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Allowance { + owner: burn_user.to_string(), + spender: approved_burner.to_string(), + }, + )?; + assert_eq!( + allowance.allowance, + Uint128::zero(), + "Allowance should be depleted after burn" + ); + + Ok(()) +} + +#[test] +/// BurnFrom uses identical logic to Burn +fn burn_from_functionality() -> Result<(), anyhow::Error> { + let Cw20App { + cw20_wrapped_contract, + mut app, + admin, + .. + } = create_cw20_wrapped_app(None); + + let burn_user = Addr::unchecked("burner"); + let approved_burner = Addr::unchecked("approved_burner"); + + // Mint some tokens to the burn user + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Mint { + recipient: burn_user.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + // Verify initial balance + let initial_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: burn_user.to_string(), + }, + )?; + assert_eq!(initial_balance.balance, Uint128::from(1_000_000u128)); + + // Try to burn tokens without allowance (should fail) + let unauthorized_self_burn = app.execute_contract( + burn_user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::BurnFrom { + owner: burn_user.to_string(), + amount: 500_000u128.into(), + }, + &[], + ); + assert!( + unauthorized_self_burn.is_err(), + "Should not be able to burn tokens without allowance from another address" + ); + + // Try to set allowance for self (should fail) + let self_allowance = app.execute_contract( + burn_user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: burn_user.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + ); + assert!( + self_allowance.is_err(), + "Should not be able to set allowance for self" + ); + + // Set up allowance for approved burner + app.execute_contract( + burn_user.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: approved_burner.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + )?; + + // Try to burn more tokens than allowance (should fail) + let excessive_burn = app.execute_contract( + approved_burner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::BurnFrom { + owner: burn_user.to_string(), + amount: 750_000u128.into(), + }, + &[], + ); + assert!( + excessive_burn.is_err(), + "Should not be able to burn more tokens than allowed" + ); + + // Successfully burn tokens with proper allowance + app.execute_contract( + approved_burner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::BurnFrom { + owner: burn_user.to_string(), + amount: 500_000u128.into(), + }, + &[], + )?; + + // Verify remaining balance + let remaining_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Balance { + address: burn_user.to_string(), + }, + )?; + assert_eq!(remaining_balance.balance, Uint128::from(500_000u128)); + + // Verify total supply has decreased + let token_info: TokenInfoResponse = app + .wrap() + .query_wasm_smart(cw20_wrapped_contract.clone(), &QueryMsg::TokenInfo {})?; + assert_eq!( + token_info.total_supply, + Uint128::from(500_000u128), + "Total supply should reflect burned tokens" + ); + + // Verify allowance was properly decreased + let allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Allowance { + owner: burn_user.to_string(), + spender: approved_burner.to_string(), + }, + )?; + assert_eq!( + allowance.allowance, + Uint128::zero(), + "Allowance should be depleted after burn" + ); + + Ok(()) +} + +#[test] +fn allowance_management() -> Result<(), anyhow::Error> { + let Cw20App { + cw20_wrapped_contract, + mut app, + admin, + .. + } = create_cw20_wrapped_app(None); + + let token_owner = Addr::unchecked("token_owner"); + let spender = Addr::unchecked("spender"); + + // Mint tokens to the token owner + app.execute_contract( + admin.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Mint { + recipient: token_owner.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + // Test that setting allowance for self fails + let self_allowance_increase = app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: token_owner.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + ); + assert!( + self_allowance_increase.is_err(), + "Should not be able to increase allowance for self" + ); + + // Test that decreasing allowance for self also fails + let self_allowance_decrease = app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::DecreaseAllowance { + spender: token_owner.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + ); + assert!( + self_allowance_decrease.is_err(), + "Should not be able to decrease allowance for self" + ); + + // Test increasing allowance for another address + app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: spender.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + )?; + + // Verify initial allowance + let allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!(allowance.allowance, Uint128::from(500_000u128)); + + // Test increasing allowance again (should add to existing) + app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: spender.to_string(), + amount: 200_000u128.into(), + expires: None, + }, + &[], + )?; + + // Verify cumulative allowance + let increased_allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!(increased_allowance.allowance, Uint128::from(700_000u128)); + + // Test decreasing allowance + app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::DecreaseAllowance { + spender: spender.to_string(), + amount: 300_000u128.into(), + expires: None, + }, + &[], + )?; + + // Verify decreased allowance + let decreased_allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!(decreased_allowance.allowance, Uint128::from(400_000u128)); + + // Decreasing allowance bellow zero results in a zero allowance + let _ = app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::DecreaseAllowance { + spender: spender.to_string(), + amount: 5_000_000u128.into(), + expires: None, + }, + &[], + )?; + + // Verify allowance is 0 + let zero_allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_wrapped_contract.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!(zero_allowance.allowance, Uint128::zero()); + + // Test expiration + let expiration = Expiration::AtHeight(app.block_info().height + 1); + + app.execute_contract( + token_owner.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: spender.to_string(), + amount: 100_000u128.into(), + expires: Some(expiration), + }, + &[], + )?; + + // Move to next block + app.update_block(|block| block.height += 2); + + // Try to use expired allowance (should fail) + let burn_with_expired = app.execute_contract( + spender.clone(), + cw20_wrapped_contract.clone(), + &ExecuteMsg::Burn { + account: token_owner.to_string(), + amount: 100_000u128.into(), + }, + &[], + ); + assert!( + burn_with_expired.is_err(), + "Should not be able to use expired allowance" + ); + + Ok(()) +} + +#[test] +fn send_from_functionality() -> Result<(), anyhow::Error> { + let Cw20AndCounterApp { + mut app, + admin, + user: spender, + counter_address, + cw20_wrapped_contract: cw20_address, + .. + } = create_cw20_and_counter(); + + let token_owner = Addr::unchecked("token_owner"); + + // Mint tokens to the token owner + app.execute_contract( + admin.clone(), + cw20_address.clone(), + &ExecuteMsg::Mint { + recipient: token_owner.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + // Try to send_from without allowance (should fail) + let unauthorized_send = app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 500_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + ); + assert!( + unauthorized_send.is_err(), + "Should not be able to send_from without allowance" + ); + + // Set up allowance for spender + app.execute_contract( + token_owner.clone(), + cw20_address.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: spender.to_string(), + amount: 500_000u128.into(), + expires: None, + }, + &[], + )?; + + // Try to send_from more than allowance (should fail) + let excessive_send = app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 600_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + ); + assert!( + excessive_send.is_err(), + "Should not be able to send_from more than allowance" + ); + + // Successfully send tokens and increment counter + app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 300_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + )?; + + // Verify counter was incremented + let counter_response: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!(counter_response.count, 1, "Counter should be incremented"); + + // Verify token owner's balance was decreased + let owner_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_address.clone(), + &QueryMsg::Balance { + address: token_owner.to_string(), + }, + )?; + assert_eq!(owner_balance.balance, Uint128::from(700_000u128)); + + // Verify allowance was decreased + let updated_allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_address.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!(updated_allowance.allowance, Uint128::from(200_000u128)); + + // Test with expired allowance + let expiration = Expiration::AtHeight(app.block_info().height + 1); + + // Set new allowance with expiration + app.execute_contract( + token_owner.clone(), + cw20_address.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: spender.to_string(), + amount: 100_000u128.into(), + expires: Some(expiration), + }, + &[], + )?; + + // Move past expiration + app.update_block(|block| block.height += 2); + + // Try to send_from with expired allowance (should fail) + let expired_send = app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 50_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + ); + assert!( + expired_send.is_err(), + "Should not be able to send_from with expired allowance" + ); + + // Verify counter wasn't incremented again + let final_count: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + final_count.count, 1, + "Counter should not be incremented again" + ); + + Ok(()) +} + +#[test] +fn multiple_send_from_functionality() -> Result<(), anyhow::Error> { + let Cw20AndCounterApp { + mut app, + admin, + user: spender, + counter_address, + cw20_wrapped_contract: cw20_address, + .. + } = create_cw20_and_counter(); + + let token_owner = Addr::unchecked("token_owner"); + + // Mint tokens to the token owner + app.execute_contract( + admin.clone(), + cw20_address.clone(), + &ExecuteMsg::Mint { + recipient: token_owner.to_string(), + amount: 1_000_000u128.into(), + }, + &[], + )?; + + // Set up allowance for spender + app.execute_contract( + token_owner.clone(), + cw20_address.clone(), + &ExecuteMsg::IncreaseAllowance { + spender: spender.to_string(), + amount: 750_000u128.into(), + expires: None, + }, + &[], + )?; + + // First send_from operation + app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 250_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + )?; + + // Verify first operation results + let count_after_first: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + count_after_first.count, 1, + "Counter should be incremented once" + ); + + let allowance_after_first: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_address.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!( + allowance_after_first.allowance, + Uint128::from(500_000u128), + "Allowance should be decreased by first send" + ); + + // Second send_from operation + app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 200_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + )?; + + // Verify second operation results + let count_after_second: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + count_after_second.count, 2, + "Counter should be incremented twice" + ); + + let allowance_after_second: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_address.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!( + allowance_after_second.allowance, + Uint128::from(300_000u128), + "Allowance should be decreased by second send" + ); + + // Third send_from operation + app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 300_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + )?; + + // Verify final state + let final_count: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + final_count.count, 3, + "Counter should be incremented three times" + ); + + let final_allowance: AllowanceResponse = app.wrap().query_wasm_smart( + cw20_address.clone(), + &QueryMsg::Allowance { + owner: token_owner.to_string(), + spender: spender.to_string(), + }, + )?; + assert_eq!( + final_allowance.allowance, + Uint128::zero(), + "Allowance should be fully spent" + ); + + let final_balance: BalanceResponse = app.wrap().query_wasm_smart( + cw20_address.clone(), + &QueryMsg::Balance { + address: token_owner.to_string(), + }, + )?; + assert_eq!( + final_balance.balance, + Uint128::from(250_000u128), + "Owner balance should reflect all three sends" + ); + + // Try one more send_from operation with depleted allowance (should fail) + let depleted_allowance_send = app.execute_contract( + spender.clone(), + cw20_address.clone(), + &ExecuteMsg::SendFrom { + owner: token_owner.to_string(), + contract: counter_address.to_string(), + amount: 100_000u128.into(), + msg: to_json_binary(&counter_msgs::ExecuteMsg::Increment {})?, + }, + &[], + ); + assert!( + depleted_allowance_send.is_err(), + "Should not be able to send_from after allowance is depleted" + ); + + // Verify counter wasn't incremented by failed transaction + let final_failed_count: counter_msgs::CountResponse = app.wrap().query_wasm_smart( + counter_address.clone(), + &counter_msgs::QueryMsg::GetCount {}, + )?; + assert_eq!( + final_failed_count.count, 3, + "Counter should not be incremented after failed send" + ); + + Ok(()) +} diff --git a/cosmwasm/contracts/global-accountant/src/contract.rs b/cosmwasm/contracts/global-accountant/src/contract.rs index 63b35352b3..19303d0982 100644 --- a/cosmwasm/contracts/global-accountant/src/contract.rs +++ b/cosmwasm/contracts/global-accountant/src/contract.rs @@ -9,7 +9,7 @@ use anyhow::{ensure, Context}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - from_binary, to_binary, Binary, ConversionOverflowError, Deps, DepsMut, Empty, Env, Event, + from_json, to_json_binary, Binary, ConversionOverflowError, Deps, DepsMut, Empty, Env, Event, MessageInfo, Order, Response, StdError, StdResult, Uint256, }; use cw2::set_contract_version; @@ -110,7 +110,7 @@ fn submit_observations( .context("failed to calculate quorum")?; let observations: Vec = - from_binary(&observations).context("failed to parse `Observations`")?; + from_json(&observations).context("failed to parse `Observations`")?; let mut responses = Vec::with_capacity(observations.len()); let mut events = Vec::with_capacity(observations.len()); @@ -139,7 +139,7 @@ fn submit_observations( } } - let data = to_binary(&responses).context("failed to serialize transfer details")?; + let data = to_json_binary(&responses).context("failed to serialize transfer details")?; Ok(Response::new() .add_attribute("action", "submit_observations") @@ -497,21 +497,22 @@ fn handle_tokenbridge_vaa( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::Balance(key) => query_balance(deps, key).and_then(|resp| to_binary(&resp)), + QueryMsg::Balance(key) => query_balance(deps, key).and_then(|resp| to_json_binary(&resp)), QueryMsg::AllAccounts { start_after, limit } => { - query_all_accounts(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_accounts(deps, start_after, limit).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllTransfers { start_after, limit } => { - query_all_transfers(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_transfers(deps, start_after, limit).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllPendingTransfers { start_after, limit } => { - query_all_pending_transfers(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_pending_transfers(deps, start_after, limit) + .and_then(|resp| to_json_binary(&resp)) } QueryMsg::Modification { sequence } => { - query_modification(deps, sequence).and_then(|resp| to_binary(&resp)) + query_modification(deps, sequence).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllModifications { start_after, limit } => { - query_all_modifications(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_modifications(deps, start_after, limit).and_then(|resp| to_json_binary(&resp)) } QueryMsg::ValidateTransfer { transfer } => validate_transfer(deps, &transfer) .map_err(|e| { @@ -519,21 +520,20 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { - query_chain_registration(deps, chain).and_then(|resp| to_binary(&resp)) + query_chain_registration(deps, chain).and_then(|resp| to_json_binary(&resp)) } QueryMsg::MissingObservations { guardian_set, index, - } => { - query_missing_observations(deps, guardian_set, index).and_then(|resp| to_binary(&resp)) - } + } => query_missing_observations(deps, guardian_set, index) + .and_then(|resp| to_json_binary(&resp)), QueryMsg::TransferStatus(key) => { - query_transfer_status(deps, &key).and_then(|resp| to_binary(&resp)) + query_transfer_status(deps, &key).and_then(|resp| to_json_binary(&resp)) } QueryMsg::BatchTransferStatus(keys) => { - query_batch_transfer_status(deps, keys).and_then(|resp| to_binary(&resp)) + query_batch_transfer_status(deps, keys).and_then(|resp| to_json_binary(&resp)) } } } diff --git a/cosmwasm/contracts/global-accountant/tests/chain_registration.rs b/cosmwasm/contracts/global-accountant/tests/chain_registration.rs index 9c804f00df..25b4b38609 100644 --- a/cosmwasm/contracts/global-accountant/tests/chain_registration.rs +++ b/cosmwasm/contracts/global-accountant/tests/chain_registration.rs @@ -1,6 +1,6 @@ mod helpers; -use cosmwasm_std::{to_binary, Event}; +use cosmwasm_std::{to_json_binary, Event}; use global_accountant::msg::ChainRegistrationResponse; use helpers::*; use wormhole_sdk::{ @@ -214,7 +214,7 @@ fn bad_serialization() { let (v, _) = sign_vaa_body(&wh, create_vaa_body()); // Rather than using the wormhole wire format use cosmwasm json. - let data = to_binary(&v).unwrap(); + let data = to_json_binary(&v).unwrap(); let err = contract .submit_vaas(vec![data]) diff --git a/cosmwasm/contracts/global-accountant/tests/missing_observations.rs b/cosmwasm/contracts/global-accountant/tests/missing_observations.rs index 5fd8c5cce7..31a5ec2991 100644 --- a/cosmwasm/contracts/global-accountant/tests/missing_observations.rs +++ b/cosmwasm/contracts/global-accountant/tests/missing_observations.rs @@ -1,7 +1,7 @@ mod helpers; use accountant::state::transfer; -use cosmwasm_std::{to_binary, Uint256}; +use cosmwasm_std::{to_json_binary, Uint256}; use global_accountant::msg::Observation; use helpers::*; use wormhole_sdk::{token::Message, Address, Amount, Chain}; @@ -44,7 +44,7 @@ fn missing_observations() { let o = create_observation(); let digest = o.digest().unwrap(); - let data = to_binary(&[o.clone()]).unwrap(); + let data = to_json_binary(&[o.clone()]).unwrap(); let signatures = sign_observations(&wh, &data); // Don't submit enough signatures for the transfer to reach quorum. @@ -95,7 +95,7 @@ fn different_observations() { .unwrap() as usize; let first = create_observation(); - let first_data = to_binary(&[first.clone()]).unwrap(); + let first_data = to_json_binary(&[first.clone()]).unwrap(); let first_signatures = sign_observations(&wh, &first_data); // Don't submit enough signatures for the transfer to reach quorum. @@ -122,7 +122,7 @@ fn different_observations() { ] .into(); second.payload = serde_wormhole::to_vec(&msg).map(From::from).unwrap(); - let second_data = to_binary(&[second.clone()]).unwrap(); + let second_data = to_json_binary(&[second.clone()]).unwrap(); let second_signatures = sign_observations(&wh, &second_data); // Submit a different set of signatures for the second observation. @@ -167,7 +167,7 @@ fn guardian_set_change() { .unwrap() as usize; let o = create_observation(); - let data = to_binary(&[o.clone()]).unwrap(); + let data = to_json_binary(&[o.clone()]).unwrap(); let signatures = sign_observations(&wh, &data); // Don't submit enough signatures for the transfer to reach quorum. diff --git a/cosmwasm/contracts/global-accountant/tests/submit_observations.rs b/cosmwasm/contracts/global-accountant/tests/submit_observations.rs index 86b18521a1..41333262ea 100644 --- a/cosmwasm/contracts/global-accountant/tests/submit_observations.rs +++ b/cosmwasm/contracts/global-accountant/tests/submit_observations.rs @@ -3,7 +3,7 @@ mod helpers; use std::collections::BTreeMap; use accountant::state::{account, transfer, Kind, Modification, TokenAddress}; -use cosmwasm_std::{from_binary, to_binary, Binary, Event, Uint256}; +use cosmwasm_std::{from_json, to_json_binary, Binary, Event, Uint256}; use cw_multi_test::AppResponse; use global_accountant::msg::{Observation, ObservationStatus, SubmitObservationResponse}; use helpers::*; @@ -53,7 +53,7 @@ fn batch() { let index = wh.guardian_set_index(); - let obs = to_binary(&observations).unwrap(); + let obs = to_json_binary(&observations).unwrap(); let signatures = sign_observations(&wh, &obs); let quorum = wh .calculate_quorum(index, contract.app().block_info().height) @@ -62,7 +62,7 @@ fn batch() { for (i, s) in signatures.into_iter().enumerate() { let resp = contract.submit_observations(obs.clone(), index, s).unwrap(); - let status = from_binary::>(&resp.data.unwrap()) + let status = from_json::>(&resp.data.unwrap()) .unwrap() .into_iter() .map(|resp| (resp.key, resp.status)) @@ -161,7 +161,7 @@ fn duplicates() { register_emitters(&wh, &mut contract, COUNT); let index = wh.guardian_set_index(); - let obs = to_binary(&observations).unwrap(); + let obs = to_json_binary(&observations).unwrap(); let signatures = sign_observations(&wh, &obs); let quorum = wh .calculate_quorum(index, contract.app().block_info().height) @@ -172,7 +172,7 @@ fn duplicates() { // Submitting a duplicate signature is not an error for pending transfers. Submitting any // signature for a committed transfer is not an error as long as the digests match. let resp = contract.submit_observations(obs.clone(), index, s).unwrap(); - let status = from_binary::>(&resp.data.unwrap()) + let status = from_json::>(&resp.data.unwrap()) .unwrap() .into_iter() .map(|details| (details.key, details.status)) @@ -257,7 +257,7 @@ fn transfer_tokens( payload, }; - let obs = to_binary(&vec![o.clone()]).unwrap(); + let obs = to_json_binary(&vec![o.clone()]).unwrap(); let signatures = sign_observations(wh, &obs); let responses = signatures @@ -491,7 +491,7 @@ fn missing_wrapped_account() { let (_, responses) = transfer_tokens(&wh, &mut contract, key.clone(), msg, index, num_guardians).unwrap(); for mut resp in responses.into_iter().skip(quorum - 1) { - let r = from_binary::>(&resp.data.take().unwrap()).unwrap(); + let r = from_json::>(&resp.data.take().unwrap()).unwrap(); assert_eq!(key, r[0].key); if let ObservationStatus::Error(ref err) = r[0].status { assert!( @@ -555,7 +555,7 @@ fn missing_native_account() { let (_, responses) = transfer_tokens(&wh, &mut contract, key.clone(), msg, index, num_guardians).unwrap(); for mut resp in responses.into_iter().skip(quorum - 1) { - let r = from_binary::>(&resp.data.take().unwrap()).unwrap(); + let r = from_json::>(&resp.data.take().unwrap()).unwrap(); assert_eq!(key, r[0].key); if let ObservationStatus::Error(ref err) = r[0].status { assert!( @@ -733,7 +733,7 @@ fn unknown_emitter() { let (_, responses) = transfer_tokens(&wh, &mut contract, key.clone(), msg, index, num_guardians).unwrap(); for mut resp in responses.into_iter().skip(quorum - 1) { - let r = from_binary::>(&resp.data.take().unwrap()).unwrap(); + let r = from_json::>(&resp.data.take().unwrap()).unwrap(); assert_eq!(key, r[0].key); if let ObservationStatus::Error(ref err) = r[0].status { assert!(err.contains("no registered emitter")); diff --git a/cosmwasm/contracts/global-accountant/tests/submit_vaas.rs b/cosmwasm/contracts/global-accountant/tests/submit_vaas.rs index 6cf6d3e984..e3d0876973 100644 --- a/cosmwasm/contracts/global-accountant/tests/submit_vaas.rs +++ b/cosmwasm/contracts/global-accountant/tests/submit_vaas.rs @@ -1,7 +1,7 @@ mod helpers; use accountant::state::{transfer, TokenAddress}; -use cosmwasm_std::{from_binary, to_binary, Binary, Event, Uint256}; +use cosmwasm_std::{from_json, to_json_binary, Binary, Event, Uint256}; use global_accountant::msg::{Observation, ObservationStatus, SubmitObservationResponse}; use helpers::*; use serde_wormhole::RawMessage; @@ -178,7 +178,7 @@ fn bad_serialization() { let (v, _) = sign_vaa_body(&wh, create_vaa_body(3)); // Rather than using the wormhole wire format use cosmwasm json. - let data = to_binary(&v).unwrap(); + let data = to_json_binary(&v).unwrap(); let err = contract .submit_vaas(vec![data]) @@ -339,13 +339,12 @@ fn reobservation() { }; let key = transfer::Key::new(o.emitter_chain, o.emitter_address.into(), o.sequence); - let obs = to_binary(&vec![o]).unwrap(); + let obs = to_json_binary(&vec![o]).unwrap(); let index = wh.guardian_set_index(); let signatures = sign_observations(&wh, &obs); for s in signatures { let resp = contract.submit_observations(obs.clone(), index, s).unwrap(); - let mut responses: Vec = - from_binary(&resp.data.unwrap()).unwrap(); + let mut responses: Vec = from_json(resp.data.unwrap()).unwrap(); assert_eq!(1, responses.len()); let d = responses.remove(0); @@ -378,12 +377,12 @@ fn digest_mismatch() { }; let key = transfer::Key::new(o.emitter_chain, o.emitter_address.into(), o.sequence); - let obs = to_binary(&vec![o]).unwrap(); + let obs = to_json_binary(&vec![o]).unwrap(); let index = wh.guardian_set_index(); let signatures = sign_observations(&wh, &obs); for s in signatures { let resp = contract.submit_observations(obs.clone(), index, s).unwrap(); - let responses = from_binary::>(&resp.data.unwrap()).unwrap(); + let responses = from_json::>(&resp.data.unwrap()).unwrap(); assert_eq!(key, responses[0].key); if let ObservationStatus::Error(ref err) = responses[0].status { assert!(err.contains("digest mismatch")); diff --git a/cosmwasm/contracts/ibc-translator/src/contract.rs b/cosmwasm/contracts/ibc-translator/src/contract.rs index c2e1a79271..70d57c0b96 100644 --- a/cosmwasm/contracts/ibc-translator/src/contract.rs +++ b/cosmwasm/contracts/ibc-translator/src/contract.rs @@ -3,7 +3,7 @@ use cosmwasm_std::entry_point; use anyhow::{bail, Context}; use cosmwasm_std::{ - to_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdResult, + to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdResult, }; use wormhole_bindings::{tokenfactory::TokenFactoryMsg, WormholeQuery}; @@ -102,6 +102,6 @@ pub fn reply( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::IbcChannel { chain_id } => to_binary(&query_ibc_channel(deps, chain_id)?), + QueryMsg::IbcChannel { chain_id } => to_json_binary(&query_ibc_channel(deps, chain_id)?), } } diff --git a/cosmwasm/contracts/ibc-translator/src/execute.rs b/cosmwasm/contracts/ibc-translator/src/execute.rs index a746948af7..69c2405db8 100644 --- a/cosmwasm/contracts/ibc-translator/src/execute.rs +++ b/cosmwasm/contracts/ibc-translator/src/execute.rs @@ -1,6 +1,6 @@ use anyhow::{bail, ensure, Context}; use cosmwasm_std::{ - to_binary, Binary, Coin, CosmosMsg, Deps, DepsMut, Empty, Env, Event, MessageInfo, + to_json_binary, Binary, Coin, CosmosMsg, Deps, DepsMut, Empty, Env, Event, MessageInfo, QueryRequest, Response, SubMsg, Uint128, WasmMsg, WasmQuery, }; use cw_token_bridge::msg::{ @@ -48,11 +48,12 @@ pub fn complete_transfer_and_convert( // craft the token bridge execute message // this will be added as a submessage to the response - let token_bridge_execute_msg = to_binary(&TokenBridgeExecuteMsg::CompleteTransferWithPayload { - data: vaa.clone(), - relayer: info.sender.to_string(), - }) - .context("could not serialize token bridge execute msg")?; + let token_bridge_execute_msg = + to_json_binary(&TokenBridgeExecuteMsg::CompleteTransferWithPayload { + data: vaa.clone(), + relayer: info.sender.to_string(), + }) + .context("could not serialize token bridge execute msg")?; let sub_msg = SubMsg::reply_on_success( CosmosMsg::Wasm(WasmMsg::Execute { @@ -64,7 +65,7 @@ pub fn complete_transfer_and_convert( ); // craft the token bridge query message to parse the payload3 vaa - let token_bridge_query_msg = to_binary(&TokenBridgeQueryMsg::TransferInfo { vaa }) + let token_bridge_query_msg = to_json_binary(&TokenBridgeQueryMsg::TransferInfo { vaa }) .context("could not serialize token bridge transfer_info query msg")?; let transfer_info: TransferInfoResponse = deps @@ -132,7 +133,7 @@ pub fn convert_and_transfer( }); // 2. cw20::increaseAllowance to the contract address for the token bridge to spend the amount of tokens - let increase_allowance_msg = to_binary(&Cw20WrappedExecuteMsg::IncreaseAllowance { + let increase_allowance_msg = to_json_binary(&Cw20WrappedExecuteMsg::IncreaseAllowance { spender: token_bridge_contract.clone(), amount: bridging_coin.amount, expires: None, @@ -174,7 +175,7 @@ pub fn convert_and_transfer( } } }; - let initiate_transfer_msg = to_binary(&token_bridge_transfer) + let initiate_transfer_msg = to_json_binary(&token_bridge_transfer) .context("could not serialize token bridge initiate_transfer msg")?; response = response.add_message(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: token_bridge_contract, diff --git a/cosmwasm/contracts/ibc-translator/src/reply.rs b/cosmwasm/contracts/ibc-translator/src/reply.rs index 64dbb21587..e79c7fd13b 100644 --- a/cosmwasm/contracts/ibc-translator/src/reply.rs +++ b/cosmwasm/contracts/ibc-translator/src/reply.rs @@ -5,8 +5,8 @@ use crate::{ use anybuf::Anybuf; use anyhow::{ensure, Context}; use cosmwasm_std::{ - from_binary, to_binary, Binary, CosmosMsg::Stargate, Deps, DepsMut, Env, QueryRequest, Reply, - Response, SubMsg, WasmQuery, + from_json, to_json_binary, Binary, CosmosMsg::Stargate, Deps, DepsMut, Env, QueryRequest, + Reply, Response, SubMsg, WasmQuery, }; use cw20::TokenInfoResponse; use cw20_base::msg::QueryMsg as TokenQuery; @@ -29,7 +29,7 @@ pub fn handle_complete_transfer_reply( .data .context("no data in the response, we should never get here")?; let res_data: CompleteTransferResponse = - from_binary(&res_data_raw).context("failed to deserialize response data")?; + from_json(&res_data_raw).context("failed to deserialize response data")?; let contract_addr = res_data .contract .context("no contract in response, we should never get here")?; @@ -114,7 +114,7 @@ pub fn convert_cw20_to_bank_and_send( // call into the cw20 contract to get the token's metadata let request = QueryRequest::Wasm(WasmQuery::Smart { contract_addr: cw20_contract_addr.clone(), - msg: to_binary(&TokenQuery::TokenInfo {})?, + msg: to_json_binary(&TokenQuery::TokenInfo {})?, }); let token_info: TokenInfoResponse = deps.querier.query(&request)?; diff --git a/cosmwasm/contracts/ibc-translator/tests/contract_test.rs b/cosmwasm/contracts/ibc-translator/tests/contract_test.rs index ef3a2e3fdf..6fcab252d5 100644 --- a/cosmwasm/contracts/ibc-translator/tests/contract_test.rs +++ b/cosmwasm/contracts/ibc-translator/tests/contract_test.rs @@ -1,7 +1,7 @@ use cosmwasm_std::{ coin, testing::{mock_dependencies, mock_env, mock_info}, - to_binary, Binary, ContractResult, CosmosMsg, Empty, Event, Reply, ReplyOn, Response, + to_json_binary, Binary, ContractResult, CosmosMsg, Empty, Event, Reply, ReplyOn, Response, SubMsgResponse, SystemError, SystemResult, Uint128, WasmMsg, WasmQuery, }; use cw_token_bridge::msg::TransferInfoResponse; @@ -98,7 +98,7 @@ fn execute_complete_transfer_and_convert() { contract_addr: _, msg: _, } => SystemResult::Ok(ContractResult::Ok( - to_binary(&transfer_info_response_copy).unwrap(), + to_json_binary(&transfer_info_response_copy).unwrap(), )), _ => SystemResult::Err(SystemError::UnsupportedRequest { kind: "wasm".to_string(), @@ -363,7 +363,7 @@ fn query_query_ibc_channel_happy_path() { .save(deps.as_mut().storage, 0, &channel) .unwrap(); - let expected_response = to_binary(&ChannelResponse { channel }).unwrap(); + let expected_response = to_json_binary(&ChannelResponse { channel }).unwrap(); let response = query(deps.as_ref(), env, msg).unwrap(); assert_eq!(expected_response, response); diff --git a/cosmwasm/contracts/ibc-translator/tests/execute_test.rs b/cosmwasm/contracts/ibc-translator/tests/execute_test.rs index ac7ee79e2f..ff2fa682ff 100644 --- a/cosmwasm/contracts/ibc-translator/tests/execute_test.rs +++ b/cosmwasm/contracts/ibc-translator/tests/execute_test.rs @@ -1,7 +1,7 @@ use cosmwasm_std::{ coin, testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR}, - to_binary, Binary, Coin, ContractResult, CosmosMsg, Event, ReplyOn, Response, SystemError, + to_json_binary, Binary, Coin, ContractResult, CosmosMsg, Event, ReplyOn, Response, SystemError, SystemResult, Uint128, WasmMsg, WasmQuery, }; use cw_token_bridge::msg::TransferInfoResponse; @@ -78,7 +78,7 @@ fn complete_transfer_and_convert_happy_path() { contract_addr: _, msg: _, } => SystemResult::Ok(ContractResult::Ok( - to_binary(&transfer_info_response_copy).unwrap(), + to_json_binary(&transfer_info_response_copy).unwrap(), )), _ => SystemResult::Err(SystemError::UnsupportedRequest { kind: "wasm".to_string(), @@ -176,7 +176,7 @@ fn complete_transfer_and_convert_failure_humanize_recipient() { let mut deps = execute_custom_mock_deps(); let env = mock_env(); - let transfer_info_response = to_binary(&cw_token_bridge::msg::TransferInfoResponse { + let transfer_info_response = to_json_binary(&cw_token_bridge::msg::TransferInfoResponse { amount: 1000000u32.into(), token_address: hex::decode("0000000000000000000000009c3c9283d3e44854697cd22d3faa240cfb032889").unwrap().try_into().unwrap(), token_chain: 5, @@ -214,7 +214,7 @@ fn complete_transfer_and_convert_nomatch_recipient_contract() { let mut deps = execute_custom_mock_deps(); let env = mock_env(); - let transfer_info_response = to_binary(&cw_token_bridge::msg::TransferInfoResponse { + let transfer_info_response = to_json_binary(&cw_token_bridge::msg::TransferInfoResponse { amount: 1000000u32.into(), token_address: hex::decode("0000000000000000000000009c3c9283d3e44854697cd22d3faa240cfb032889").unwrap().try_into().unwrap(), token_chain: 5, diff --git a/cosmwasm/contracts/ibc-translator/tests/reply_test.rs b/cosmwasm/contracts/ibc-translator/tests/reply_test.rs index f5b97ca5ae..fa43267ca1 100644 --- a/cosmwasm/contracts/ibc-translator/tests/reply_test.rs +++ b/cosmwasm/contracts/ibc-translator/tests/reply_test.rs @@ -1,6 +1,6 @@ use cosmwasm_std::{ testing::{mock_dependencies, mock_env}, - to_binary, to_vec, Binary, ContractResult, + to_json_binary, to_json_vec, Binary, ContractResult, CosmosMsg::Stargate, Reply, Response, SubMsgResponse, SystemError, SystemResult, Uint128, WasmQuery, }; @@ -206,7 +206,7 @@ fn handle_complete_transfer_reply_invalid_response_data_type() { // encode a payload as protobuf // the payload is NOT a CompleteTransferResponse let execute_reply = MsgExecuteContractResponse { - data: to_vec(&AssetInfo::NativeToken { + data: to_json_vec(&AssetInfo::NativeToken { denom: "denomA".to_string(), }) .unwrap(), @@ -235,7 +235,7 @@ fn handle_complete_transfer_reply_no_response_contract() { // encode a payload as protobuf // the payload is a CompleteTransferResponse let execute_reply = MsgExecuteContractResponse { - data: to_vec(&CompleteTransferResponse { + data: to_json_vec(&CompleteTransferResponse { contract: None, denom: None, recipient: "fake".to_string(), @@ -472,7 +472,7 @@ fn convert_cw20_to_bank_happy_path_create_denom() { contract_addr: _, msg: _, } => SystemResult::Ok(ContractResult::Ok( - to_binary(&token_info_response_copy).unwrap(), + to_json_binary(&token_info_response_copy).unwrap(), )), _ => SystemResult::Err(SystemError::UnsupportedRequest { kind: "wasm".to_string(), @@ -578,7 +578,7 @@ fn convert_cw20_to_bank_happy_path_create_denom_empty_symbol() { contract_addr: _, msg: _, } => SystemResult::Ok(ContractResult::Ok( - to_binary(&token_info_response_copy).unwrap(), + to_json_binary(&token_info_response_copy).unwrap(), )), _ => SystemResult::Err(SystemError::UnsupportedRequest { kind: "wasm".to_string(), diff --git a/cosmwasm/contracts/ibc-translator/tests/test_setup/mod.rs b/cosmwasm/contracts/ibc-translator/tests/test_setup/mod.rs index 8cf7c0886d..5be33eb206 100644 --- a/cosmwasm/contracts/ibc-translator/tests/test_setup/mod.rs +++ b/cosmwasm/contracts/ibc-translator/tests/test_setup/mod.rs @@ -2,7 +2,7 @@ use serde::de::DeserializeOwned; use std::marker::PhantomData; use cosmwasm_std::{ - from_slice, + from_json, testing::{mock_env, BankQuerier, MockQuerierCustomHandlerResult, MockStorage}, Addr, Api, Binary, CanonicalAddr, Coin, ContractResult, CustomQuery, Empty, Env, OwnedDeps, Querier, QuerierResult, QueryRequest, RecoverPubkeyError, StdError, StdResult, SystemError, @@ -263,7 +263,7 @@ impl Default for MockQuerier { impl Querier for MockQuerier { fn raw_query(&self, bin_request: &[u8]) -> QuerierResult { - let request: QueryRequest = match from_slice(bin_request) { + let request: QueryRequest = match from_json(bin_request) { Ok(v) => v, Err(e) => { return SystemResult::Err(SystemError::InvalidRequest { diff --git a/cosmwasm/contracts/mock-bridge-integration/src/contract.rs b/cosmwasm/contracts/mock-bridge-integration/src/contract.rs index 01dcaec2b0..1141ea54f5 100644 --- a/cosmwasm/contracts/mock-bridge-integration/src/contract.rs +++ b/cosmwasm/contracts/mock-bridge-integration/src/contract.rs @@ -1,5 +1,5 @@ use cosmwasm_std::{ - entry_point, to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, QueryRequest, + entry_point, to_json_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, QueryRequest, Reply, Response, StdError, StdResult, SubMsg, WasmMsg, WasmQuery, }; @@ -63,7 +63,7 @@ fn complete_transfer_with_payload( let messages = vec![SubMsg::reply_on_success( CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: cfg.token_bridge_contract, - msg: to_binary(&TokenBridgeExecuteMsg::CompleteTransferWithPayload { + msg: to_json_binary(&TokenBridgeExecuteMsg::CompleteTransferWithPayload { data: data.clone(), relayer: info.sender.to_string(), })?, @@ -88,7 +88,7 @@ fn parse_transfer_vaa(deps: Deps, data: &Binary) -> StdResult = Item::new("count"); + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn instantiate( + deps: DepsMut, + _env: Env, + _info: MessageInfo, + _msg: InstantiateMsg, +) -> StdResult { + COUNT.save(deps.storage, &0)?; + + Ok(Response::new() + .add_attribute("method", "instantiate") + .add_attribute("count", 0.to_string())) +} + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn execute( + deps: DepsMut, + _env: Env, + _info: MessageInfo, + msg: ExecuteMsg, +) -> StdResult { + match msg { + ExecuteMsg::Increment {} => increment(deps), + ExecuteMsg::Reset {} => reset(deps), + ExecuteMsg::Receive(receive_msg) => match from_json(receive_msg.msg)? { + ReceiveMsg::Increment {} => increment(deps), + ReceiveMsg::Reset {} => reset(deps), + }, + } +} + +pub fn increment(deps: DepsMut) -> StdResult { + COUNT.update(deps.storage, |mut count| -> StdResult<_> { + count += 1; + Ok(count) + })?; + + Ok(Response::new().add_attribute("method", "increment")) +} + +pub fn reset(deps: DepsMut) -> StdResult { + COUNT.save(deps.storage, &0)?; + + Ok(Response::new() + .add_attribute("method", "reset") + .add_attribute("count", 0u32.to_string())) +} + +#[cfg_attr(not(feature = "library"), entry_point)] +pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { + match msg { + QueryMsg::GetCount {} => to_json_binary(&query_count(deps)?), + } +} + +fn query_count(deps: Deps) -> StdResult { + let count = COUNT.load(deps.storage)?; + Ok(CountResponse { count }) +} diff --git a/cosmwasm/contracts/mock-counter/src/interface.rs b/cosmwasm/contracts/mock-counter/src/interface.rs new file mode 100644 index 0000000000..f2261ffd73 --- /dev/null +++ b/cosmwasm/contracts/mock-counter/src/interface.rs @@ -0,0 +1,57 @@ +use cosmwasm_std::{Addr, StdError, StdResult}; +use cw_multi_test::{App, ContractWrapper, Executor}; + +use crate::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; + +pub struct CounterContract(Addr); + +impl CounterContract { + pub fn addr(&self) -> &Addr { + &self.0 + } + + pub fn store_code(app: &mut App) -> u64 { + let contract = ContractWrapper::new( + crate::contract::execute, + crate::contract::instantiate, + crate::contract::query, + ); + app.store_code(Box::new(contract)) + } + + #[track_caller] + pub fn instantiate(app: &mut App, code_id: u64, sender: &str, label: &str) -> StdResult { + let msg = InstantiateMsg {}; + let addr = app + .instantiate_contract(code_id, Addr::unchecked(sender), &msg, &[], label, None) + .unwrap(); + Ok(CounterContract(addr)) + } + + #[track_caller] + pub fn increment(&self, app: &mut App, sender: &str) -> StdResult<()> { + let msg = ExecuteMsg::Increment {}; + let _ = app + .execute_contract(Addr::unchecked(sender), self.0.clone(), &msg, &[]) + .map_err(|err| StdError::generic_err(err.to_string()))?; + + Ok(()) + } + + #[track_caller] + pub fn reset(&self, app: &mut App, sender: &str) -> StdResult<()> { + let msg = ExecuteMsg::Reset {}; + let _ = app + .execute_contract(Addr::unchecked(sender), self.0.clone(), &msg, &[]) + .map_err(|err| StdError::generic_err(err.to_string()))?; + + Ok(()) + } + + #[track_caller] + pub fn query_count(&self, app: &App) -> StdResult { + let msg = QueryMsg::GetCount {}; + let result: CountResponse = app.wrap().query_wasm_smart(self.0.clone(), &msg)?; + Ok(result.count) + } +} diff --git a/cosmwasm/contracts/mock-counter/src/lib.rs b/cosmwasm/contracts/mock-counter/src/lib.rs new file mode 100644 index 0000000000..8ed39e74b9 --- /dev/null +++ b/cosmwasm/contracts/mock-counter/src/lib.rs @@ -0,0 +1,9 @@ +pub use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; + +pub mod contract; +pub mod msg; +#[cfg(test)] +mod tests; + +#[cfg(any(test, feature = "interface"))] +pub mod interface; diff --git a/cosmwasm/contracts/mock-counter/src/msg.rs b/cosmwasm/contracts/mock-counter/src/msg.rs new file mode 100644 index 0000000000..7ec6e71a1b --- /dev/null +++ b/cosmwasm/contracts/mock-counter/src/msg.rs @@ -0,0 +1,28 @@ +use cosmwasm_schema::cw_serde; +use cw20::Cw20ReceiveMsg; + +#[cw_serde] +pub struct InstantiateMsg {} + +#[cw_serde] +pub struct CountResponse { + pub count: u32, +} + +#[cw_serde] +pub enum ExecuteMsg { + Increment {}, + Reset {}, + Receive(Cw20ReceiveMsg), +} + +#[cw_serde] +pub enum ReceiveMsg { + Increment {}, + Reset {}, +} + +#[cw_serde] +pub enum QueryMsg { + GetCount {}, +} diff --git a/cosmwasm/contracts/mock-counter/src/tests.rs b/cosmwasm/contracts/mock-counter/src/tests.rs new file mode 100644 index 0000000000..ddfdb11637 --- /dev/null +++ b/cosmwasm/contracts/mock-counter/src/tests.rs @@ -0,0 +1,95 @@ +use cw_multi_test::App; + +use crate::interface::CounterContract; + +#[test] +fn proper_initialization() { + let mut app = App::default(); + let code_id = CounterContract::store_code(&mut app); + + let contract = CounterContract::instantiate(&mut app, code_id, "owner", "Counter 1").unwrap(); + + let count = contract.query_count(&app).unwrap(); + assert_eq!(0, count); +} + +#[test] +fn increment_works() { + let mut app = App::default(); + let code_id = CounterContract::store_code(&mut app); + + // Instantiate with count 0 + let contract = CounterContract::instantiate(&mut app, code_id, "owner", "Counter 1").unwrap(); + + // Anyone can increment + contract.increment(&mut app, "anyone").unwrap(); + let count = contract.query_count(&app).unwrap(); + assert_eq!(1, count); + + // Multiple increments + contract.increment(&mut app, "anyone").unwrap(); + contract.increment(&mut app, "anyone").unwrap(); + let count = contract.query_count(&app).unwrap(); + assert_eq!(3, count); +} + +#[test] +fn reset_works() { + let mut app = App::default(); + let code_id = CounterContract::store_code(&mut app); + + // Instantiate with count 5 + let contract = CounterContract::instantiate(&mut app, code_id, "owner", "Counter 1").unwrap(); + + // Increment the counter + contract.increment(&mut app, "anyone").unwrap(); + contract.increment(&mut app, "anyone").unwrap(); + + let count = contract.query_count(&app).unwrap(); + assert_eq!(2, count); + + // Can reset to same value + contract.reset(&mut app, "anyone").unwrap(); + let count = contract.query_count(&app).unwrap(); + assert_eq!(0, count); + + // Can reset to lower value + contract.reset(&mut app, "anyone").unwrap(); + let count = contract.query_count(&app).unwrap(); + assert_eq!(0, count); +} + +#[test] +fn multiple_counters() { + let mut app = App::default(); + let code_id = CounterContract::store_code(&mut app); + + // Create two counters + let contract1 = CounterContract::instantiate(&mut app, code_id, "owner", "Counter 1").unwrap(); + + let contract2 = CounterContract::instantiate(&mut app, code_id, "owner", "Counter 2").unwrap(); + + // increment contract1 5 times + for _ in 0..5 { + contract1.increment(&mut app, "anyone").unwrap(); + } + + // increment contract2 10 times + for _ in 0..10 { + contract2.increment(&mut app, "anyone").unwrap(); + } + + // Check initial values + assert_eq!(5, contract1.query_count(&app).unwrap()); + assert_eq!(10, contract2.query_count(&app).unwrap()); + + // Increment first counter + contract1.increment(&mut app, "anyone").unwrap(); + assert_eq!(6, contract1.query_count(&app).unwrap()); + assert_eq!(10, contract2.query_count(&app).unwrap()); + + // Reset second counter + contract2.reset(&mut app, "anyone").unwrap(); + assert_eq!(6, contract1.query_count(&app).unwrap()); + assert_eq!(0, contract2.query_count(&app).unwrap()); +} diff --git a/cosmwasm/contracts/ntt-global-accountant/src/contract.rs b/cosmwasm/contracts/ntt-global-accountant/src/contract.rs index f2a086434a..fc2a9c1417 100644 --- a/cosmwasm/contracts/ntt-global-accountant/src/contract.rs +++ b/cosmwasm/contracts/ntt-global-accountant/src/contract.rs @@ -9,7 +9,7 @@ use anyhow::{ensure, Context}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - from_binary, to_binary, Binary, ConversionOverflowError, Deps, DepsMut, Empty, Env, Event, + from_json, to_json_binary, Binary, ConversionOverflowError, Deps, DepsMut, Empty, Env, Event, MessageInfo, Order, Response, StdError, StdResult, Uint256, }; use cw2::set_contract_version; @@ -124,7 +124,7 @@ fn submit_observations( .context("failed to calculate quorum")?; let observations: Vec = - from_binary(&observations).context("failed to parse `Observations`")?; + from_json(&observations).context("failed to parse `Observations`")?; let mut responses = Vec::with_capacity(observations.len()); let mut events = Vec::with_capacity(observations.len()); @@ -154,7 +154,7 @@ fn submit_observations( } } - let data = to_binary(&responses).context("failed to serialize transfer details")?; + let data = to_json_binary(&responses).context("failed to serialize transfer details")?; Ok(Response::new() .add_attribute("action", "submit_observations") @@ -635,21 +635,22 @@ fn handle_ntt_vaa( #[cfg_attr(not(feature = "library"), entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { - QueryMsg::Balance(key) => query_balance(deps, key).and_then(|resp| to_binary(&resp)), + QueryMsg::Balance(key) => query_balance(deps, key).and_then(|resp| to_json_binary(&resp)), QueryMsg::AllAccounts { start_after, limit } => { - query_all_accounts(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_accounts(deps, start_after, limit).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllTransfers { start_after, limit } => { - query_all_transfers(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_transfers(deps, start_after, limit).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllPendingTransfers { start_after, limit } => { - query_all_pending_transfers(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_pending_transfers(deps, start_after, limit) + .and_then(|resp| to_json_binary(&resp)) } QueryMsg::Modification { sequence } => { - query_modification(deps, sequence).and_then(|resp| to_binary(&resp)) + query_modification(deps, sequence).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllModifications { start_after, limit } => { - query_all_modifications(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_modifications(deps, start_after, limit).and_then(|resp| to_json_binary(&resp)) } QueryMsg::ValidateTransfer { transfer } => validate_transfer(deps, &transfer) .map_err(|e| { @@ -657,27 +658,28 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { - query_relayer_chain_registration(deps, chain).and_then(|resp| to_binary(&resp)) + query_relayer_chain_registration(deps, chain).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllTransceiverHubs { start_after, limit } => { - query_all_transceiver_hubs(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_transceiver_hubs(deps, start_after, limit) + .and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllTransceiverPeers { start_after, limit } => { - query_all_transceiver_peers(deps, start_after, limit).and_then(|resp| to_binary(&resp)) + query_all_transceiver_peers(deps, start_after, limit) + .and_then(|resp| to_json_binary(&resp)) } QueryMsg::MissingObservations { guardian_set, index, - } => { - query_missing_observations(deps, guardian_set, index).and_then(|resp| to_binary(&resp)) - } + } => query_missing_observations(deps, guardian_set, index) + .and_then(|resp| to_json_binary(&resp)), QueryMsg::TransferStatus(key) => { - query_transfer_status(deps, &key).and_then(|resp| to_binary(&resp)) + query_transfer_status(deps, &key).and_then(|resp| to_json_binary(&resp)) } QueryMsg::BatchTransferStatus(keys) => { - query_batch_transfer_status(deps, keys).and_then(|resp| to_binary(&resp)) + query_batch_transfer_status(deps, keys).and_then(|resp| to_json_binary(&resp)) } } } diff --git a/cosmwasm/contracts/ntt-global-accountant/tests/chain_registration.rs b/cosmwasm/contracts/ntt-global-accountant/tests/chain_registration.rs index d64b84e51a..c62c13d88b 100644 --- a/cosmwasm/contracts/ntt-global-accountant/tests/chain_registration.rs +++ b/cosmwasm/contracts/ntt-global-accountant/tests/chain_registration.rs @@ -1,6 +1,6 @@ mod helpers; -use cosmwasm_std::{to_binary, Event}; +use cosmwasm_std::{to_json_binary, Event}; use helpers::*; use ntt_global_accountant::msg::RelayerChainRegistrationResponse; use wormhole_sdk::{ @@ -216,7 +216,7 @@ fn bad_serialization() { let (v, _) = sign_vaa_body(&wh, create_vaa_body()); // Rather than using the wormhole wire format use cosmwasm json. - let data = to_binary(&v).unwrap(); + let data = to_json_binary(&v).unwrap(); let err = contract .submit_vaas(vec![data]) diff --git a/cosmwasm/contracts/ntt-global-accountant/tests/submit_observations.rs b/cosmwasm/contracts/ntt-global-accountant/tests/submit_observations.rs index 83c9b6992e..3a9e3a0b7f 100644 --- a/cosmwasm/contracts/ntt-global-accountant/tests/submit_observations.rs +++ b/cosmwasm/contracts/ntt-global-accountant/tests/submit_observations.rs @@ -1,7 +1,7 @@ mod helpers; use accountant::state::transfer; -use cosmwasm_std::{to_binary, Binary, Uint256}; +use cosmwasm_std::{to_json_binary, Binary, Uint256}; use cw_multi_test::AppResponse; use helpers::*; use ntt_global_accountant::msg::Observation; @@ -61,7 +61,7 @@ fn transfer_tokens( payload, }; - let obs = to_binary(&vec![o.clone()]).unwrap(); + let obs = to_json_binary(&vec![o.clone()]).unwrap(); let signatures = sign_observations(wh, &obs); let responses = signatures diff --git a/cosmwasm/contracts/ntt-global-accountant/tests/submit_vaas.rs b/cosmwasm/contracts/ntt-global-accountant/tests/submit_vaas.rs index 4c08a8c4cd..6d858acc30 100644 --- a/cosmwasm/contracts/ntt-global-accountant/tests/submit_vaas.rs +++ b/cosmwasm/contracts/ntt-global-accountant/tests/submit_vaas.rs @@ -1,6 +1,6 @@ mod helpers; -use cosmwasm_std::{to_binary, Binary, Uint256}; +use cosmwasm_std::{to_json_binary, Binary, Uint256}; use helpers::*; use wormhole_bindings::fake::WormholeKeeper; @@ -120,7 +120,7 @@ fn bad_serialization() { let (v, _) = sign_vaa_body(&wh, create_vaa_body(3)); // Rather than using the wormhole wire format use cosmwasm json. - let data = to_binary(&v).unwrap(); + let data = to_json_binary(&v).unwrap(); let err = contract .submit_vaas(vec![data]) diff --git a/cosmwasm/contracts/token-bridge/src/contract.rs b/cosmwasm/contracts/token-bridge/src/contract.rs index 090210c28e..cddfe74450 100644 --- a/cosmwasm/contracts/token-bridge/src/contract.rs +++ b/cosmwasm/contracts/token-bridge/src/contract.rs @@ -23,7 +23,7 @@ use cw_wormhole::{ use cosmwasm_std::entry_point; use cosmwasm_std::{ - coin, to_binary, BankMsg, Binary, CanonicalAddr, CosmosMsg, Deps, DepsMut, Empty, Env, + coin, to_json_binary, BankMsg, Binary, CanonicalAddr, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, QueryRequest, Reply, Response, StdError, StdResult, SubMsg, Uint128, WasmMsg, WasmQuery, }; @@ -169,7 +169,7 @@ pub fn reply(deps: DepsMut, env: Env, _msg: Reply) -> StdResult { let new_balance: BalanceResponse = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: state.token_address.to_string(), - msg: to_binary(&TokenQuery::Balance { + msg: to_json_binary(&TokenQuery::Balance { address: env.contract.address.to_string(), })?, }))?; @@ -212,7 +212,7 @@ pub fn reply(deps: DepsMut, env: Env, _msg: Reply) -> StdResult { let message = CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: cfg.wormhole_contract, funds: vec![], - msg: to_binary(&WormholeExecuteMsg::PostMessage { + msg: to_json_binary(&WormholeExecuteMsg::PostMessage { message: Binary::from(token_bridge_message.serialize()), nonce: state.nonce, })?, @@ -229,7 +229,7 @@ fn parse_vaa(deps: Deps, block_time: u64, data: &Binary) -> StdResult let cfg = config_read(deps.storage).load()?; let vaa: ParsedVAA = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: cfg.wormhole_contract, - msg: to_binary(&WormholeQueryMsg::VerifyVAA { + msg: to_json_binary(&WormholeQueryMsg::VerifyVAA { vaa: data.clone(), block_time, })?, @@ -454,7 +454,7 @@ fn handle_attest_meta( } CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract.into_string(), - msg: to_binary(&WrappedMsg::UpdateMetadata { + msg: to_json_binary(&WrappedMsg::UpdateMetadata { name: get_string_from_32(&meta.name), symbol: get_string_from_32(&meta.symbol), })?, @@ -464,7 +464,7 @@ fn handle_attest_meta( CosmosMsg::Wasm(WasmMsg::Instantiate { admin: Some(env.contract.address.clone().into_string()), code_id: cfg.wrapped_asset_code_id, - msg: to_binary(&WrappedInit { + msg: to_json_binary(&WrappedInit { name: get_string_from_32(&meta.name), symbol: get_string_from_32(&meta.symbol), asset_chain: meta.token_chain, @@ -473,7 +473,7 @@ fn handle_attest_meta( mint: None, init_hook: Some(InitHook { contract_addr: env.contract.address.to_string(), - msg: to_binary(&ExecuteMsg::RegisterAssetHook { + msg: to_json_binary(&ExecuteMsg::RegisterAssetHook { chain: meta.token_chain, token_address: meta.token_address.clone(), })?, @@ -515,7 +515,7 @@ fn handle_create_asset_meta_token( let request = QueryRequest::Wasm(WasmQuery::Smart { contract_addr: asset_address.clone(), - msg: to_binary(&TokenQuery::TokenInfo {})?, + msg: to_json_binary(&TokenQuery::TokenInfo {})?, }); let asset_human = deps.api.addr_validate(&asset_address)?; @@ -541,7 +541,7 @@ fn handle_create_asset_meta_token( Ok(Response::new() .add_message(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: cfg.wormhole_contract, - msg: to_binary(&WormholeExecuteMsg::PostMessage { + msg: to_json_binary(&WormholeExecuteMsg::PostMessage { message: Binary::from(token_bridge_message.serialize()), nonce, })?, @@ -584,7 +584,7 @@ fn handle_create_asset_meta_native_token( Ok(Response::new() .add_message(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: cfg.wormhole_contract, - msg: to_binary(&WormholeExecuteMsg::PostMessage { + msg: to_json_binary(&WormholeExecuteMsg::PostMessage { message: Binary::from(token_bridge_message.serialize()), nonce, })?, @@ -729,7 +729,7 @@ fn handle_upgrade_contract(_deps: DepsMut, env: Env, data: &Vec) -> StdResul .add_message(CosmosMsg::Wasm(WasmMsg::Migrate { contract_addr: env.contract.address.to_string(), new_code_id: new_contract, - msg: to_binary(&MigrateMsg {})?, + msg: to_json_binary(&MigrateMsg {})?, })) .add_attribute("action", "contract_upgrade")) } @@ -870,7 +870,7 @@ fn handle_complete_transfer_token( // Asset already deployed, just mint let mut messages = vec![CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_addr.clone(), - msg: to_binary(&WrappedMsg::Mint { + msg: to_json_binary(&WrappedMsg::Mint { recipient: recipient.to_string(), amount: Uint128::from(amount), })?, @@ -879,7 +879,7 @@ fn handle_complete_transfer_token( if fee != 0 { messages.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_addr.clone(), - msg: to_binary(&WrappedMsg::Mint { + msg: to_json_binary(&WrappedMsg::Mint { recipient: relayer_address.to_string(), amount: Uint128::from(fee), })?, @@ -888,7 +888,7 @@ fn handle_complete_transfer_token( } // serialize response data that will be returned to the caller - let response_data = to_binary(&CompleteTransferResponse { + let response_data = to_json_binary(&CompleteTransferResponse { contract: Some(contract_addr.clone()), denom: None, recipient: recipient.clone().into_string(), @@ -916,7 +916,7 @@ fn handle_complete_transfer_token( let token_info: TokenInfoResponse = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: contract_address.to_string(), - msg: to_binary(&TokenQuery::TokenInfo {})?, + msg: to_json_binary(&TokenQuery::TokenInfo {})?, }))?; let decimals = token_info.decimals; @@ -926,7 +926,7 @@ fn handle_complete_transfer_token( let mut messages = vec![CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_address.to_string(), - msg: to_binary(&TokenMsg::Transfer { + msg: to_json_binary(&TokenMsg::Transfer { recipient: recipient.to_string(), amount: Uint128::from(amount), })?, @@ -936,7 +936,7 @@ fn handle_complete_transfer_token( if fee != 0 { messages.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_address.to_string(), - msg: to_binary(&TokenMsg::Transfer { + msg: to_json_binary(&TokenMsg::Transfer { recipient: relayer_address.to_string(), amount: Uint128::from(fee), })?, @@ -945,7 +945,7 @@ fn handle_complete_transfer_token( } // serialize response data that will be returned to the caller - let response_data = to_binary(&CompleteTransferResponse { + let response_data = to_json_binary(&CompleteTransferResponse { contract: Some(contract_address.to_string()), denom: None, recipient: recipient.clone().into_string(), @@ -1046,7 +1046,7 @@ fn handle_complete_transfer_token_native( } // serialize response data that will be returned to the caller - let response_data = to_binary(&CompleteTransferResponse { + let response_data = to_json_binary(&CompleteTransferResponse { contract: None, denom: Some(denom.clone()), recipient: recipient.clone().into_string(), @@ -1150,7 +1150,7 @@ fn handle_initiate_transfer_token( // This is a deployed wrapped asset, burn it messages.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: asset.clone(), - msg: to_binary(&WrappedMsg::Burn { + msg: to_json_binary(&WrappedMsg::Burn { account: info.sender.to_string(), amount, })?, @@ -1158,7 +1158,7 @@ fn handle_initiate_transfer_token( })); let request = QueryRequest::::Wasm(WasmQuery::Smart { contract_addr: asset, - msg: to_binary(&WrappedQuery::WrappedAssetInfo {})?, + msg: to_json_binary(&WrappedQuery::WrappedAssetInfo {})?, }); let wrapped_token_info: WrappedAssetInfoResponse = deps.querier.query(&request)?; asset_chain = wrapped_token_info.asset_chain; @@ -1201,7 +1201,7 @@ fn handle_initiate_transfer_token( messages.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: cfg.wormhole_contract, - msg: to_binary(&WormholeExecuteMsg::PostMessage { + msg: to_json_binary(&WormholeExecuteMsg::PostMessage { message: Binary::from(token_bridge_message.serialize()), nonce, })?, @@ -1216,7 +1216,7 @@ fn handle_initiate_transfer_token( let token_info: TokenInfoResponse = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: asset.clone(), - msg: to_binary(&TokenQuery::TokenInfo {})?, + msg: to_json_binary(&TokenQuery::TokenInfo {})?, }))?; let decimals = token_info.decimals; @@ -1241,7 +1241,7 @@ fn handle_initiate_transfer_token( submessages.push(SubMsg::reply_on_success( CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: asset.clone(), - msg: to_binary(&TokenMsg::TransferFrom { + msg: to_json_binary(&TokenMsg::TransferFrom { owner: info.sender.to_string(), recipient: env.contract.address.to_string(), amount, @@ -1267,7 +1267,7 @@ fn handle_initiate_transfer_token( let balance: BalanceResponse = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: asset.to_string(), - msg: to_binary(&TokenQuery::Balance { + msg: to_json_binary(&TokenQuery::Balance { address: env.contract.address.to_string(), })?, }))?; @@ -1449,7 +1449,7 @@ fn handle_initiate_transfer_native_token( let sender = deps.api.addr_canonicalize(info.sender.as_str())?; messages.push(CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: cfg.wormhole_contract, - msg: to_binary(&WormholeExecuteMsg::PostMessage { + msg: to_json_binary(&WormholeExecuteMsg::PostMessage { message: Binary::from(token_bridge_message.serialize()), nonce, })?, @@ -1475,13 +1475,15 @@ fn handle_initiate_transfer_native_token( pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { match msg { QueryMsg::WrappedRegistry { chain, address } => { - to_binary(&query_wrapped_registry(deps, chain, address.as_slice())?) + to_json_binary(&query_wrapped_registry(deps, chain, address.as_slice())?) } - QueryMsg::TransferInfo { vaa } => to_binary(&query_transfer_info(deps, env, &vaa)?), - QueryMsg::ExternalId { external_id } => to_binary(&query_external_id(deps, external_id)?), - QueryMsg::IsVaaRedeemed { vaa } => to_binary(&query_is_vaa_redeemed(deps, env, &vaa)?), + QueryMsg::TransferInfo { vaa } => to_json_binary(&query_transfer_info(deps, env, &vaa)?), + QueryMsg::ExternalId { external_id } => { + to_json_binary(&query_external_id(deps, external_id)?) + } + QueryMsg::IsVaaRedeemed { vaa } => to_json_binary(&query_is_vaa_redeemed(deps, env, &vaa)?), QueryMsg::ChainRegistration { chain } => { - query_chain_registration(deps, chain).and_then(|r| to_binary(&r)) + query_chain_registration(deps, chain).and_then(|r| to_json_binary(&r)) } } } diff --git a/cosmwasm/contracts/wormchain-ibc-receiver/src/contract.rs b/cosmwasm/contracts/wormchain-ibc-receiver/src/contract.rs index 16613f483d..b4a50b9987 100644 --- a/cosmwasm/contracts/wormchain-ibc-receiver/src/contract.rs +++ b/cosmwasm/contracts/wormchain-ibc-receiver/src/contract.rs @@ -2,7 +2,7 @@ use crate::error::ContractError; use crate::msg::{AllChannelChainsResponse, ChannelChainResponse, ExecuteMsg, QueryMsg}; use crate::state::{CHANNEL_CHAIN, VAA_ARCHIVE}; use anyhow::{bail, ensure, Context}; -use cosmwasm_std::{entry_point, to_binary, Binary, Deps, Empty, Event, StdResult}; +use cosmwasm_std::{entry_point, to_json_binary, Binary, Deps, Empty, Event, StdResult}; use cosmwasm_std::{DepsMut, Env, MessageInfo, Order, Response}; use serde_wormhole::RawMessage; use std::str; @@ -132,10 +132,10 @@ fn handle_vaa(deps: DepsMut, vaa: Binary) -> anyhow::Result StdResult { match msg { QueryMsg::ChannelChain { channel_id } => { - query_channel_chain(deps, channel_id).and_then(|resp| to_binary(&resp)) + query_channel_chain(deps, channel_id).and_then(|resp| to_json_binary(&resp)) } QueryMsg::AllChannelChains {} => { - query_all_channel_chains(deps).and_then(|resp| to_binary(&resp)) + query_all_channel_chains(deps).and_then(|resp| to_json_binary(&resp)) } } } diff --git a/cosmwasm/contracts/wormchain-ibc-receiver/src/ibc.rs b/cosmwasm/contracts/wormchain-ibc-receiver/src/ibc.rs index 8b24b75968..d3111fdf30 100644 --- a/cosmwasm/contracts/wormchain-ibc-receiver/src/ibc.rs +++ b/cosmwasm/contracts/wormchain-ibc-receiver/src/ibc.rs @@ -1,6 +1,6 @@ use anyhow::{bail, ensure}; use cosmwasm_std::{ - entry_point, from_slice, to_binary, Attribute, Binary, ContractResult, DepsMut, Env, + entry_point, from_json, to_json_binary, Attribute, Binary, ContractResult, DepsMut, Env, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, StdError, StdResult, @@ -91,7 +91,7 @@ fn handle_packet_receive(msg: IbcPacketReceiveMsg) -> Result { receive_publish(channel_id, publish_attrs) @@ -135,7 +135,7 @@ fn receive_publish( } // send the ack and emit the message with the attributes from the wormhole message - let acknowledgement = to_binary(&ContractResult::<()>::Ok(()))?; + let acknowledgement = to_json_binary(&ContractResult::<()>::Ok(()))?; Ok(IbcReceiveResponse::new() .set_ack(acknowledgement) .add_attribute("action", "receive_publish") @@ -146,7 +146,7 @@ fn receive_publish( // this encode an error or error message into a proper acknowledgement to the recevier fn encode_ibc_error(msg: impl Into) -> Binary { // this cannot error, unwrap to keep the interface simple - to_binary(&ContractResult::<()>::Err(msg.into())).unwrap() + to_json_binary(&ContractResult::<()>::Err(msg.into())).unwrap() } /// 5. Acknowledging a packet. Called when the other chain successfully receives a packet from us. diff --git a/cosmwasm/contracts/wormchain-ibc-receiver/src/tests/integration_tests.rs b/cosmwasm/contracts/wormchain-ibc-receiver/src/tests/integration_tests.rs index 86210bfe20..860febd798 100644 --- a/cosmwasm/contracts/wormchain-ibc-receiver/src/tests/integration_tests.rs +++ b/cosmwasm/contracts/wormchain-ibc-receiver/src/tests/integration_tests.rs @@ -5,9 +5,9 @@ use crate::{ }; use anyhow::Error; use cosmwasm_std::{ - from_binary, + from_json, testing::{mock_env, mock_info, MockApi, MockQuerier, MockStorage}, - to_binary, Binary, ContractResult, Deps, DepsMut, Empty, QuerierWrapper, SystemResult, + to_json_binary, Binary, ContractResult, Deps, DepsMut, Empty, QuerierWrapper, SystemResult, }; use wormhole_bindings::{fake::WormholeKeeper, WormholeQuery}; use wormhole_sdk::{ @@ -24,7 +24,7 @@ pub fn add_channel_chain_happy_path() -> anyhow::Result<(), Error> { MockQuerier::new(&[]).with_custom_handler(|q| match q { WormholeQuery::VerifyVaa { vaa } => { match WormholeKeeper::new().verify_vaa(&vaa.0, 0u64) { - Ok(_) => SystemResult::Ok(if let Ok(data) = to_binary(&Empty {}) { + Ok(_) => SystemResult::Ok(if let Ok(data) = to_json_binary(&Empty {}) { ContractResult::Ok(data) } else { ContractResult::Err("Unable to convert to binary".to_string()) @@ -33,7 +33,7 @@ pub fn add_channel_chain_happy_path() -> anyhow::Result<(), Error> { } } _ => cosmwasm_std::SystemResult::Ok(cosmwasm_std::ContractResult::Ok( - to_binary(&Empty {}).unwrap(), + to_json_binary(&Empty {}).unwrap(), )), }); @@ -71,7 +71,7 @@ pub fn add_channel_chain_happy_path() -> anyhow::Result<(), Error> { }; let channel_binary = query(readonly_deps, env, QueryMsg::AllChannelChains {})?; - let channel: AllChannelChainsResponse = from_binary(&channel_binary)?; + let channel: AllChannelChainsResponse = from_json(&channel_binary)?; assert_eq!(channel.channels_chains.len(), 1); let channel_entry = channel.channels_chains.first().unwrap(); @@ -97,7 +97,7 @@ pub fn add_channel_chain_happy_path_multiple() -> anyhow::Result<(), Error> { MockQuerier::new(&[]).with_custom_handler(|q| match q { WormholeQuery::VerifyVaa { vaa } => { match WormholeKeeper::new().verify_vaa(&vaa.0, 0u64) { - Ok(_) => SystemResult::Ok(if let Ok(data) = to_binary(&Empty {}) { + Ok(_) => SystemResult::Ok(if let Ok(data) = to_json_binary(&Empty {}) { ContractResult::Ok(data) } else { ContractResult::Err("Unable to convert to binary".to_string()) @@ -106,7 +106,7 @@ pub fn add_channel_chain_happy_path_multiple() -> anyhow::Result<(), Error> { } } _ => cosmwasm_std::SystemResult::Ok(cosmwasm_std::ContractResult::Ok( - to_binary(&Empty {}).unwrap(), + to_json_binary(&Empty {}).unwrap(), )), }); @@ -149,7 +149,7 @@ pub fn add_channel_chain_happy_path_multiple() -> anyhow::Result<(), Error> { let channel_binary = query(readonly_deps, mock_env(), QueryMsg::AllChannelChains {})?; let AllChannelChainsResponse { channels_chains: mut channels, - }: AllChannelChainsResponse = from_binary(&channel_binary)?; + }: AllChannelChainsResponse = from_json(&channel_binary)?; channels.sort_by(|(_, a_chain_id), (_, b_chain_id)| a_chain_id.cmp(b_chain_id)); @@ -190,7 +190,7 @@ pub fn reject_invalid_add_channel_chain_vaas() { MockQuerier::new(&[]).with_custom_handler(|q| match q { WormholeQuery::VerifyVaa { vaa } => { match WormholeKeeper::new().verify_vaa(&vaa.0, 0u64) { - Ok(_) => SystemResult::Ok(if let Ok(data) = to_binary(&Empty {}) { + Ok(_) => SystemResult::Ok(if let Ok(data) = to_json_binary(&Empty {}) { ContractResult::Ok(data) } else { ContractResult::Err("Unable to convert to binary".to_string()) @@ -199,7 +199,7 @@ pub fn reject_invalid_add_channel_chain_vaas() { } } _ => cosmwasm_std::SystemResult::Ok(cosmwasm_std::ContractResult::Ok( - to_binary(&Empty {}).unwrap(), + to_json_binary(&Empty {}).unwrap(), )), }); @@ -312,7 +312,7 @@ pub fn reject_replayed_add_channel_chain_vaas() { MockQuerier::new(&[]).with_custom_handler(|q| match q { WormholeQuery::VerifyVaa { vaa } => { match WormholeKeeper::new().verify_vaa(&vaa.0, 0u64) { - Ok(_) => SystemResult::Ok(if let Ok(data) = to_binary(&Empty {}) { + Ok(_) => SystemResult::Ok(if let Ok(data) = to_json_binary(&Empty {}) { ContractResult::Ok(data) } else { ContractResult::Err("Unable to convert to binary".to_string()) @@ -321,7 +321,7 @@ pub fn reject_replayed_add_channel_chain_vaas() { } } _ => cosmwasm_std::SystemResult::Ok(cosmwasm_std::ContractResult::Ok( - to_binary(&Empty {}).unwrap(), + to_json_binary(&Empty {}).unwrap(), )), }); diff --git a/cosmwasm/contracts/wormhole-ibc/src/contract.rs b/cosmwasm/contracts/wormhole-ibc/src/contract.rs index 68384d2758..de5b48eaef 100644 --- a/cosmwasm/contracts/wormhole-ibc/src/contract.rs +++ b/cosmwasm/contracts/wormhole-ibc/src/contract.rs @@ -19,7 +19,7 @@ use crate::{ }; use anyhow::{bail, ensure, Context}; use cosmwasm_std::{ - to_binary, Binary, Deps, DepsMut, Env, Event, IbcMsg, MessageInfo, Response, StdResult, + to_json_binary, Binary, Deps, DepsMut, Env, Event, IbcMsg, MessageInfo, Response, StdResult, }; use cw_wormhole::msg::{ExecuteMsg as WormholeExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}; @@ -157,7 +157,7 @@ fn post_message_ibc( }; let ibc_msg = IbcMsg::SendPacket { channel_id, - data: to_binary(&packet)?, + data: to_json_binary(&packet)?, timeout: packet_timeout, }; diff --git a/cosmwasm/contracts/wormhole-ibc/src/msg.rs b/cosmwasm/contracts/wormhole-ibc/src/msg.rs index 2ba9c8e380..543ad3069b 100644 --- a/cosmwasm/contracts/wormhole-ibc/src/msg.rs +++ b/cosmwasm/contracts/wormhole-ibc/src/msg.rs @@ -31,7 +31,7 @@ pub enum WormholeIbcPacketMsg { #[cfg(test)] mod test { - use cosmwasm_std::to_binary; + use cosmwasm_std::to_json_binary; use cw_wormhole::msg::ExecuteMsg as WormholeExecuteMsg; use super::ExecuteMsg; @@ -48,12 +48,12 @@ mod test { let wormhole_submit_vaa = WormholeExecuteMsg::SubmitVAA { vaa: signed_vaa.clone().into(), }; - let wormhole_msg = to_binary(&wormhole_submit_vaa).unwrap(); + let wormhole_msg = to_json_binary(&wormhole_submit_vaa).unwrap(); let submit_vaa = ExecuteMsg::SubmitVAA { vaa: signed_vaa.into(), }; - let msg = to_binary(&submit_vaa).unwrap(); + let msg = to_json_binary(&submit_vaa).unwrap(); assert_eq!(wormhole_msg, msg); } diff --git a/cosmwasm/contracts/wormhole/src/contract.rs b/cosmwasm/contracts/wormhole/src/contract.rs index 99dd40c100..9d107f48be 100644 --- a/cosmwasm/contracts/wormhole/src/contract.rs +++ b/cosmwasm/contracts/wormhole/src/contract.rs @@ -1,5 +1,5 @@ use cosmwasm_std::{ - has_coins, to_binary, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, + has_coins, to_json_binary, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, Storage, WasmMsg, }; @@ -272,7 +272,7 @@ fn vaa_update_contract(_deps: DepsMut, env: Env, data: &[u8]) -> StdResult StdResult { match msg { - QueryMsg::GuardianSetInfo {} => to_binary(&query_guardian_set_info(deps)?), - QueryMsg::VerifyVAA { vaa, block_time } => to_binary(&query_parse_and_verify_vaa( + QueryMsg::GuardianSetInfo {} => to_json_binary(&query_guardian_set_info(deps)?), + QueryMsg::VerifyVAA { vaa, block_time } => to_json_binary(&query_parse_and_verify_vaa( deps, vaa.as_slice(), block_time, )?), - QueryMsg::GetState {} => to_binary(&query_state(deps)?), - QueryMsg::QueryAddressHex { address } => to_binary(&query_address_hex(deps, &address)?), + QueryMsg::GetState {} => to_json_binary(&query_state(deps)?), + QueryMsg::QueryAddressHex { address } => { + to_json_binary(&query_address_hex(deps, &address)?) + } } } diff --git a/cosmwasm/contracts/wormhole/src/testing/integration.rs b/cosmwasm/contracts/wormhole/src/testing/integration.rs index 0e5e2f5800..e97dcf57b9 100644 --- a/cosmwasm/contracts/wormhole/src/testing/integration.rs +++ b/cosmwasm/contracts/wormhole/src/testing/integration.rs @@ -9,7 +9,7 @@ use crate::{ state::{ConfigInfo, GuardianAddress, ParsedVAA, CONFIG_KEY}, }; use cosmwasm_std::{ - from_slice, + from_json, testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage}, Coin, OwnedDeps, Response, StdResult, Storage, }; @@ -29,7 +29,7 @@ static INITIALIZER: &str = "initializer"; fn get_config_info(storage: &S) -> ConfigInfo { let key = to_length_prefixed(CONFIG_KEY); let data = storage.get(&key).expect("data should exist"); - from_slice(&data).expect("invalid data") + from_json(&data).expect("invalid data") } fn do_init(guardians: &[GuardianAddress]) -> OwnedDeps { diff --git a/cosmwasm/packages/wormhole-bindings/src/fake.rs b/cosmwasm/packages/wormhole-bindings/src/fake.rs index eb47a990ca..9bc65a3f72 100644 --- a/cosmwasm/packages/wormhole-bindings/src/fake.rs +++ b/cosmwasm/packages/wormhole-bindings/src/fake.rs @@ -1,7 +1,9 @@ use std::{cell::RefCell, collections::BTreeSet, fmt::Debug, rc::Rc}; use anyhow::{anyhow, bail, ensure, Context}; -use cosmwasm_std::{to_binary, Addr, Api, Binary, BlockInfo, CustomQuery, Empty, Querier, Storage}; +use cosmwasm_std::{ + to_json_binary, Addr, Api, Binary, BlockInfo, CustomQuery, Empty, Querier, Storage, +}; use cw_multi_test::{AppResponse, CosmosRouter, Module}; use k256::ecdsa::{recoverable, signature::Signer, SigningKey}; use schemars::JsonSchema; @@ -178,7 +180,7 @@ impl WormholeKeeper { match request { WormholeQuery::VerifyVaa { vaa } => self .verify_vaa(&vaa, block.height) - .and_then(|e| to_binary(&e).map_err(From::from)), + .and_then(|e| to_json_binary(&e).map_err(From::from)), WormholeQuery::VerifyMessageSignature { prefix, data, @@ -186,10 +188,10 @@ impl WormholeKeeper { signature, } => self .verify_signature(&prefix, &data, guardian_set_index, &signature, block.height) - .and_then(|e| to_binary(&e).map_err(From::from)), + .and_then(|e| to_json_binary(&e).map_err(From::from)), WormholeQuery::CalculateQuorum { guardian_set_index } => self .calculate_quorum(guardian_set_index, block.height) - .and_then(|q| to_binary(&q).map_err(From::from)), + .and_then(|q| to_json_binary(&q).map_err(From::from)), } }