From 01676a8ab28b1768da9bbc827689729e5831ea5a Mon Sep 17 00:00:00 2001 From: Bucur David Date: Tue, 11 Jun 2024 11:36:03 +0300 Subject: [PATCH 1/4] fix: audit fix issues: 1,4,5 and suggestion(2) --- Cargo.lock | 21 --- Cargo.toml | 9 - src/admin.rs | 62 ++++--- src/config.rs | 8 +- src/events.rs | 24 +-- src/lib.rs | 32 +--- src/proxies/mod.rs | 1 - src/proxies/wegld_proxy.rs | 129 -------------- src/storage.rs | 6 +- src/utils.rs | 6 +- .../multiversx-wegld-swap-sc.mxsc.json | 167 ------------------ tests/bridge_sc/bridge_sc.rs | 82 ++------- tests/endpoints/admin.rs | 15 +- tests/endpoints/public.rs | 26 +-- tests/unit_tests.rs | 17 +- wasm/Cargo.lock | 10 -- wasm/src/lib.rs | 10 +- 17 files changed, 98 insertions(+), 527 deletions(-) delete mode 100644 src/proxies/mod.rs delete mode 100644 src/proxies/wegld_proxy.rs delete mode 100644 tests-contracts/multiversx-wegld-swap-sc.mxsc.json diff --git a/Cargo.lock b/Cargo.lock index 4ad994d..f02aa4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,9 +346,7 @@ name = "core-mx-bridge-sc" version = "0.1.0" dependencies = [ "multiversx-sc", - "multiversx-sc-modules", "multiversx-sc-scenario", - "multiversx-wegld-swap-sc", ] [[package]] @@ -1100,15 +1098,6 @@ dependencies = [ "zip", ] -[[package]] -name = "multiversx-sc-modules" -version = "0.50.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9a8c10ddadc7adfc04edbdd1ea80a9ae9a7e11202fc4cfb2e08c8e27f17aa5" -dependencies = [ - "multiversx-sc", -] - [[package]] name = "multiversx-sc-scenario" version = "0.50.3" @@ -1164,16 +1153,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "multiversx-wegld-swap-sc" -version = "0.50.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72d3aa3ee209e07f3562407530a636080437f348811961ab3127cf646a1e32ce" -dependencies = [ - "multiversx-sc", - "multiversx-sc-modules", -] - [[package]] name = "native-tls" version = "0.2.11" diff --git a/Cargo.toml b/Cargo.toml index c377e1c..83da49d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,18 +11,9 @@ path = "src/lib.rs" [dependencies.multiversx-sc] version = "0.50.3" -[dependencies.multiversx-sc-modules] -version = "0.50.3" - -[dev-dependencies] -multiversx-wegld-swap-sc = "0.50.3" - - [dev-dependencies.multiversx-sc-scenario] version = "0.50.3" - - [workspace] members = [ ".", diff --git a/src/admin.rs b/src/admin.rs index cd5a9a5..c5976b4 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -100,7 +100,7 @@ pub trait AdminModule: ) { only_privileged!(self, ERR_NOT_PRIVILEGED); require!( - self.tokens_whitelist().contains(&token_identifier), + self.token_whitelist().get() == token_identifier, ERR_TOKEN_NOT_WHITELISTED ); require!(minimum <= maximum, ERR_WRONG_VALUES); @@ -119,44 +119,46 @@ pub trait AdminModule: #[endpoint(setFeeValue)] fn set_fee_value(&self, fee_value: BigUint) { only_privileged!(self, ERR_NOT_PRIVILEGED); + self.set_fee_value_event(&fee_value); self.fee_value().set(fee_value); } - #[endpoint(setWegldContractAddress)] - fn set_wegld_contract_address(&self, wegld_contract_address: ManagedAddress) { + #[endpoint(setWegldTokenIdentifier)] + fn set_wegld_contract_address(&self, wegld_token_identifier: TokenIdentifier) { only_privileged!(self, ERR_NOT_PRIVILEGED); - self.set_wegld_contract_address_event(&wegld_contract_address); - self.wegld_contract_address().set(wegld_contract_address); + self.set_wegld_token_identifier(&wegld_token_identifier); + self.wegld_token_identifier().set(wegld_token_identifier); } - #[endpoint(addTokensToWhitelist)] - fn add_tokens_to_whitelist( - &self, - tokens: MultiValueEncoded>, - ) { + #[endpoint(addTokenToWhitelist)] + fn add_tokens_to_whitelist(&self, token: TokenIdentifier, decimals: u32) { only_privileged!(self, ERR_NOT_PRIVILEGED); - for token in tokens.into_iter() { - let (token_identifier, token_decimals) = token.into_tuple(); - self.token_decimals(&token_identifier).set(token_decimals); - require!( - self.tokens_whitelist().insert(token_identifier), - ERR_TOKEN_ALREADY_IN_WHITELIST - ); + self.add_token_to_whitelist_event(&token, &decimals); + if !self.token_whitelist().is_empty() { + self.token_decimals(&token).set(decimals); + self.token_whitelist().update(|whitelist_token| { + require!(*whitelist_token != token, ERR_TOKEN_ALREADY_IN_WHITELIST); + *whitelist_token = token + }); + } else { + self.token_decimals(&token).set(decimals); + self.token_whitelist().set(token); } } - #[endpoint(removeTokensFromWhitelist)] - fn remove_tokens_from_whitelist(&self, tokens: MultiValueEncoded) { + #[endpoint(removeTokenFromWhitelist)] + fn remove_token_from_whitelist(&self, token: TokenIdentifier) { only_privileged!(self, ERR_NOT_PRIVILEGED); - self.remove_tokens_from_whitelist_event(&tokens.to_vec()); - for token in tokens.into_iter() { - self.token_decimals(&token).clear(); - require!( - self.tokens_whitelist().swap_remove(&token), - ERR_TOKEN_NOT_WHITELISTED - ); - } + + self.remove_token_from_whitelist_event(&token); + require!( + self.token_whitelist().get() == token, + ERR_TOKEN_NOT_WHITELISTED + ); + + self.token_decimals(&token).clear(); + self.token_whitelist().clear(); } #[endpoint(addToWhitelist)] @@ -164,10 +166,11 @@ pub trait AdminModule: only_privileged!(self, ERR_NOT_PRIVILEGED); for address in addresses.into_iter() { require!( - self.whitelist().contains(&address) == false, + !self.whitelist().contains(&address), ERR_ADDRESS_ALREADY_WHITELISTED ); self.whitelist().add(&address); + self.add_to_whitelist_event(&address); } } @@ -180,6 +183,7 @@ pub trait AdminModule: ERR_ADDRESS_NOT_WHITELISTED ); self.whitelist().remove(&address); + self.remove_from_whitelist_event(&address); } } @@ -198,7 +202,7 @@ pub trait AdminModule: let payment = self.call_value().single_esdt(); require!( - self.tokens_whitelist().contains(&payment.token_identifier), + self.token_whitelist().get() == payment.token_identifier, ERR_TOKEN_NOT_WHITELISTED ); diff --git a/src/config.rs b/src/config.rs index f117405..473f639 100644 --- a/src/config.rs +++ b/src/config.rs @@ -84,7 +84,7 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule { is_ready = false; } - if self.wegld_contract_address().is_empty() { + if self.wegld_token_identifier().is_empty() { is_ready = false; } } @@ -92,7 +92,7 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule { if self.relayer().is_empty() { is_ready = false; } - if self.tokens_whitelist().is_empty() { + if self.token_whitelist().is_empty() { is_ready = false; } is_ready @@ -103,8 +103,8 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule { fn administrator(&self) -> SingleValueMapper; #[view(getTokensWhitelist)] - #[storage_mapper("tokens_whitelist")] - fn tokens_whitelist(&self) -> UnorderedSetMapper; + #[storage_mapper("token_whitelist")] + fn token_whitelist(&self) -> SingleValueMapper; #[storage_mapper("whitelist")] fn whitelist(&self) -> WhitelistMapper; diff --git a/src/events.rs b/src/events.rs index b27a3df..31ce044 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,5 +1,3 @@ -use multiversx_sc::types::ManagedVec; - use crate::config::State; #[multiversx_sc::module] @@ -14,6 +12,12 @@ pub trait EventsModule { #[indexed] destination_signature: &ManagedBuffer, ); + #[event("addToWhitelist")] + fn add_to_whitelist_event(&self, #[indexed] address: &ManagedAddress); + + #[event("removeFromWhitelist")] + fn remove_from_whitelist_event(&self, #[indexed] address: &ManagedAddress); + #[event("sendFromLiquidityEvent")] fn send_from_liquidity_event( &self, @@ -29,12 +33,6 @@ pub trait EventsModule { #[event("setAdministratorEvent")] fn set_administrator_event(&self, #[indexed] administrator: &ManagedAddress); - #[event("addTokensToWhitelistEvent")] - fn add_tokens_to_whitelist_event(&self, #[indexed] tokens: &ManagedVec); - - #[event("removeTokensFromWhitelistEvent")] - fn remove_tokens_from_whitelist_event(&self, #[indexed] tokens: &ManagedVec); - #[event("setRelayerEvent")] fn set_relayer_event(&self, #[indexed] relayer: &ManagedAddress); @@ -65,14 +63,20 @@ pub trait EventsModule { #[indexed] maximum: &BigUint, ); + #[event("addTokenToWhitelistEvent")] + fn add_token_to_whitelist_event(&self, #[indexed] token: &TokenIdentifier, decimals: &u32); + + #[event("removeTokenFromWhitelistEvent")] + fn remove_token_from_whitelist_event(&self, #[indexed] token: &TokenIdentifier); + #[event("setFeeCollectorEvent")] fn set_fee_collector_event(&self, #[indexed] fee_collector: &ManagedAddress); #[event("setFeeValueEvent")] fn set_fee_value_event(&self, #[indexed] fee_value: &BigUint); - #[event("setWegldContractAddressEvent")] - fn set_wegld_contract_address_event(&self, #[indexed] wegld_contract_address: &ManagedAddress); + #[event("setWegldTokenIdentifier")] + fn set_wegld_token_identifier(&self, #[indexed] wegld_token_identifier: &TokenIdentifier); #[event("setRelayerStateEvent")] fn set_relayer_state_event(&self, #[indexed] state: &State); diff --git a/src/lib.rs b/src/lib.rs index 9b3e7e2..5981790 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,6 @@ use crate::errors::{ ERR_TOKEN_NOT_WHITELISTED, ERR_WRONG_FEE_TOKEN_IDENTIFIER, ERR_WRONG_VALUES, }; -use crate::proxies::wegld_proxy; - multiversx_sc::imports!(); pub mod admin; @@ -15,7 +13,6 @@ pub mod config; pub mod errors; pub mod events; pub mod macros; -pub mod proxies; pub mod storage; pub mod utils; #[multiversx_sc::contract] @@ -56,7 +53,7 @@ pub trait CoreMxBridgeSc: let deposit = self.call_value().single_esdt(); require!( - self.tokens_whitelist().contains(&deposit.token_identifier), + self.token_whitelist().get() == deposit.token_identifier, ERR_TOKEN_NOT_WHITELISTED ); @@ -89,21 +86,13 @@ pub trait CoreMxBridgeSc: require!(fee_value == fee.amount, ERR_WRONG_VALUES); - let wegld_token_identifier = self - .tx() - .to(&self.wegld_contract_address().get()) - .typed(wegld_proxy::EgldEsdtSwapProxy) - .wrapped_egld_token_id() - .returns(ReturnsResult) - .sync_call(); - require!( - fee.token_identifier == wegld_token_identifier, + fee.token_identifier == self.wegld_token_identifier().get(), ERR_WRONG_FEE_TOKEN_IDENTIFIER ); require!( - self.tokens_whitelist().contains(&deposit.token_identifier), + self.token_whitelist().get() == deposit.token_identifier, ERR_TOKEN_NOT_WHITELISTED ); @@ -121,18 +110,11 @@ pub trait CoreMxBridgeSc: ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE ); - let back_transfers = self - .tx() - .to(&self.wegld_contract_address().get()) - .typed(wegld_proxy::EgldEsdtSwapProxy) - .unwrap_egld() - .with_esdt_transfer(fee) - .returns(ReturnsBackTransfers) - .sync_call(); - - self.send().direct_egld( + self.send().direct_esdt( &self.fee_collector().get(), - &back_transfers.total_egld_amount, + &fee.token_identifier, + 0u64, + &fee.amount, ); self.send_to_liquidity_event( diff --git a/src/proxies/mod.rs b/src/proxies/mod.rs deleted file mode 100644 index 74f14be..0000000 --- a/src/proxies/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod wegld_proxy; diff --git a/src/proxies/wegld_proxy.rs b/src/proxies/wegld_proxy.rs deleted file mode 100644 index 404831a..0000000 --- a/src/proxies/wegld_proxy.rs +++ /dev/null @@ -1,129 +0,0 @@ -// Code generated by the multiversx-sc proxy generator. DO NOT EDIT. - -//////////////////////////////////////////////////// -////////////////// AUTO-GENERATED ////////////////// -//////////////////////////////////////////////////// - -#![allow(dead_code)] -#![allow(clippy::all)] - -use multiversx_sc::proxy_imports::*; - -pub struct EgldEsdtSwapProxy; - -impl TxProxyTrait for EgldEsdtSwapProxy -where - Env: TxEnv, - From: TxFrom, - To: TxTo, - Gas: TxGas, -{ - type TxProxyMethods = EgldEsdtSwapProxyMethods; - - fn proxy_methods(self, tx: Tx) -> Self::TxProxyMethods { - EgldEsdtSwapProxyMethods { wrapped_tx: tx } - } -} - -pub struct EgldEsdtSwapProxyMethods -where - Env: TxEnv, - From: TxFrom, - To: TxTo, - Gas: TxGas, -{ - wrapped_tx: Tx, -} - -#[rustfmt::skip] -impl EgldEsdtSwapProxyMethods -where - Env: TxEnv, - Env::Api: VMApi, - From: TxFrom, - Gas: TxGas, -{ - pub fn init< - Arg0: ProxyArg>, - >( - self, - wrapped_egld_token_id: Arg0, - ) -> TxTypedDeploy { - self.wrapped_tx - .payment(NotPayable) - .raw_deploy() - .argument(&wrapped_egld_token_id) - .original_result() - } -} - -#[rustfmt::skip] -impl EgldEsdtSwapProxyMethods -where - Env: TxEnv, - Env::Api: VMApi, - From: TxFrom, - To: TxTo, - Gas: TxGas, -{ - pub fn wrap_egld( - self, - ) -> TxTypedCall> { - self.wrapped_tx - .raw_call("wrapEgld") - .original_result() - } - - pub fn unwrap_egld( - self, - ) -> TxTypedCall { - self.wrapped_tx - .raw_call("unwrapEgld") - .original_result() - } - - pub fn get_locked_egld_balance( - self, - ) -> TxTypedCall> { - self.wrapped_tx - .payment(NotPayable) - .raw_call("getLockedEgldBalance") - .original_result() - } - - pub fn wrapped_egld_token_id( - self, - ) -> TxTypedCall> { - self.wrapped_tx - .payment(NotPayable) - .raw_call("getWrappedEgldTokenId") - .original_result() - } - - pub fn pause_endpoint( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("pause") - .original_result() - } - - pub fn unpause_endpoint( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("unpause") - .original_result() - } - - pub fn paused_status( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("isPaused") - .original_result() - } -} diff --git a/src/storage.rs b/src/storage.rs index 85e5b8b..aebf399 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -27,7 +27,7 @@ pub trait StorageModule { #[storage_mapper("fee_value")] fn fee_value(&self) -> SingleValueMapper; - #[view(getWegldContractAddress)] - #[storage_mapper("wegld_contract_address")] - fn wegld_contract_address(&self) -> SingleValueMapper; + #[view(getWegldTokenIdentifier)] + #[storage_mapper("wegld_token_identifier")] + fn wegld_token_identifier(&self) -> SingleValueMapper; } diff --git a/src/utils.rs b/src/utils.rs index 6a78afc..ba0cf4d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,10 +5,6 @@ pub trait UtilsModule { fn check_amount(&self, amount: &BigUint, decimals: u32) -> bool { let token_decimals = BigUint::from(10u32).pow(decimals); - if amount % &token_decimals == 0 { - return true; - } else { - return false; - } + amount % &token_decimals == 0 } } diff --git a/tests-contracts/multiversx-wegld-swap-sc.mxsc.json b/tests-contracts/multiversx-wegld-swap-sc.mxsc.json deleted file mode 100644 index 8de68d9..0000000 --- a/tests-contracts/multiversx-wegld-swap-sc.mxsc.json +++ /dev/null @@ -1,167 +0,0 @@ -{ - "buildInfo": { - "rustc": { - "version": "1.78.0", - "commitHash": "9b00956e56009bab2aa15d7bff10916599e3d6d6", - "commitDate": "2024-04-29", - "channel": "Stable", - "short": "rustc 1.78.0 (9b00956e5 2024-04-29)" - }, - "contractCrate": { - "name": "multiversx-wegld-swap-sc", - "version": "0.50.3" - }, - "framework": { - "name": "multiversx-sc", - "version": "0.50.3" - } - }, - "abi": { - "name": "EgldEsdtSwap", - "constructor": { - "inputs": [ - { - "name": "wrapped_egld_token_id", - "type": "TokenIdentifier" - } - ], - "outputs": [] - }, - "endpoints": [ - { - "name": "wrapEgld", - "mutability": "mutable", - "payableInTokens": [ - "EGLD" - ], - "inputs": [], - "outputs": [ - { - "type": "EsdtTokenPayment" - } - ] - }, - { - "name": "unwrapEgld", - "mutability": "mutable", - "payableInTokens": [ - "*" - ], - "inputs": [], - "outputs": [] - }, - { - "name": "getLockedEgldBalance", - "mutability": "readonly", - "inputs": [], - "outputs": [ - { - "type": "BigUint" - } - ] - }, - { - "name": "getWrappedEgldTokenId", - "mutability": "readonly", - "inputs": [], - "outputs": [ - { - "type": "TokenIdentifier" - } - ] - }, - { - "name": "pause", - "onlyOwner": true, - "mutability": "mutable", - "inputs": [], - "outputs": [] - }, - { - "name": "unpause", - "onlyOwner": true, - "mutability": "mutable", - "inputs": [], - "outputs": [] - }, - { - "name": "isPaused", - "mutability": "readonly", - "inputs": [], - "outputs": [ - { - "type": "bool" - } - ] - } - ], - "esdtAttributes": [], - "hasCallback": false, - "types": { - "EsdtTokenPayment": { - "type": "struct", - "fields": [ - { - "name": "token_identifier", - "type": "TokenIdentifier" - }, - { - "name": "token_nonce", - "type": "u64" - }, - { - "name": "amount", - "type": "BigUint" - } - ] - } - } - }, - "size": 3528, - "code": "0061736d0100000001580f60000060027f7f017f60017f006000017f60017f017f60027f7f0060037f7f7f017f60057f7f7e7f7f017f60047f7f7f7f0060027f7e0060037f7f7f006000017e60067e7f7f7f7f7f017f60047f7f7f7f017f60017e0002cc062303656e760e626967496e74536574496e743634000903656e7609626967496e74416464000a03656e760b7369676e616c4572726f72000503656e760a6d4275666665724e6577000303656e760d6d427566666572417070656e64000103656e76096d4275666665724571000103656e760d6d616e6167656443616c6c6572000203656e76136d616e616765644f776e657241646472657373000203656e76126d427566666572476574417267756d656e74000103656e760f6765744e756d417267756d656e7473000303656e760a6765744761734c656674000b03656e76106d616e61676564534341646472657373000203656e761b6d616e61676564457865637574654f6e44657374436f6e74657874000c03656e760f636c65616e52657475726e44617461000003656e760f6d4275666665725365744279746573000603656e76196d42756666657246726f6d426967496e74556e7369676e6564000103656e76126d427566666572417070656e644279746573000603656e76126d42756666657253746f726167654c6f6164000103656e76106d4275666665724765744c656e677468000403656e76136d42756666657247657442797465536c696365000d03656e76126d616e616765645369676e616c4572726f72000203656e76136d42756666657253746f7261676553746f7265000103656e7618626967496e7447657445787465726e616c42616c616e6365000503656e760a626967496e745369676e000403656e760e636865636b4e6f5061796d656e74000003656e76136765744e756d455344545472616e7366657273000303656e7612626967496e7447657443616c6c56616c7565000203656e76226d616e616765644d756c74695472616e73666572455344544e465445786563757465000703656e760d6d42756666657246696e697368000403656e761c6d616e616765644765744d756c74694553445443616c6c56616c7565000203656e7609626967496e74436d70000103656e761b6d616e616765645472616e7366657256616c756545786563757465000703656e7614626967496e7446696e697368556e7369676e6564000203656e7614736d616c6c496e7446696e6973685369676e6564000e03656e760f6d4275666665724765744279746573000103252404030401050300020803040508050105040405050203000304020300000000000000000005030100030616037f01418080080b7f0041cd83080b7f0041d083080b079b010c066d656d6f7279020004696e6974003e087772617045676c64003f0a756e7772617045676c640040146765744c6f636b656445676c6442616c616e63650041156765745772617070656445676c64546f6b656e49640042057061757365004307756e7061757365004408697350617573656400450863616c6c4261636b00460a5f5f646174615f656e6403010b5f5f686561705f6261736503020af90e241601017f1024220142001000200120012000100120010b1901017f41a4830841a4830828020041016b220036020020000b0f01017f10032201200010041a20010b0b0020002001100541004a0b0900200020011002000b0c01017f10242200100620000b1d01017f10242200100720001028102604400f0b41f0820841241002000b1400100920004604400f0b41c7800841191002000b1b00102c1a20022003102d102e20002002360204200020013602000b1301017f1024220041d482084100100e1a20000b0f01017f102422012000100f1a20010b4601017f230041106b220224002002200141187420014180fe03714108747220014108764180fe03712001411876727236020c20002002410c6a410410101a200241106a24000b1b00102c1a200220031025102e20002002360204200020013602000b18002001410d103121012000102c360204200020013602000b1101017f1024220220002001100e1a20020b1f01017e100a4162100b41764200100041624176200020011024100c1a100d0b0d0020001024220010111a20000bc70102017e037f230041106b2202240020024200370308200010332204101222004109490440200441002000200220006b41106a10131a024002402002290308220142388620014280fe0383422886842001428080fc0783421886200142808080f80f834208868484200142088842808080f80f832001421888428080fc07838420014228884280fe03832001423888848422018442015804402001a741016b0d020c010b419e810841121035000b410121030b200241106a240020030f0b41fa8008410e1035000b1a01017f418881084116103122022000200110101a20021014000b5101027f230041106b22022400200220001012220341187420034180fe03714108747220034108764180fe03712003411876727236020c20012002410c6a410410101a2001200010041a200241106a24000b5701027f230041106b2201240010382001410036000b20014100360208200120003a000f4107410820001b2200200141086a22022000410771722c00004107756a220020026a410820006b103110151a200141106a24000b0a0041be8208411310310b120010381034044041b0810841121027000b0b2101027f10242201100b10242100200141ad830810221a41ad83082000101620000b09002000101741004a0b1601017f102c21012000102c360204200020013602000b0a0041ac8208411210310b1a01017f10184101102a41001024220010081a103d200010151a0bf00201087f230041306b22002400024010194504404100102a103941752101024041a883082d000022020440417541ffffffff0720021b21010c010b41a8830841013a00004175101a0b2001103b450d01103d10332104200041186a41ed80081030200041106a2000280218200028021c2004102f200041086a200028021020002802142001102b2000280208200028020c1032416210062000103c2000280204210620002802002107102c2105200410252102200110232103200042003702242000200241187420024180fe03714108747220024108764180fe0371200241187672723602202000200341187420034180fe03714108747220034108764180fe03712003411876727236022c2005200041206a2202411010101a41622005420020072006101b1a20011023200441d4820841001031220110362000420037032020012002410810101a102d200110362001101c1a200041306a24000f0b41a2800841251002000b41918208411b1027000b8d0402097f017e230041306b220024004100102a1039416b2102024041ac83082d000022010440416b41ffffffff0720011b21020c010b41ac830841013a0000416b101d0b024020021012417071411046044041002101200210122105200041286a2106410121040340200141106a220720054b0d022006420037030020004200370320200220014110200041206a10131a200404402000290224220942388620094280fe0383422886842009428080fc0783421886200942808080f80f834208868484200942088842808080f80f832009421888428080fc07838420094228884280fe0383200942388884848421092000280220220141187420014180fe03714108747220014108764180fe0371200141187672722108200028022c220141187420014180fe03714108747220014108764180fe037120014118767272210341002104200721010c010b0b41948308410e1002000b4180800841221002000b02400240024020095004402008103d103322011026450d012003103b450d022003103a101e41004a0d03200041186a41e080081030200041106a2000280218200028021c2001102f200041086a200028021020002802142003102b2000280208200028020c103210282000103c2003420020002802002000280204101f1a200041306a24000f0b41d48208411c1002000b41c2810841101027000b41d28108411c1027000b41ee810841231027000b0c0010184100102a103a10200b0f0010184100102a103d1033101c1a0b0e00101810294100102a410110370b0e00101810294100102a410010370b0f0010184100102a10381034ad10210b0300010b0bb6030200418080080ba203696e636f7272656374206e756d626572206f662045534454207472616e736665727366756e6374696f6e20646f6573206e6f74206163636570742045534454207061796d656e7477726f6e67206e756d626572206f6620617267756d656e7473455344544c6f63616c4275726e455344544c6f63616c4d696e74696e70757420746f6f206c6f6e6773746f72616765206465636f6465206572726f723a20696e707574206f7574206f662072616e6765436f6e74726163742069732070617573656457726f6e67206573647420746f6b656e4d75737420706179206d6f7265207468616e203020746f6b656e7321436f6e747261637420646f6573206e6f74206861766520656e6f7567682066756e64735061796d656e74206d757374206265206d6f7265207468616e20307772617070656445676c64546f6b656e496470617573655f6d6f64756c653a70617573656400000066756e6769626c65204553445420746f6b656e206578706563746564456e64706f696e742063616e206f6e6c792062652063616c6c6564206279206f776e657270616e6963206f636375727265640041a483080b049cffffff", - "report": { - "imports": [ - "bigIntAdd", - "bigIntCmp", - "bigIntFinishUnsigned", - "bigIntGetCallValue", - "bigIntGetExternalBalance", - "bigIntSetInt64", - "bigIntSign", - "checkNoPayment", - "cleanReturnData", - "getGasLeft", - "getNumArguments", - "getNumESDTTransfers", - "mBufferAppend", - "mBufferAppendBytes", - "mBufferEq", - "mBufferFinish", - "mBufferFromBigIntUnsigned", - "mBufferGetArgument", - "mBufferGetByteSlice", - "mBufferGetBytes", - "mBufferGetLength", - "mBufferNew", - "mBufferSetBytes", - "mBufferStorageLoad", - "mBufferStorageStore", - "managedCaller", - "managedExecuteOnDestContext", - "managedGetMultiESDTCallValue", - "managedMultiTransferESDTNFTExecute", - "managedOwnerAddress", - "managedSCAddress", - "managedSignalError", - "managedTransferValueExecute", - "signalError", - "smallIntFinishSigned" - ], - "memoryAllocationError": false, - "isMemGrow": false, - "eiCheck": { - "eiVersion": "1.3", - "ok": true - } - } -} diff --git a/tests/bridge_sc/bridge_sc.rs b/tests/bridge_sc/bridge_sc.rs index 53af9e3..b1ab1ce 100644 --- a/tests/bridge_sc/bridge_sc.rs +++ b/tests/bridge_sc/bridge_sc.rs @@ -11,15 +11,8 @@ use multiversx_sc_scenario::scenario_model::BigUintValue; pub const BRIDGE_CONTRACT_PATH: &str = "mxsc:output/core-mx-bridge-sc-mxsc.json"; -pub const WEGLD_SWAP_CONTRACT_PATH: &str = - "mxsc:tests-contracts/multiversx-wegld-swap-sc.mxsc.json"; - pub const BRIDGE_CONTRACT_ADDRESS_EXPR: &str = "sc:bridge-sc"; -pub const WEGLD_SWAP_CONTRACT_ADDRESS_EXPR: &str = "sc:wegld-swap-sc"; - -pub const OWNER_WEGLD_SWAP_CONTRACT_ADDRESS_EXPR: &str = "address:owner-wegld-swap-sc"; - pub const OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR: &str = "address:owner-bridge-sc"; pub const ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR: &str = "address:admin-bridge-sc"; @@ -41,7 +34,6 @@ pub const SECOND_USER_ADDRESS_EXPR: &str = "address:second_user"; pub const THIRD_USER_ADDRESS_EXPR: &str = "address:third_user"; type Contract = ContractInfo>; -type WegldSwapContract = ContractInfo>; pub fn world() -> ScenarioWorld { let mut blockchain = ScenarioWorld::new(); @@ -50,18 +42,12 @@ pub fn world() -> ScenarioWorld { blockchain.register_contract(BRIDGE_CONTRACT_PATH, core_mx_bridge_sc::ContractBuilder); - blockchain.register_contract( - WEGLD_SWAP_CONTRACT_PATH, - multiversx_wegld_swap_sc::ContractBuilder, - ); - blockchain } pub struct ContractState { pub world: ScenarioWorld, pub contract: Contract, - pub wegld_swap_contract: WegldSwapContract, pub contract_owner: Address, pub admin: Address, pub relayer: Address, @@ -88,15 +74,6 @@ impl ContractState { 1, BRIDGE_CONTRACT_ADDRESS_EXPR, ) - .put_account( - OWNER_WEGLD_SWAP_CONTRACT_ADDRESS_EXPR, - Account::new().nonce(1).balance("1_000"), - ) - .new_address( - OWNER_WEGLD_SWAP_CONTRACT_ADDRESS_EXPR, - 1, - WEGLD_SWAP_CONTRACT_ADDRESS_EXPR, - ) .put_account( ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, Account::new() @@ -127,7 +104,6 @@ impl ContractState { ); let contract = Contract::new(BRIDGE_CONTRACT_ADDRESS_EXPR); - let wegld_swap_contract = WegldSwapContract::new(WEGLD_SWAP_CONTRACT_ADDRESS_EXPR); let contract_owner = AddressValue::from(OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR).to_address(); let admin = AddressValue::from(ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR).to_address(); @@ -139,7 +115,6 @@ impl ContractState { Self { world, contract, - wegld_swap_contract, contract_owner, admin, relayer, @@ -171,48 +146,15 @@ impl ContractState { AddressValue::from(RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR).to_address(), None, ) - .set_wegld_contract_address( + .set_wegld_token_identifier( OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, - AddressValue::from(WEGLD_SWAP_CONTRACT_ADDRESS_EXPR).to_address(), + WEGLD_TOKEN_IDENTIFIER, None, ); self } - pub fn deploy_wegld_swap(&mut self) -> &mut Self { - let wegld_swap_contract_code = self.world.code_expression(WEGLD_SWAP_CONTRACT_PATH); - - let mut acc = Account::new() - .esdt_roles( - WEGLD_TOKEN_IDENTIFIER_EXPR, - vec![ - "ESDTRoleLocalBurn".to_string(), - "ESDTRoleLocalMint".to_string(), - ], - ) - .code(wegld_swap_contract_code); - - acc.storage.insert( - b"wrappedEgldTokenId".to_vec().into(), - WEGLD_TOKEN_IDENTIFIER.to_vec().into(), - ); - - acc.storage.insert( - b"pause_module:paused".to_vec().into(), - "false".to_string().into(), - ); - - acc.owner = Option::Some(AddressValue::from(OWNER_WEGLD_SWAP_CONTRACT_ADDRESS_EXPR)); - self.world.set_state_step( - SetStateStep::new() - .new_token_identifier(WEGLD_TOKEN_IDENTIFIER_EXPR) - .put_account(WEGLD_SWAP_CONTRACT_ADDRESS_EXPR, acc), - ); - - self - } - pub fn deploy(&mut self) -> &mut Self { let bridge_contract_code = self.world.code_expression(BRIDGE_CONTRACT_PATH); @@ -452,10 +394,10 @@ impl ContractState { self } - pub fn set_wegld_contract_address( + pub fn set_wegld_token_identifier( &mut self, caller: &str, - wegld_contract_address: Address, + wegld_token_identifier: &[u8], expect: Option, ) -> &mut Self { let tx_expect = expect.unwrap_or(TxExpect::ok()); @@ -465,7 +407,7 @@ impl ContractState { .from(caller) .call( self.contract - .set_wegld_contract_address(wegld_contract_address), + .set_wegld_contract_address(managed_token_id!(wegld_token_identifier)), ) .expect(tx_expect), ); @@ -480,12 +422,13 @@ impl ContractState { ) -> &mut Self { let tx_expect = expect.unwrap_or(TxExpect::ok()); - let mut args = MultiValueEncoded::new(); - args.push(MultiValue2((managed_token_id!(token_identifier), 18u32))); self.world.sc_call( ScCallStep::new() .from(caller) - .call(self.contract.add_tokens_to_whitelist(args)) + .call( + self.contract + .add_tokens_to_whitelist(managed_token_id!(token_identifier), 18u32), + ) .expect(tx_expect), ); self @@ -499,12 +442,13 @@ impl ContractState { ) -> &mut Self { let tx_expect = expect.unwrap_or(TxExpect::ok()); - let mut args = MultiValueEncoded::new(); - args.push(managed_token_id!(token_identifier)); self.world.sc_call( ScCallStep::new() .from(caller) - .call(self.contract.remove_tokens_from_whitelist(args)) + .call( + self.contract + .remove_token_from_whitelist(managed_token_id!(token_identifier)), + ) .expect(tx_expect), ); self diff --git a/tests/endpoints/admin.rs b/tests/endpoints/admin.rs index aa92a4c..62552b0 100644 --- a/tests/endpoints/admin.rs +++ b/tests/endpoints/admin.rs @@ -9,6 +9,7 @@ use multiversx_sc::{ use multiversx_sc_scenario::{ managed_token_id, scenario_model::{CheckAccount, CheckStateStep, ScQueryStep, TxExpect}, + ExpectValue, }; use crate::bridge_sc::bridge_sc::{ @@ -142,14 +143,12 @@ fn add_remove_token_from_whitelist() { None, ); - let mut tokens = MultiValueEncoded::new(); - - tokens.push(managed_token_id!(ITHEUM_TOKEN_IDENTIFIER)); - state.world.sc_query( ScQueryStep::new() - .call(state.contract.tokens_whitelist()) - .expect_value(tokens), + .call(state.contract.token_whitelist()) + .expect_value(SingleValue::from(managed_token_id!( + ITHEUM_TOKEN_IDENTIFIER + ))), ); state.remove_token_from_whitelist( @@ -166,8 +165,8 @@ fn add_remove_token_from_whitelist() { state.world.sc_query( ScQueryStep::new() - .call(state.contract.tokens_whitelist()) - .expect_value(MultiValueEncoded::new()), + .call(state.contract.token_whitelist()) + .expect_value(SingleValue::from(managed_token_id!(b""))), ); } diff --git a/tests/endpoints/public.rs b/tests/endpoints/public.rs index 4f31053..3bf3e80 100644 --- a/tests/endpoints/public.rs +++ b/tests/endpoints/public.rs @@ -6,13 +6,9 @@ use crate::bridge_sc::bridge_sc::{ ContractState, ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, ANOTHER_TOKEN_IDENTIFIER_EXPR, BRIDGE_CONTRACT_ADDRESS_EXPR, FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER, ITHEUM_TOKEN_IDENTIFIER_EXPR, OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, - OWNER_WEGLD_SWAP_CONTRACT_ADDRESS_EXPR, RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR, - THIRD_USER_ADDRESS_EXPR, WEGLD_TOKEN_IDENTIFIER_EXPR, + RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR, THIRD_USER_ADDRESS_EXPR, WEGLD_TOKEN_IDENTIFIER_EXPR, }; -use multiversx_sc_modules::pause::ProxyTrait; -use multiversx_wegld_swap_sc::ProxyTrait as _; - #[test] fn send_to_bridge_test() { let mut state = ContractState::new(); @@ -141,7 +137,6 @@ fn send_to_bridge_require_fee_test() { state .default_deploy_and_set() - .deploy_wegld_swap() .set_contract_state_active(OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, None) .set_fee_value(OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, 1_000u64, None); @@ -158,27 +153,12 @@ fn send_to_bridge_require_fee_test() { THIRD_USER_ADDRESS_EXPR, Account::new() .nonce(1) - .balance("1_000") + .esdt_balance(WEGLD_TOKEN_IDENTIFIER_EXPR, "1_000") .esdt_balance(ITHEUM_TOKEN_IDENTIFIER_EXPR, "100_000_000_000_000_000_000") // 100 tokens .esdt_balance(ANOTHER_TOKEN_IDENTIFIER_EXPR, "1_000"), ), ); - state.world.sc_call( - ScCallStep::new() - .from(OWNER_WEGLD_SWAP_CONTRACT_ADDRESS_EXPR) - .call(state.wegld_swap_contract.unpause_endpoint()) - .expect(TxExpect::ok()), - ); - - state.world.sc_call( - ScCallStep::new() - .from(THIRD_USER_ADDRESS_EXPR) - .egld_value("1_000") - .call(state.wegld_swap_contract.wrap_egld()) - .expect(TxExpect::ok()), - ); - state.send_to_liquidity( THIRD_USER_ADDRESS_EXPR, ( @@ -244,7 +224,7 @@ fn send_to_bridge_require_fee_test() { ) .put_account( RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR, - CheckAccount::new().balance("1_000"), // fee + CheckAccount::new().esdt_balance(WEGLD_TOKEN_IDENTIFIER_EXPR, "1_000"), // fee ), ); } diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 4e5a0cc..8b8aacf 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -9,11 +9,13 @@ use core_mx_bridge_sc::{ use multiversx_sc::types::{BigUint, TokenIdentifier}; use multiversx_sc_scenario::{ api::SingleTxApi, - managed_address, + managed_address, managed_token_id, scenario_model::{AddressValue, TxExpect}, }; -use crate::bridge_sc::bridge_sc::{ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER}; +use crate::bridge_sc::bridge_sc::{ + ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER, ITHEUM_TOKEN_IDENTIFIER_EXPR, +}; mod bridge_sc; mod endpoints; @@ -82,8 +84,8 @@ fn contract_is_ready_test() { bridge_sc.fee_value().set(BigUint::from(10u64)); bridge_sc - .tokens_whitelist() - .insert(TokenIdentifier::from(ITHEUM_TOKEN_IDENTIFIER)); + .token_whitelist() + .set(TokenIdentifier::from(ITHEUM_TOKEN_IDENTIFIER)); check = bridge_sc.contract_is_ready(); @@ -101,11 +103,8 @@ fn contract_is_ready_test() { assert_eq!(check, false); bridge_sc - .wegld_contract_address() - .set(managed_address!(&AddressValue::from( - ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR - ) - .to_address())); + .wegld_token_identifier() + .set(managed_token_id!(ITHEUM_TOKEN_IDENTIFIER_EXPR)); check = bridge_sc.contract_is_ready(); diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index e57496e..e81f2bd 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -25,7 +25,6 @@ name = "core-mx-bridge-sc" version = "0.1.0" dependencies = [ "multiversx-sc", - "multiversx-sc-modules", ] [[package]] @@ -104,15 +103,6 @@ dependencies = [ "syn", ] -[[package]] -name = "multiversx-sc-modules" -version = "0.50.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9a8c10ddadc7adfc04edbdd1ea80a9ae9a7e11202fc4cfb2e08c8e27f17aa5" -dependencies = [ - "multiversx-sc", -] - [[package]] name = "multiversx-sc-wasm-adapter" version = "0.50.3" diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 5b8484f..2f2129a 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -28,10 +28,10 @@ multiversx_sc_wasm_adapter::endpoints! { getMaximumDeposit => maximum_deposit getFeeCollector => fee_collector getFeeValue => fee_value - getWegldContractAddress => wegld_contract_address + getWegldTokenIdentifier => wegld_token_identifier setAdministrator => set_administrator getAdministrator => administrator - getTokensWhitelist => tokens_whitelist + getTokensWhitelist => token_whitelist getWhitelistState => whitelist_state getPublicState => public_state getRelayerState => relayer_state @@ -45,9 +45,9 @@ multiversx_sc_wasm_adapter::endpoints! { setDepositLimits => set_deposit_limits setFeeCollector => set_fee_collector setFeeValue => set_fee_value - setWegldContractAddress => set_wegld_contract_address - addTokensToWhitelist => add_tokens_to_whitelist - removeTokensFromWhitelist => remove_tokens_from_whitelist + setWegldTokenIdentifier => set_wegld_contract_address + addTokenToWhitelist => add_tokens_to_whitelist + removeTokenFromWhitelist => remove_token_from_whitelist addToWhitelist => add_to_whitelist removeFromWhitelist => remove_from_whitelist setRelayer => set_relayer From 82c2d4a215feec74d9a6fe55df83b03303457c93 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Thu, 13 Jun 2024 10:18:53 +0300 Subject: [PATCH 2/4] feat: one token whitelist support --- src/admin.rs | 17 ++++------------- src/events.rs | 7 +------ src/lib.rs | 12 ++++++------ src/storage.rs | 4 ++-- tests/bridge_sc/bridge_sc.rs | 2 -- tests/endpoints/admin.rs | 30 ++---------------------------- tests/endpoints/public.rs | 10 ++++------ 7 files changed, 19 insertions(+), 63 deletions(-) diff --git a/src/admin.rs b/src/admin.rs index c5976b4..e93be83 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -92,21 +92,12 @@ pub trait AdminModule: } #[endpoint(setDepositLimits)] - fn set_deposit_limits( - &self, - token_identifier: TokenIdentifier, - minimum: BigUint, - maximum: BigUint, - ) { + fn set_deposit_limits(&self, minimum: BigUint, maximum: BigUint) { only_privileged!(self, ERR_NOT_PRIVILEGED); - require!( - self.token_whitelist().get() == token_identifier, - ERR_TOKEN_NOT_WHITELISTED - ); require!(minimum <= maximum, ERR_WRONG_VALUES); - self.set_deposit_limits_event(&token_identifier, &minimum, &maximum); - self.minimum_deposit(&token_identifier).set(minimum); - self.maximum_deposit(&token_identifier).set(maximum); + self.set_deposit_limits_event(&minimum, &maximum); + self.minimum_deposit().set(minimum); + self.maximum_deposit().set(maximum); } #[endpoint(setFeeCollector)] diff --git a/src/events.rs b/src/events.rs index 31ce044..c74e679 100644 --- a/src/events.rs +++ b/src/events.rs @@ -56,12 +56,7 @@ pub trait EventsModule { fn set_whitelist_state_event(&self, #[indexed] state: &State); #[event("setDepositLimitsEvent")] - fn set_deposit_limits_event( - &self, - #[indexed] token_identifier: &TokenIdentifier, - #[indexed] minimum: &BigUint, - #[indexed] maximum: &BigUint, - ); + fn set_deposit_limits_event(&self, #[indexed] minimum: &BigUint, #[indexed] maximum: &BigUint); #[event("addTokenToWhitelistEvent")] fn add_token_to_whitelist_event(&self, #[indexed] token: &TokenIdentifier, decimals: &u32); diff --git a/src/lib.rs b/src/lib.rs index 5981790..d721421 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,8 +28,8 @@ pub trait CoreMxBridgeSc: #[upgrade] fn upgrade(&self) { - self.set_public_state_inactive(); - self.set_relayer_state_inactive(); + self.public_state().set(config::State::Inactive); + self.relayer_state().set(config::State::Inactive); } #[payable("*")] @@ -66,8 +66,8 @@ pub trait CoreMxBridgeSc: ); require!( - self.minimum_deposit(&deposit.token_identifier).get() <= deposit.amount - && deposit.amount <= self.maximum_deposit(&deposit.token_identifier).get(), + self.minimum_deposit().get() <= deposit.amount + && deposit.amount <= self.maximum_deposit().get(), ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE ); @@ -105,8 +105,8 @@ pub trait CoreMxBridgeSc: ); require!( - self.minimum_deposit(&deposit.token_identifier).get() <= deposit.amount - && deposit.amount <= self.maximum_deposit(&deposit.token_identifier).get(), + self.minimum_deposit().get() <= deposit.amount + && deposit.amount <= self.maximum_deposit().get(), ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE ); diff --git a/src/storage.rs b/src/storage.rs index aebf399..df4ea6b 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -13,11 +13,11 @@ pub trait StorageModule { #[view(getMinimumDeposit)] #[storage_mapper("minimum_deposit")] - fn minimum_deposit(&self, token_identifier: &TokenIdentifier) -> SingleValueMapper; + fn minimum_deposit(&self) -> SingleValueMapper; #[view(getMaximumDeposit)] #[storage_mapper("maximum_deposit")] - fn maximum_deposit(&self, token_identifier: &TokenIdentifier) -> SingleValueMapper; + fn maximum_deposit(&self) -> SingleValueMapper; #[view(getFeeCollector)] #[storage_mapper("fee_collector")] diff --git a/tests/bridge_sc/bridge_sc.rs b/tests/bridge_sc/bridge_sc.rs index b1ab1ce..8993aef 100644 --- a/tests/bridge_sc/bridge_sc.rs +++ b/tests/bridge_sc/bridge_sc.rs @@ -357,7 +357,6 @@ impl ContractState { pub fn set_deposit_limits( &mut self, caller: &str, - token_identifier: &[u8], min_deposit: &[u8], max_deposit: &[u8], expect: Option, @@ -368,7 +367,6 @@ impl ContractState { ScCallStep::new() .from(caller) .call(self.contract.set_deposit_limits( - managed_token_id!(token_identifier), BigUint::from(managed_buffer!(min_deposit)), BigUint::from(managed_buffer!(max_deposit)), )) diff --git a/tests/endpoints/admin.rs b/tests/endpoints/admin.rs index 62552b0..171081b 100644 --- a/tests/endpoints/admin.rs +++ b/tests/endpoints/admin.rs @@ -2,14 +2,10 @@ use core_mx_bridge_sc::{ config::{ProxyTrait as _, State}, storage::ProxyTrait as _, }; -use multiversx_sc::{ - imports::SingleValue, - types::{BigUint, MultiValueEncoded}, -}; +use multiversx_sc::{imports::SingleValue, types::BigUint}; use multiversx_sc_scenario::{ managed_token_id, scenario_model::{CheckAccount, CheckStateStep, ScQueryStep, TxExpect}, - ExpectValue, }; use crate::bridge_sc::bridge_sc::{ @@ -85,21 +81,6 @@ fn set_deposit_limits_test() { state.set_deposit_limits( ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, - b"10", - b"20", - Some(TxExpect::user_error("str:Token not whitelisted")), - ); - - state.add_token_to_whitelist( - ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, - None, - ); - - state.set_deposit_limits( - ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, b"20", b"10", Some(TxExpect::user_error("str:Wrong values")), @@ -107,19 +88,12 @@ fn set_deposit_limits_test() { state.set_deposit_limits( FIRST_USER_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, b"10", b"20", Some(TxExpect::user_error("str:Not privileged")), ); - state.set_deposit_limits( - ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, - b"0", - b"10", - None, - ); + state.set_deposit_limits(ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, b"0", b"10", None); } #[test] diff --git a/tests/endpoints/public.rs b/tests/endpoints/public.rs index 3bf3e80..13db45c 100644 --- a/tests/endpoints/public.rs +++ b/tests/endpoints/public.rs @@ -1,12 +1,12 @@ use multiversx_sc_scenario::imports::{ - Account, CheckAccount, CheckStateStep, ScCallStep, SetStateStep, TxExpect, + Account, CheckAccount, CheckStateStep, SetStateStep, TxExpect, }; use crate::bridge_sc::bridge_sc::{ ContractState, ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, ANOTHER_TOKEN_IDENTIFIER_EXPR, - BRIDGE_CONTRACT_ADDRESS_EXPR, FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER, - ITHEUM_TOKEN_IDENTIFIER_EXPR, OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, - RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR, THIRD_USER_ADDRESS_EXPR, WEGLD_TOKEN_IDENTIFIER_EXPR, + BRIDGE_CONTRACT_ADDRESS_EXPR, FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER_EXPR, + OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR, + THIRD_USER_ADDRESS_EXPR, WEGLD_TOKEN_IDENTIFIER_EXPR, }; #[test] @@ -88,7 +88,6 @@ fn send_to_bridge_test() { ); state.set_deposit_limits( OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, b"0", b"100000000000000000000", // 100 tokens None, @@ -142,7 +141,6 @@ fn send_to_bridge_require_fee_test() { state.set_deposit_limits( OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, - ITHEUM_TOKEN_IDENTIFIER, b"0", b"1000000000000000000000", // 1000 tokens None, From 66760b89a742e8086f5921ddeff860458272e631 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Thu, 13 Jun 2024 11:08:44 +0300 Subject: [PATCH 3/4] test: more coverage --- tests/endpoints/admin.rs | 12 ++++++++++++ tests/endpoints/public.rs | 4 ++++ tests/endpoints/relayer.rs | 2 ++ 3 files changed, 18 insertions(+) diff --git a/tests/endpoints/admin.rs b/tests/endpoints/admin.rs index 171081b..c688ed3 100644 --- a/tests/endpoints/admin.rs +++ b/tests/endpoints/admin.rs @@ -111,12 +111,24 @@ fn add_remove_token_from_whitelist() { Some(TxExpect::user_error("str:Not privileged")), ); + state.add_token_to_whitelist( + ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, + ANOTHER_TOKEN_IDENTIFIER, + None, + ); + state.add_token_to_whitelist( ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER, None, ); + state.add_token_to_whitelist( + ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, + ITHEUM_TOKEN_IDENTIFIER, + Some(TxExpect::user_error("str:Token already in whitelist")), + ); + state.world.sc_query( ScQueryStep::new() .call(state.contract.token_whitelist()) diff --git a/tests/endpoints/public.rs b/tests/endpoints/public.rs index 13db45c..415f96b 100644 --- a/tests/endpoints/public.rs +++ b/tests/endpoints/public.rs @@ -38,6 +38,10 @@ fn send_to_bridge_test() { state.add_to_whitelist(ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, first_user.clone(), None); + state.remove_from_whitelist(ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, first_user.clone(), None); + + state.add_to_whitelist(ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, first_user.clone(), None); + state.send_to_liquidity( FIRST_USER_ADDRESS_EXPR, (ITHEUM_TOKEN_IDENTIFIER_EXPR, 0u64, "1_000"), diff --git a/tests/endpoints/relayer.rs b/tests/endpoints/relayer.rs index 668f884..024b791 100644 --- a/tests/endpoints/relayer.rs +++ b/tests/endpoints/relayer.rs @@ -22,6 +22,8 @@ fn relayer_test() { state .default_deploy_and_set() + .set_relayer_state_active(OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, None) + .set_relayer_state_inactive(OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, None) .set_relayer_state_active(OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, None); state.send_from_liquidity( From 88f6efe9e9c3ed74431dddf439a4a831e0c903c1 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Thu, 13 Jun 2024 11:10:49 +0300 Subject: [PATCH 4/4] chore: snippets update --- interaction/devnet.snippets.sh | 52 +++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/interaction/devnet.snippets.sh b/interaction/devnet.snippets.sh index 333348e..8ff2802 100644 --- a/interaction/devnet.snippets.sh +++ b/interaction/devnet.snippets.sh @@ -52,6 +52,26 @@ upgrade(){ +addTokenToWhitelist(){ + # $1 = token + # $2 = decimals + + token="0x$(echo -n ${1} | xxd -p -u | tr -d '\n')" + + mxpy --verbose contract call ${ADDRESS} \ + --recall-nonce \ + --pem=${WALLET} \ + --gas-limit=6000000 \ + --function "addTokenToWhitelist" \ + --arguments $token $2 \ + --proxy ${PROXY} \ + --chain ${CHAIN_ID} \ + --send || return + +} + + + setAdministrator(){ # $1 = address @@ -88,8 +108,6 @@ setFeeCollector(){ setFeeValue(){ # $1 = value - - mxpy --verbose contract call ${ADDRESS} \ --recall-nonce \ --pem=${WALLET} \ @@ -102,17 +120,37 @@ setFeeValue(){ } -setWegldContractAddress(){ - # $1 = address - address="0x$(mxpy wallet bech32 --decode ${1})" +setDepositLimits(){ + # $1 = min + # $2 = max + + mxpy --verbose contract call ${ADDRESS} \ --recall-nonce \ --pem=${WALLET} \ --gas-limit=6000000 \ - --function "setWegldContractAddress" \ - --arguments $address \ + --function "setDepositLimits" \ + --arguments $1 $2 \ + --proxy ${PROXY} \ + --chain ${CHAIN_ID} \ + --send || return + + +} + +setWegldTokenIdentifier(){ + # $1 = token + + token="0x$(echo -n ${1} | xxd -p -u | tr -d '\n')" + + mxpy --verbose contract call ${ADDRESS} \ + --recall-nonce \ + --pem=${WALLET} \ + --gas-limit=6000000 \ + --function "setWegldTokenIdentifier" \ + --arguments $token \ --proxy ${PROXY} \ --chain ${CHAIN_ID} \ --send || return