diff --git a/Cargo.lock b/Cargo.lock index 9633b32..cf146f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1710,7 +1710,7 @@ dependencies = [ [[package]] name = "multiversx-wegld-swap-sc" version = "0.46.0" -source = "git+https://github.com/multiversx/mx-contracts-rs?rev=038a6d8#038a6d81d13bf5863ba552b297fe5c2b7543433d" +source = "git+https://github.com/multiversx/mx-contracts-rs/?rev=038a6d8#038a6d81d13bf5863ba552b297fe5c2b7543433d" dependencies = [ "multiversx-sc", "multiversx-sc-modules", diff --git a/composable-tasks/src/config.rs b/composable-tasks/src/config.rs index d66a6dd..8a816fb 100644 --- a/composable-tasks/src/config.rs +++ b/composable-tasks/src/config.rs @@ -8,7 +8,6 @@ pub const SEND_TOKENS_ARGS_LEN: usize = 1; pub const SWAP_TOKENS_FIXED_INPUT_FUNC_NAME: &[u8] = b"swapTokensFixedInput"; pub const SWAP_TOKENS_FIXED_OUTPUT_FUNC_NAME: &[u8] = b"swapTokensFixedOutput"; - #[multiversx_sc::module] pub trait ConfigModule: external_sc_interactions::pair_actions::PairActionsModule diff --git a/composable-tasks/src/external_sc_interactions/pair_actions.rs b/composable-tasks/src/external_sc_interactions/pair_actions.rs index a663e70..a509722 100644 --- a/composable-tasks/src/external_sc_interactions/pair_actions.rs +++ b/composable-tasks/src/external_sc_interactions/pair_actions.rs @@ -26,20 +26,34 @@ pub trait PairActionsModule: router_actions::RouterActionsModule { &self, from_tokens: TokenIdentifier, from_amount: BigUint, - to_tokens: TokenIdentifier, + to_token_id: TokenIdentifier, min_amount_out: BigUint, ) -> EsdtTokenPayment { - if from_tokens == to_tokens { + if from_tokens == to_token_id { return EsdtTokenPayment::new(from_tokens, 0, from_amount); } - let pair_address = self.get_pair(from_tokens.clone(), to_tokens.clone()); + let pair_address = self.get_pair(from_tokens.clone(), to_token_id.clone()); let payment = EsdtTokenPayment::new(from_tokens, 0, from_amount); - self.pair_proxy(pair_address) - .swap_tokens_fixed_input(to_tokens, min_amount_out) + let ((), back_transfers) = self + .pair_proxy(pair_address) + .swap_tokens_fixed_input(to_token_id.clone(), min_amount_out) .with_esdt_transfer(payment) - .execute_on_dest_context() + .execute_on_dest_context_with_back_transfers(); + + require!( + back_transfers.esdt_payments.len() == 1, + "Swap tokens fixed output: Back transfers expected 1 payment" + ); + + let payment_out = back_transfers.esdt_payments.get(0); + require!( + payment_out.token_identifier == to_token_id, + "Wrong returned token identifier!" + ); + + payment_out } fn perform_swap_tokens_fixed_output( @@ -72,7 +86,10 @@ pub trait PairActionsModule: router_actions::RouterActionsModule { ); let payment_out = back_transfers.esdt_payments.get(0); - require!(payment_out.token_identifier == to_token_id, "Wrong returned token identifier!"); + require!( + payment_out.token_identifier == to_token_id, + "Wrong returned token identifier!" + ); back_transfers.esdt_payments } diff --git a/composable-tasks/tests/composable_tasks_tests.rs b/composable-tasks/tests/composable_tasks_tests.rs index 59360cf..da3abd6 100644 --- a/composable-tasks/tests/composable_tasks_tests.rs +++ b/composable-tasks/tests/composable_tasks_tests.rs @@ -564,7 +564,6 @@ fn swap_unwrap_wrap_send_test() { ); } - #[test] fn wrap_swap_tokens_fixed_output_test() { let composable_tasks_setup = ComposableTasksSetup::new( @@ -580,10 +579,9 @@ fn wrap_swap_tokens_fixed_output_test() { let user_first_token_balance = 200_000_001u64; - b_mock.borrow_mut().set_egld_balance( - &first_user_addr, - &rust_biguint!(user_first_token_balance), - ); + b_mock + .borrow_mut() + .set_egld_balance(&first_user_addr, &rust_biguint!(user_first_token_balance)); let expected_balance = 166_666_666u64; b_mock @@ -615,18 +613,15 @@ fn wrap_swap_tokens_fixed_output_test() { managed_biguint!(expected_balance), ); - sc.compose_tasks(expected_token_out, tasks); }, ) .assert_ok(); // rest of the swap (166_666_666 swapped to 200_000_000 and 1 remaining) - b_mock.borrow_mut().check_esdt_balance( - &second_user_addr, - WEGLD_TOKEN_ID, - &rust_biguint!(1u64), - ); + b_mock + .borrow_mut() + .check_esdt_balance(&second_user_addr, WEGLD_TOKEN_ID, &rust_biguint!(1u64)); } fn _wrap_swap_tokens_fixed_output_exact_amount_test() { @@ -643,10 +638,9 @@ fn _wrap_swap_tokens_fixed_output_exact_amount_test() { let user_first_token_balance = 200_000_000u64; - b_mock.borrow_mut().set_egld_balance( - &first_user_addr, - &rust_biguint!(user_first_token_balance), - ); + b_mock + .borrow_mut() + .set_egld_balance(&first_user_addr, &rust_biguint!(user_first_token_balance)); let expected_balance = 166_666_666u64; b_mock @@ -678,21 +672,17 @@ fn _wrap_swap_tokens_fixed_output_exact_amount_test() { managed_biguint!(expected_balance), ); - sc.compose_tasks(expected_token_out, tasks); }, ) .assert_ok(); // rest of the swap (166_666_666 swapped to 200_000_000 and 1 remaining) - b_mock.borrow_mut().check_esdt_balance( - &second_user_addr, - WEGLD_TOKEN_ID, - &rust_biguint!(1u64), - ); + b_mock + .borrow_mut() + .check_esdt_balance(&second_user_addr, WEGLD_TOKEN_ID, &rust_biguint!(1u64)); } - #[test] fn swap_tokens_fixed_output_unwrap_test() { let composable_tasks_setup = ComposableTasksSetup::new( @@ -746,22 +736,17 @@ fn swap_tokens_fixed_output_unwrap_test() { managed_biguint!(expected_balance), ); - sc.compose_tasks(expected_token_out, tasks); }, ) .assert_ok(); // rest of the input token - b_mock.borrow_mut().check_esdt_balance( - &second_user_addr, - TOKEN_IDS[0], - &rust_biguint!(1u64), - ); + b_mock + .borrow_mut() + .check_esdt_balance(&second_user_addr, TOKEN_IDS[0], &rust_biguint!(1u64)); } - - ///////// ROUTER //////////// #[test] fn swap_router_single_task_test() {