From bea42a1212479aa814da3aa48acb2f1d77e2d16e Mon Sep 17 00:00:00 2001 From: Freddyli7 Date: Wed, 6 Sep 2023 14:04:40 +0800 Subject: [PATCH 1/5] refacot runtime bridge account to be a vec --- Cargo.lock | 226 ++++++++++++++++-------------- bridge/src/lib.rs | 35 ++++- substrate-node/runtime/src/lib.rs | 18 ++- 3 files changed, 164 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 548e30f..6e18a62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -411,13 +411,13 @@ dependencies = [ [[package]] name = "async-recursion" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e97ce7de6cf12de5d7226c73f5ba9811622f4db3a5b91b55c53e987e5f91cba" +checksum = "5fd55a5ba1179988837d24ab4c7cc8ed6efdeff578ede0416b4225a5fca35bd0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -428,7 +428,7 @@ checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -484,7 +484,7 @@ dependencies = [ "cfg-if", "libc", "miniz_oxide", - "object 0.32.0", + "object 0.32.1", "rustc-demangle", ] @@ -554,13 +554,13 @@ dependencies = [ "lazy_static", "lazycell", "peeking_take_while", - "prettyplease 0.2.12", + "prettyplease 0.2.14", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -706,9 +706,9 @@ checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" [[package]] name = "bstr" -version = "1.6.1" +version = "1.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8042c26c77e5bd6897a7358e0abb3ec412ed126d826988135653fc669263899d" +checksum = "4c2f7349907b712260e64b0afe2f84692af14a454be26187d9df565c7f69266a" dependencies = [ "memchr", "serde", @@ -743,9 +743,9 @@ checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" [[package]] name = "bytemuck" -version = "1.13.1" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17febce684fd15d89027105661fec94afb475cb995fbc59d2865198446ba2eea" +checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" [[package]] name = "byteorder" @@ -880,9 +880,9 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.27" +version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f56b4c72906975ca04becb8a30e102dfecddd0c06181e3e95ddc444be28881f8" +checksum = "d87d9d13be47a5b7c3907137f1290b0459a7f80efb26be8c52afb11963bccb02" dependencies = [ "android-tzdata", "iana-time-zone", @@ -947,20 +947,19 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.1" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c8d502cbaec4595d2e7d5f61e318f05417bd2b66fdc3809498f0d3fdf0bea27" +checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6" dependencies = [ "clap_builder", "clap_derive", - "once_cell", ] [[package]] name = "clap_builder" -version = "4.4.1" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5891c7bc0edb3e1c2204fc5e94009affabeb1821c9e5fdc3959536c5c0bb984d" +checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" dependencies = [ "anstream", "anstyle", @@ -970,14 +969,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.4.0" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9fd1a5729c4548118d7d70ff234a44868d00489a4b6597b0b020918a0e91a1a" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1287,9 +1286,9 @@ dependencies = [ [[package]] name = "crypto-bigint" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4c2f4e1afd912bc40bfd6fed5d9dc1f288e0ba01bfcc835cc5bc3eb13efe15" +checksum = "740fe28e594155f10cfc383984cbefd529d7396050557148f79cb0f621204124" dependencies = [ "generic-array 0.14.7", "rand_core 0.6.4", @@ -1432,7 +1431,7 @@ checksum = "83fdaf97f4804dcebfa5862639bc9ce4121e82140bec2a987ac5140294865b5b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1459,7 +1458,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1476,7 +1475,7 @@ checksum = "2fa16a70dd58129e4dfffdff535fb1bce66673f7bbeec4a5a1765a504e1ccd84" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1746,7 +1745,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -1914,7 +1913,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "968405c8fdc9b3bf4df0a6638858cc0b52462836ab6b1c87377785dd09cf1c0b" dependencies = [ "base16ct 0.2.0", - "crypto-bigint 0.5.2", + "crypto-bigint 0.5.3", "digest 0.10.7", "ff 0.13.0", "generic-array 0.14.7", @@ -1946,13 +1945,13 @@ dependencies = [ [[package]] name = "enumn" -version = "0.1.11" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b893c4eb2dc092c811165f84dc7447fae16fb66521717968c34c509b39b1a5c5" +checksum = "c2ad8cef1d801a4686bfd8919f0b30eac4c8e48968c437a6405ded4fb5272d2b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2325,7 +2324,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2442,7 +2441,7 @@ dependencies = [ "proc-macro-warning", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2454,7 +2453,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2464,7 +2463,7 @@ source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.43#5e dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2544,7 +2543,7 @@ version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eeb4ed9e12f43b7fa0baae3f9cdda28352770132ef2e09a23760c29cae8bd47" dependencies = [ - "rustix 0.38.10", + "rustix 0.38.11", "windows-sys 0.48.0", ] @@ -2632,7 +2631,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -2643,7 +2642,7 @@ checksum = "d2411eed028cdf8c8034eaf21f9915f956b6c3abec4d4c7949ee67f0721127bd" dependencies = [ "futures-io", "rustls 0.20.9", - "webpki 0.22.0", + "webpki 0.22.1", ] [[package]] @@ -2852,9 +2851,9 @@ dependencies = [ [[package]] name = "handlebars" -version = "4.3.7" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83c3372087601b532857d332f5957cbae686da52bb7810bf038c3e3c3cc2fa0d" +checksum = "c39b3bc2a8f715298032cf5087e58573809374b08160aa7d750582bdb82d2683" dependencies = [ "log", "pest", @@ -2991,6 +2990,15 @@ dependencies = [ "hmac 0.8.1", ] +[[package]] +name = "home" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "hostname" version = "0.3.1" @@ -3334,7 +3342,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ "hermit-abi 0.3.2", - "rustix 0.38.10", + "rustix 0.38.11", "windows-sys 0.48.0", ] @@ -3947,7 +3955,7 @@ dependencies = [ "ring", "rustls 0.20.9", "thiserror", - "webpki 0.22.0", + "webpki 0.22.1", "x509-parser 0.14.0", "yasna", ] @@ -4274,9 +4282,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.6.1" +version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f478948fd84d9f8e86967bf432640e46adfb5a4bd4f14ef7e864ab38220534ae" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "memfd" @@ -4827,9 +4835,9 @@ dependencies = [ [[package]] name = "object" -version = "0.32.0" +version = "0.32.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ac5bbd07aea88c60a577a1ce218075ffd59208b2d7ca97adf9bfc5aeb21ebe" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" dependencies = [ "memchr", ] @@ -5519,19 +5527,20 @@ checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" [[package]] name = "pest" -version = "2.7.2" +version = "2.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1acb4a4365a13f749a93f1a094a7805e5cfa0955373a9de860d962eaa3a5fe5a" +checksum = "d7a4d085fd991ac8d5b05a147b437791b4260b76326baf0fc60cf7c9c27ecd33" dependencies = [ + "memchr", "thiserror", "ucd-trie", ] [[package]] name = "pest_derive" -version = "2.7.2" +version = "2.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "666d00490d4ac815001da55838c500eafb0320019bbaa44444137c48b443a853" +checksum = "a2bee7be22ce7918f641a33f08e3f43388c7656772244e2bbb2477f44cc9021a" dependencies = [ "pest", "pest_generator", @@ -5539,22 +5548,22 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.2" +version = "2.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68ca01446f50dbda87c1786af8770d535423fa8a53aec03b8f4e3d7eb10e0929" +checksum = "d1511785c5e98d79a05e8a6bc34b4ac2168a0e3e92161862030ad84daa223141" dependencies = [ "pest", "pest_meta", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] name = "pest_meta" -version = "2.7.2" +version = "2.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56af0a30af74d0445c0bf6d9d051c979b516a1a5af790d251daee76005420a48" +checksum = "b42f0394d3123e33353ca5e1e89092e533d2cc490389f2bd6131c43c634ebc5f" dependencies = [ "once_cell", "pest", @@ -5588,7 +5597,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -5905,12 +5914,12 @@ dependencies = [ [[package]] name = "prettyplease" -version = "0.2.12" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c64d9ba0963cdcea2e1b2230fbae2bab30eb25a174be395c41e764bfb65dd62" +checksum = "8832c0f9be7e3cae60727e6256cfd2cd3c3e2b6cd5dad4190ecb2fd658c9030b" dependencies = [ "proc-macro2", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -5968,7 +5977,7 @@ checksum = "0e99670bafb56b9a106419397343bdbc8b8742c3cc449fec6345f86173f47cd4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -6014,7 +6023,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -6134,7 +6143,7 @@ dependencies = [ "thiserror", "tinyvec", "tracing", - "webpki 0.22.0", + "webpki 0.22.1", ] [[package]] @@ -6331,7 +6340,7 @@ checksum = "7f7473c2cfcf90008193dd0e3e16599455cb601a9fce322b5bb55de799664925" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -6348,13 +6357,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.3.7", + "regex-automata 0.3.8", "regex-syntax 0.7.5", ] @@ -6369,9 +6378,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" dependencies = [ "aho-corasick", "memchr", @@ -6582,9 +6591,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.10" +version = "0.38.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed6248e1caa625eb708e266e06159f135e8c26f2bb7ceb72dc4b2766d0340964" +checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453" dependencies = [ "bitflags 2.4.0", "errno", @@ -6615,7 +6624,7 @@ dependencies = [ "log", "ring", "sct 0.7.0", - "webpki 0.22.0", + "webpki 0.22.1", ] [[package]] @@ -6797,7 +6806,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -7600,7 +7609,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -7895,7 +7904,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -7999,9 +8008,9 @@ dependencies = [ [[package]] name = "shlex" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" [[package]] name = "signal-hook-registry" @@ -8175,7 +8184,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -8398,7 +8407,7 @@ dependencies = [ "proc-macro2", "quote", "sp-core-hashing", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -8417,7 +8426,7 @@ source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.43#5e dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -8610,7 +8619,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -8796,7 +8805,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -9232,9 +9241,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.29" +version = "2.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "718fa2415bcb8d8bd775917a1bf12a7931b6dfa890753378538118181e0cb398" dependencies = [ "proc-macro2", "quote", @@ -9295,7 +9304,7 @@ dependencies = [ "cfg-if", "fastrand 2.0.0", "redox_syscall 0.3.5", - "rustix 0.38.10", + "rustix 0.38.11", "windows-sys 0.48.0", ] @@ -9316,22 +9325,22 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "thiserror" -version = "1.0.47" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" +checksum = "9d6d7a740b8a666a7e828dd00da9c0dc290dff53154ea77ac109281de90589b7" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.47" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" +checksum = "49922ecae66cc8a249b77e68d1d0623c1b2c514f0060c27cdc68bd62a1219d35" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -9488,7 +9497,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -9510,7 +9519,7 @@ checksum = "c43ee83903113e03984cb9e5cebe6c04a5116269e900e3ddba8f068a62adda59" dependencies = [ "rustls 0.20.9", "tokio", - "webpki 0.22.0", + "webpki 0.22.1", ] [[package]] @@ -9606,9 +9615,9 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55ae70283aba8d2a8b411c695c437fe25b8b5e44e23e780662002fc72fb47a82" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ "bitflags 2.4.0", "bytes", @@ -9655,7 +9664,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -9949,9 +9958,9 @@ dependencies = [ [[package]] name = "unsigned-varint" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d86a8dc7f45e4c1b0d30e43038c38f274e77af056aa5f74b93c2cf9eb3c1c836" +checksum = "6889a77d49f1f013504cec6bf97a2c730394adedaeb1deb5ea08949a50541105" dependencies = [ "asynchronous-codec", "bytes", @@ -10032,9 +10041,9 @@ checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" [[package]] name = "walkdir" -version = "2.3.3" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", "winapi-util", @@ -10088,7 +10097,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", "wasm-bindgen-shared", ] @@ -10122,7 +10131,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -10457,9 +10466,9 @@ dependencies = [ [[package]] name = "webpki" -version = "0.22.0" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +checksum = "f0e74f82d49d545ad128049b7e88f6576df2da6b02e9ce565c6f533be576957e" dependencies = [ "ring", "untrusted", @@ -10471,7 +10480,7 @@ version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" dependencies = [ - "webpki 0.22.0", + "webpki 0.22.1", ] [[package]] @@ -10699,13 +10708,14 @@ dependencies = [ [[package]] name = "which" -version = "4.4.0" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" dependencies = [ "either", - "libc", + "home", "once_cell", + "rustix 0.38.11", ] [[package]] @@ -11092,7 +11102,7 @@ dependencies = [ "Inflector", "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] @@ -11135,7 +11145,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.29", + "syn 2.0.31", ] [[package]] diff --git a/bridge/src/lib.rs b/bridge/src/lib.rs index 0a51199..3f69f2f 100644 --- a/bridge/src/lib.rs +++ b/bridge/src/lib.rs @@ -85,9 +85,9 @@ pub mod pallet { pub trait Config: frame_system::Config + sygma_access_segregator::Config { type RuntimeEvent: From> + IsType<::RuntimeEvent>; - /// Bridge transfer reserve account + /// Bridge transfer reserve accounts mapping with designated assets #[pallet::constant] - type TransferReserveAccount: Get; + type TransferReserveAccounts: Get)>>; /// EIP712 Verifying contract address /// This is used in EIP712 typed data domain @@ -476,14 +476,27 @@ pub mod pallet { let bridge_amount = amount - fee; + let token_reserved_account = Self::get_token_reserved_account(asset.id).ok_or(Error::::TransactFailed)?; + // Deposit `bridge_amount` of asset to reserve account if asset is reserved in local // chain. if T::IsReserve::contains(&asset, &MultiLocation::here()) { + // T::AssetTransactor::deposit_asset( + // &(asset.id, Fungible(bridge_amount)).into(), + // &Junction::AccountId32 { + // network: None, + // id: T::TransferReserveAccount::get().into(), + // } + // .into(), + // // Put empty message hash here because we are not sending XCM message + // &XcmContext::with_message_hash([0; 32]), + // ) + T::AssetTransactor::deposit_asset( &(asset.id, Fungible(bridge_amount)).into(), &Junction::AccountId32 { network: None, - id: T::TransferReserveAccount::get().into(), + id: token_reserved_account, } .into(), // Put empty message hash here because we are not sending XCM message @@ -705,6 +718,18 @@ pub mod pallet { } } + /// Return the TokenReservedAccount account by given token + pub fn get_token_reserved_account(token_location: AssetId) -> Option<[u8; 32]> { + let idx = T::TransferReserveAccounts::get().iter().position(|a| { + let i: Option = a.1.iter().position(|b| b.id == token_location); + if i != None { + true; + } + false + })?; + Some(T::TransferReserveAccounts::get()[idx].0.clone().into()) + } + /// convert the ECDSA 64-byte uncompressed pubkey to H160 address pub fn public_key_to_address(public_key: &[u8]) -> [u8; 20] { let hash = keccak_256(public_key); @@ -883,13 +908,15 @@ pub mod pallet { T::DecimalConverter::convert_from(&(asset_id, amount).into()) .ok_or(Error::::DecimalConversionFail)?; + let token_reserved_account = Self::get_token_reserved_account(asset_id).ok_or(Error::::TransactFailed)?; + // Withdraw `decimal_converted_asset` of asset from reserve account if T::IsReserve::contains(&decimal_converted_asset, &MultiLocation::here()) { T::AssetTransactor::withdraw_asset( &decimal_converted_asset, &Junction::AccountId32 { network: None, - id: T::TransferReserveAccount::get().into(), + id: token_reserved_account, } .into(), None, diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 6d5217e..052cabb 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -20,7 +20,7 @@ use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{ - AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, Verify, + AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, Verify, }, transaction_validity::{TransactionSource, TransactionValidity}, AccountId32, ApplyExtrinsicResult, MultiSignature, Perbill, @@ -398,8 +398,20 @@ parameter_types! { // TreasuryAccount address: 5ELLU7ibt5ZrNEYRwohtaRBDBa3TzcWwwPELBPSWWd2mbgv3 pub TreasuryAccount: AccountId32 = AccountId32::new([100u8; 32]); // BridgeAccount is an account for holding transferred asset collection + // BridgeAccount address: 5EMepC39b7E2zfM9g6CkPp8KCAxGTh7D4w4T2tFjmjpd4tPw - pub BridgeAccount: AccountId32 = AccountId32::new([101u8; 32]); + // pub BridgeAccount: AccountId32 = AccountId32::new([101u8; 32]); + + // derived bridgeAccount: 5EYCAe5jLbHcAAMKvLFSXgCTbPrLgBJusvPwfKcaKzuf5X5e + // derived account 0, seed 0u32 = empty seed + pub BridgeAccountNative: AccountId32 = SygmaBridgePalletId::get().into_account_truncating(); + + // derived account 1, seed 1u32 5EYCAe5jLbHcAAMKvLFiGhk3htXY8jQncbLTDGJQnpnPMAVp + pub BridgeAccountUSDC: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); + + pub UsdcAsset: MultiAsset = (Concrete(UsdcLocation::get()), 0u128).into(); + pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeAsset::get()]), (BridgeAccountUSDC::get(), vec![UsdcAsset::get()])]; + // EIP712ChainID is the chainID that pallet is assigned with, used in EIP712 typed data domain pub EIP712ChainID: ChainID = U256::from(5); // DestVerifyingContractAddress is a H160 address that is used in proposal signature verification, specifically EIP712 typed data @@ -714,7 +726,7 @@ impl ExtractDestinationData for DestinationDataParser { impl sygma_bridge::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type TransferReserveAccount = BridgeAccount; + type TransferReserveAccounts = BridgeAccounts; type FeeReserveAccount = TreasuryAccount; type EIP712ChainID = EIP712ChainID; type DestVerifyingContractAddress = DestVerifyingContractAddress; From f896ff235df370ec7975d771fde1f804d8108886 Mon Sep 17 00:00:00 2001 From: Freddyli7 Date: Fri, 8 Sep 2023 12:01:24 +0800 Subject: [PATCH 2/5] add tests --- bridge/src/lib.rs | 328 +++++++++++++++++++++--------- bridge/src/mock.rs | 34 ++-- substrate-node/runtime/src/lib.rs | 56 +++-- 3 files changed, 268 insertions(+), 150 deletions(-) diff --git a/bridge/src/lib.rs b/bridge/src/lib.rs index 3f69f2f..e1a5379 100644 --- a/bridge/src/lib.rs +++ b/bridge/src/lib.rs @@ -87,7 +87,7 @@ pub mod pallet { /// Bridge transfer reserve accounts mapping with designated assets #[pallet::constant] - type TransferReserveAccounts: Get)>>; + type TransferReserveAccounts: Get)>>; /// EIP712 Verifying contract address /// This is used in EIP712 typed data domain @@ -226,8 +226,10 @@ pub mod pallet { ExtractDestDataFailed, /// Failed on the decimal converter DecimalConversionFail, - /// Deposit nonce has reached max integer qvalue + /// Deposit nonce has reached max integer value DepositNonceOverflow, + /// Asset not bound to a liquidity holder account + NoLiquidityHolderAccountBound, /// Function unimplemented Unimplemented, } @@ -476,7 +478,8 @@ pub mod pallet { let bridge_amount = amount - fee; - let token_reserved_account = Self::get_token_reserved_account(asset.id).ok_or(Error::::TransactFailed)?; + let token_reserved_account = Self::get_token_reserved_account(&asset.id) + .ok_or(Error::::NoLiquidityHolderAccountBound)?; // Deposit `bridge_amount` of asset to reserve account if asset is reserved in local // chain. @@ -494,11 +497,7 @@ pub mod pallet { T::AssetTransactor::deposit_asset( &(asset.id, Fungible(bridge_amount)).into(), - &Junction::AccountId32 { - network: None, - id: token_reserved_account, - } - .into(), + &Junction::AccountId32 { network: None, id: token_reserved_account }.into(), // Put empty message hash here because we are not sending XCM message &XcmContext::with_message_hash([0; 32]), ) @@ -718,12 +717,12 @@ pub mod pallet { } } - /// Return the TokenReservedAccount account by given token - pub fn get_token_reserved_account(token_location: AssetId) -> Option<[u8; 32]> { + /// Return the TokenReservedAccount address by the given token + pub fn get_token_reserved_account(token_id: &AssetId) -> Option<[u8; 32]> { let idx = T::TransferReserveAccounts::get().iter().position(|a| { - let i: Option = a.1.iter().position(|b| b.id == token_location); - if i != None { - true; + let i: Option = a.1.iter().position(|b| b == token_id); + if i.is_some() { + return true; } false })?; @@ -908,17 +907,14 @@ pub mod pallet { T::DecimalConverter::convert_from(&(asset_id, amount).into()) .ok_or(Error::::DecimalConversionFail)?; - let token_reserved_account = Self::get_token_reserved_account(asset_id).ok_or(Error::::TransactFailed)?; + let token_reserved_account = Self::get_token_reserved_account(&asset_id) + .ok_or(Error::::NoLiquidityHolderAccountBound)?; // Withdraw `decimal_converted_asset` of asset from reserve account if T::IsReserve::contains(&decimal_converted_asset, &MultiLocation::here()) { T::AssetTransactor::withdraw_asset( &decimal_converted_asset, - &Junction::AccountId32 { - network: None, - id: token_reserved_account, - } - .into(), + &Junction::AccountId32 { network: None, id: token_reserved_account }.into(), None, ) .map_err(|_| Error::::TransactFailed)?; @@ -959,23 +955,59 @@ pub mod pallet { }; use bridge::mock::{ assert_events, new_test_ext, slice_to_generalkey, AccessSegregator, Assets, Balances, - BridgeAccount, BridgePalletIndex, NativeLocation, NativeResourceId, Runtime, - RuntimeEvent, RuntimeOrigin as Origin, SygmaBasicFeeHandler, SygmaBridge, - SygmaFeeHandlerRouter, SygmaPercentageFeeHandler, TreasuryAccount, UsdcAssetId, - UsdcLocation, UsdcResourceId, ALICE, ASSET_OWNER, BOB, DEST_DOMAIN_ID, ENDOWED_BALANCE, + BridgeAccountNative, BridgeAccountOtherTokens, BridgePalletIndex, NativeLocation, + NativeResourceId, Runtime, RuntimeEvent, RuntimeOrigin as Origin, SygmaBasicFeeHandler, + SygmaBridge, SygmaFeeHandlerRouter, SygmaPercentageFeeHandler, TreasuryAccount, + UsdtAssetId, UsdtLocation, UsdtResourceId, ALICE, ASSET_OWNER, BOB, DEST_DOMAIN_ID, + ENDOWED_BALANCE, }; use codec::{self, Encode}; use frame_support::{ assert_noop, assert_ok, crypto::ecdsa::ECDSAExt, traits::tokens::fungibles::Create as FungibleCerate, }; + use parachains_common::AccountId; use primitive_types::U256; - use sp_core::{ecdsa, Pair}; + use sp_core::{ecdsa, ByteArray, Pair}; use sp_std::{boxed::Box, vec}; use sygma_fee_handler_router::FeeHandlerType; use sygma_traits::{DomainID, MpcAddress, TransferType}; use xcm::latest::prelude::*; + #[test] + fn get_token_reserved_account_test() { + new_test_ext().execute_with(|| { + assert_eq!( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()).unwrap(), + BridgeAccountOtherTokens::get().as_slice() + ); + assert_eq!( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()).unwrap(), + BridgeAccountNative::get().as_slice() + ); + assert_eq!( + SygmaBridge::get_token_reserved_account(&AstrLocation::get().into()).unwrap(), + BridgeAccountOtherTokens::get().as_slice() + ); + + // unknown token should return None + assert_eq!( + SygmaBridge::get_token_reserved_account( + &MultiLocation::new( + 2, + X3( + Parachain(1000), + slice_to_generalkey(b"sygma"), + slice_to_generalkey(b"unknown"), + ), + ) + .into() + ), + None + ); + }) + } + #[test] fn set_mpc_address() { new_test_ext().execute_with(|| { @@ -1262,7 +1294,13 @@ pub mod pallet { )); // Check balances assert_eq!(Balances::free_balance(ALICE), ENDOWED_BALANCE - amount); - assert_eq!(Balances::free_balance(BridgeAccount::get()), amount - fee); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + amount - fee + ); assert_eq!(Balances::free_balance(TreasuryAccount::get()), fee); // Check event assert_events(vec![ @@ -1340,13 +1378,13 @@ pub mod pallet { assert_ok!(SygmaBasicFeeHandler::set_fee( Origin::root(), DEST_DOMAIN_ID, - Box::new(UsdcLocation::get().into()), + Box::new(UsdtLocation::get().into()), fee )); assert_ok!(SygmaFeeHandlerRouter::set_fee_handler( Origin::root(), DEST_DOMAIN_ID, - Box::new(UsdcLocation::get().into()), + Box::new(UsdtLocation::get().into()), FeeHandlerType::BasicFeeHandler, )); assert_ok!(SygmaBridge::register_domain( @@ -1356,23 +1394,23 @@ pub mod pallet { )); assert_ok!(SygmaBridge::set_mpc_address(Origin::root(), test_mpc_addr)); - // Register foreign asset (USDC) with asset id 0 + // Register foreign asset (USDT) with asset id 0 assert_ok!( as FungibleCerate< ::AccountId, - >>::create(UsdcAssetId::get(), ASSET_OWNER, true, 1,)); + >>::create(UsdtAssetId::get(), ASSET_OWNER, true, 1,)); - // Mint some USDC to ALICE for test + // Mint some USDT to ALICE for test assert_ok!(Assets::mint( Origin::signed(ASSET_OWNER), codec::Compact(0), ALICE, ENDOWED_BALANCE, )); - assert_eq!(Assets::balance(UsdcAssetId::get(), &ALICE), ENDOWED_BALANCE); + assert_eq!(Assets::balance(UsdtAssetId::get(), &ALICE), ENDOWED_BALANCE); assert_ok!(SygmaBridge::deposit( Origin::signed(ALICE), - Box::new((Concrete(UsdcLocation::get()), Fungible(amount)).into()), + Box::new((Concrete(UsdtLocation::get()), Fungible(amount)).into()), Box::new(MultiLocation { parents: 0, interior: X2( @@ -1382,14 +1420,23 @@ pub mod pallet { }), )); // Check balances - assert_eq!(Assets::balance(UsdcAssetId::get(), &ALICE), ENDOWED_BALANCE - amount); - assert_eq!(Assets::balance(UsdcAssetId::get(), BridgeAccount::get()), 0); - assert_eq!(Assets::balance(UsdcAssetId::get(), TreasuryAccount::get()), fee); + assert_eq!(Assets::balance(UsdtAssetId::get(), &ALICE), ENDOWED_BALANCE - amount); + assert_eq!( + Assets::balance( + UsdtAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + ) + ), + 0 + ); + assert_eq!(Assets::balance(UsdtAssetId::get(), TreasuryAccount::get()), fee); // Check event assert_events(vec![ RuntimeEvent::SygmaBridge(SygmaBridgeEvent::Deposit { dest_domain_id: DEST_DOMAIN_ID, - resource_id: UsdcResourceId::get(), + resource_id: UsdtResourceId::get(), deposit_nonce: 0, sender: ALICE, transfer_type: TransferType::FungibleTransfer, @@ -1402,9 +1449,9 @@ pub mod pallet { RuntimeEvent::SygmaBridge(SygmaBridgeEvent::FeeCollected { fee_payer: ALICE, dest_domain_id: DEST_DOMAIN_ID, - resource_id: UsdcResourceId::get(), + resource_id: UsdtResourceId::get(), fee_amount: fee, - fee_asset_id: UsdcLocation::get().into(), + fee_asset_id: UsdtLocation::get().into(), }), ]); }) @@ -1747,10 +1794,10 @@ pub mod pallet { }), )); - // Register foreign asset (USDC) with asset id 0 + // Register foreign asset (USDT) with asset id 0 assert_ok!( as FungibleCerate< ::AccountId, - >>::create(UsdcAssetId::get(), ASSET_OWNER, true, 1,)); + >>::create(UsdtAssetId::get(), ASSET_OWNER, true, 1,)); // Generate proposals // amount is in 18 decimal 0.000200000000000000, will be convert to 12 decimal @@ -1767,10 +1814,10 @@ pub mod pallet { }; // amount is in 18 decimal 0.000200000000000000, will be convert to 18 decimal // 0.000200000000000000 - let valid_usdc_transfer_proposal = Proposal { + let valid_usdt_transfer_proposal = Proposal { origin_domain_id: DEST_DOMAIN_ID, deposit_nonce: 2, - resource_id: UsdcResourceId::get(), + resource_id: UsdtResourceId::get(), data: SygmaBridge::create_deposit_data( amount, MultiLocation::new(0, X1(AccountId32 { network: None, id: BOB.into() })) @@ -1816,13 +1863,13 @@ pub mod pallet { let empty_data_proposal = Proposal { origin_domain_id: DEST_DOMAIN_ID, deposit_nonce: 3, - resource_id: UsdcResourceId::get(), + resource_id: UsdtResourceId::get(), data: vec![], }; let proposals = vec![ valid_native_transfer_proposal, - valid_usdc_transfer_proposal, + valid_usdt_transfer_proposal, invalid_depositnonce_proposal, invalid_domainid_proposal, invalid_resourceid_proposal, @@ -1861,7 +1908,7 @@ pub mod pallet { bridge::Error::::BadMpcSignature, ); assert_eq!(Balances::free_balance(&BOB), ENDOWED_BALANCE); - assert_eq!(Assets::balance(UsdcAssetId::get(), &BOB), 0); + assert_eq!(Assets::balance(UsdtAssetId::get(), &BOB), 0); assert!(SygmaBridge::verify_by_mpc_address( final_message, proposals_with_valid_signature.encode() @@ -1874,8 +1921,8 @@ pub mod pallet { // proposal amount is in 18 decimal 0.000200000000000000, will be convert to 12 // decimal 0.000200000000(200000000) because native asset is defined in 12 decimal assert_eq!(Balances::free_balance(&BOB), ENDOWED_BALANCE + 200000000); - // usdc is defined in 18 decimal so that converted amount is the same as in proposal - assert_eq!(Assets::balance(UsdcAssetId::get(), &BOB), amount); + // usdt is defined in 18 decimal so that converted amount is the same as in proposal + assert_eq!(Assets::balance(UsdtAssetId::get(), &BOB), amount); }) } @@ -2071,10 +2118,10 @@ pub mod pallet { let amount_native_asset = 123_456_789_123_456u128; // 123.456_789_123_456 let adjusted_amount_native_asset = 122_456_789_123_456_000_000u128; // amount_native_asset - fee_native_asset then adjust it to 18 decimals - // usdc asset with 18 decimal - let fee_usdc_asset = 1_000_000_000_000_000_000u128; // 1.0 usdc asset - let amount_usdc_asset = 123_456_789_123_456_789_123u128; // 123.456_789_123_456_789_123 - let adjusted_amount_usdc_asset = 122_456_789_123_456_789_123u128; // amount_usdc_asset - fee_usdc_asset then adjust it to 18 decimals + // usdt asset with 18 decimal + let fee_usdt_asset = 1_000_000_000_000_000_000u128; // 1.0 usdt asset + let amount_usdt_asset = 123_456_789_123_456_789_123u128; // 123.456_789_123_456_789_123 + let adjusted_amount_usdt_asset = 122_456_789_123_456_789_123u128; // amount_usdt_asset - fee_usdt_asset then adjust it to 18 decimals // astr asset with 24 decimal let fee_astr_asset = 1_000_000_000_000_000_000_000_000u128; // 1.0 astr asset @@ -2091,8 +2138,8 @@ pub mod pallet { assert_ok!(SygmaBasicFeeHandler::set_fee( Origin::root(), DEST_DOMAIN_ID, - Box::new(UsdcLocation::get().into()), - fee_usdc_asset + Box::new(UsdtLocation::get().into()), + fee_usdt_asset )); assert_ok!(SygmaBasicFeeHandler::set_fee( Origin::root(), @@ -2109,7 +2156,7 @@ pub mod pallet { assert_ok!(SygmaFeeHandlerRouter::set_fee_handler( Origin::root(), DEST_DOMAIN_ID, - Box::new(UsdcLocation::get().into()), + Box::new(UsdtLocation::get().into()), FeeHandlerType::BasicFeeHandler, )); assert_ok!(SygmaFeeHandlerRouter::set_fee_handler( @@ -2143,7 +2190,10 @@ pub mod pallet { assert_eq!(Balances::free_balance(ALICE), ENDOWED_BALANCE - amount_native_asset); // native asset should be reserved so that BridgeAccount should hold it assert_eq!( - Balances::free_balance(BridgeAccount::get()), + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), amount_native_asset - fee_native_asset ); // TreasuryAccount is collecting the bridging fee @@ -2171,25 +2221,25 @@ pub mod pallet { }), ]); - // deposit usdc asset which has 18 decimal - // Register foreign asset (usdc) with asset id 0 + // deposit usdt asset which has 18 decimal + // Register foreign asset (usdt) with asset id 0 assert_ok!( as FungibleCerate< ::AccountId, - >>::create(UsdcAssetId::get(), ASSET_OWNER, true, 1,)); + >>::create(UsdtAssetId::get(), ASSET_OWNER, true, 1,)); - // Mint some usdc to ALICE for test + // Mint some usdt to ALICE for test assert_ok!(Assets::mint( Origin::signed(ASSET_OWNER), codec::Compact(0), ALICE, ENDOWED_BALANCE, )); // make sure Alice owns enough funds here - assert_eq!(Assets::balance(UsdcAssetId::get(), &ALICE), ENDOWED_BALANCE); + assert_eq!(Assets::balance(UsdtAssetId::get(), &ALICE), ENDOWED_BALANCE); // deposit assert_ok!(SygmaBridge::deposit( Origin::signed(ALICE), - Box::new((Concrete(UsdcLocation::get()), Fungible(amount_usdc_asset)).into()), + Box::new((Concrete(UsdtLocation::get()), Fungible(amount_usdt_asset)).into()), Box::new(MultiLocation { parents: 0, interior: X2( @@ -2200,27 +2250,36 @@ pub mod pallet { )); // Check balances assert_eq!( - Assets::balance(UsdcAssetId::get(), &ALICE), - ENDOWED_BALANCE - amount_usdc_asset + Assets::balance(UsdtAssetId::get(), &ALICE), + ENDOWED_BALANCE - amount_usdt_asset + ); + // usdt asset should not be reserved so that BridgeAccount should not hold it + assert_eq!( + Assets::balance( + UsdtAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) + .unwrap() + ) + ), + 0 ); - // usdc asset should not be reserved so that BridgeAccount should not hold it - assert_eq!(Assets::balance(UsdcAssetId::get(), BridgeAccount::get()), 0); // TreasuryAccount is collecting the bridging fee assert_eq!( - Assets::balance(UsdcAssetId::get(), TreasuryAccount::get()), - fee_usdc_asset + Assets::balance(UsdtAssetId::get(), TreasuryAccount::get()), + fee_usdt_asset ); // Check event assert_events(vec![ RuntimeEvent::SygmaBridge(SygmaBridgeEvent::Deposit { dest_domain_id: DEST_DOMAIN_ID, - resource_id: UsdcResourceId::get(), + resource_id: UsdtResourceId::get(), deposit_nonce: 1, sender: ALICE, transfer_type: TransferType::FungibleTransfer, deposit_data: SygmaBridge::create_deposit_data( - adjusted_amount_usdc_asset, + adjusted_amount_usdt_asset, b"ethereum recipient".to_vec(), ), handler_response: vec![], @@ -2228,9 +2287,9 @@ pub mod pallet { RuntimeEvent::SygmaBridge(SygmaBridgeEvent::FeeCollected { fee_payer: ALICE, dest_domain_id: DEST_DOMAIN_ID, - resource_id: UsdcResourceId::get(), - fee_amount: fee_usdc_asset, - fee_asset_id: UsdcLocation::get().into(), + resource_id: UsdtResourceId::get(), + fee_amount: fee_usdt_asset, + fee_asset_id: UsdtLocation::get().into(), }), ]); @@ -2269,7 +2328,13 @@ pub mod pallet { // astr asset should be reserved so that BridgeAccount should hold it(Astr is not // defined in ConcrateSygmaAsset) assert_eq!( - Assets::balance(AstrAssetId::get(), BridgeAccount::get()), + Assets::balance( + AstrAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&AstrLocation::get().into()) + .unwrap() + ) + ), amount_astr_asset - fee_astr_asset ); // TreasuryAccount is collecting the bridging fee @@ -2380,7 +2445,13 @@ pub mod pallet { }), )); // BridgeAccount should have half of alice native asset - fee - assert_eq!(Balances::free_balance(BridgeAccount::get()), ENDOWED_BALANCE / 2 - fee); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + ENDOWED_BALANCE / 2 - fee + ); // TreasuryAccount is collecting the bridging fee assert_eq!(Balances::free_balance(TreasuryAccount::get()), fee); @@ -2416,37 +2487,37 @@ pub mod pallet { ENDOWED_BALANCE / 2 + 100_000_000_000_000 ); - // proposal for bridging usdc asset to alice(usdc asset is 18 decimal) - // Register foreign asset (usdc) with asset id 0 + // proposal for bridging usdt asset to alice(usdt asset is 18 decimal) + // Register foreign asset (usdt) with asset id 0 assert_ok!( as FungibleCerate< ::AccountId, - >>::create(UsdcAssetId::get(), ASSET_OWNER, true, 1,)); + >>::create(UsdtAssetId::get(), ASSET_OWNER, true, 1,)); - let p_usdc = Proposal { + let p_usdt = Proposal { origin_domain_id: 1, deposit_nonce: 2, - resource_id: UsdcResourceId::get(), + resource_id: UsdtResourceId::get(), data: SygmaBridge::create_deposit_data( bridge_amount, MultiLocation::new(0, X1(AccountId32 { network: None, id: ALICE.into() })) .encode(), ), }; - let proposals_usdc = vec![p_usdc]; - let final_message_usdc = - SygmaBridge::construct_ecdsa_signing_proposals_data(&proposals_usdc); - let signature_usdc = pair.sign_prehashed(&final_message_usdc); + let proposals_usdt = vec![p_usdt]; + let final_message_usdt = + SygmaBridge::construct_ecdsa_signing_proposals_data(&proposals_usdt); + let signature_usdt = pair.sign_prehashed(&final_message_usdt); - // alice does not have any usdc at this moment - assert_eq!(Assets::balance(UsdcAssetId::get(), &ALICE), 0); + // alice does not have any usdt at this moment + assert_eq!(Assets::balance(UsdtAssetId::get(), &ALICE), 0); assert_ok!(SygmaBridge::execute_proposal( Origin::signed(ALICE), - proposals_usdc, - signature_usdc.encode() + proposals_usdt, + signature_usdt.encode() )); - // alice should have 100 usdc at this moment (100 usdc with 18 decimals) + // alice should have 100 usdt at this moment (100 usdt with 18 decimals) assert_eq!( - Assets::balance(UsdcAssetId::get(), &ALICE), + Assets::balance(UsdtAssetId::get(), &ALICE), 100_000_000_000_000_000_000 ); @@ -2460,11 +2531,20 @@ pub mod pallet { assert_ok!(Assets::mint( Origin::signed(ASSET_OWNER), codec::Compact(1), - BridgeAccount::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&AstrLocation::get().into()) + .unwrap() + ), ENDOWED_BALANCE )); assert_eq!( - Assets::balance(AstrAssetId::get(), BridgeAccount::get()), + Assets::balance( + AstrAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&AstrLocation::get().into()) + .unwrap() + ) + ), ENDOWED_BALANCE ); @@ -2723,7 +2803,13 @@ pub mod pallet { )); // Check balances assert_eq!(Balances::free_balance(ALICE), ENDOWED_BALANCE - amount); - assert_eq!(Balances::free_balance(BridgeAccount::get()), amount - fee); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + amount - fee + ); assert_eq!(Balances::free_balance(TreasuryAccount::get()), fee); // proposal execution should work @@ -2801,7 +2887,13 @@ pub mod pallet { // Check balances of Alice after deposit 200 native token assert_eq!(Balances::free_balance(ALICE), ENDOWED_BALANCE - amount); // Check reserved native token - assert_eq!(Balances::free_balance(BridgeAccount::get()), 190_000_000_000_000u128); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 190_000_000_000_000u128 + ); // Check fee collected assert_eq!(Balances::free_balance(TreasuryAccount::get()), 10_000_000_000_000u128); // Check event @@ -2864,7 +2956,13 @@ pub mod pallet { }), )); // Check reserved native token, should increase by 0.02 to 190.020000000000 - assert_eq!(Balances::free_balance(BridgeAccount::get()), 190_020_000_000_000u128); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 190_020_000_000_000u128 + ); // Check fee collected, should increase by 199.98 to 209.980000000000 assert_eq!(Balances::free_balance(TreasuryAccount::get()), 209_980_000_000_000u128); @@ -2890,7 +2988,13 @@ pub mod pallet { }), )); // Check reserved native token, should increase by 200 to 390.020000000000 - assert_eq!(Balances::free_balance(BridgeAccount::get()), 390_020_000_000_000u128); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 390_020_000_000_000u128 + ); // Check fee collected, should increase by 0 to 209.980000000000 assert_eq!(Balances::free_balance(TreasuryAccount::get()), 209_980_000_000_000u128); @@ -2909,7 +3013,13 @@ pub mod pallet { ); // Check reserved native token, should remain as 390.020000000000 - assert_eq!(Balances::free_balance(BridgeAccount::get()), 390_020_000_000_000u128); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 390_020_000_000_000u128 + ); // Check fee collected, should remain as 209.980000000000 assert_eq!(Balances::free_balance(TreasuryAccount::get()), 209_980_000_000_000u128); @@ -2939,7 +3049,13 @@ pub mod pallet { }), )); // Check reserved native token, should increase by 100 to 490.020000000000 - assert_eq!(Balances::free_balance(BridgeAccount::get()), 490_020_000_000_000u128); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 490_020_000_000_000u128 + ); // Check fee collected, should increase by 100 to 309.980000000000 assert_eq!(Balances::free_balance(TreasuryAccount::get()), 309_980_000_000_000u128); @@ -2961,7 +3077,10 @@ pub mod pallet { )); // Check reserved native token, should increase by 199000 to 199490.020000000000 assert_eq!( - Balances::free_balance(BridgeAccount::get()), + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), 199_490_020_000_000_000u128 ); // Check fee collected, should increase by 1000 to 1309.980000000000 @@ -3053,7 +3172,13 @@ pub mod pallet { )); // Check balances assert_eq!(Balances::free_balance(ALICE), ENDOWED_BALANCE - amount); - assert_eq!(Balances::free_balance(BridgeAccount::get()), amount - fee); + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + amount - fee + ); assert_eq!(Balances::free_balance(TreasuryAccount::get()), fee); // Override Basic fee handler to Percentage fee handler with 5% fee rate @@ -3087,7 +3212,10 @@ pub mod pallet { assert_eq!(Balances::free_balance(ALICE), ENDOWED_BALANCE - amount * 2); // Check reserved native token, should increase by 190 assert_eq!( - Balances::free_balance(BridgeAccount::get()), + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), amount - fee + 190_000_000_000_000u128 ); // Check fee collected, should increase by 10 diff --git a/bridge/src/mock.rs b/bridge/src/mock.rs index 92437cc..d9d4fc5 100644 --- a/bridge/src/mock.rs +++ b/bridge/src/mock.rs @@ -15,7 +15,7 @@ use polkadot_parachain::primitives::Sibling; use sp_core::{hash::H256, Get}; use sp_runtime::{ testing::Header, - traits::{BlakeTwo256, IdentityLookup}, + traits::{AccountIdConversion, BlakeTwo256, IdentityLookup}, AccountId32, Perbill, }; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result}; @@ -211,21 +211,21 @@ parameter_types! { pub TreasuryAccount: AccountId32 = AccountId32::new([100u8; 32]); pub EIP712ChainID: ChainID = primitive_types::U256([1u64; 4]); pub DestVerifyingContractAddress: VerifyingContractAddress = primitive_types::H160([1u8; 20]); - pub BridgeAccount: AccountId32 = AccountId32::new([101u8; 32]); + pub BridgeAccountNative: AccountId32 = SygmaBridgePalletId::get().into_account_truncating(); + pub BridgeAccountOtherTokens: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); + pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherTokens::get(), vec![UsdtLocation::get().into(), AstrLocation::get().into()])]; pub CheckingAccount: AccountId32 = AccountId32::new([102u8; 32]); pub RelayNetwork: NetworkId = NetworkId::Polkadot; pub AssetsPalletLocation: MultiLocation = PalletInstance(::index() as u8).into(); pub NativeLocation: MultiLocation = MultiLocation::here(); - // amount = 0 act as placeholder - pub NativeAsset: MultiAsset = (Concrete(MultiLocation::here()), 0u128).into(); - pub UsdcAssetId: AssetId = 0; - pub UsdcLocation: MultiLocation = MultiLocation::new( + pub UsdtAssetId: AssetId = 0; + pub UsdtLocation: MultiLocation = MultiLocation::new( 1, X3( Parachain(2004), slice_to_generalkey(b"sygma"), - slice_to_generalkey(b"usdc"), + slice_to_generalkey(b"usdt"), ), ); pub AstrAssetId: AssetId = 1; @@ -238,10 +238,10 @@ parameter_types! { ), ); pub NativeResourceId: ResourceId = hex_literal::hex!("00e6dfb61a2fb903df487c401663825643bb825d41695e63df8af6162ab145a6"); - pub UsdcResourceId: ResourceId = hex_literal::hex!("00b14e071ddad0b12be5aca6dffc5f2584ea158d9b0ce73e1437115e97a32a3e"); + pub UsdtResourceId: ResourceId = hex_literal::hex!("00b14e071ddad0b12be5aca6dffc5f2584ea158d9b0ce73e1437115e97a32a3e"); pub AstrResourceId: ResourceId = hex_literal::hex!("4e071db61a2fb903df487c401663825643ba158d9b0ce73e1437163825643bba"); - pub ResourcePairs: Vec<(XcmAssetId, ResourceId)> = vec![(NativeLocation::get().into(), NativeResourceId::get()), (UsdcLocation::get().into(), UsdcResourceId::get()), (AstrLocation::get().into(), AstrResourceId::get())]; - pub AssetDecimalPairs: Vec<(XcmAssetId, u8)> = vec![(NativeLocation::get().into(), 12u8), (UsdcLocation::get().into(), 18u8), (AstrLocation::get().into(), 24u8)]; + pub ResourcePairs: Vec<(XcmAssetId, ResourceId)> = vec![(NativeLocation::get().into(), NativeResourceId::get()), (UsdtLocation::get().into(), UsdtResourceId::get()), (AstrLocation::get().into(), AstrResourceId::get())]; + pub AssetDecimalPairs: Vec<(XcmAssetId, u8)> = vec![(NativeLocation::get().into(), 12u8), (UsdtLocation::get().into(), 18u8), (AstrLocation::get().into(), 24u8)]; pub const SygmaBridgePalletId: PalletId = PalletId(*b"sygma/01"); } @@ -277,8 +277,8 @@ pub struct SimpleForeignAssetConverter(PhantomData<()>); impl Convert for SimpleForeignAssetConverter { fn convert_ref(id: impl Borrow) -> result::Result { - if &UsdcLocation::get() == id.borrow() { - Ok(UsdcAssetId::get()) + if &UsdtLocation::get() == id.borrow() { + Ok(UsdtAssetId::get()) } else if &AstrLocation::get() == id.borrow() { Ok(AstrAssetId::get()) } else { @@ -286,8 +286,8 @@ impl Convert for SimpleForeignAssetConverter { } } fn reverse_ref(what: impl Borrow) -> result::Result { - if *what.borrow() == UsdcAssetId::get() { - Ok(UsdcLocation::get()) + if *what.borrow() == UsdtAssetId::get() { + Ok(UsdtLocation::get()) } else if *what.borrow() == AstrAssetId::get() { Ok(AstrLocation::get()) } else { @@ -300,8 +300,8 @@ impl MatchesFungibles for SimpleForeignAssetConverter { fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), ExecutionError> { match (&a.fun, &a.id) { (Fungible(ref amount), Concrete(ref id)) => { - if id == &UsdcLocation::get() { - Ok((UsdcAssetId::get(), *amount)) + if id == &UsdtLocation::get() { + Ok((UsdtAssetId::get(), *amount)) } else if id == &AstrLocation::get() { Ok((AstrAssetId::get(), *amount)) } else { @@ -494,7 +494,7 @@ impl ExtractDestinationData for DestinationDataParser { impl sygma_bridge::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type TransferReserveAccount = BridgeAccount; + type TransferReserveAccounts = BridgeAccounts; type FeeReserveAccount = TreasuryAccount; type EIP712ChainID = EIP712ChainID; type DestVerifyingContractAddress = DestVerifyingContractAddress; diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 052cabb..c7caf55 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -20,7 +20,8 @@ use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, traits::{ - AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, NumberFor, One, Verify, + AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, IdentifyAccount, + NumberFor, One, Verify, }, transaction_validity::{TransactionSource, TransactionValidity}, AccountId32, ApplyExtrinsicResult, MultiSignature, Perbill, @@ -397,21 +398,12 @@ parameter_types! { // TreasuryAccount is an substrate account and currently used for substrate -> EVM bridging fee collection // TreasuryAccount address: 5ELLU7ibt5ZrNEYRwohtaRBDBa3TzcWwwPELBPSWWd2mbgv3 pub TreasuryAccount: AccountId32 = AccountId32::new([100u8; 32]); - // BridgeAccount is an account for holding transferred asset collection - - // BridgeAccount address: 5EMepC39b7E2zfM9g6CkPp8KCAxGTh7D4w4T2tFjmjpd4tPw - // pub BridgeAccount: AccountId32 = AccountId32::new([101u8; 32]); - - // derived bridgeAccount: 5EYCAe5jLbHcAAMKvLFSXgCTbPrLgBJusvPwfKcaKzuf5X5e - // derived account 0, seed 0u32 = empty seed + // BridgeAccountNative: 5EYCAe5jLbHcAAMKvLFSXgCTbPrLgBJusvPwfKcaKzuf5X5e pub BridgeAccountNative: AccountId32 = SygmaBridgePalletId::get().into_account_truncating(); - - // derived account 1, seed 1u32 5EYCAe5jLbHcAAMKvLFiGhk3htXY8jQncbLTDGJQnpnPMAVp - pub BridgeAccountUSDC: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); - - pub UsdcAsset: MultiAsset = (Concrete(UsdcLocation::get()), 0u128).into(); - pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeAsset::get()]), (BridgeAccountUSDC::get(), vec![UsdcAsset::get()])]; - + // BridgeAccountOtherToken 5EYCAe5jLbHcAAMKvLFiGhk3htXY8jQncbLTDGJQnpnPMAVp + pub BridgeAccountOtherToken: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); + // BridgeAccounts is a list of accounts for holding transferred asset collection + pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherToken::get(), vec![UsdtLocation::get().into(), ERC20TSTLocation::get().into(), ERC20TSTD20Location::get().into()])]; // EIP712ChainID is the chainID that pallet is assigned with, used in EIP712 typed data domain pub EIP712ChainID: ChainID = U256::from(5); // DestVerifyingContractAddress is a H160 address that is used in proposal signature verification, specifically EIP712 typed data @@ -424,16 +416,14 @@ parameter_types! { PalletInstance(::index() as u8).into(); // NativeLocation is the representation of the current parachain's native asset location in substrate, it can be various on different parachains pub NativeLocation: MultiLocation = MultiLocation::here(); - // amount = 0 act as placeholder - pub NativeAsset: MultiAsset = (Concrete(MultiLocation::here()), 0u128).into(); - // UsdcLocation is the representation of the USDC asset location in substrate - // USDC is a foreign asset, and in our local testing env, it's being registered on Parachain 2004 with the following location - pub UsdcLocation: MultiLocation = MultiLocation::new( + // UsdtLocation is the representation of the USDT asset location in substrate + // USDT is a foreign asset, and in our local testing env, it's being registered on Parachain 2004 with the following location + pub UsdtLocation: MultiLocation = MultiLocation::new( 1, X3( Parachain(2004), slice_to_generalkey(b"sygma"), - slice_to_generalkey(b"usdc"), + slice_to_generalkey(b"usdt"), ), ); pub ERC20TSTLocation: MultiLocation = MultiLocation::new( @@ -452,23 +442,23 @@ parameter_types! { slice_to_generalkey(b"erc20tstd20"), ), ); - // UsdcAssetId is the substrate assetID of USDC - pub UsdcAssetId: AssetId = 2000; + // UsdtAssetId is the substrate assetID of USDT + pub UsdtAssetId: AssetId = 2000; pub ERC20TSTAssetId: AssetId = 2001; pub ERC20TSTD20AssetId: AssetId = 2002; // NativeResourceId is the resourceID that mapping with the current parachain native asset pub NativeResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000000001"); - // UsdcResourceId is the resourceID that mapping with the foreign asset USDC - pub UsdcResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000000300"); + // UsdtResourceId is the resourceID that mapping with the foreign asset USDT + pub UsdtResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000000300"); pub ERC20TSTResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000000000"); pub ERC20TSTD20ResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000000900"); // ResourcePairs is where all supported assets and their associated resourceID are binding - pub ResourcePairs: Vec<(XcmAssetId, ResourceId)> = vec![(NativeLocation::get().into(), NativeResourceId::get()), (UsdcLocation::get().into(), UsdcResourceId::get()), (ERC20TSTLocation::get().into(), ERC20TSTResourceId::get()), (ERC20TSTD20Location::get().into(), ERC20TSTD20ResourceId::get())]; + pub ResourcePairs: Vec<(XcmAssetId, ResourceId)> = vec![(NativeLocation::get().into(), NativeResourceId::get()), (UsdtLocation::get().into(), UsdtResourceId::get()), (ERC20TSTLocation::get().into(), ERC20TSTResourceId::get()), (ERC20TSTD20Location::get().into(), ERC20TSTD20ResourceId::get())]; // SygmaBridgePalletId is the palletIDl // this is used as the replacement of handler address in the ProposalExecution event pub const SygmaBridgePalletId: PalletId = PalletId(*b"sygma/01"); - pub AssetDecimalPairs: Vec<(XcmAssetId, u8)> = vec![(NativeLocation::get().into(), 12u8), (UsdcLocation::get().into(), 12u8), (ERC20TSTLocation::get().into(), 18u8), (ERC20TSTD20Location::get().into(), 20u8)]; + pub AssetDecimalPairs: Vec<(XcmAssetId, u8)> = vec![(NativeLocation::get().into(), 12u8), (UsdtLocation::get().into(), 12u8), (ERC20TSTLocation::get().into(), 18u8), (ERC20TSTD20Location::get().into(), 20u8)]; } /// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used @@ -503,8 +493,8 @@ pub struct SimpleForeignAssetConverter(PhantomData<()>); impl Convert for SimpleForeignAssetConverter { fn convert_ref(id: impl Borrow) -> result::Result { - if &UsdcLocation::get() == id.borrow() { - Ok(UsdcAssetId::get()) + if &UsdtLocation::get() == id.borrow() { + Ok(UsdtAssetId::get()) } else if &ERC20TSTLocation::get() == id.borrow() { Ok(ERC20TSTAssetId::get()) } else if &ERC20TSTD20Location::get() == id.borrow() { @@ -514,8 +504,8 @@ impl Convert for SimpleForeignAssetConverter { } } fn reverse_ref(what: impl Borrow) -> result::Result { - if *what.borrow() == UsdcAssetId::get() { - Ok(UsdcLocation::get()) + if *what.borrow() == UsdtAssetId::get() { + Ok(UsdtLocation::get()) } else if *what.borrow() == ERC20TSTAssetId::get() { Ok(ERC20TSTLocation::get()) } else if *what.borrow() == ERC20TSTD20AssetId::get() { @@ -530,8 +520,8 @@ impl MatchesFungibles for SimpleForeignAssetConverter { fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), ExecutionError> { match (&a.fun, &a.id) { (Fungible(ref amount), Concrete(ref id)) => { - if id == &UsdcLocation::get() { - Ok((UsdcAssetId::get(), *amount)) + if id == &UsdtLocation::get() { + Ok((UsdtAssetId::get(), *amount)) } else if id == &ERC20TSTLocation::get() { Ok((ERC20TSTAssetId::get(), *amount)) } else if id == &ERC20TSTD20Location::get() { From cbb05c84edbdc1958091cb1ed48480e4ed4a7be6 Mon Sep 17 00:00:00 2001 From: Freddyli7 Date: Thu, 14 Sep 2023 18:55:46 +0800 Subject: [PATCH 3/5] change USDT to reserved token and update the unit tests --- bridge/src/lib.rs | 80 +++++++++++++++++++++++++++++-- bridge/src/mock.rs | 2 +- substrate-node/runtime/src/lib.rs | 6 +-- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/bridge/src/lib.rs b/bridge/src/lib.rs index e1a5379..1465c93 100644 --- a/bridge/src/lib.rs +++ b/bridge/src/lib.rs @@ -1421,15 +1421,16 @@ pub mod pallet { )); // Check balances assert_eq!(Assets::balance(UsdtAssetId::get(), &ALICE), ENDOWED_BALANCE - amount); + // USDT in the mock runtime has been configured as the reserved token, so the corresponding account should hold the deposit balance assert_eq!( Assets::balance( UsdtAssetId::get(), AccountId::new( - SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) .unwrap() ) ), - 0 + amount - fee ); assert_eq!(Assets::balance(UsdtAssetId::get(), TreasuryAccount::get()), fee); // Check event @@ -1799,6 +1800,36 @@ pub mod pallet { ::AccountId, >>::create(UsdtAssetId::get(), ASSET_OWNER, true, 1,)); + // Mint 400 USDT to liquidity holder for test + assert_ok!(Assets::mint( + Origin::signed(ASSET_OWNER), + codec::Compact(0), + AccountId::new( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) + .unwrap() + ), + 400_000_000_000_000, + )); + // alice deposit 200 - 1 token fee native token, so the native token holder should have 199 tokens + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 199_000_000_000_000 + ); + // USDT liquidity holder should have 400 USDT at this moment + assert_eq!( + Assets::balance( + UsdtAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) + .unwrap() + ) + ), + 400_000_000_000_000 + ); + // Generate proposals // amount is in 18 decimal 0.000200000000000000, will be convert to 12 decimal // 0.000200000000 @@ -1923,6 +1954,27 @@ pub mod pallet { assert_eq!(Balances::free_balance(&BOB), ENDOWED_BALANCE + 200000000); // usdt is defined in 18 decimal so that converted amount is the same as in proposal assert_eq!(Assets::balance(UsdtAssetId::get(), &BOB), amount); + + // liquidity holder accounts balance after proposals execution + // 199 - 0.0002 native token is 198.999800000000 + assert_eq!( + Balances::free_balance(AccountId::new( + SygmaBridge::get_token_reserved_account(&NativeLocation::get().into()) + .unwrap() + )), + 199_000_000_000_000 - 200_000_000 + ); + // 400 USDT after transferring out the USDT proposal, should remain 200 USDT + assert_eq!( + Assets::balance( + UsdtAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) + .unwrap() + ) + ), + 200_000_000_000_000 + ); }) } @@ -2253,7 +2305,6 @@ pub mod pallet { Assets::balance(UsdtAssetId::get(), &ALICE), ENDOWED_BALANCE - amount_usdt_asset ); - // usdt asset should not be reserved so that BridgeAccount should not hold it assert_eq!( Assets::balance( UsdtAssetId::get(), @@ -2262,7 +2313,7 @@ pub mod pallet { .unwrap() ) ), - 0 + 122_456_789_123_456_789_123 ); // TreasuryAccount is collecting the bridging fee assert_eq!( @@ -2493,6 +2544,27 @@ pub mod pallet { ::AccountId, >>::create(UsdtAssetId::get(), ASSET_OWNER, true, 1,)); + // Mint some USDT to liquidity holder for test + assert_ok!(Assets::mint( + Origin::signed(ASSET_OWNER), + codec::Compact(0), + AccountId::new( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) + .unwrap() + ), + ENDOWED_BALANCE, + )); + assert_eq!( + Assets::balance( + UsdtAssetId::get(), + AccountId::new( + SygmaBridge::get_token_reserved_account(&UsdtLocation::get().into()) + .unwrap() + ) + ), + ENDOWED_BALANCE + ); + let p_usdt = Proposal { origin_domain_id: 1, deposit_nonce: 2, diff --git a/bridge/src/mock.rs b/bridge/src/mock.rs index d9d4fc5..58095db 100644 --- a/bridge/src/mock.rs +++ b/bridge/src/mock.rs @@ -223,7 +223,7 @@ parameter_types! { pub UsdtLocation: MultiLocation = MultiLocation::new( 1, X3( - Parachain(2004), + Parachain(2005), slice_to_generalkey(b"sygma"), slice_to_generalkey(b"usdt"), ), diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index c7caf55..6eb6b86 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -421,7 +421,7 @@ parameter_types! { pub UsdtLocation: MultiLocation = MultiLocation::new( 1, X3( - Parachain(2004), + Parachain(2005), slice_to_generalkey(b"sygma"), slice_to_generalkey(b"usdt"), ), @@ -429,7 +429,7 @@ parameter_types! { pub ERC20TSTLocation: MultiLocation = MultiLocation::new( 1, X3( - Parachain(2004), + Parachain(2005), slice_to_generalkey(b"sygma"), slice_to_generalkey(b"erc20tst"), ), @@ -437,7 +437,7 @@ parameter_types! { pub ERC20TSTD20Location: MultiLocation = MultiLocation::new( 1, X3( - Parachain(2004), + Parachain(2005), slice_to_generalkey(b"sygma"), slice_to_generalkey(b"erc20tstd20"), ), From 0b906818b385f9090b89f8bd4bd432d307c7576d Mon Sep 17 00:00:00 2001 From: Freddyli7 Date: Mon, 18 Sep 2023 10:11:05 +0800 Subject: [PATCH 4/5] remove unused code --- bridge/src/lib.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/bridge/src/lib.rs b/bridge/src/lib.rs index 1465c93..6891a8e 100644 --- a/bridge/src/lib.rs +++ b/bridge/src/lib.rs @@ -484,17 +484,6 @@ pub mod pallet { // Deposit `bridge_amount` of asset to reserve account if asset is reserved in local // chain. if T::IsReserve::contains(&asset, &MultiLocation::here()) { - // T::AssetTransactor::deposit_asset( - // &(asset.id, Fungible(bridge_amount)).into(), - // &Junction::AccountId32 { - // network: None, - // id: T::TransferReserveAccount::get().into(), - // } - // .into(), - // // Put empty message hash here because we are not sending XCM message - // &XcmContext::with_message_hash([0; 32]), - // ) - T::AssetTransactor::deposit_asset( &(asset.id, Fungible(bridge_amount)).into(), &Junction::AccountId32 { network: None, id: token_reserved_account }.into(), From 44441e6eed8d84d2764916c0e3242b824f2bc1b5 Mon Sep 17 00:00:00 2001 From: Freddyli7 Date: Wed, 11 Oct 2023 14:36:32 -0400 Subject: [PATCH 5/5] address the BTreeMap comment --- bridge/src/lib.rs | 14 +++++--------- bridge/src/mock.rs | 11 ++++++++++- substrate-node/runtime/src/lib.rs | 12 +++++++++++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/bridge/src/lib.rs b/bridge/src/lib.rs index 6891a8e..78eadb4 100644 --- a/bridge/src/lib.rs +++ b/bridge/src/lib.rs @@ -47,6 +47,7 @@ pub mod pallet { use xcm_executor::traits::TransactAsset; use crate::eip712; + use sp_std::collections::btree_map::BTreeMap; use sygma_traits::{ ChainID, DecimalConverter, DepositNonce, DomainID, ExtractDestinationData, FeeHandler, MpcAddress, ResourceId, TransferType, VerifyingContractAddress, @@ -87,7 +88,7 @@ pub mod pallet { /// Bridge transfer reserve accounts mapping with designated assets #[pallet::constant] - type TransferReserveAccounts: Get)>>; + type TransferReserveAccounts: Get>; /// EIP712 Verifying contract address /// This is used in EIP712 typed data domain @@ -708,14 +709,9 @@ pub mod pallet { /// Return the TokenReservedAccount address by the given token pub fn get_token_reserved_account(token_id: &AssetId) -> Option<[u8; 32]> { - let idx = T::TransferReserveAccounts::get().iter().position(|a| { - let i: Option = a.1.iter().position(|b| b == token_id); - if i.is_some() { - return true; - } - false - })?; - Some(T::TransferReserveAccounts::get()[idx].0.clone().into()) + T::TransferReserveAccounts::get() + .get(token_id) + .map(|account| (*account).clone().into()) } /// convert the ECDSA 64-byte uncompressed pubkey to H160 address diff --git a/bridge/src/mock.rs b/bridge/src/mock.rs index 58095db..9163a53 100644 --- a/bridge/src/mock.rs +++ b/bridge/src/mock.rs @@ -18,6 +18,7 @@ use sp_runtime::{ traits::{AccountIdConversion, BlakeTwo256, IdentityLookup}, AccountId32, Perbill, }; +use sp_std::collections::btree_map::BTreeMap; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result}; use sygma_traits::{ ChainID, DecimalConverter, DomainID, ExtractDestinationData, ResourceId, @@ -207,13 +208,21 @@ impl sygma_percentage_feehandler::Config for Runtime { type WeightInfo = sygma_percentage_feehandler::weights::SygmaWeightInfo; } +fn bridge_accounts_generator() -> BTreeMap { + let mut account_map: BTreeMap = BTreeMap::new(); + account_map.insert(NativeLocation::get().into(), BridgeAccountNative::get()); + account_map.insert(UsdtLocation::get().into(), BridgeAccountOtherTokens::get()); + account_map.insert(AstrLocation::get().into(), BridgeAccountOtherTokens::get()); + account_map +} + parameter_types! { pub TreasuryAccount: AccountId32 = AccountId32::new([100u8; 32]); pub EIP712ChainID: ChainID = primitive_types::U256([1u64; 4]); pub DestVerifyingContractAddress: VerifyingContractAddress = primitive_types::H160([1u8; 20]); pub BridgeAccountNative: AccountId32 = SygmaBridgePalletId::get().into_account_truncating(); pub BridgeAccountOtherTokens: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); - pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherTokens::get(), vec![UsdtLocation::get().into(), AstrLocation::get().into()])]; + pub BridgeAccounts: BTreeMap = bridge_accounts_generator(); pub CheckingAccount: AccountId32 = AccountId32::new([102u8; 32]); pub RelayNetwork: NetworkId = NetworkId::Polkadot; pub AssetsPalletLocation: MultiLocation = diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 6eb6b86..44b60e7 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -26,6 +26,7 @@ use sp_runtime::{ transaction_validity::{TransactionSource, TransactionValidity}, AccountId32, ApplyExtrinsicResult, MultiSignature, Perbill, }; +use sp_std::collections::btree_map::BTreeMap; use sp_std::{borrow::Borrow, marker::PhantomData, prelude::*, result, vec::Vec}; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -394,6 +395,15 @@ impl sygma_fee_handler_router::Config for Runtime { // This address is defined in the substrate E2E test of sygma-relayer const DEST_VERIFYING_CONTRACT_ADDRESS: &str = "6CdE2Cd82a4F8B74693Ff5e194c19CA08c2d1c68"; +fn bridge_accounts_generator() -> BTreeMap { + let mut account_map: BTreeMap = BTreeMap::new(); + account_map.insert(NativeLocation::get().into(), BridgeAccountNative::get()); + account_map.insert(UsdtLocation::get().into(), BridgeAccountOtherToken::get()); + account_map.insert(ERC20TSTLocation::get().into(), BridgeAccountOtherToken::get()); + account_map.insert(ERC20TSTD20Location::get().into(), BridgeAccountOtherToken::get()); + account_map +} + parameter_types! { // TreasuryAccount is an substrate account and currently used for substrate -> EVM bridging fee collection // TreasuryAccount address: 5ELLU7ibt5ZrNEYRwohtaRBDBa3TzcWwwPELBPSWWd2mbgv3 @@ -403,7 +413,7 @@ parameter_types! { // BridgeAccountOtherToken 5EYCAe5jLbHcAAMKvLFiGhk3htXY8jQncbLTDGJQnpnPMAVp pub BridgeAccountOtherToken: AccountId32 = SygmaBridgePalletId::get().into_sub_account_truncating(1u32); // BridgeAccounts is a list of accounts for holding transferred asset collection - pub BridgeAccounts: Vec<(AccountId32, Vec)> = vec![(BridgeAccountNative::get(), vec![NativeLocation::get().into()]), (BridgeAccountOtherToken::get(), vec![UsdtLocation::get().into(), ERC20TSTLocation::get().into(), ERC20TSTD20Location::get().into()])]; + pub BridgeAccounts: BTreeMap = bridge_accounts_generator(); // EIP712ChainID is the chainID that pallet is assigned with, used in EIP712 typed data domain pub EIP712ChainID: ChainID = U256::from(5); // DestVerifyingContractAddress is a H160 address that is used in proposal signature verification, specifically EIP712 typed data