Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check amounts in router_v2 swap token for token test #119

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions amm/drink-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ mod psp22;
#[cfg(test)]
mod router_contract;
#[cfg(test)]
mod router_v2_contract;
#[cfg(test)]
mod router_tests;
#[cfg(test)]
mod router_v2_contract;
#[cfg(test)]
mod router_v2_tests;
#[cfg(test)]
mod stable_pool_contract;
Expand Down
88 changes: 86 additions & 2 deletions amm/drink-tests/src/router_v2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,37 @@ fn test_psp22_swap(mut session: Session) {

let swap_amount = 100 * TOKEN;

router_v2::swap_exact_tokens_for_tokens(
let first_step_output = v2_amounts::get_amount_out(
&mut session,
ice_usdc_pair.into(),
ice.into(),
usdc.into(),
swap_amount,
)
.unwrap();

let second_step_output = stable_swap::get_swap_amount_out(
&mut session,
usdt_usdc_pool,
usdc.into(),
usdt.into(),
first_step_output,
)
.unwrap();

let third_step_output = v2_amounts::get_amount_out(
&mut session,
wood_usdt_pair.into(),
usdt.into(),
wood.into(),
second_step_output.0,
)
.unwrap();

let init_bob_ice_balance = psp22_utils::balance_of(&mut session, ice.into(), bob());
let init_bob_wood_balance = psp22_utils::balance_of(&mut session, wood.into(), bob());

let amounts = router_v2::swap_exact_tokens_for_tokens(
&mut session,
router.into(),
swap_amount,
Expand All @@ -503,7 +533,49 @@ fn test_psp22_swap(mut session: Session) {
)
.expect("Should swap");

router_v2::swap_tokens_for_exact_tokens(
assert_eq!(amounts.len(), 4);
assert_eq!(amounts[0], swap_amount);
assert_eq!(amounts[1], first_step_output);
assert_eq!(amounts[2], second_step_output.0);
assert_eq!(amounts[3], third_step_output);

let bob_ice_balance = psp22_utils::balance_of(&mut session, ice.into(), bob());
let bob_wood_balance = psp22_utils::balance_of(&mut session, wood.into(), bob());

assert_eq!(init_bob_ice_balance - bob_ice_balance, swap_amount);
assert_eq!(bob_wood_balance - init_bob_wood_balance, third_step_output);

let init_bob_ice_balance = bob_ice_balance;
let init_bob_wood_balance = bob_wood_balance;

let third_step_input = v2_amounts::get_amount_in(
&mut session,
wood_usdt_pair.into(),
usdt.into(),
wood.into(),
swap_amount,
)
.unwrap();

let second_step_input = stable_swap::get_swap_amount_in(
&mut session,
usdt_usdc_pool,
usdc.into(),
usdt.into(),
third_step_input,
)
.unwrap();

let first_step_input = v2_amounts::get_amount_in(
&mut session,
ice_usdc_pair.into(),
ice.into(),
usdc.into(),
second_step_input.0,
)
.unwrap();

let amounts = router_v2::swap_tokens_for_exact_tokens(
&mut session,
router.into(),
swap_amount,
Expand All @@ -528,6 +600,18 @@ fn test_psp22_swap(mut session: Session) {
)
.expect("Should swap");

assert_eq!(amounts.len(), 4);
assert_eq!(amounts[0], first_step_input);
assert_eq!(amounts[1], second_step_input.0);
assert_eq!(amounts[2], third_step_input);
assert_eq!(amounts[3], swap_amount);

let bob_ice_balance = psp22_utils::balance_of(&mut session, ice.into(), bob());
let bob_wood_balance = psp22_utils::balance_of(&mut session, wood.into(), bob());

assert_eq!(init_bob_ice_balance - bob_ice_balance, first_step_input);
assert_eq!(bob_wood_balance - init_bob_wood_balance, swap_amount);

for token in [usdt, usdc, ice.into(), wood.into()] {
assert_eq!(
psp22_utils::balance_of(&mut session, token, router.into()),
Expand Down
26 changes: 13 additions & 13 deletions amm/drink-tests/src/stable_swap_tests/tests_rated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn setup_rated_swap_with_tokens(
}

fn set_mock_rate(session: &mut Session<MinimalRuntime>, mock_rate_contract: AccountId, rate: u128) {
_ = handle_ink_error(
let _ = handle_contract_result(
session
.execute(mock_rate_provider_contract::Instance::from(mock_rate_contract).set_rate(rate))
.unwrap(),
Expand Down Expand Up @@ -118,7 +118,7 @@ fn test_01(mut session: Session) {
set_timestamp(&mut session, now + 1);
set_mock_rate(&mut session, mock_rate_provider, 2 * RATE_PRECISION);

_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
BOB,
Expand Down Expand Up @@ -149,7 +149,7 @@ fn test_01(mut session: Session) {
vec![100000 * ONE_SAZERO, 100000 * ONE_WAZERO],
BOB,
);
_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
CHARLIE,
Expand All @@ -172,7 +172,7 @@ fn test_01(mut session: Session) {
);
assert_eq!(last_share_price, 100000000, "Incorrect share price");

_ = stable_swap::remove_liquidity_by_shares(
let _ = stable_swap::remove_liquidity_by_shares(
&mut session,
rated_swap.into(),
CHARLIE,
Expand All @@ -198,7 +198,7 @@ fn test_01(mut session: Session) {
// --- DIFF ----
// Allow withdrawing all liquidity from the pool

_ = stable_swap::remove_liquidity_by_shares(
let _ = stable_swap::remove_liquidity_by_shares(
&mut session,
rated_swap.into(),
BOB,
Expand Down Expand Up @@ -249,7 +249,7 @@ fn test_02(mut session: Session) {
set_timestamp(&mut session, now);
set_mock_rate(&mut session, mock_token_2_rate, 2 * RATE_PRECISION);

_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
BOB,
Expand Down Expand Up @@ -284,7 +284,7 @@ fn test_02(mut session: Session) {
],
BOB,
);
_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
CHARLIE,
Expand All @@ -307,7 +307,7 @@ fn test_02(mut session: Session) {
);
assert_eq!(last_share_price, 100000000, "Incorrect share price");

_ = stable_swap::remove_liquidity_by_shares(
let _ = stable_swap::remove_liquidity_by_shares(
&mut session,
rated_swap.into(),
CHARLIE,
Expand Down Expand Up @@ -360,7 +360,7 @@ fn test_03(mut session: Session) {
set_mock_rate(&mut session, mock_token_2_rate, 2 * RATE_PRECISION);
set_mock_rate(&mut session, mock_token_3_rate, 4 * RATE_PRECISION);

_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
BOB,
Expand Down Expand Up @@ -396,7 +396,7 @@ fn test_03(mut session: Session) {
BOB,
);

_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
CHARLIE,
Expand All @@ -419,7 +419,7 @@ fn test_03(mut session: Session) {
);
assert_eq!(last_share_price, 100000000, "Incorrect share price");

_ = stable_swap::remove_liquidity_by_shares(
let _ = stable_swap::remove_liquidity_by_shares(
&mut session,
rated_swap.into(),
CHARLIE,
Expand Down Expand Up @@ -463,7 +463,7 @@ fn test_04(mut session: Session) {
200_000_000,
);

_ = stable_swap::add_liquidity(
let _ = stable_swap::add_liquidity(
&mut session,
rated_swap.into(),
BOB,
Expand Down Expand Up @@ -513,7 +513,7 @@ fn test_04(mut session: Session) {
"Incorrect user token balance"
);

_ = stable_swap::swap_exact_in(
let _ = stable_swap::swap_exact_in(
&mut session,
rated_swap.into(),
CHARLIE,
Expand Down
Loading