Skip to content

Commit

Permalink
Merge pull request #66 from multiversx/composable-tasks-add-back-tran…
Browse files Browse the repository at this point in the history
…sfers-for-swap-input

composable tasks improvement
  • Loading branch information
claudiulataretu authored Apr 4, 2024
2 parents 062d7f4 + cef61ef commit ea866c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion composable-tasks/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 24 additions & 7 deletions composable-tasks/src/external_sc_interactions/pair_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
45 changes: 15 additions & 30 deletions composable-tasks/tests/composable_tasks_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ fn swap_unwrap_wrap_send_test() {
);
}


#[test]
fn wrap_swap_tokens_fixed_output_test() {
let composable_tasks_setup = ComposableTasksSetup::new(
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit ea866c9

Please sign in to comment.