From 3c5b4de3be23359a10d1ed428f1159d93b7f07e6 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Wed, 30 Oct 2024 16:04:30 +0200 Subject: [PATCH 01/15] merge and more unit tests --- multisig/tests/multisig_blackbox_test.rs | 93 +++++++++++++++++++++++- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index f776cbc5..47ad3acd 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -896,13 +896,100 @@ fn ethereum_to_multiversx_tx_batch_rejected_test() { .run(); } +#[test] +fn init_test() { + let mut state = MultiTransferTestState::new(); + + let mut board: MultiValueEncoded> = + MultiValueEncoded::new(); + board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + state + .world + .tx() + .from(OWNER_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init( + ESDT_SAFE_ADDRESS, + MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + 1_000u64, + 5000u64, + 2usize, + board.clone(), + ) + .code(MULTISIG_CODE_PATH) + .new_address(MULTISIG_ADDRESS) + .returns(ExpectError( + 4, + "slash amount must be less than or equal to required stake", + )) + .run(); + + let mut board2: MultiValueEncoded> = + MultiValueEncoded::new(); + board2.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board2.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + let multisig2 = TestSCAddress::new("multisig2"); + state + .world + .tx() + .from(OWNER_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init( + ESDT_SAFE_ADDRESS, + MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + 1_000u64, + 500u64, + 2usize, + board2.clone(), + ) + .code(MULTISIG_CODE_PATH) + .new_address(multisig2) + .returns(ExpectError(4, "duplicate board member")) + .run(); +} + +#[test] +fn upgrade_test() { + let mut state = MultiTransferTestState::new(); + + state.multisig_deploy(); + state.safe_deploy(); + state.multi_transfer_deploy(); + state.bridge_proxy_deploy(); + state.bridged_tokens_wrapper_deploy(); + state.config_multisig(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .upgrade( + ESDT_SAFE_ADDRESS, + MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + ) + .code(MULTISIG_CODE_PATH) + .run(); +} + #[test] fn multisig_non_board_member_interaction_test() { let mut state = MultiTransferTestState::new(); let token_amount = BigUint::from(76_000_000_000u64); state.multisig_deploy(); - state.safe_deploy(Address::zero()); + state.safe_deploy(); state.multi_transfer_deploy(); state.bridge_proxy_deploy(); state.bridged_tokens_wrapper_deploy(); @@ -953,7 +1040,7 @@ fn multisig_insuficient_signatures_test() { let token_amount = BigUint::from(76_000_000_000u64); state.multisig_deploy(); - state.safe_deploy(Address::zero()); + state.safe_deploy(); state.multi_transfer_deploy(); state.bridge_proxy_deploy(); state.bridged_tokens_wrapper_deploy(); @@ -1000,7 +1087,7 @@ fn multisig_non_board_member_sign_test() { let token_amount = BigUint::from(76_000_000_000u64); state.multisig_deploy(); - state.safe_deploy(Address::zero()); + state.safe_deploy(); state.multi_transfer_deploy(); state.bridge_proxy_deploy(); state.bridged_tokens_wrapper_deploy(); From 0280749c833ade3ebf92c4b4a03bfb1fa69e1f05 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Thu, 31 Oct 2024 17:52:36 +0200 Subject: [PATCH 02/15] more tests --- Cargo.lock | 1 + .../src/mock_multi_transfer_esdt.rs | 4 + .../mock-multi-transfer-esdt/wasm/src/lib.rs | 5 +- multisig/Cargo.toml | 3 + multisig/tests/multisig_blackbox_test.rs | 676 ++++++++++++++++-- 5 files changed, 615 insertions(+), 74 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac17940c..9d3543f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -710,6 +710,7 @@ dependencies = [ "eth-address", "fee-estimator-module", "max-bridged-amount-module", + "mock-multi-transfer-esdt", "multi-transfer-esdt", "multiversx-price-aggregator-sc", "multiversx-sc", diff --git a/common/mock-contracts/mock-multi-transfer-esdt/src/mock_multi_transfer_esdt.rs b/common/mock-contracts/mock-multi-transfer-esdt/src/mock_multi_transfer_esdt.rs index 629d799b..16c4cbb2 100644 --- a/common/mock-contracts/mock-multi-transfer-esdt/src/mock_multi_transfer_esdt.rs +++ b/common/mock-contracts/mock-multi-transfer-esdt/src/mock_multi_transfer_esdt.rs @@ -11,4 +11,8 @@ pub trait MockMultiTransferEsdt { #[upgrade] fn upgrade(&self) {} + + #[only_owner] + #[endpoint(addUnprocessedRefundTxToBatch)] + fn add_unprocessed_refund_tx_to_batch(&self, _tx_id: u64) {} } diff --git a/common/mock-contracts/mock-multi-transfer-esdt/wasm/src/lib.rs b/common/mock-contracts/mock-multi-transfer-esdt/wasm/src/lib.rs index 36a8284b..8704aadc 100644 --- a/common/mock-contracts/mock-multi-transfer-esdt/wasm/src/lib.rs +++ b/common/mock-contracts/mock-multi-transfer-esdt/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 0 +// Endpoints: 1 // Async Callback (empty): 1 -// Total number of exported functions: 3 +// Total number of exported functions: 4 #![no_std] @@ -20,6 +20,7 @@ multiversx_sc_wasm_adapter::endpoints! { ( init => init upgrade => upgrade + addUnprocessedRefundTxToBatch => add_unprocessed_refund_tx_to_batch ) } diff --git a/multisig/Cargo.toml b/multisig/Cargo.toml index 18413c6c..504cf8e3 100644 --- a/multisig/Cargo.toml +++ b/multisig/Cargo.toml @@ -26,6 +26,9 @@ path = "../common/tx-batch-module" [dependencies.max-bridged-amount-module] path = "../common/max-bridged-amount-module" +[dependencies.mock-multi-transfer-esdt] +path = "../common/mock-contracts/mock-multi-transfer-esdt" + [dependencies.sc-proxies] path = "../common/sc-proxies" diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 47ad3acd..5b0eec38 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -14,6 +14,7 @@ use multiversx_sc::{ }, contract_base::ManagedSerializer, hex_literal::hex, + imports::MultiValue2, storage::mappers::SingleValue, types::{ Address, BigUint, CodeMetadata, EsdtLocalRole, ManagedAddress, ManagedBuffer, @@ -44,6 +45,7 @@ use transaction::{ const WEGLD_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("WEGLD-123456"); const ETH_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("ETH-123456"); +const NATIVE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("EGLD-123456"); const USER_ETHEREUM_ADDRESS: &[u8] = b"0x0102030405060708091011121314151617181920"; @@ -53,6 +55,9 @@ const ETH_TX_GAS_LIMIT: u64 = 150_000; const MULTISIG_CODE_PATH: MxscPath = MxscPath::new("output/multisig.mxsc.json"); const MULTI_TRANSFER_CODE_PATH: MxscPath = MxscPath::new("../multi-transfer-esdt/output/multi-transfer-esdt.mxsc.json"); +const MOCK_MULTI_TRANSFER_PATH_EXPR: MxscPath = MxscPath::new( + "../common/mock-contracts/mock-multi-transfer-esdt/output/mock-multi-transfer-esdt.mxsc.json", +); const BRIDGE_PROXY_CODE_PATH: MxscPath = MxscPath::new("../bridge-proxy/output/bridge-proxy.mxsc.json"); const ESDT_SAFE_CODE_PATH: MxscPath = MxscPath::new("../esdt-safe/output/esdt-safe.mxsc.json"); @@ -63,6 +68,7 @@ const PRICE_AGGREGATOR_CODE_PATH: MxscPath = const MULTISIG_ADDRESS: TestSCAddress = TestSCAddress::new("multisig"); const MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("multi-transfer"); +const MOCK_MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("mock-multi-transfer"); const BRIDGE_PROXY_ADDRESS: TestSCAddress = TestSCAddress::new("bridge-proxy"); const ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("esdt-safe"); const BRIDGED_TOKENS_WRAPPER_ADDRESS: TestSCAddress = TestSCAddress::new("bridged-tokens-wrapper"); @@ -100,6 +106,10 @@ fn world() -> ScenarioWorld { PRICE_AGGREGATOR_CODE_PATH, fee_estimator_module::ContractBuilder, ); + blockchain.register_contract( + MOCK_MULTI_TRANSFER_PATH_EXPR, + mock_multi_transfer_esdt::ContractBuilder, + ); blockchain } @@ -122,21 +132,21 @@ impl MultiTransferTestState { .nonce(1) .esdt_balance(WEGLD_TOKEN_ID, 1001u64) .esdt_balance(ETH_TOKEN_ID, 1001u64) + .esdt_balance(NATIVE_TOKEN_ID, 100_000u64) .account(USER1_ADDRESS) .nonce(1) .account(RELAYER1_ADDRESS) .nonce(1) - .balance(1_000u64) + .balance(2_000u64) .account(RELAYER2_ADDRESS) .nonce(1) - .balance(1_000u64) + .balance(2_000u64) .account(NON_BOARD_MEMEBER_ADDRESS) .nonce(1); - let roles = vec![ - "ESDTRoleLocalMint".to_string(), - "ESDTRoleLocalBurn".to_string(), - ]; + world + .account(MOCK_MULTI_TRANSFER_ADDRESS) + .code(MOCK_MULTI_TRANSFER_PATH_EXPR); Self { world } } @@ -252,6 +262,29 @@ impl MultiTransferTestState { ) .run(); + self.world.set_esdt_balance( + MULTISIG_ADDRESS, + b"EGLD-123456", + BigUint::from(100_000_000_000u64), + ); + + self.world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .add_token_to_whitelist( + TokenIdentifier::from_esdt_bytes("EGLD-123456"), + "EGLD", + false, + true, + BigUint::zero(), + BigUint::zero(), + BigUint::zero(), + OptionalValue::Some(BigUint::from(ESDT_SAFE_ETH_TX_GAS_LIMIT)), + ) + .run(); + self.world .tx() .from(OWNER_ADDRESS) @@ -342,18 +375,22 @@ impl MultiTransferTestState { .to_vec() .contains(&RELAYER2_ADDRESS.to_managed_address())); } + + fn deploy_contracts_config(&mut self) { + self.multisig_deploy(); + self.safe_deploy(); + self.multi_transfer_deploy(); + self.bridge_proxy_deploy(); + self.bridged_tokens_wrapper_deploy(); + self.config_multisig(); + } } #[test] fn config_test() { let mut state = MultiTransferTestState::new(); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); } #[test] @@ -361,12 +398,7 @@ fn ethereum_to_multiversx_call_data_empty_test() { let mut state = MultiTransferTestState::new(); let token_amount = BigUint::from(76_000_000u64); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let eth_tx = EthTxAsMultiValue::::from(( EthAddress { @@ -423,12 +455,7 @@ fn ethereum_to_multiversx_relayer_call_data_several_tx_test() { state.world.start_trace(); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let addr = Address::from_slice(b"erd1dyw7aysn0nwmuahvxnh2e0pm0kgjvs2gmfdxjgz3x0pet2nkvt8s7tkyrj"); @@ -536,12 +563,7 @@ fn ethereum_to_multiversx_relayer_query_test() { let token_amount = BigUint::from(76_000_000_000u64); state.world.start_trace(); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let eth_tx = EthTxAsMultiValue::::from(( EthAddress { @@ -623,12 +645,7 @@ fn ethereum_to_multiversx_relayer_query2_test() { let token_amount = BigUint::from(5_000u64); state.world.start_trace(); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let addr = Address::from_slice(b"erd1dyw7aysn0nwmuahvxnh2e0pm0kgjvs2gmfdxjgz3x0pet2nkvt8s7tkyrj"); @@ -711,12 +728,7 @@ fn ethereum_to_multiversx_tx_batch_ok_test() { let token_amount = BigUint::from(76_000_000_000u64); state.world.start_trace(); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let mut args = ManagedVec::new(); args.push(ManagedBuffer::from(&[5u8, 6u8])); @@ -802,12 +814,7 @@ fn ethereum_to_multiversx_tx_batch_rejected_test() { let mut state = MultiTransferTestState::new(); let over_the_limit_token_amount = BigUint::from(101_000_000_000u64); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let mut args = ManagedVec::new(); args.push(ManagedBuffer::from(&[5u8])); @@ -959,12 +966,7 @@ fn init_test() { fn upgrade_test() { let mut state = MultiTransferTestState::new(); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); state .world @@ -988,12 +990,7 @@ fn multisig_non_board_member_interaction_test() { let mut state = MultiTransferTestState::new(); let token_amount = BigUint::from(76_000_000_000u64); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let eth_tx = EthTxAsMultiValue::::from(( EthAddress { @@ -1039,12 +1036,7 @@ fn multisig_insuficient_signatures_test() { let mut state = MultiTransferTestState::new(); let token_amount = BigUint::from(76_000_000_000u64); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let eth_tx = EthTxAsMultiValue::::from(( EthAddress { @@ -1086,12 +1078,7 @@ fn multisig_non_board_member_sign_test() { let mut state = MultiTransferTestState::new(); let token_amount = BigUint::from(76_000_000_000u64); - state.multisig_deploy(); - state.safe_deploy(); - state.multi_transfer_deploy(); - state.bridge_proxy_deploy(); - state.bridged_tokens_wrapper_deploy(); - state.config_multisig(); + state.deploy_contracts_config(); let eth_tx = EthTxAsMultiValue::::from(( EthAddress { @@ -1137,3 +1124,548 @@ fn multisig_non_board_member_sign_test() { .returns(ExpectError(4, "quorum has not been reached")) .run(); } + +#[test] +fn test_distribute_fees_from_child_contracts_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let dest_address1 = USER1_ADDRESS.to_managed_address(); + let dest_address2 = USER2_ADDRESS.to_managed_address(); + + const PERCENTAGE_TOTAL: u32 = 10000; + + let percentage1 = 6000; // 60% + let percentage2 = 4000; // 40% + + let mut dest_address_percentage_pairs: MultiValueEncoded< + StaticApi, + MultiValue2, u32>, + > = MultiValueEncoded::new(); + + dest_address_percentage_pairs.push(MultiValue2::from((dest_address1.clone(), percentage1))); + dest_address_percentage_pairs.push(MultiValue2::from((dest_address2.clone(), percentage2))); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .distribute_fees_from_child_contracts(dest_address_percentage_pairs) + .run(); +} + +#[test] +fn test_distribute_fees_from_child_contracts_invalid_percentage_sum() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + let dest_address1 = USER1_ADDRESS.to_managed_address(); + let dest_address2 = USER2_ADDRESS.to_managed_address(); + + let percentage1 = 5000; // 50% + let percentage2 = 4000; // 40% + + let mut dest_address_percentage_pairs: MultiValueEncoded< + StaticApi, + MultiValue2, u32>, + > = MultiValueEncoded::new(); + + dest_address_percentage_pairs.push(MultiValue2::from((dest_address1.clone(), percentage1))); + dest_address_percentage_pairs.push(MultiValue2::from((dest_address2.clone(), percentage2))); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .distribute_fees_from_child_contracts(dest_address_percentage_pairs) + .returns(ExpectError(4, "Percentages do not add up to 100%")) + .run(); +} + +#[test] +fn test_distribute_fees_from_child_contracts_with_sc_address() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let dest_address1 = USER1_ADDRESS.to_managed_address(); + let dest_address2 = ESDT_SAFE_ADDRESS.to_managed_address(); + let percentage1 = 6000; // 60% + let percentage2 = 4000; // 40% + + let mut dest_address_percentage_pairs: MultiValueEncoded< + StaticApi, + MultiValue2, u32>, + > = MultiValueEncoded::new(); + + dest_address_percentage_pairs.push(MultiValue2::from((dest_address1.clone(), percentage1))); + dest_address_percentage_pairs.push(MultiValue2::from((dest_address2.clone(), percentage2))); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .distribute_fees_from_child_contracts(dest_address_percentage_pairs) + .returns(ExpectError( + 4, + "Cannot transfer to smart contract dest_address", + )) + .run(); +} + +#[test] +fn test_unstake_successful_board_member() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + let stake_amount = BigUint::from(1_000u64); + + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .stake() + .egld(&stake_amount) + .run(); + + let remaining_stake = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER1_ADDRESS.to_managed_address()) + .returns(ReturnsResult) + .run(); + + let unstake_amount = BigUint::from(500u64); + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unstake(unstake_amount.clone()) + .run(); + + let remaining_stake = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER1_ADDRESS.to_managed_address()) + .returns(ReturnsResult) + .run(); + + let expected_remaining_stake = BigUint::from(2000u64) - &unstake_amount; + assert_eq!(remaining_stake, expected_remaining_stake); + + state + .world + .check_account(RELAYER1_ADDRESS) + .balance(unstake_amount.to_u64().unwrap()); +} + +#[test] +fn test_unstake_more_than_staked_amount() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let unstake_amount = BigUint::from(1_500u64); + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unstake(unstake_amount) + .returns(ExpectError(4, "can't unstake more than amount staked")) + .run(); +} + +#[test] +fn test_unstake_below_required_stake_board_member() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let additional_unstake_amount = BigUint::from(600u64); + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unstake(additional_unstake_amount) + .returns(ExpectError( + 4, + "can't unstake, must keep minimum amount as insurance", + )) + .run(); +} + +#[test] +fn test_unstake_updates_amount_staked_correctly() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let stake_amount_relayer1 = BigUint::from(1_000u64); + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .stake() + .egld(&stake_amount_relayer1) + .run(); + + let stake_amount_relayer2 = BigUint::from(1_000u64); + state + .world + .tx() + .from(RELAYER2_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .stake() + .egld(&stake_amount_relayer2) + .run(); + + let unstake_amount_relayer1 = BigUint::from(200u64); + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unstake(unstake_amount_relayer1.clone()) + .run(); + + let remaining_stake_relayer1 = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER1_ADDRESS.to_managed_address()) + .returns(ReturnsResult) + .run(); + + let expected_remaining_stake_relayer1 = &BigUint::from(2_000u64) - &unstake_amount_relayer1; + assert_eq!(remaining_stake_relayer1, expected_remaining_stake_relayer1); + + let remaining_stake_relayer2 = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER2_ADDRESS.to_managed_address()) + .returns(ReturnsResult) + .run(); + + assert_eq!(remaining_stake_relayer2, BigUint::from(2_000u64)); +} + +// #[test] +// fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { +// let mut state = MultiTransferTestState::new(); + +// // Deploy and configure contracts +// state.deploy_contracts_config(); + +// // Simulate a current transaction batch in EsdtSafe +// let esdt_safe_address = ESDT_SAFE_ADDRESS; +// let token_id = WEGLD_TOKEN_ID; +// let amount = BigUint::from(1_000u64); +// let destination = USER1_ADDRESS.to_managed_address(); +// let nonce = 1u64; + +// // Prepare a transaction to be added to the batch +// let tx = transaction::Transaction { +// token_identifier: TokenIdentifier::from(token_id), +// amount: amount.clone(), +// destination: destination.clone(), +// tx_nonce: nonce, +// call_data: ManagedOption::none(), +// block_nonce: todo!(), +// nonce, +// from: todo!(), +// to: todo!(), +// is_refund_tx: todo!(), +// }; + +// let mut txs = ManagedVec::new(); +// txs.push(tx.clone()); + +// // Add the transaction to EsdtSafe as a new batch +// state +// .world +// .tx() +// .from(MULTISIG_ADDRESS) +// .to(esdt_safe_address) +// .typed(esdt_safe_proxy::EsdtSafeProxy) +// .add_transaction_batch(txs) +// .run(); + +// // Get the current batch ID from EsdtSafe +// let current_batch: OptionalValue> = state +// .world +// .query() +// .to(esdt_safe_address) +// .typed(esdt_safe_proxy::EsdtSafeProxy) +// .get_current_tx_batch() +// .returns(ReturnsResult) +// .run(); + +// let (current_batch_id, _current_batch_transactions) = match current_batch { +// OptionalValue::Some(batch) => batch.into_tuple(), +// OptionalValue::None => panic!("No current batch found in EsdtSafe"), +// }; + +// // Prepare the statuses vector matching the number of transactions +// let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); + +// // Propose setting the statuses +// let action_id: usize = state +// .world +// .tx() +// .from(RELAYER1_ADDRESS) // Board member +// .to(MULTISIG_ADDRESS) +// .typed(multisig_proxy::MultisigProxy) +// .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses.clone()) +// .returns(ReturnsResult) +// .run(); + +// // Verify that an action ID was returned +// assert!(action_id > 0); + +// // Verify that the action was stored in the Multisig contract +// let action: Action = state +// .world +// .query() +// .to(MULTISIG_ADDRESS) +// .typed(multisig_proxy::MultisigProxy) +// .get_action_data(action_id) +// .returns(ReturnsResult) +// .run(); + +// assert!(matches!( +// action, +// Action::SetCurrentTransactionBatchStatus { .. } +// )); +// } + +// #[test] +// fn test_propose_esdt_safe_set_current_transaction_batch_status_already_proposed() { +// let mut state = MultiTransferTestState::new(); + +// // Deploy and configure contracts +// state.deploy_contracts_config(); + +// // Simulate a current transaction batch in EsdtSafe +// let esdt_safe_address = ESDT_SAFE_ADDRESS; +// let token_id = WEGLD_TOKEN_ID; +// let amount = BigUint::from(1_000u64); +// let destination = USER1_ADDRESS.to_managed_address(); +// let nonce = 1u64; + +// let tx = transaction::Transaction { +// token_identifier: TokenIdentifier::from(token_id), +// amount: amount.clone(), +// destination: destination.clone(), +// tx_nonce: nonce, +// call_data: ManagedOption::none(), +// }; + +// let mut txs = ManagedVec::new(); +// txs.push(tx.clone()); + +// state +// .world +// .tx() +// .from(MULTISIG_ADDRESS) +// .to(esdt_safe_address) +// .typed(esdt_safe_proxy::EsdtSafeProxy) +// .add_transaction_batch(txs) +// .run(); + +// let current_batch: OptionalValue> = state +// .world +// .query() +// .to(esdt_safe_address) +// .typed(esdt_safe_proxy::EsdtSafeProxy) +// .get_current_tx_batch() +// .returns(ReturnsResult) +// .run(); + +// let (current_batch_id, _current_batch_transactions) = match current_batch { +// OptionalValue::Some(batch) => batch.into_tuple(), +// OptionalValue::None => panic!("No current batch found in EsdtSafe"), +// }; + +// // Prepare the statuses vector +// let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); + +// // First proposal +// state +// .world +// .tx() +// .from(RELAYER1_ADDRESS) +// .to(MULTISIG_ADDRESS) +// .typed(multisig_proxy::MultisigProxy) +// .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses.clone()) +// .run(); + +// // Attempt to propose the same action again +// state +// .world +// .tx() +// .from(RELAYER2_ADDRESS) +// .to(MULTISIG_ADDRESS) +// .typed(multisig_proxy::MultisigProxy) +// .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses) +// .returns(ExpectError(4, "Action already proposed")) +// .run(); +// } + +// #[test] +// fn test_propose_esdt_safe_set_current_transaction_batch_status_wrong_batch_id() { +// let mut state = MultiTransferTestState::new(); + +// // Deploy and configure contracts +// state.deploy_contracts_config(); + +// // Simulate a current transaction batch in EsdtSafe +// let esdt_safe_address = ESDT_SAFE_ADDRESS; +// let token_id = WEGLD_TOKEN_ID; +// let amount = BigUint::from(1_000u64); +// let destination = USER1_ADDRESS.to_managed_address(); +// let nonce = 1u64; + +// let tx = transaction::Transaction { +// token_identifier: TokenIdentifier::from(token_id), +// amount: amount.clone(), +// destination: destination.clone(), +// tx_nonce: nonce, +// call_data: ManagedOption::none(), +// }; + +// let mut txs = ManagedVec::new(); +// txs.push(tx.clone()); + +// state +// .world +// .tx() +// .from(MULTISIG_ADDRESS) +// .to(esdt_safe_address) +// .typed(esdt_safe_proxy::EsdtSafeProxy) +// .add_transaction_batch(txs) +// .run(); + +// let current_batch: OptionalValue> = state +// .world +// .query() +// .to(esdt_safe_address) +// .typed(esdt_safe_proxy::EsdtSafeProxy) +// .get_current_tx_batch() +// .returns(ReturnsResult) +// .run(); + +// let (current_batch_id, _current_batch_transactions) = match current_batch { +// OptionalValue::Some(batch) => batch.into_tuple(), +// OptionalValue::None => panic!("No current batch found in EsdtSafe"), +// }; + +// // Provide an incorrect batch ID +// let incorrect_batch_id = current_batch_id + 1; + +// let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); + +// // Attempt to propose setting the statuses with incorrect batch ID +// state +// .world +// .tx() +// .from(RELAYER1_ADDRESS) +// .to(MULTISIG_ADDRESS) +// .typed(multisig_proxy::MultisigProxy) +// .propose_esdt_safe_set_current_transaction_batch_status(incorrect_batch_id, statuses) +// .returns(ExpectError( +// 4, +// "Current EsdtSafe tx batch does not have the provided ID", +// )) +// .run(); +// } + +#[test] +fn test_init_supply_from_child_contract_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id = NATIVE_TOKEN_ID; + let amount = BigUint::from(1_000u64); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init_supply_from_child_contract(TokenIdentifier::from(token_id), amount.clone()) + .single_esdt(&TokenIdentifier::from(token_id), 0, &amount.clone()) + .run(); + + state + .world + .check_account(ESDT_SAFE_ADDRESS) + .esdt_balance(token_id, amount.clone()); +} + +#[test] +fn test_add_unprocessed_refund_tx_to_batch_success() { + let mut state = MultiTransferTestState::new(); + + let mut board: MultiValueEncoded> = + MultiValueEncoded::new(); + board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + state + .world + .tx() + .from(OWNER_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init( + ESDT_SAFE_ADDRESS, + MOCK_MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + 1_000u64, + 500u64, + 2usize, + board, + ) + .code(MULTISIG_CODE_PATH) + .new_address(MULTISIG_ADDRESS) + .run(); + + let tx_id = 1u64; + + // state + // .world + // .tx() + // .from(OWNER_ADDRESS) + // .to(MULTISIG_ADDRESS) + // .typed(multisig_proxy::MultisigProxy) + // .add_unprocessed_refund_tx_to_batch(tx_id) + // .run(); +} From 19fbf7e7980697a08ca8f6719d690bb51f56e546 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Mon, 4 Nov 2024 14:20:04 +0200 Subject: [PATCH 03/15] add mock multitransfer and fix test --- multisig/tests/multisig_blackbox_test.rs | 358 +++++++---------------- 1 file changed, 111 insertions(+), 247 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 5b0eec38..578cee8c 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -17,8 +17,8 @@ use multiversx_sc::{ imports::MultiValue2, storage::mappers::SingleValue, types::{ - Address, BigUint, CodeMetadata, EsdtLocalRole, ManagedAddress, ManagedBuffer, - ManagedByteArray, ManagedOption, ManagedType, ManagedVec, MultiValueEncoded, + Address, BigUint, CodeMetadata, EgldOrEsdtTokenIdentifier, EsdtLocalRole, ManagedAddress, + ManagedBuffer, ManagedByteArray, ManagedOption, ManagedType, ManagedVec, MultiValueEncoded, ReturnsNewManagedAddress, ReturnsResult, TestAddress, TestSCAddress, TestTokenIdentifier, TokenIdentifier, }, @@ -145,8 +145,8 @@ impl MultiTransferTestState { .nonce(1); world - .account(MOCK_MULTI_TRANSFER_ADDRESS) - .code(MOCK_MULTI_TRANSFER_PATH_EXPR); + .account(PRICE_AGGREGATOR_ADDRESS) + .code(PRICE_AGGREGATOR_CODE_PATH); Self { world } } @@ -190,6 +190,19 @@ impl MultiTransferTestState { self } + fn mock_multi_transfer_deploy(&mut self) -> &mut Self { + self.world + .tx() + .from(MULTISIG_ADDRESS) + .typed(multi_transfer_esdt_proxy::MultiTransferEsdtProxy) + .init() + .code(MOCK_MULTI_TRANSFER_PATH_EXPR) + .new_address(MOCK_MULTI_TRANSFER_ADDRESS) + .run(); + + self + } + fn bridged_tokens_wrapper_deploy(&mut self) -> &mut Self { self.world .tx() @@ -1136,8 +1149,8 @@ fn test_distribute_fees_from_child_contracts_success() { const PERCENTAGE_TOTAL: u32 = 10000; - let percentage1 = 6000; // 60% - let percentage2 = 4000; // 40% + let percentage1 = 6000; + let percentage2 = 4000; let mut dest_address_percentage_pairs: MultiValueEncoded< StaticApi, @@ -1165,8 +1178,8 @@ fn test_distribute_fees_from_child_contracts_invalid_percentage_sum() { let dest_address1 = USER1_ADDRESS.to_managed_address(); let dest_address2 = USER2_ADDRESS.to_managed_address(); - let percentage1 = 5000; // 50% - let percentage2 = 4000; // 40% + let percentage1 = 5000; + let percentage2 = 4000; let mut dest_address_percentage_pairs: MultiValueEncoded< StaticApi, @@ -1195,8 +1208,8 @@ fn test_distribute_fees_from_child_contracts_with_sc_address() { let dest_address1 = USER1_ADDRESS.to_managed_address(); let dest_address2 = ESDT_SAFE_ADDRESS.to_managed_address(); - let percentage1 = 6000; // 60% - let percentage2 = 4000; // 40% + let percentage1 = 6000; + let percentage2 = 4000; let mut dest_address_percentage_pairs: MultiValueEncoded< StaticApi, @@ -1375,235 +1388,84 @@ fn test_unstake_updates_amount_staked_correctly() { assert_eq!(remaining_stake_relayer2, BigUint::from(2_000u64)); } -// #[test] -// fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { -// let mut state = MultiTransferTestState::new(); - -// // Deploy and configure contracts -// state.deploy_contracts_config(); - -// // Simulate a current transaction batch in EsdtSafe -// let esdt_safe_address = ESDT_SAFE_ADDRESS; -// let token_id = WEGLD_TOKEN_ID; -// let amount = BigUint::from(1_000u64); -// let destination = USER1_ADDRESS.to_managed_address(); -// let nonce = 1u64; - -// // Prepare a transaction to be added to the batch -// let tx = transaction::Transaction { -// token_identifier: TokenIdentifier::from(token_id), -// amount: amount.clone(), -// destination: destination.clone(), -// tx_nonce: nonce, -// call_data: ManagedOption::none(), -// block_nonce: todo!(), -// nonce, -// from: todo!(), -// to: todo!(), -// is_refund_tx: todo!(), -// }; - -// let mut txs = ManagedVec::new(); -// txs.push(tx.clone()); - -// // Add the transaction to EsdtSafe as a new batch -// state -// .world -// .tx() -// .from(MULTISIG_ADDRESS) -// .to(esdt_safe_address) -// .typed(esdt_safe_proxy::EsdtSafeProxy) -// .add_transaction_batch(txs) -// .run(); - -// // Get the current batch ID from EsdtSafe -// let current_batch: OptionalValue> = state -// .world -// .query() -// .to(esdt_safe_address) -// .typed(esdt_safe_proxy::EsdtSafeProxy) -// .get_current_tx_batch() -// .returns(ReturnsResult) -// .run(); - -// let (current_batch_id, _current_batch_transactions) = match current_batch { -// OptionalValue::Some(batch) => batch.into_tuple(), -// OptionalValue::None => panic!("No current batch found in EsdtSafe"), -// }; - -// // Prepare the statuses vector matching the number of transactions -// let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); - -// // Propose setting the statuses -// let action_id: usize = state -// .world -// .tx() -// .from(RELAYER1_ADDRESS) // Board member -// .to(MULTISIG_ADDRESS) -// .typed(multisig_proxy::MultisigProxy) -// .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses.clone()) -// .returns(ReturnsResult) -// .run(); - -// // Verify that an action ID was returned -// assert!(action_id > 0); - -// // Verify that the action was stored in the Multisig contract -// let action: Action = state -// .world -// .query() -// .to(MULTISIG_ADDRESS) -// .typed(multisig_proxy::MultisigProxy) -// .get_action_data(action_id) -// .returns(ReturnsResult) -// .run(); - -// assert!(matches!( -// action, -// Action::SetCurrentTransactionBatchStatus { .. } -// )); -// } - -// #[test] -// fn test_propose_esdt_safe_set_current_transaction_batch_status_already_proposed() { -// let mut state = MultiTransferTestState::new(); - -// // Deploy and configure contracts -// state.deploy_contracts_config(); - -// // Simulate a current transaction batch in EsdtSafe -// let esdt_safe_address = ESDT_SAFE_ADDRESS; -// let token_id = WEGLD_TOKEN_ID; -// let amount = BigUint::from(1_000u64); -// let destination = USER1_ADDRESS.to_managed_address(); -// let nonce = 1u64; - -// let tx = transaction::Transaction { -// token_identifier: TokenIdentifier::from(token_id), -// amount: amount.clone(), -// destination: destination.clone(), -// tx_nonce: nonce, -// call_data: ManagedOption::none(), -// }; - -// let mut txs = ManagedVec::new(); -// txs.push(tx.clone()); - -// state -// .world -// .tx() -// .from(MULTISIG_ADDRESS) -// .to(esdt_safe_address) -// .typed(esdt_safe_proxy::EsdtSafeProxy) -// .add_transaction_batch(txs) -// .run(); - -// let current_batch: OptionalValue> = state -// .world -// .query() -// .to(esdt_safe_address) -// .typed(esdt_safe_proxy::EsdtSafeProxy) -// .get_current_tx_batch() -// .returns(ReturnsResult) -// .run(); - -// let (current_batch_id, _current_batch_transactions) = match current_batch { -// OptionalValue::Some(batch) => batch.into_tuple(), -// OptionalValue::None => panic!("No current batch found in EsdtSafe"), -// }; - -// // Prepare the statuses vector -// let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); - -// // First proposal -// state -// .world -// .tx() -// .from(RELAYER1_ADDRESS) -// .to(MULTISIG_ADDRESS) -// .typed(multisig_proxy::MultisigProxy) -// .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses.clone()) -// .run(); - -// // Attempt to propose the same action again -// state -// .world -// .tx() -// .from(RELAYER2_ADDRESS) -// .to(MULTISIG_ADDRESS) -// .typed(multisig_proxy::MultisigProxy) -// .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses) -// .returns(ExpectError(4, "Action already proposed")) -// .run(); -// } - -// #[test] -// fn test_propose_esdt_safe_set_current_transaction_batch_status_wrong_batch_id() { -// let mut state = MultiTransferTestState::new(); - -// // Deploy and configure contracts -// state.deploy_contracts_config(); - -// // Simulate a current transaction batch in EsdtSafe -// let esdt_safe_address = ESDT_SAFE_ADDRESS; -// let token_id = WEGLD_TOKEN_ID; -// let amount = BigUint::from(1_000u64); -// let destination = USER1_ADDRESS.to_managed_address(); -// let nonce = 1u64; - -// let tx = transaction::Transaction { -// token_identifier: TokenIdentifier::from(token_id), -// amount: amount.clone(), -// destination: destination.clone(), -// tx_nonce: nonce, -// call_data: ManagedOption::none(), -// }; - -// let mut txs = ManagedVec::new(); -// txs.push(tx.clone()); - -// state -// .world -// .tx() -// .from(MULTISIG_ADDRESS) -// .to(esdt_safe_address) -// .typed(esdt_safe_proxy::EsdtSafeProxy) -// .add_transaction_batch(txs) -// .run(); - -// let current_batch: OptionalValue> = state -// .world -// .query() -// .to(esdt_safe_address) -// .typed(esdt_safe_proxy::EsdtSafeProxy) -// .get_current_tx_batch() -// .returns(ReturnsResult) -// .run(); - -// let (current_batch_id, _current_batch_transactions) = match current_batch { -// OptionalValue::Some(batch) => batch.into_tuple(), -// OptionalValue::None => panic!("No current batch found in EsdtSafe"), -// }; - -// // Provide an incorrect batch ID -// let incorrect_batch_id = current_batch_id + 1; - -// let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); - -// // Attempt to propose setting the statuses with incorrect batch ID -// state -// .world -// .tx() -// .from(RELAYER1_ADDRESS) -// .to(MULTISIG_ADDRESS) -// .typed(multisig_proxy::MultisigProxy) -// .propose_esdt_safe_set_current_transaction_batch_status(incorrect_batch_id, statuses) -// .returns(ExpectError( -// 4, -// "Current EsdtSafe tx batch does not have the provided ID", -// )) -// .run(); -// } +#[test] +fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + let token_id = WEGLD_TOKEN_ID; + let amount = BigUint::::from(1_000u64); + let destination = USER1_ADDRESS.to_managed_address::(); + let nonce = 1u64; +} + +#[test] +fn test_propose_esdt_safe_set_current_transaction_batch_status_already_proposed() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + let token_id = WEGLD_TOKEN_ID; + let amount = BigUint::::from(1_000u64); + let destination = USER1_ADDRESS.to_managed_address::(); + let nonce = 1u64; + + let eth_tx = EthTransaction { + from: EthAddress::::zero(), + to: ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + token_id: TokenIdentifier::from(WEGLD_TOKEN_ID), + amount: BigUint::from(1000u64), + tx_nonce: 1u64, + call_data: ManagedOption::none(), + }; +} + +#[test] +fn test_propose_esdt_safe_set_current_transaction_batch_status_wrong_batch_id() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + let token_id = WEGLD_TOKEN_ID; + let amount = BigUint::::from(1_000u64); + let destination = USER1_ADDRESS.to_managed_address::(); + let nonce = 1u64; + + let statuses: MultiValueEncoded = + MultiValueEncoded::from(ManagedVec::from_single_item(TransactionStatus::Pending)); + + // state + // .world + // .tx() + // .from(MULTISIG_ADDRESS) + // .to(ESDT_SAFE_ADDRESS) + // .typed(esdt_safe_proxy::EsdtSafeProxy) + // .create_transaction( + // EthAddress::zero(), + // OptionalValue::None::>, + // ) + // .egld_or_single_esdt( + // &EgldOrEsdtTokenIdentifier::esdt(token_id), + // 0, + // &BigUint::from(amount), + // ) + // .returns(ReturnsResult) + // .run(); + + // state + // .world + // .tx() + // .from(RELAYER1_ADDRESS) + // .to(MULTISIG_ADDRESS) + // .typed(multisig_proxy::MultisigProxy) + // .propose_esdt_safe_set_current_transaction_batch_status(5u64, statuses) + // .returns(ExpectError(4, "Can only propose for next batch ID")) + // .run(); +} #[test] fn test_init_supply_from_child_contract_success() { @@ -1658,14 +1520,16 @@ fn test_add_unprocessed_refund_tx_to_batch_success() { .new_address(MULTISIG_ADDRESS) .run(); + state.mock_multi_transfer_deploy(); + let tx_id = 1u64; - // state - // .world - // .tx() - // .from(OWNER_ADDRESS) - // .to(MULTISIG_ADDRESS) - // .typed(multisig_proxy::MultisigProxy) - // .add_unprocessed_refund_tx_to_batch(tx_id) - // .run(); + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_unprocessed_refund_tx_to_batch(tx_id) + .run(); } From 22a2cd455b32a2901a1a1dde421572125328d3f7 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 5 Nov 2024 01:57:45 +0200 Subject: [PATCH 04/15] more unit tests --- Cargo.lock | 1 + .../src/mock_bridge_proxy.rs | 2 +- .../mock-esdt-safe/src/mock_esdt_safe.rs | 21 +- multisig/Cargo.toml | 3 + ...elayer_call_data_several_tx_test.scen.json | 24 ++ ...o_multiversx_relayer_query2_test.scen.json | 24 ++ ...to_multiversx_relayer_query_test.scen.json | 24 ++ ...sx_tx_batch_ok_call_data_encoded.scen.json | 24 ++ multisig/tests/multisig_blackbox_test.rs | 256 +++++++++++++++++- 9 files changed, 366 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 132040e2..1fb8a517 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -721,6 +721,7 @@ dependencies = [ "eth-address", "fee-estimator-module", "max-bridged-amount-module", + "mock-esdt-safe", "mock-multi-transfer-esdt", "multi-transfer-esdt", "multiversx-price-aggregator-sc", diff --git a/common/mock-contracts/mock-bridge-proxy/src/mock_bridge_proxy.rs b/common/mock-contracts/mock-bridge-proxy/src/mock_bridge_proxy.rs index ebd51f63..55a8bcaa 100644 --- a/common/mock-contracts/mock-bridge-proxy/src/mock_bridge_proxy.rs +++ b/common/mock-contracts/mock-bridge-proxy/src/mock_bridge_proxy.rs @@ -7,7 +7,7 @@ use multiversx_sc::imports::*; #[multiversx_sc::contract] pub trait MockBridgeProxy { #[init] - fn init(&self, _opt_multi_transfer_address: OptionalValue) {} + fn init(&self) {} #[upgrade] fn upgrade(&self) {} diff --git a/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs b/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs index 991fc205..dacd9296 100644 --- a/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs +++ b/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs @@ -16,13 +16,7 @@ pub struct RefundInfo { #[multiversx_sc::contract] pub trait MockEsdtSafe { #[init] - fn init( - &self, - _fee_estimator_contract_address: ManagedAddress, - _multi_transfer_contract_address: ManagedAddress, - _eth_tx_gas_limit: BigUint, - ) { - } + fn init(&self, _eth_tx_gas_limit: BigUint) {} #[upgrade] fn upgrade(&self) {} @@ -35,4 +29,17 @@ pub trait MockEsdtSafe { _opt_refund_info: OptionalValue>, ) { } + + #[only_owner] + #[endpoint(withdrawTransactionFees)] + fn withdraw_transaction_fees( + &self, + _token_id: TokenIdentifier, + _multisig_owner: ManagedAddress, + ) { + } + + #[only_owner] + #[endpoint(withdrawRefundFeesForEthereum)] + fn withdraw_refund_fees_for_ethereum(&self, _token_id: TokenIdentifier) {} } diff --git a/multisig/Cargo.toml b/multisig/Cargo.toml index 504cf8e3..0caa1673 100644 --- a/multisig/Cargo.toml +++ b/multisig/Cargo.toml @@ -29,6 +29,9 @@ path = "../common/max-bridged-amount-module" [dependencies.mock-multi-transfer-esdt] path = "../common/mock-contracts/mock-multi-transfer-esdt" +[dependencies.mock-esdt-safe] +path = "../common/mock-contracts/mock-esdt-safe" + [dependencies.sc-proxies] path = "../common/sc-proxies" diff --git a/multisig/scenarios/ethereum_to_multiversx_relayer_call_data_several_tx_test.scen.json b/multisig/scenarios/ethereum_to_multiversx_relayer_call_data_several_tx_test.scen.json index 1808087b..be6db6b8 100644 --- a/multisig/scenarios/ethereum_to_multiversx_relayer_call_data_several_tx_test.scen.json +++ b/multisig/scenarios/ethereum_to_multiversx_relayer_call_data_several_tx_test.scen.json @@ -181,6 +181,30 @@ "status": "0" } }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "sc:multisig", + "to": "sc:esdt-safe", + "function": "addTokenToWhitelist", + "arguments": [ + "0x45474c442d313233343536", + "0x45474c44", + "0x", + "0x01", + "0x", + "0x", + "0x", + "0x0249f0" + ], + "gasLimit": "5,000,000" + }, + "expect": { + "out": [], + "status": "0" + } + }, { "step": "scCall", "id": "", diff --git a/multisig/scenarios/ethereum_to_multiversx_relayer_query2_test.scen.json b/multisig/scenarios/ethereum_to_multiversx_relayer_query2_test.scen.json index b242dfb2..2ded2f90 100644 --- a/multisig/scenarios/ethereum_to_multiversx_relayer_query2_test.scen.json +++ b/multisig/scenarios/ethereum_to_multiversx_relayer_query2_test.scen.json @@ -181,6 +181,30 @@ "status": "0" } }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "sc:multisig", + "to": "sc:esdt-safe", + "function": "addTokenToWhitelist", + "arguments": [ + "0x45474c442d313233343536", + "0x45474c44", + "0x", + "0x01", + "0x", + "0x", + "0x", + "0x0249f0" + ], + "gasLimit": "5,000,000" + }, + "expect": { + "out": [], + "status": "0" + } + }, { "step": "scCall", "id": "", diff --git a/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json b/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json index 5d68ad84..f4ae7a43 100644 --- a/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json +++ b/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json @@ -181,6 +181,30 @@ "status": "0" } }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "sc:multisig", + "to": "sc:esdt-safe", + "function": "addTokenToWhitelist", + "arguments": [ + "0x45474c442d313233343536", + "0x45474c44", + "0x", + "0x01", + "0x", + "0x", + "0x", + "0x0249f0" + ], + "gasLimit": "5,000,000" + }, + "expect": { + "out": [], + "status": "0" + } + }, { "step": "scCall", "id": "", diff --git a/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json b/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json index 0d4c80ee..0259b4e1 100644 --- a/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json +++ b/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json @@ -181,6 +181,30 @@ "status": "0" } }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "sc:multisig", + "to": "sc:esdt-safe", + "function": "addTokenToWhitelist", + "arguments": [ + "0x45474c442d313233343536", + "0x45474c44", + "0x", + "0x01", + "0x", + "0x", + "0x", + "0x0249f0" + ], + "gasLimit": "5,000,000" + }, + "expect": { + "out": [], + "status": "0" + } + }, { "step": "scCall", "id": "", diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 578cee8c..53b3b98d 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -17,10 +17,10 @@ use multiversx_sc::{ imports::MultiValue2, storage::mappers::SingleValue, types::{ - Address, BigUint, CodeMetadata, EgldOrEsdtTokenIdentifier, EsdtLocalRole, ManagedAddress, - ManagedBuffer, ManagedByteArray, ManagedOption, ManagedType, ManagedVec, MultiValueEncoded, - ReturnsNewManagedAddress, ReturnsResult, TestAddress, TestSCAddress, TestTokenIdentifier, - TokenIdentifier, + Address, BigUint, CodeMetadata, EgldOrEsdtTokenIdentifier, EgldOrMultiEsdtPayment, + EsdtLocalRole, EsdtTokenPayment, ManagedAddress, ManagedBuffer, ManagedByteArray, + ManagedOption, ManagedType, ManagedVec, MultiValueEncoded, ReturnsNewManagedAddress, + ReturnsResult, TestAddress, TestSCAddress, TestTokenIdentifier, TokenIdentifier, }, }; use multiversx_sc_modules::pause::ProxyTrait; @@ -40,7 +40,7 @@ use sc_proxies::{ use token_module::ProxyTrait as _; use transaction::{ transaction_status::TransactionStatus, CallData, EthTransaction, EthTxAsMultiValue, - TxBatchSplitInFields, + Transaction, TxBatchSplitInFields, }; const WEGLD_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("WEGLD-123456"); @@ -58,6 +58,8 @@ const MULTI_TRANSFER_CODE_PATH: MxscPath = const MOCK_MULTI_TRANSFER_PATH_EXPR: MxscPath = MxscPath::new( "../common/mock-contracts/mock-multi-transfer-esdt/output/mock-multi-transfer-esdt.mxsc.json", ); +const MOCK_ESDT_SAFE_PATH_EXPR: MxscPath = + MxscPath::new("../common/mock-contracts/mock-esdt-safe/output/mock-esdt-safe.mxsc.json"); const BRIDGE_PROXY_CODE_PATH: MxscPath = MxscPath::new("../bridge-proxy/output/bridge-proxy.mxsc.json"); const ESDT_SAFE_CODE_PATH: MxscPath = MxscPath::new("../esdt-safe/output/esdt-safe.mxsc.json"); @@ -71,6 +73,7 @@ const MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("multi-transfer const MOCK_MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("mock-multi-transfer"); const BRIDGE_PROXY_ADDRESS: TestSCAddress = TestSCAddress::new("bridge-proxy"); const ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("esdt-safe"); +const MOCK_ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("mock-esdt-safe"); const BRIDGED_TOKENS_WRAPPER_ADDRESS: TestSCAddress = TestSCAddress::new("bridged-tokens-wrapper"); const PRICE_AGGREGATOR_ADDRESS: TestSCAddress = TestSCAddress::new("price-aggregator"); @@ -110,6 +113,7 @@ fn world() -> ScenarioWorld { MOCK_MULTI_TRANSFER_PATH_EXPR, mock_multi_transfer_esdt::ContractBuilder, ); + blockchain.register_contract(MOCK_ESDT_SAFE_PATH_EXPR, mock_esdt_safe::ContractBuilder); blockchain } @@ -240,6 +244,17 @@ impl MultiTransferTestState { .run(); } + fn mock_safe_deploy(&mut self) { + self.world + .tx() + .from(MULTISIG_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .init(ETH_TX_GAS_LIMIT) + .code(MOCK_ESDT_SAFE_PATH_EXPR) + .new_address(MOCK_ESDT_SAFE_ADDRESS) + .run(); + } + fn config_multisig(&mut self) { self.world .tx() @@ -1533,3 +1548,234 @@ fn test_add_unprocessed_refund_tx_to_batch_success() { .add_unprocessed_refund_tx_to_batch(tx_id) .run(); } + +#[test] +fn test_withdraw_slashed_amount_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let slashed_amount = BigUint::from(500u64); + state.world.set_esdt_balance( + MULTISIG_ADDRESS, + WEGLD_TOKEN_ID.as_bytes(), + slashed_amount.clone(), + ); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .slash_board_member(RELAYER1_ADDRESS.to_managed_address()) + .run(); + + let remaining_stake = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER1_ADDRESS.to_managed_address()) + .returns(ReturnsResult) + .run(); + + assert_eq!(remaining_stake, BigUint::from(500u64)); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .withdraw_slashed_amount() + .run(); + + let remaining_slashed_amount = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .slash_amount() + .returns(ReturnsResult) + .run(); + assert_eq!(remaining_slashed_amount, BigUint::from(500u64)); +} + +#[test] +fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + let token_id = WEGLD_TOKEN_ID; + let amount: BigUint = BigUint::from(1_000u64); + let destination: ManagedAddress = USER1_ADDRESS.to_managed_address(); + let nonce = 1u64; + + // let eth_tx = EthTransaction { + // from: EthAddress::::zero(), + // to: ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + // token_id: TokenIdentifier::from(WEGLD_TOKEN_ID), + // amount: BigUint::from(1000u64), + // tx_nonce: 1u64, + // call_data: ManagedOption::none(), + // }; + + // let mut txs = ManagedVec::new(); + // txs.push(eth_tx.clone()); + + // state + // .world + // .tx() + // .from(MULTISIG_ADDRESS) + // .to(esdt_safe_address) + // .typed(esdt_safe_proxy::EsdtSafeProxy) + // .set_transaction_batch_status(1, statuses.clone()) + // .run(); + + // let current_batch: OptionalValue> = state + // .world + // .query() + // .to(esdt_safe_address) + // .typed(esdt_safe_proxy::EsdtSafeProxy) + // .get_current_tx_batch() + // .returns(ReturnsResult) + // .run(); + + // let (current_batch_id, _current_batch_transactions) = match current_batch { + // OptionalValue::Some(batch) => batch.into_tuple(), + // OptionalValue::None => panic!("No current batch found in EsdtSafe"), + // }; + + // let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); + + // let action_id: usize = state + // .world + // .tx() + // .from(RELAYER1_ADDRESS) + // .to(MULTISIG_ADDRESS) + // .typed(multisig_proxy::MultisigProxy) + // .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses.clone()) + // .returns(ReturnsResult) + // .run(); + + // state + // .world + // .tx() + // .from(RELAYER2_ADDRESS) // Another board member + // .to(MULTISIG_ADDRESS) + // .typed(multisig_proxy::MultisigProxy) + // .sign(action_id) + // .run(); + + state + .world + .tx() + .from(RELAYER1_ADDRESS) // Board member + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .perform_action_endpoint(1usize) + .run(); + + // let action_exists = state + // .world + // .query() + // .to(MULTISIG_ADDRESS) + // .typed(multisig_proxy::MultisigProxy) + // .action_exists(action_id) + // .returns(ReturnsResult) + // .run(); + + // assert!(!action_exists); + + // let batch_statuses: MultiValueEncoded = state + // .world + // .query() + // .to(esdt_safe_address) + // .typed(esdt_safe_proxy::EsdtSafeProxy) + // .get_transaction_batch_status(current_batch_id) + // .returns(ReturnsResult) + // .run(); + + // assert_eq!(batch_statuses.to_vec(), statuses.to_vec()); +} + +#[test] +fn test_withdraw_refund_fees_for_ethereum_success() { + let mut state = MultiTransferTestState::new(); + + let mut board: MultiValueEncoded> = + MultiValueEncoded::new(); + board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + state + .world + .tx() + .from(OWNER_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init( + MOCK_ESDT_SAFE_ADDRESS, + MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + 1_000u64, + 500u64, + 2usize, + board, + ) + .code(MULTISIG_CODE_PATH) + .new_address(MULTISIG_ADDRESS) + .run(); + state.mock_safe_deploy(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .withdraw_refund_fees_for_ethereum(TokenIdentifier::from(WEGLD_TOKEN_ID)) + .run(); +} + +#[test] +fn test_withdraw_transaction_fees_success() { + let mut state = MultiTransferTestState::new(); + + let mut board: MultiValueEncoded> = + MultiValueEncoded::new(); + board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + state + .world + .tx() + .from(OWNER_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init( + MOCK_ESDT_SAFE_ADDRESS, + MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + 1_000u64, + 500u64, + 2usize, + board, + ) + .code(MULTISIG_CODE_PATH) + .new_address(MULTISIG_ADDRESS) + .run(); + state.mock_safe_deploy(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .withdraw_transaction_fees(TokenIdentifier::from(WEGLD_TOKEN_ID)) + .run(); +} From 8630423b87152d6ca6e5b95f3981da74c51bd94a Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 5 Nov 2024 14:34:30 +0200 Subject: [PATCH 05/15] add setup unit tests --- multisig/tests/multisig_blackbox_test.rs | 869 ++++++++++++++++++++++- 1 file changed, 865 insertions(+), 4 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 53b3b98d..676115c5 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -1416,7 +1416,7 @@ fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { let nonce = 1u64; } -#[test] +#[test] //TODO: Implement this test fn test_propose_esdt_safe_set_current_transaction_batch_status_already_proposed() { let mut state = MultiTransferTestState::new(); @@ -1438,7 +1438,7 @@ fn test_propose_esdt_safe_set_current_transaction_batch_status_already_proposed( }; } -#[test] +#[test] //TODO: Fix this test fn test_propose_esdt_safe_set_current_transaction_batch_status_wrong_batch_id() { let mut state = MultiTransferTestState::new(); @@ -1602,7 +1602,7 @@ fn test_withdraw_slashed_amount_success() { assert_eq!(remaining_slashed_amount, BigUint::from(500u64)); } -#[test] +#[test] // TODO: Fix this test fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { let mut state = MultiTransferTestState::new(); @@ -1702,7 +1702,7 @@ fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { // assert_eq!(batch_statuses.to_vec(), statuses.to_vec()); } -#[test] +#[test] // TODO: Fix this test fn test_withdraw_refund_fees_for_ethereum_success() { let mut state = MultiTransferTestState::new(); @@ -1779,3 +1779,864 @@ fn test_withdraw_transaction_fees_success() { .withdraw_transaction_fees(TokenIdentifier::from(WEGLD_TOKEN_ID)) .run(); } + +#[test] +fn test_upgrade_child_contract_from_source_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let child_sc_address = MULTI_TRANSFER_ADDRESS.to_managed_address(); + + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .typed(multi_transfer_esdt_proxy::MultiTransferEsdtProxy) + .init() + .code(MOCK_MULTI_TRANSFER_PATH_EXPR) + .new_address(MOCK_MULTI_TRANSFER_ADDRESS) + .run(); + + let init_args = MultiValueEncoded::new(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .upgrade_child_contract_from_source( + child_sc_address.clone(), + MOCK_MULTI_TRANSFER_ADDRESS.clone(), + false, + init_args.clone(), + ) + .run(); + + // how to check ? +} + +#[test] +fn test_add_board_member_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let num_board_members = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert_eq!(num_board_members, 2); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_board_member_endpoint(USER1_ADDRESS.to_managed_address()) + .run(); + + let num_board_members_after = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert_eq!(num_board_members_after, 3); +} + +#[test] +fn test_remove_user_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_board_member_endpoint(USER1_ADDRESS.to_managed_address()) + .run(); + + let num_board_members = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert_eq!(num_board_members, 3); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .remove_user(USER1_ADDRESS.to_managed_address()) + .run(); + + let num_board_members = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert_eq!(num_board_members, 2); +} + +#[test] +fn test_remove_user_cannot_remove_all() { + let mut state = MultiTransferTestState::new(); + + let mut board: MultiValueEncoded> = + MultiValueEncoded::new(); + board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + state + .world + .tx() + .from(OWNER_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init( + ESDT_SAFE_ADDRESS, + MULTI_TRANSFER_ADDRESS, + BRIDGE_PROXY_ADDRESS, + BRIDGED_TOKENS_WRAPPER_ADDRESS, + PRICE_AGGREGATOR_ADDRESS, + 1_000u64, + 500u64, + 1usize, + board, + ) + .code(MULTISIG_CODE_PATH) + .new_address(MULTISIG_ADDRESS) + .run(); + state.safe_deploy(); + state.multi_transfer_deploy(); + state.bridge_proxy_deploy(); + state.bridged_tokens_wrapper_deploy(); + state.config_multisig(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .remove_user(RELAYER1_ADDRESS.to_managed_address()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .remove_user(RELAYER2_ADDRESS.to_managed_address()) + .returns(ExpectError(4u64, "cannot remove all board members")) + .run(); +} + +#[test] +fn test_remove_user_quorum_exceed_board_size() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let num_board_members = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert_eq!(num_board_members, 2); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .remove_user(RELAYER1_ADDRESS.to_managed_address()) + .returns(ExpectError(4u64, "quorum cannot exceed board size")) + .run(); + + let num_board_members_after = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert_eq!(num_board_members_after, 2); +} + +#[test] +fn test_change_quorum_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let initial_num_board_members: usize = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .num_board_members() + .returns(ReturnsResult) + .run(); + + assert!(initial_num_board_members >= 1); + + let new_quorum = 1usize; + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .change_quorum(new_quorum) + .run(); + + let updated_quorum: usize = state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .quorum() + .returns(ReturnsResult) + .run(); + + assert_eq!(updated_quorum, new_quorum); +} + +#[test] +fn test_add_mapping_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id = TokenIdentifier::from(WEGLD_TOKEN_ID); + + let erc20_address = EthAddress::zero(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address.clone(), token_id.clone()) + .run(); + + state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .erc20_address_for_token_id(token_id.clone()) + .returns(ExpectValue(erc20_address.clone())) + .run(); + + state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .token_id_for_erc20_address(erc20_address.clone()) + .returns(ExpectValue(token_id.clone())) + .run(); +} + +#[test] +fn test_add_mapping_token_id_already_mapped() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id = TokenIdentifier::from(WEGLD_TOKEN_ID); + + let erc20_address = EthAddress::zero(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address.clone(), token_id.clone()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address.clone(), token_id.clone()) + .returns(ExpectError(4u64, "Mapping already exists for token ID")) + .run(); +} + +#[test] +fn test_add_mapping_erc20_address_already_mapped() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id = TokenIdentifier::from(WEGLD_TOKEN_ID); + + let erc20_address = EthAddress::zero(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address.clone(), token_id.clone()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address.clone(), TokenIdentifier::from(ETH_TOKEN_ID)) + .returns(ExpectError(4u64, "Mapping already exists for ERC20 token")) + .run(); +} + +#[test] +fn test_clear_mapping_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id = TokenIdentifier::from(WEGLD_TOKEN_ID); + + let erc20_address = EthAddress::zero(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address.clone(), token_id.clone()) + .run(); + state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .erc20_address_for_token_id(token_id.clone()) + .returns(ExpectValue(erc20_address.clone())) + .run(); + + state + .world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .token_id_for_erc20_address(erc20_address.clone()) + .returns(ExpectValue(token_id.clone())) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .clear_mapping(erc20_address.clone(), token_id.clone()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .clear_mapping(erc20_address.clone(), token_id.clone()) + .returns(ExpectError(4u64, "Mapping does not exist for ERC20 token")) + .run(); + + let erc20_address2 = EthAddress { + raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080900"), + }; + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address2, token_id.clone()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .clear_mapping(erc20_address.clone(), token_id.clone()) + .returns(ExpectError(4u64, "Mapping does not exist for token id")) + .run(); +} + +#[test] +fn test_clear_mapping_invalid_mapping() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id1 = TokenIdentifier::from(WEGLD_TOKEN_ID); + let token_id2 = TokenIdentifier::from("OTHER-123456"); + + let erc20_address1 = EthAddress { + raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), + }; + + let erc20_address2 = EthAddress { + raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080900"), + }; + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address1.clone(), token_id1.clone()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .add_mapping(erc20_address2.clone(), token_id2.clone()) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .clear_mapping(erc20_address1.clone(), token_id2.clone()) + .returns(ExpectError(4, "Invalid mapping")) + .run(); +} + +#[test] +fn test_pause_unpause_esdt_safe() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let is_paused_before: bool = state + .world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .paused_status() + .returns(ReturnsResult) + .run(); + + assert!(!is_paused_before); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .pause_esdt_safe() + .run(); + + let is_paused_after: bool = state + .world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .paused_status() + .returns(ReturnsResult) + .run(); + + assert!(is_paused_after); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unpause_esdt_safe() + .run(); + + let is_paused_after_unpause: bool = state + .world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .paused_status() + .returns(ReturnsResult) + .run(); + + assert!(!is_paused_after_unpause); +} + +#[test] +fn test_init_supply_functions_success() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let amount = BigUint::from(100u64); + + state + .world + .set_esdt_balance(OWNER_ADDRESS, NATIVE_TOKEN_ID.as_bytes(), amount.clone()); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init_supply_esdt_safe(NATIVE_TOKEN_ID.clone(), amount.clone()) + .single_esdt(&TokenIdentifier::from(NATIVE_TOKEN_ID), 0, &amount) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init_supply_mint_burn_esdt_safe(ETH_TOKEN_ID.clone(), amount.clone(), amount.clone()) + .run(); +} + +#[test] +fn test_pause_unpause_proxy() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let is_paused_before: bool = state + .world + .query() + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .paused_status() + .returns(ReturnsResult) + .run(); + + assert!(!is_paused_before); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .pause_proxy() + .run(); + + let is_paused_after: bool = state + .world + .query() + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .paused_status() + .returns(ReturnsResult) + .run(); + + assert!(is_paused_after); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unpause_proxy() + .run(); + + let is_paused_after_unpause: bool = state + .world + .query() + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .paused_status() + .returns(ReturnsResult) + .run(); + + assert!(!is_paused_after_unpause); +} + +#[test] +fn test_change_esdt_safe_settings() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + + let new_gas_limit = BigUint::from(5_000_000u64); + let token_id = TokenIdentifier::from(WEGLD_TOKEN_ID); + let new_price_per_gas_unit = BigUint::from(100u64); + let new_ticker = ManagedBuffer::from("WEGLD"); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .change_multiversx_to_eth_gas_limit(new_gas_limit.clone()) + .run(); + + let updated_gas_limit = state + .world + .query() + .to(esdt_safe_address) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .eth_tx_gas_limit() + .returns(ReturnsResult) + .run(); + + assert_eq!(updated_gas_limit, new_gas_limit); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .change_default_price_per_gas_unit(token_id.clone(), new_price_per_gas_unit.clone()) + .run(); + + let updated_price_per_gas_unit = state + .world + .query() + .to(esdt_safe_address) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .default_price_per_gas_unit(token_id.clone()) + .returns(ReturnsResult) + .run(); + + assert_eq!(updated_price_per_gas_unit, new_price_per_gas_unit); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .change_token_ticker(token_id.clone(), new_ticker.clone()) + .run(); +} + +#[test] +fn test_esdt_safe_whitelist_management() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + + let token_id = TokenIdentifier::from("TEST-123456"); + let ticker = ManagedBuffer::from("TEST"); + let mint_burn_allowed = true; + let is_native_token = false; + let total_balance = BigUint::from(0u64); + let mint_balance = BigUint::from(500_000u64); + let burn_balance = BigUint::from(200_000u64); + let opt_default_price_per_gas_unit = OptionalValue::Some(BigUint::from(100u64)); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_add_token_to_whitelist( + &token_id, + ticker.clone(), + mint_burn_allowed, + is_native_token, + &total_balance, + &mint_balance, + &burn_balance, + opt_default_price_per_gas_unit.clone(), + ) + .run(); + + let tokens_whitelisted = state + .world + .query() + .to(esdt_safe_address) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .token_whitelist() + .returns(ReturnsResult) + .run(); + + assert!(tokens_whitelisted.to_vec().contains(&token_id)); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_remove_token_from_whitelist(token_id.clone()) + .run(); + + let tokens_whitelisted_after = state + .world + .query() + .to(esdt_safe_address) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .token_whitelist() + .returns(ReturnsResult) + .run(); + + assert!(!tokens_whitelisted_after.to_vec().contains(&token_id)); +} + +#[test] +fn test_esdt_safe_settings_management() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let esdt_safe_address = ESDT_SAFE_ADDRESS; + + let new_max_tx_batch_size = 100usize; + let new_max_tx_batch_block_duration = 600u64; // e.g., 600 blocks + let token_id = TokenIdentifier::from("TEST-123456"); + let max_bridged_amount = BigUint::from(1_000_000u64); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_set_max_tx_batch_size(new_max_tx_batch_size) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_set_max_tx_batch_block_duration(new_max_tx_batch_block_duration) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_set_max_bridged_amount_for_token(token_id.clone(), max_bridged_amount.clone()) + .run(); + + let updated_max_bridged_amount = state + .world + .query() + .to(esdt_safe_address) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .max_bridged_amount(token_id.clone()) + .returns(ReturnsResult) + .run(); + + assert_eq!(updated_max_bridged_amount, max_bridged_amount); +} + +#[test] +fn test_multi_transfer_esdt_settings_management() { + let mut state = MultiTransferTestState::new(); + + state.deploy_contracts_config(); + + let token_id = TokenIdentifier::from("TEST-123456"); + let max_bridged_amount = BigUint::from(1_000_000u64); + let new_max_refund_tx_batch_size = 100usize; + let new_max_refund_tx_batch_block_duration = 600u64; + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .multi_transfer_esdt_set_max_bridged_amount_for_token( + token_id.clone(), + max_bridged_amount.clone(), + ) + .run(); + + let updated_max_bridged_amount = state + .world + .query() + .to(MULTI_TRANSFER_ADDRESS) + .typed(multi_transfer_esdt_proxy::MultiTransferEsdtProxy) + .max_bridged_amount(token_id.clone()) + .returns(ReturnsResult) + .run(); + + assert_eq!(updated_max_bridged_amount, max_bridged_amount); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .multi_transfer_esdt_set_max_refund_tx_batch_size(new_max_refund_tx_batch_size) + .run(); + + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .multi_transfer_esdt_set_max_refund_tx_batch_block_duration( + new_max_refund_tx_batch_block_duration, + ) + .run(); +} From 3298ce77a3ebbe5f739dea60dc9cb9651e760a8d Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 5 Nov 2024 14:53:32 +0200 Subject: [PATCH 06/15] 2 lines coverage --- multisig/tests/multisig_blackbox_test.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 676115c5..7ea80cfc 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -1814,6 +1814,20 @@ fn test_upgrade_child_contract_from_source_success() { ) .run(); + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .upgrade_child_contract_from_source( + child_sc_address.clone(), + MOCK_MULTI_TRANSFER_ADDRESS.clone(), + true, + init_args.clone(), + ) + .run(); + // how to check ? } From 7b2595cc60ab1ee3f88701cf7786cb45c5ae858b Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 5 Nov 2024 15:05:56 +0200 Subject: [PATCH 07/15] remove comm --- multisig/tests/multisig_blackbox_test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 7ea80cfc..b0a51056 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -1664,7 +1664,7 @@ fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { // state // .world // .tx() - // .from(RELAYER2_ADDRESS) // Another board member + // .from(RELAYER2_ADDRESS) // .to(MULTISIG_ADDRESS) // .typed(multisig_proxy::MultisigProxy) // .sign(action_id) @@ -1673,7 +1673,7 @@ fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { state .world .tx() - .from(RELAYER1_ADDRESS) // Board member + .from(RELAYER1_ADDRESS) .to(MULTISIG_ADDRESS) .typed(multisig_proxy::MultisigProxy) .perform_action_endpoint(1usize) @@ -2557,7 +2557,7 @@ fn test_esdt_safe_settings_management() { let esdt_safe_address = ESDT_SAFE_ADDRESS; let new_max_tx_batch_size = 100usize; - let new_max_tx_batch_block_duration = 600u64; // e.g., 600 blocks + let new_max_tx_batch_block_duration = 600u64; let token_id = TokenIdentifier::from("TEST-123456"); let max_bridged_amount = BigUint::from(1_000_000u64); From e2f2cc736f1c0b848773392e784a820a9fc13c21 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 5 Nov 2024 15:14:38 +0200 Subject: [PATCH 08/15] clippy fix --- multisig/tests/multisig_blackbox_test.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index b0a51056..baaec905 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -1808,7 +1808,7 @@ fn test_upgrade_child_contract_from_source_success() { .typed(multisig_proxy::MultisigProxy) .upgrade_child_contract_from_source( child_sc_address.clone(), - MOCK_MULTI_TRANSFER_ADDRESS.clone(), + MOCK_MULTI_TRANSFER_ADDRESS, false, init_args.clone(), ) @@ -1822,7 +1822,7 @@ fn test_upgrade_child_contract_from_source_success() { .typed(multisig_proxy::MultisigProxy) .upgrade_child_contract_from_source( child_sc_address.clone(), - MOCK_MULTI_TRANSFER_ADDRESS.clone(), + MOCK_MULTI_TRANSFER_ADDRESS, true, init_args.clone(), ) @@ -2346,7 +2346,7 @@ fn test_init_supply_functions_success() { .from(OWNER_ADDRESS) .to(MULTISIG_ADDRESS) .typed(multisig_proxy::MultisigProxy) - .init_supply_esdt_safe(NATIVE_TOKEN_ID.clone(), amount.clone()) + .init_supply_esdt_safe(NATIVE_TOKEN_ID, amount.clone()) .single_esdt(&TokenIdentifier::from(NATIVE_TOKEN_ID), 0, &amount) .run(); @@ -2356,7 +2356,7 @@ fn test_init_supply_functions_success() { .from(OWNER_ADDRESS) .to(MULTISIG_ADDRESS) .typed(multisig_proxy::MultisigProxy) - .init_supply_mint_burn_esdt_safe(ETH_TOKEN_ID.clone(), amount.clone(), amount.clone()) + .init_supply_mint_burn_esdt_safe(ETH_TOKEN_ID, amount.clone(), amount.clone()) .run(); } @@ -2557,7 +2557,7 @@ fn test_esdt_safe_settings_management() { let esdt_safe_address = ESDT_SAFE_ADDRESS; let new_max_tx_batch_size = 100usize; - let new_max_tx_batch_block_duration = 600u64; + let new_max_tx_batch_block_duration = 600u64; let token_id = TokenIdentifier::from("TEST-123456"); let max_bridged_amount = BigUint::from(1_000_000u64); From 5160b179b0ccd3b4244045116d7d59dacb3b8de7 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 5 Nov 2024 15:22:46 +0200 Subject: [PATCH 09/15] change mock esdt safe signature function --- common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs b/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs index dacd9296..1c5a8e3a 100644 --- a/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs +++ b/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs @@ -41,5 +41,10 @@ pub trait MockEsdtSafe { #[only_owner] #[endpoint(withdrawRefundFeesForEthereum)] - fn withdraw_refund_fees_for_ethereum(&self, _token_id: TokenIdentifier) {} + fn withdraw_refund_fees_for_ethereum( + &self, + _token_id: TokenIdentifier, + _multisig_owner: ManagedAddress, + ) { + } } From 6fc1722aa19025384956384431164f452a926e24 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Thu, 7 Nov 2024 23:58:44 +0200 Subject: [PATCH 10/15] solved todo's --- Cargo.lock | 1 + multisig/Cargo.toml | 3 + multisig/tests/multisig_blackbox_test.rs | 276 ++++++++++++----------- 3 files changed, 151 insertions(+), 129 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1fb8a517..b51a8060 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -723,6 +723,7 @@ dependencies = [ "max-bridged-amount-module", "mock-esdt-safe", "mock-multi-transfer-esdt", + "mock-price-aggregator", "multi-transfer-esdt", "multiversx-price-aggregator-sc", "multiversx-sc", diff --git a/multisig/Cargo.toml b/multisig/Cargo.toml index 0caa1673..72a7fee8 100644 --- a/multisig/Cargo.toml +++ b/multisig/Cargo.toml @@ -32,6 +32,9 @@ path = "../common/mock-contracts/mock-multi-transfer-esdt" [dependencies.mock-esdt-safe] path = "../common/mock-contracts/mock-esdt-safe" +[dependencies.mock-price-aggregator] +path = "../common/mock-contracts/mock-price-aggregator" + [dependencies.sc-proxies] path = "../common/sc-proxies" diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index baaec905..4f49817f 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -42,6 +42,7 @@ use transaction::{ transaction_status::TransactionStatus, CallData, EthTransaction, EthTxAsMultiValue, Transaction, TxBatchSplitInFields, }; +use tx_batch_module::BatchStatus; const WEGLD_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("WEGLD-123456"); const ETH_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("ETH-123456"); @@ -66,7 +67,7 @@ const ESDT_SAFE_CODE_PATH: MxscPath = MxscPath::new("../esdt-safe/output/esdt-sa const BRIDGED_TOKENS_WRAPPER_CODE_PATH: MxscPath = MxscPath::new("../bridged-tokens-wrapper/output/bridged-tokens-wrapper.mxsc.json"); const PRICE_AGGREGATOR_CODE_PATH: MxscPath = - MxscPath::new("../price-aggregator/price-aggregator.mxsc.json"); + MxscPath::new("../price-aggregator/multiversx-price-aggregator.mxsc.json"); const MULTISIG_ADDRESS: TestSCAddress = TestSCAddress::new("multisig"); const MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("multi-transfer"); @@ -107,7 +108,7 @@ fn world() -> ScenarioWorld { ); blockchain.register_contract( PRICE_AGGREGATOR_CODE_PATH, - fee_estimator_module::ContractBuilder, + mock_price_aggregator::ContractBuilder, ); blockchain.register_contract( MOCK_MULTI_TRANSFER_PATH_EXPR, @@ -150,6 +151,7 @@ impl MultiTransferTestState { world .account(PRICE_AGGREGATOR_ADDRESS) + .nonce(1) .code(PRICE_AGGREGATOR_CODE_PATH); Self { world } @@ -1403,7 +1405,7 @@ fn test_unstake_updates_amount_staked_correctly() { assert_eq!(remaining_stake_relayer2, BigUint::from(2_000u64)); } -#[test] +#[test] //TODO: Fix this test fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { let mut state = MultiTransferTestState::new(); @@ -1414,32 +1416,88 @@ fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { let amount = BigUint::::from(1_000u64); let destination = USER1_ADDRESS.to_managed_address::(); let nonce = 1u64; -} + let statuses: MultiValueEncoded = + MultiValueEncoded::from(ManagedVec::from_single_item(TransactionStatus::Pending)); + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .set_default_price_per_gas_unit(token_id, 0u64) + .run(); -#[test] //TODO: Implement this test -fn test_propose_esdt_safe_set_current_transaction_batch_status_already_proposed() { - let mut state = MultiTransferTestState::new(); + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .init_supply_mint_burn(token_id, BigUint::from(100_000u64), BigUint::zero()) + .run(); - state.deploy_contracts_config(); + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_set_max_tx_batch_size(1u32) + .run(); - let esdt_safe_address = ESDT_SAFE_ADDRESS; - let token_id = WEGLD_TOKEN_ID; - let amount = BigUint::::from(1_000u64); - let destination = USER1_ADDRESS.to_managed_address::(); - let nonce = 1u64; + state + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .esdt_safe_set_max_tx_batch_block_duration(1u32) + .run(); - let eth_tx = EthTransaction { - from: EthAddress::::zero(), - to: ManagedAddress::from(USER1_ADDRESS.eval_to_array()), - token_id: TokenIdentifier::from(WEGLD_TOKEN_ID), - amount: BigUint::from(1000u64), - tx_nonce: 1u64, - call_data: ManagedOption::none(), - }; + for i in 0..15 { + println!("i: {}", i); + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .create_transaction( + EthAddress::zero(), + OptionalValue::None::>, + ) + .egld_or_single_esdt( + &EgldOrEsdtTokenIdentifier::esdt(token_id), + 0, + &BigUint::from(amount.clone()), + ) + .returns(ReturnsResult) + .run(); + } + + let result = state + .world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .get_current_tx_batch() + .returns(ReturnsResult) + .run(); + + println!("{:?}", result); + + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .propose_esdt_safe_set_current_transaction_batch_status(1u64, statuses) + .run(); } -#[test] //TODO: Fix this test -fn test_propose_esdt_safe_set_current_transaction_batch_status_wrong_batch_id() { +#[test] +fn test_propose_esdt_safe_set_current_transaction_batch_status_batch_empty() { let mut state = MultiTransferTestState::new(); state.deploy_contracts_config(); @@ -1447,39 +1505,19 @@ fn test_propose_esdt_safe_set_current_transaction_batch_status_wrong_batch_id() let esdt_safe_address = ESDT_SAFE_ADDRESS; let token_id = WEGLD_TOKEN_ID; let amount = BigUint::::from(1_000u64); - let destination = USER1_ADDRESS.to_managed_address::(); - let nonce = 1u64; let statuses: MultiValueEncoded = MultiValueEncoded::from(ManagedVec::from_single_item(TransactionStatus::Pending)); - // state - // .world - // .tx() - // .from(MULTISIG_ADDRESS) - // .to(ESDT_SAFE_ADDRESS) - // .typed(esdt_safe_proxy::EsdtSafeProxy) - // .create_transaction( - // EthAddress::zero(), - // OptionalValue::None::>, - // ) - // .egld_or_single_esdt( - // &EgldOrEsdtTokenIdentifier::esdt(token_id), - // 0, - // &BigUint::from(amount), - // ) - // .returns(ReturnsResult) - // .run(); - - // state - // .world - // .tx() - // .from(RELAYER1_ADDRESS) - // .to(MULTISIG_ADDRESS) - // .typed(multisig_proxy::MultisigProxy) - // .propose_esdt_safe_set_current_transaction_batch_status(5u64, statuses) - // .returns(ExpectError(4, "Can only propose for next batch ID")) - // .run(); + state + .world + .tx() + .from(RELAYER1_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .propose_esdt_safe_set_current_transaction_batch_status(1u64, statuses) + .returns(ExpectError(4, "Current batch is empty")) + .run(); } #[test] @@ -1614,61 +1652,53 @@ fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { let destination: ManagedAddress = USER1_ADDRESS.to_managed_address(); let nonce = 1u64; - // let eth_tx = EthTransaction { - // from: EthAddress::::zero(), - // to: ManagedAddress::from(USER1_ADDRESS.eval_to_array()), - // token_id: TokenIdentifier::from(WEGLD_TOKEN_ID), - // amount: BigUint::from(1000u64), - // tx_nonce: 1u64, - // call_data: ManagedOption::none(), - // }; - - // let mut txs = ManagedVec::new(); - // txs.push(eth_tx.clone()); - - // state - // .world - // .tx() - // .from(MULTISIG_ADDRESS) - // .to(esdt_safe_address) - // .typed(esdt_safe_proxy::EsdtSafeProxy) - // .set_transaction_batch_status(1, statuses.clone()) - // .run(); - - // let current_batch: OptionalValue> = state - // .world - // .query() - // .to(esdt_safe_address) - // .typed(esdt_safe_proxy::EsdtSafeProxy) - // .get_current_tx_batch() - // .returns(ReturnsResult) - // .run(); - - // let (current_batch_id, _current_batch_transactions) = match current_batch { - // OptionalValue::Some(batch) => batch.into_tuple(), - // OptionalValue::None => panic!("No current batch found in EsdtSafe"), - // }; - - // let statuses = MultiValueEncoded::from(vec![TransactionStatus::Success]); - - // let action_id: usize = state - // .world - // .tx() - // .from(RELAYER1_ADDRESS) - // .to(MULTISIG_ADDRESS) - // .typed(multisig_proxy::MultisigProxy) - // .propose_esdt_safe_set_current_transaction_batch_status(current_batch_id, statuses.clone()) - // .returns(ReturnsResult) - // .run(); - - // state - // .world - // .tx() - // .from(RELAYER2_ADDRESS) - // .to(MULTISIG_ADDRESS) - // .typed(multisig_proxy::MultisigProxy) - // .sign(action_id) - // .run(); + let mut tx_statuses = MultiValueEncoded::::new(); + tx_statuses.push(TransactionStatus::Executed); + + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .set_default_price_per_gas_unit(token_id, 0u64) + .run(); + + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .init_supply_mint_burn(token_id, BigUint::from(100_000u64), BigUint::zero()) + .run(); + + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .create_transaction( + EthAddress::zero(), + OptionalValue::None::>, + ) + .egld_or_single_esdt( + &EgldOrEsdtTokenIdentifier::esdt(token_id), + 0, + &BigUint::from(amount.clone()), + ) + .returns(ReturnsResult) + .run(); + + state + .world + .tx() + .from(MULTISIG_ADDRESS) + .to(esdt_safe_address) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .set_transaction_batch_status(1u64, tx_statuses.clone()) + .run(); state .world @@ -1679,30 +1709,18 @@ fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { .perform_action_endpoint(1usize) .run(); - // let action_exists = state - // .world - // .query() - // .to(MULTISIG_ADDRESS) - // .typed(multisig_proxy::MultisigProxy) - // .action_exists(action_id) - // .returns(ReturnsResult) - // .run(); - - // assert!(!action_exists); - - // let batch_statuses: MultiValueEncoded = state - // .world - // .query() - // .to(esdt_safe_address) - // .typed(esdt_safe_proxy::EsdtSafeProxy) - // .get_transaction_batch_status(current_batch_id) - // .returns(ReturnsResult) - // .run(); - - // assert_eq!(batch_statuses.to_vec(), statuses.to_vec()); + let result = state + .world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .get_batch_status(1u64) + .returns(ReturnsResult) + .run(); + assert_eq!(result, BatchStatus::AlreadyProcessed); } -#[test] // TODO: Fix this test +#[test] fn test_withdraw_refund_fees_for_ethereum_success() { let mut state = MultiTransferTestState::new(); From 416e610f7442c007420b9eaa523a58d7ec9e14ab Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Fri, 8 Nov 2024 11:29:30 +0200 Subject: [PATCH 11/15] remove comments & unachievable tests --- multisig/tests/multisig_blackbox_test.rs | 173 ----------------------- 1 file changed, 173 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 4f49817f..ee2fb0bd 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -1405,97 +1405,6 @@ fn test_unstake_updates_amount_staked_correctly() { assert_eq!(remaining_stake_relayer2, BigUint::from(2_000u64)); } -#[test] //TODO: Fix this test -fn test_propose_esdt_safe_set_current_transaction_batch_status_success() { - let mut state = MultiTransferTestState::new(); - - state.deploy_contracts_config(); - - let esdt_safe_address = ESDT_SAFE_ADDRESS; - let token_id = WEGLD_TOKEN_ID; - let amount = BigUint::::from(1_000u64); - let destination = USER1_ADDRESS.to_managed_address::(); - let nonce = 1u64; - let statuses: MultiValueEncoded = - MultiValueEncoded::from(ManagedVec::from_single_item(TransactionStatus::Pending)); - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .set_default_price_per_gas_unit(token_id, 0u64) - .run(); - - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .init_supply_mint_burn(token_id, BigUint::from(100_000u64), BigUint::zero()) - .run(); - - state - .world - .tx() - .from(OWNER_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .esdt_safe_set_max_tx_batch_size(1u32) - .run(); - - state - .world - .tx() - .from(OWNER_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .esdt_safe_set_max_tx_batch_block_duration(1u32) - .run(); - - for i in 0..15 { - println!("i: {}", i); - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .create_transaction( - EthAddress::zero(), - OptionalValue::None::>, - ) - .egld_or_single_esdt( - &EgldOrEsdtTokenIdentifier::esdt(token_id), - 0, - &BigUint::from(amount.clone()), - ) - .returns(ReturnsResult) - .run(); - } - - let result = state - .world - .query() - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .get_current_tx_batch() - .returns(ReturnsResult) - .run(); - - println!("{:?}", result); - - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .propose_esdt_safe_set_current_transaction_batch_status(1u64, statuses) - .run(); -} - #[test] fn test_propose_esdt_safe_set_current_transaction_batch_status_batch_empty() { let mut state = MultiTransferTestState::new(); @@ -1640,86 +1549,6 @@ fn test_withdraw_slashed_amount_success() { assert_eq!(remaining_slashed_amount, BigUint::from(500u64)); } -#[test] // TODO: Fix this test -fn test_perform_action_endpoint_set_current_transaction_batch_status_success() { - let mut state = MultiTransferTestState::new(); - - state.deploy_contracts_config(); - - let esdt_safe_address = ESDT_SAFE_ADDRESS; - let token_id = WEGLD_TOKEN_ID; - let amount: BigUint = BigUint::from(1_000u64); - let destination: ManagedAddress = USER1_ADDRESS.to_managed_address(); - let nonce = 1u64; - - let mut tx_statuses = MultiValueEncoded::::new(); - tx_statuses.push(TransactionStatus::Executed); - - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .set_default_price_per_gas_unit(token_id, 0u64) - .run(); - - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .init_supply_mint_burn(token_id, BigUint::from(100_000u64), BigUint::zero()) - .run(); - - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .create_transaction( - EthAddress::zero(), - OptionalValue::None::>, - ) - .egld_or_single_esdt( - &EgldOrEsdtTokenIdentifier::esdt(token_id), - 0, - &BigUint::from(amount.clone()), - ) - .returns(ReturnsResult) - .run(); - - state - .world - .tx() - .from(MULTISIG_ADDRESS) - .to(esdt_safe_address) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .set_transaction_batch_status(1u64, tx_statuses.clone()) - .run(); - - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .perform_action_endpoint(1usize) - .run(); - - let result = state - .world - .query() - .to(ESDT_SAFE_ADDRESS) - .typed(esdt_safe_proxy::EsdtSafeProxy) - .get_batch_status(1u64) - .returns(ReturnsResult) - .run(); - assert_eq!(result, BatchStatus::AlreadyProcessed); -} - #[test] fn test_withdraw_refund_fees_for_ethereum_success() { let mut state = MultiTransferTestState::new(); @@ -1845,8 +1674,6 @@ fn test_upgrade_child_contract_from_source_success() { init_args.clone(), ) .run(); - - // how to check ? } #[test] From ceaaa21f943a312a73407ac088ffb2a811c652e8 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 12 Nov 2024 11:42:11 +0200 Subject: [PATCH 12/15] add more checks for storages and balances --- ...to_multiversx_relayer_query_test.scen.json | 2 +- ...sx_tx_batch_ok_call_data_encoded.scen.json | 2 +- multisig/tests/multisig_blackbox_test.rs | 162 +++++++++++++++--- 3 files changed, 139 insertions(+), 27 deletions(-) diff --git a/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json b/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json index f4ae7a43..3bf2132c 100644 --- a/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json +++ b/multisig/scenarios/ethereum_to_multiversx_relayer_query_test.scen.json @@ -423,7 +423,7 @@ "accounts": { "address:user1": { "esdt": { - "str:WEGLD-123456": "76000000000", + "str:WEGLD-123456": "76000100000", "+": "" }, "storage": "*", diff --git a/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json b/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json index 0259b4e1..783cd978 100644 --- a/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json +++ b/multisig/scenarios/ethereum_to_multiversx_tx_batch_ok_call_data_encoded.scen.json @@ -383,7 +383,7 @@ "accounts": { "address:user1": { "esdt": { - "str:WEGLD-123456": "76000000000", + "str:WEGLD-123456": "76000100000", "+": "" }, "storage": "*", diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index ee2fb0bd..9b873393 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -139,6 +139,10 @@ impl MultiTransferTestState { .esdt_balance(ETH_TOKEN_ID, 1001u64) .esdt_balance(NATIVE_TOKEN_ID, 100_000u64) .account(USER1_ADDRESS) + .esdt_balance(WEGLD_TOKEN_ID, 100000u64) + .nonce(1) + .account(USER2_ADDRESS) + .esdt_balance(WEGLD_TOKEN_ID, 1000u64) .nonce(1) .account(RELAYER1_ADDRESS) .nonce(1) @@ -414,6 +418,105 @@ impl MultiTransferTestState { self.bridged_tokens_wrapper_deploy(); self.config_multisig(); } + + fn setup_before_distribute_fees(&mut self) { + self.world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .init_supply_mint_burn_esdt_safe(WEGLD_TOKEN_ID, 10000u64, 1000u64) + .run(); + + self.world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .change_multiversx_to_eth_gas_limit(10u64) + .run(); + + self.world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .change_default_price_per_gas_unit(WEGLD_TOKEN_ID.clone(), 1u64) + .run(); + + self.world + .tx() + .from(USER1_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .create_transaction( + EthAddress::zero(), + OptionalValue::None::>, + ) + .single_esdt( + &TokenIdentifier::from(WEGLD_TOKEN_ID), + 0, + &BigUint::from(100u64), + ) + .run(); + + self.world + .tx() + .from(USER2_ADDRESS) + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .create_transaction( + EthAddress::zero(), + OptionalValue::None::>, + ) + .single_esdt( + &TokenIdentifier::from(WEGLD_TOKEN_ID), + 0, + &BigUint::from(100u64), + ) + .run(); + } + + fn check_distributed_fees( + &mut self, + accumulated_fees: u64, + user1_amount: u64, + user2_amount: u64, + ) { + self.world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .accumulated_transaction_fees(WEGLD_TOKEN_ID) + .returns(ExpectValue(accumulated_fees)) + .run(); + + self.world + .check_account(USER1_ADDRESS) + .esdt_balance(WEGLD_TOKEN_ID, user1_amount); + + self.world + .check_account(USER2_ADDRESS) + .esdt_balance(WEGLD_TOKEN_ID, user2_amount); + } + + fn check_amount_staked(&mut self, relayer1_amount: u64, relayer2_amount: u64) { + self.world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER1_ADDRESS) + .returns(ExpectValue(relayer1_amount)) + .run(); + + self.world + .query() + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .amount_staked(RELAYER2_ADDRESS) + .returns(ExpectValue(relayer2_amount)) + .run(); + } } #[test] @@ -475,7 +578,7 @@ fn ethereum_to_multiversx_call_data_empty_test() { state .world .check_account(USER1_ADDRESS) - .esdt_balance(WEGLD_TOKEN_ID, token_amount.clone()); + .esdt_balance(WEGLD_TOKEN_ID, token_amount.clone() + 100000u64); } #[test] @@ -662,7 +765,7 @@ fn ethereum_to_multiversx_relayer_query_test() { state .world .check_account(USER1_ADDRESS) - .esdt_balance(WEGLD_TOKEN_ID, token_amount.clone()); + .esdt_balance(WEGLD_TOKEN_ID, token_amount.clone() + 100000u64); state .world @@ -831,7 +934,7 @@ fn ethereum_to_multiversx_tx_batch_ok_test() { state .world .check_account(USER1_ADDRESS) - .esdt_balance(WEGLD_TOKEN_ID, token_amount.clone()) + .esdt_balance(WEGLD_TOKEN_ID, token_amount.clone() + 100000u64) .esdt_balance(ETH_TOKEN_ID, token_amount.clone()); state.world.write_scenario_trace( @@ -1177,6 +1280,10 @@ fn test_distribute_fees_from_child_contracts_success() { dest_address_percentage_pairs.push(MultiValue2::from((dest_address1.clone(), percentage1))); dest_address_percentage_pairs.push(MultiValue2::from((dest_address2.clone(), percentage2))); + state.setup_before_distribute_fees(); + + state.check_distributed_fees(20u64, 99900, 900); + state .world .tx() @@ -1185,6 +1292,8 @@ fn test_distribute_fees_from_child_contracts_success() { .typed(multisig_proxy::MultisigProxy) .distribute_fees_from_child_contracts(dest_address_percentage_pairs) .run(); + + state.check_distributed_fees(0u64, 99912, 908); } #[test] @@ -1206,6 +1315,10 @@ fn test_distribute_fees_from_child_contracts_invalid_percentage_sum() { dest_address_percentage_pairs.push(MultiValue2::from((dest_address1.clone(), percentage1))); dest_address_percentage_pairs.push(MultiValue2::from((dest_address2.clone(), percentage2))); + state.setup_before_distribute_fees(); + + state.check_distributed_fees(20u64, 99900, 900); + state .world .tx() @@ -1215,6 +1328,8 @@ fn test_distribute_fees_from_child_contracts_invalid_percentage_sum() { .distribute_fees_from_child_contracts(dest_address_percentage_pairs) .returns(ExpectError(4, "Percentages do not add up to 100%")) .run(); + + state.check_distributed_fees(20u64, 99900, 900); } #[test] @@ -1236,6 +1351,9 @@ fn test_distribute_fees_from_child_contracts_with_sc_address() { dest_address_percentage_pairs.push(MultiValue2::from((dest_address1.clone(), percentage1))); dest_address_percentage_pairs.push(MultiValue2::from((dest_address2.clone(), percentage2))); + state.setup_before_distribute_fees(); + + state.check_distributed_fees(20u64, 99900, 900); state .world .tx() @@ -1248,6 +1366,8 @@ fn test_distribute_fees_from_child_contracts_with_sc_address() { "Cannot transfer to smart contract dest_address", )) .run(); + + state.check_distributed_fees(20u64, 99900, 900); } #[test] @@ -1320,6 +1440,8 @@ fn test_unstake_more_than_staked_amount() { .unstake(unstake_amount) .returns(ExpectError(4, "can't unstake more than amount staked")) .run(); + + state.check_amount_staked(1000u64, 1000u64); } #[test] @@ -1341,6 +1463,8 @@ fn test_unstake_below_required_stake_board_member() { "can't unstake, must keep minimum amount as insurance", )) .run(); + + state.check_amount_staked(1000u64, 1000u64); } #[test] @@ -1381,28 +1505,7 @@ fn test_unstake_updates_amount_staked_correctly() { .unstake(unstake_amount_relayer1.clone()) .run(); - let remaining_stake_relayer1 = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER1_ADDRESS.to_managed_address()) - .returns(ReturnsResult) - .run(); - - let expected_remaining_stake_relayer1 = &BigUint::from(2_000u64) - &unstake_amount_relayer1; - assert_eq!(remaining_stake_relayer1, expected_remaining_stake_relayer1); - - let remaining_stake_relayer2 = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER2_ADDRESS.to_managed_address()) - .returns(ReturnsResult) - .run(); - - assert_eq!(remaining_stake_relayer2, BigUint::from(2_000u64)); + state.check_amount_staked(1800u64, 2000u64); } #[test] @@ -1452,6 +1555,15 @@ fn test_init_supply_from_child_contract_success() { .world .check_account(ESDT_SAFE_ADDRESS) .esdt_balance(token_id, amount.clone()); + + state + .world + .query() + .to(ESDT_SAFE_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .total_balances(TokenIdentifier::from(token_id)) + .returns(ExpectValue(amount.clone())) + .run(); } #[test] From ee15680b3b05e0ebf1dbdfe72a5db400d5548cbb Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 12 Nov 2024 13:33:06 +0200 Subject: [PATCH 13/15] self review refactor --- multisig/tests/multisig_blackbox_test.rs | 349 +++++++---------------- 1 file changed, 107 insertions(+), 242 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 9b873393..6cdecaeb 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -1,35 +1,16 @@ -#![allow(unused)] - -use std::ops::Add; - -use bridge_proxy::{config::ProxyTrait as _, ProxyTrait as _}; -use esdt_safe::{EsdtSafe, ProxyTrait as _}; - -use multisig::__endpoints_5__::multi_transfer_esdt_address; use multiversx_sc::{ - api::{HandleConstraints, ManagedTypeApi}, - codec::{ - multi_types::{MultiValueVec, OptionalValue}, - Empty, - }, + codec::multi_types::OptionalValue, contract_base::ManagedSerializer, hex_literal::hex, imports::MultiValue2, - storage::mappers::SingleValue, types::{ - Address, BigUint, CodeMetadata, EgldOrEsdtTokenIdentifier, EgldOrMultiEsdtPayment, - EsdtLocalRole, EsdtTokenPayment, ManagedAddress, ManagedBuffer, ManagedByteArray, - ManagedOption, ManagedType, ManagedVec, MultiValueEncoded, ReturnsNewManagedAddress, - ReturnsResult, TestAddress, TestSCAddress, TestTokenIdentifier, TokenIdentifier, + Address, BigUint, EsdtLocalRole, ManagedAddress, ManagedBuffer, ManagedByteArray, + ManagedOption, ManagedVec, MultiValueEncoded, ReturnsResult, TestAddress, TestSCAddress, + TestTokenIdentifier, TokenIdentifier, }, }; -use multiversx_sc_modules::pause::ProxyTrait; use multiversx_sc_scenario::{ - api::{StaticApi, VMHooksApi, VMHooksApiBackend}, - imports::MxscPath, - scenario_format::interpret_trait::{InterpretableFrom, InterpreterContext}, - scenario_model::*, - ContractInfo, DebugApi, ExpectError, ExpectValue, ScenarioTxRun, ScenarioWorld, + api::StaticApi, imports::MxscPath, ExpectError, ExpectValue, ScenarioTxRun, ScenarioWorld, }; use eth_address::*; @@ -37,19 +18,12 @@ use sc_proxies::{ bridge_proxy_contract_proxy, bridged_tokens_wrapper_proxy, esdt_safe_proxy, multi_transfer_esdt_proxy, multisig_proxy, }; -use token_module::ProxyTrait as _; -use transaction::{ - transaction_status::TransactionStatus, CallData, EthTransaction, EthTxAsMultiValue, - Transaction, TxBatchSplitInFields, -}; -use tx_batch_module::BatchStatus; +use transaction::{transaction_status::TransactionStatus, CallData, EthTxAsMultiValue}; const WEGLD_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("WEGLD-123456"); const ETH_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("ETH-123456"); const NATIVE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("EGLD-123456"); -const USER_ETHEREUM_ADDRESS: &[u8] = b"0x0102030405060708091011121314151617181920"; - const GAS_LIMIT: u64 = 100_000_000; const ETH_TX_GAS_LIMIT: u64 = 150_000; @@ -78,7 +52,6 @@ const MOCK_ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("mock-esdt-safe const BRIDGED_TOKENS_WRAPPER_ADDRESS: TestSCAddress = TestSCAddress::new("bridged-tokens-wrapper"); const PRICE_AGGREGATOR_ADDRESS: TestSCAddress = TestSCAddress::new("price-aggregator"); -const ORACLE_ADDRESS: TestAddress = TestAddress::new("oracle"); const OWNER_ADDRESS: TestAddress = TestAddress::new("owner"); const USER1_ADDRESS: TestAddress = TestAddress::new("user1"); const USER2_ADDRESS: TestAddress = TestAddress::new("user2"); @@ -86,11 +59,8 @@ const NON_BOARD_MEMEBER_ADDRESS: TestAddress = TestAddress::new("non-board-membe const RELAYER1_ADDRESS: TestAddress = TestAddress::new("relayer1"); const RELAYER2_ADDRESS: TestAddress = TestAddress::new("relayer2"); -const RANDOM_SC_ADDRESS: TestSCAddress = TestSCAddress::new("random-sc"); - const ESDT_SAFE_ETH_TX_GAS_LIMIT: u64 = 150_000; - -const BALANCE: &str = "2,000,000"; +const INITIAL_STAKE: u64 = 1_000u64; fn world() -> ScenarioWorld { let mut blockchain = ScenarioWorld::new(); @@ -119,11 +89,6 @@ fn world() -> ScenarioWorld { blockchain } -type MultiTransferContract = ContractInfo>; -type BridgeProxyContract = ContractInfo>; -type EsdtSafeContract = ContractInfo>; -type BridgedTokensWrapperContract = ContractInfo>; - struct MultiTransferTestState { world: ScenarioWorld, } @@ -500,23 +465,76 @@ impl MultiTransferTestState { .esdt_balance(WEGLD_TOKEN_ID, user2_amount); } - fn check_amount_staked(&mut self, relayer1_amount: u64, relayer2_amount: u64) { + fn stake(&mut self, address: TestAddress, amount: u64) -> &mut Self { + self.world + .tx() + .from(address) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .stake() + .egld(amount) + .run(); + self + } + + fn unstake(&mut self, address: TestAddress, amount: u64) -> &mut Self { self.world + .tx() + .from(address) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unstake(amount) + .run(); + self + } + + fn unstake_error(&mut self, address: TestAddress, amount: u64, error: &str) -> &mut Self { + self.world + .tx() + .from(address) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .unstake(amount) + .returns(ExpectError(4u64, error)) + .run(); + self + } + + fn check_stake(&mut self, address: TestAddress, expected_amount: u64) { + let amount = self + .world .query() .to(MULTISIG_ADDRESS) .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER1_ADDRESS) - .returns(ExpectValue(relayer1_amount)) + .amount_staked(address.to_managed_address()) + .returns(ReturnsResult) .run(); + assert_eq!(amount, BigUint::from(expected_amount)); + } + fn assert_board_member_count(&mut self, expected_count: usize) { self.world .query() .to(MULTISIG_ADDRESS) .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER2_ADDRESS) - .returns(ExpectValue(relayer2_amount)) + .num_board_members() + .returns(ExpectValue(expected_count)) .run(); } + + fn slash_amount(&mut self) -> u64 { + let slash_amount = self + .world + .tx() + .from(OWNER_ADDRESS) + .to(MULTISIG_ADDRESS) + .typed(multisig_proxy::MultisigProxy) + .slash_amount() + .returns(ReturnsResult) + .run(); + + slash_amount.to_u64().unwrap() + } } #[test] @@ -780,9 +798,6 @@ fn ethereum_to_multiversx_relayer_query2_test() { state.deploy_contracts_config(); - let addr = - Address::from_slice(b"erd1dyw7aysn0nwmuahvxnh2e0pm0kgjvs2gmfdxjgz3x0pet2nkvt8s7tkyrj"); - const ADDR: [u8; 32] = hex!("691dee92137cddbe76ec34eeacbc3b7d91264148da5a69205133c395aa7662cf"); let eth_tx = EthTxAsMultiValue::::from(( @@ -1267,8 +1282,6 @@ fn test_distribute_fees_from_child_contracts_success() { let dest_address1 = USER1_ADDRESS.to_managed_address(); let dest_address2 = USER2_ADDRESS.to_managed_address(); - const PERCENTAGE_TOTAL: u32 = 10000; - let percentage1 = 6000; let percentage2 = 4000; @@ -1375,53 +1388,21 @@ fn test_unstake_successful_board_member() { let mut state = MultiTransferTestState::new(); state.deploy_contracts_config(); - let stake_amount = BigUint::from(1_000u64); + let stake_amount = 1_000u64; - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .stake() - .egld(&stake_amount) - .run(); + state.stake(RELAYER1_ADDRESS, stake_amount); - let remaining_stake = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER1_ADDRESS.to_managed_address()) - .returns(ReturnsResult) - .run(); - - let unstake_amount = BigUint::from(500u64); - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .unstake(unstake_amount.clone()) - .run(); + state.check_stake(RELAYER1_ADDRESS, stake_amount + INITIAL_STAKE); - let remaining_stake = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER1_ADDRESS.to_managed_address()) - .returns(ReturnsResult) - .run(); + let unstake_amount = 500u64; + state.unstake(RELAYER1_ADDRESS, unstake_amount); - let expected_remaining_stake = BigUint::from(2000u64) - &unstake_amount; - assert_eq!(remaining_stake, expected_remaining_stake); + state.check_stake(RELAYER1_ADDRESS, stake_amount + INITIAL_STAKE - 500u64); state .world .check_account(RELAYER1_ADDRESS) - .balance(unstake_amount.to_u64().unwrap()); + .balance(unstake_amount); } #[test] @@ -1430,18 +1411,13 @@ fn test_unstake_more_than_staked_amount() { state.deploy_contracts_config(); - let unstake_amount = BigUint::from(1_500u64); - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .unstake(unstake_amount) - .returns(ExpectError(4, "can't unstake more than amount staked")) - .run(); + state.unstake_error( + RELAYER1_ADDRESS, + 1500u64, + "can't unstake more than amount staked", + ); - state.check_amount_staked(1000u64, 1000u64); + state.check_stake(RELAYER1_ADDRESS, INITIAL_STAKE); } #[test] @@ -1450,21 +1426,13 @@ fn test_unstake_below_required_stake_board_member() { state.deploy_contracts_config(); - let additional_unstake_amount = BigUint::from(600u64); - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .unstake(additional_unstake_amount) - .returns(ExpectError( - 4, - "can't unstake, must keep minimum amount as insurance", - )) - .run(); + state.unstake_error( + RELAYER1_ADDRESS, + 600u64, + "can't unstake, must keep minimum amount as insurance", + ); - state.check_amount_staked(1000u64, 1000u64); + state.check_stake(RELAYER1_ADDRESS, 1000u64); } #[test] @@ -1472,40 +1440,20 @@ fn test_unstake_updates_amount_staked_correctly() { let mut state = MultiTransferTestState::new(); state.deploy_contracts_config(); + let stake_amount_relayer1 = 1_000u64; + state.stake(RELAYER1_ADDRESS, stake_amount_relayer1); - let stake_amount_relayer1 = BigUint::from(1_000u64); - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .stake() - .egld(&stake_amount_relayer1) - .run(); + let stake_amount_relayer2 = 1_000u64; + state.stake(RELAYER2_ADDRESS, stake_amount_relayer2); - let stake_amount_relayer2 = BigUint::from(1_000u64); - state - .world - .tx() - .from(RELAYER2_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .stake() - .egld(&stake_amount_relayer2) - .run(); - - let unstake_amount_relayer1 = BigUint::from(200u64); - state - .world - .tx() - .from(RELAYER1_ADDRESS) - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .unstake(unstake_amount_relayer1.clone()) - .run(); + let unstake_amount_relayer1 = 200u64; + state.unstake(RELAYER1_ADDRESS, unstake_amount_relayer1); - state.check_amount_staked(1800u64, 2000u64); + state.check_stake( + RELAYER1_ADDRESS, + INITIAL_STAKE + stake_amount_relayer1 - unstake_amount_relayer1, + ); + state.check_stake(RELAYER2_ADDRESS, INITIAL_STAKE + stake_amount_relayer2); } #[test] @@ -1514,10 +1462,6 @@ fn test_propose_esdt_safe_set_current_transaction_batch_status_batch_empty() { state.deploy_contracts_config(); - let esdt_safe_address = ESDT_SAFE_ADDRESS; - let token_id = WEGLD_TOKEN_ID; - let amount = BigUint::::from(1_000u64); - let statuses: MultiValueEncoded = MultiValueEncoded::from(ManagedVec::from_single_item(TransactionStatus::Pending)); @@ -1614,7 +1558,8 @@ fn test_withdraw_slashed_amount_success() { state.deploy_contracts_config(); - let slashed_amount = BigUint::from(500u64); + let slashed_amount = state.slash_amount(); + state.world.set_esdt_balance( MULTISIG_ADDRESS, WEGLD_TOKEN_ID.as_bytes(), @@ -1630,16 +1575,7 @@ fn test_withdraw_slashed_amount_success() { .slash_board_member(RELAYER1_ADDRESS.to_managed_address()) .run(); - let remaining_stake = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .amount_staked(RELAYER1_ADDRESS.to_managed_address()) - .returns(ReturnsResult) - .run(); - - assert_eq!(remaining_stake, BigUint::from(500u64)); + state.check_stake(RELAYER1_ADDRESS, INITIAL_STAKE - slashed_amount); state .world @@ -1650,15 +1586,8 @@ fn test_withdraw_slashed_amount_success() { .withdraw_slashed_amount() .run(); - let remaining_slashed_amount = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .slash_amount() - .returns(ReturnsResult) - .run(); - assert_eq!(remaining_slashed_amount, BigUint::from(500u64)); + let remaining_slashed_amount = state.slash_amount(); + assert_eq!(remaining_slashed_amount, 500u64); } #[test] @@ -1794,17 +1723,7 @@ fn test_add_board_member_success() { state.deploy_contracts_config(); - let num_board_members = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert_eq!(num_board_members, 2); - + state.assert_board_member_count(2usize); state .world .tx() @@ -1814,16 +1733,7 @@ fn test_add_board_member_success() { .add_board_member_endpoint(USER1_ADDRESS.to_managed_address()) .run(); - let num_board_members_after = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert_eq!(num_board_members_after, 3); + state.assert_board_member_count(3usize); } #[test] @@ -1841,16 +1751,7 @@ fn test_remove_user_success() { .add_board_member_endpoint(USER1_ADDRESS.to_managed_address()) .run(); - let num_board_members = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert_eq!(num_board_members, 3); + state.assert_board_member_count(3usize); state .world @@ -1861,16 +1762,7 @@ fn test_remove_user_success() { .remove_user(USER1_ADDRESS.to_managed_address()) .run(); - let num_board_members = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert_eq!(num_board_members, 2); + state.assert_board_member_count(2usize); } #[test] @@ -1932,16 +1824,7 @@ fn test_remove_user_quorum_exceed_board_size() { state.deploy_contracts_config(); - let num_board_members = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert_eq!(num_board_members, 2); + state.assert_board_member_count(2usize); state .world @@ -1953,16 +1836,7 @@ fn test_remove_user_quorum_exceed_board_size() { .returns(ExpectError(4u64, "quorum cannot exceed board size")) .run(); - let num_board_members_after = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert_eq!(num_board_members_after, 2); + state.assert_board_member_count(2usize); } #[test] @@ -1971,16 +1845,7 @@ fn test_change_quorum_success() { state.deploy_contracts_config(); - let initial_num_board_members: usize = state - .world - .query() - .to(MULTISIG_ADDRESS) - .typed(multisig_proxy::MultisigProxy) - .num_board_members() - .returns(ReturnsResult) - .run(); - - assert!(initial_num_board_members >= 1); + state.assert_board_member_count(2usize); let new_quorum = 1usize; state From 0ec529d09b370647941d69ccb7f1e4aedb81bc84 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Tue, 12 Nov 2024 13:35:10 +0200 Subject: [PATCH 14/15] clippy... --- multisig/tests/multisig_blackbox_test.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 6cdecaeb..991795c8 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -406,7 +406,7 @@ impl MultiTransferTestState { .from(OWNER_ADDRESS) .to(MULTISIG_ADDRESS) .typed(multisig_proxy::MultisigProxy) - .change_default_price_per_gas_unit(WEGLD_TOKEN_ID.clone(), 1u64) + .change_default_price_per_gas_unit(WEGLD_TOKEN_ID, 1u64) .run(); self.world @@ -1560,11 +1560,9 @@ fn test_withdraw_slashed_amount_success() { let slashed_amount = state.slash_amount(); - state.world.set_esdt_balance( - MULTISIG_ADDRESS, - WEGLD_TOKEN_ID.as_bytes(), - slashed_amount.clone(), - ); + state + .world + .set_esdt_balance(MULTISIG_ADDRESS, WEGLD_TOKEN_ID.as_bytes(), slashed_amount); state .world From de97869c6e3f95a12000a5207c50233209da0286 Mon Sep 17 00:00:00 2001 From: Eveline Molnar Date: Wed, 13 Nov 2024 15:02:21 +0200 Subject: [PATCH 15/15] refactor managed address --- multisig/tests/multisig_blackbox_test.rs | 46 ++++++++++++------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/multisig/tests/multisig_blackbox_test.rs b/multisig/tests/multisig_blackbox_test.rs index 991795c8..b1af3e82 100644 --- a/multisig/tests/multisig_blackbox_test.rs +++ b/multisig/tests/multisig_blackbox_test.rs @@ -129,8 +129,8 @@ impl MultiTransferTestState { fn multisig_deploy(&mut self) -> &mut Self { let mut board: MultiValueEncoded> = MultiValueEncoded::new(); - board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + board.push(RELAYER1_ADDRESS.to_managed_address()); + board.push(RELAYER2_ADDRESS.to_managed_address()); self.world .tx() .from(OWNER_ADDRESS) @@ -555,7 +555,7 @@ fn ethereum_to_multiversx_call_data_empty_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), token_amount.clone(), 1u64, @@ -720,7 +720,7 @@ fn ethereum_to_multiversx_relayer_query_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), token_amount.clone(), 1u64, @@ -896,7 +896,7 @@ fn ethereum_to_multiversx_tx_batch_ok_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), token_amount.clone(), 1u64, @@ -907,7 +907,7 @@ fn ethereum_to_multiversx_tx_batch_ok_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(ETH_TOKEN_ID), token_amount.clone(), 2u64, @@ -980,7 +980,7 @@ fn ethereum_to_multiversx_tx_batch_rejected_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(BRIDGE_PROXY_ADDRESS.eval_to_array()), + BRIDGE_PROXY_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), over_the_limit_token_amount.clone(), 1u64, @@ -991,7 +991,7 @@ fn ethereum_to_multiversx_tx_batch_rejected_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(BRIDGE_PROXY_ADDRESS.eval_to_array()), + BRIDGE_PROXY_ADDRESS.to_managed_address(), TokenIdentifier::from(ETH_TOKEN_ID), over_the_limit_token_amount.clone(), 2u64, @@ -1057,8 +1057,8 @@ fn init_test() { let mut board: MultiValueEncoded> = MultiValueEncoded::new(); - board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + board.push(RELAYER1_ADDRESS.to_managed_address()); + board.push(RELAYER2_ADDRESS.to_managed_address()); state .world .tx() @@ -1085,8 +1085,8 @@ fn init_test() { let mut board2: MultiValueEncoded> = MultiValueEncoded::new(); - board2.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board2.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); + board2.push(RELAYER1_ADDRESS.to_managed_address()); + board2.push(RELAYER1_ADDRESS.to_managed_address()); let multisig2 = TestSCAddress::new("multisig2"); state .world @@ -1144,7 +1144,7 @@ fn multisig_non_board_member_interaction_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), token_amount.clone(), 1u64, @@ -1190,7 +1190,7 @@ fn multisig_insuficient_signatures_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), token_amount.clone(), 1u64, @@ -1232,7 +1232,7 @@ fn multisig_non_board_member_sign_test() { EthAddress { raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), }, - ManagedAddress::from(USER1_ADDRESS.eval_to_array()), + USER1_ADDRESS.to_managed_address(), TokenIdentifier::from(WEGLD_TOKEN_ID), token_amount.clone(), 1u64, @@ -1516,8 +1516,8 @@ fn test_add_unprocessed_refund_tx_to_batch_success() { let mut board: MultiValueEncoded> = MultiValueEncoded::new(); - board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + board.push(RELAYER1_ADDRESS.to_managed_address()); + board.push(RELAYER2_ADDRESS.to_managed_address()); state .world .tx() @@ -1594,8 +1594,8 @@ fn test_withdraw_refund_fees_for_ethereum_success() { let mut board: MultiValueEncoded> = MultiValueEncoded::new(); - board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + board.push(RELAYER1_ADDRESS.to_managed_address()); + board.push(RELAYER2_ADDRESS.to_managed_address()); state .world .tx() @@ -1633,8 +1633,8 @@ fn test_withdraw_transaction_fees_success() { let mut board: MultiValueEncoded> = MultiValueEncoded::new(); - board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + board.push(RELAYER1_ADDRESS.to_managed_address()); + board.push(RELAYER2_ADDRESS.to_managed_address()); state .world .tx() @@ -1769,8 +1769,8 @@ fn test_remove_user_cannot_remove_all() { let mut board: MultiValueEncoded> = MultiValueEncoded::new(); - board.push(ManagedAddress::from(RELAYER1_ADDRESS.eval_to_array())); - board.push(ManagedAddress::from(RELAYER2_ADDRESS.eval_to_array())); + board.push(RELAYER1_ADDRESS.to_managed_address()); + board.push(RELAYER2_ADDRESS.to_managed_address()); state .world .tx()