From 9648a82f835c1ce87fc68d57e8e655d06227a488 Mon Sep 17 00:00:00 2001 From: woocash2 Date: Thu, 3 Oct 2024 12:50:22 +0200 Subject: [PATCH 1/5] get_swap_amount utils for stable pool --- .../src/stable_swap_tests/tests_rated.rs | 26 +++--- amm/drink-tests/src/utils.rs | 83 ++++++++++++------- 2 files changed, 68 insertions(+), 41 deletions(-) diff --git a/amm/drink-tests/src/stable_swap_tests/tests_rated.rs b/amm/drink-tests/src/stable_swap_tests/tests_rated.rs index d71646c4..5ffea714 100644 --- a/amm/drink-tests/src/stable_swap_tests/tests_rated.rs +++ b/amm/drink-tests/src/stable_swap_tests/tests_rated.rs @@ -84,7 +84,7 @@ fn setup_rated_swap_with_tokens( } fn set_mock_rate(session: &mut Session, 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(), @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/amm/drink-tests/src/utils.rs b/amm/drink-tests/src/utils.rs index 3f4488f8..e7169142 100644 --- a/amm/drink-tests/src/utils.rs +++ b/amm/drink-tests/src/utils.rs @@ -219,7 +219,6 @@ pub mod router { pub mod router_v2 { use super::*; - use drink::frame_support::dispatch::PaysFee; use router_v2_contract::RouterV2 as _; use router_v2_contract::{Pool, RouterV2Error, Step}; @@ -670,7 +669,7 @@ pub mod psp22_utils { ) -> Result<(), psp22::PSP22Error> { let _ = session.set_actor(caller); - handle_ink_error( + handle_contract_result( session .execute(PSP22::increase_allowance(&token.into(), spender, amount)) .unwrap(), @@ -687,7 +686,7 @@ pub mod psp22_utils { ) -> Result<(), psp22::PSP22Error> { let _ = session.set_actor(caller); - handle_ink_error( + handle_contract_result( session .execute(PSP22::transfer(&token.into(), to, amount, [].to_vec())) .unwrap(), @@ -701,7 +700,7 @@ pub mod psp22_utils { token: AccountId, account: AccountId, ) -> u128 { - handle_ink_error( + handle_contract_result( session .query(PSP22::balance_of(&token.into(), account)) .unwrap(), @@ -709,11 +708,11 @@ pub mod psp22_utils { } pub fn total_supply(session: &mut Session, token: AccountId) -> u128 { - handle_ink_error(session.query(PSP22::total_supply(&token.into())).unwrap()) + handle_contract_result(session.query(PSP22::total_supply(&token.into())).unwrap()) } pub fn token_decimals(session: &mut Session, token: AccountId) -> u8 { - handle_ink_error(session.query(PSP22::token_decimals(&token.into())).unwrap()) + handle_contract_result(session.query(PSP22::token_decimals(&token.into())).unwrap()) } } @@ -758,8 +757,8 @@ pub mod stable_swap { amounts: Vec, to: AccountId, ) -> Result<(u128, u128), StablePoolError> { - _ = session.set_actor(caller); - handle_ink_error( + let _ = session.set_actor(caller); + handle_contract_result( session .execute( stable_pool_contract::Instance::from(stable_pool).add_liquidity( @@ -780,8 +779,8 @@ pub mod stable_swap { amounts: Vec, to: AccountId, ) -> Result<(u128, u128), StablePoolError> { - _ = session.set_actor(caller); - handle_ink_error( + let _ = session.set_actor(caller); + handle_contract_result( session .execute( stable_pool_contract::Instance::from(stable_pool).remove_liquidity_by_amounts( @@ -802,8 +801,8 @@ pub mod stable_swap { min_amounts: Vec, to: AccountId, ) -> Result, StablePoolError> { - _ = session.set_actor(caller); - handle_ink_error( + let _ = session.set_actor(caller); + handle_contract_result( session .execute( stable_pool_contract::Instance::from(stable_pool).remove_liquidity_by_shares( @@ -826,8 +825,8 @@ pub mod stable_swap { min_token_out_amount: u128, to: AccountId, ) -> Result<(u128, u128), StablePoolError> { - _ = session.set_actor(caller); - handle_ink_error( + let _ = session.set_actor(caller); + handle_contract_result( session .execute( stable_pool_contract::Instance::from(stable_pool).swap_exact_in( @@ -852,8 +851,8 @@ pub mod stable_swap { max_token_in_amount: u128, to: AccountId, ) -> Result<(u128, u128), StablePoolError> { - _ = session.set_actor(caller); - handle_ink_error( + let _ = session.set_actor(caller); + handle_contract_result( session .execute( stable_pool_contract::Instance::from(stable_pool).swap_exact_out( @@ -877,8 +876,8 @@ pub mod stable_swap { min_token_out_amount: u128, to: AccountId, ) -> Result<(u128, u128), StablePoolError> { - _ = session.set_actor(caller); - handle_ink_error( + let _ = session.set_actor(caller); + handle_contract_result( session .execute( stable_pool_contract::Instance::from(stable_pool).swap_received( @@ -893,7 +892,7 @@ pub mod stable_swap { } pub fn reserves(session: &mut Session, stable_pool: AccountId) -> Vec { - handle_ink_error( + handle_contract_result( session .query(stable_pool_contract::Instance::from(stable_pool).reserves()) .unwrap(), @@ -904,7 +903,7 @@ pub mod stable_swap { session: &mut Session, stable_pool: AccountId, ) -> Result { - handle_ink_error( + handle_contract_result( session .query(stable_pool_contract::Instance::from(stable_pool).amp_coef()) .unwrap(), @@ -912,7 +911,7 @@ pub mod stable_swap { } pub fn fees(session: &mut Session, stable_pool: AccountId) -> (u32, u32) { - handle_ink_error( + handle_contract_result( session .query(stable_pool_contract::Instance::from(stable_pool).fees()) .unwrap(), @@ -920,7 +919,7 @@ pub mod stable_swap { } pub fn token_rates(session: &mut Session, stable_pool: AccountId) -> Vec { - handle_ink_error( + handle_contract_result( session .query(stable_pool_contract::Instance::from(stable_pool).token_rates()) .unwrap(), @@ -928,7 +927,7 @@ pub mod stable_swap { } pub fn tokens(session: &mut Session, stable_pool: AccountId) -> Vec { - handle_ink_error( + handle_contract_result( session .query(stable_pool_contract::Instance::from(stable_pool).tokens()) .unwrap(), @@ -940,7 +939,7 @@ pub mod stable_swap { stable_pool: AccountId, liquidity: u128, ) -> Result, StablePoolError> { - handle_ink_error( + handle_contract_result( session .query( stable_pool_contract::Instance::from(stable_pool) @@ -955,7 +954,7 @@ pub mod stable_swap { stable_pool: AccountId, liquidity: u128, ) -> Result, StablePoolError> { - handle_ink_error( + handle_contract_result( session .query( stable_pool_contract::Instance::from(stable_pool) @@ -970,7 +969,7 @@ pub mod stable_swap { stable_pool: AccountId, amounts: Vec, ) -> Result<(u128, u128), StablePoolError> { - handle_ink_error( + handle_contract_result( session .query( stable_pool_contract::Instance::from(stable_pool) @@ -985,7 +984,7 @@ pub mod stable_swap { stable_pool: AccountId, amounts: Vec, ) -> Result<(u128, u128), StablePoolError> { - handle_ink_error( + handle_contract_result( session .query( stable_pool_contract::Instance::from(stable_pool) @@ -994,6 +993,34 @@ pub mod stable_swap { .unwrap(), ) } + + fn get_swap_amount_out( + session: &mut Session, + stable_pool: AccountId, + token_in: AccountId, + token_out: AccountId, + token_in_amount: u128, + ) -> Result<(u128, u128), StablePoolError> { + handle_contract_result( + session.query( + stable_pool_contract::Instance::from(stable_pool).get_swap_amount_out(token_in, token_out, token_in_amount) + ).unwrap() + ) + } + + fn get_swap_amount_in( + session: &mut Session, + stable_pool: AccountId, + token_in: AccountId, + token_out: AccountId, + token_out_amount: u128, + ) -> Result<(u128, u128), StablePoolError> { + handle_contract_result( + session.query( + stable_pool_contract::Instance::from(stable_pool).get_swap_amount_in(token_in, token_out, token_out_amount) + ).unwrap() + ) + } } pub fn get_timestamp(session: &mut Session) -> u64 { @@ -1004,7 +1031,7 @@ pub fn set_timestamp(session: &mut Session, timestamp: u64) { session.sandbox().set_timestamp(timestamp); } -pub fn handle_ink_error(res: ContractResult>) -> R { +pub fn handle_contract_result(res: ContractResult>) -> R { match res.result { Err(ink_lang_err) => panic!("InkLangError: {:?}", ink_lang_err), Ok(r) => r, From 6e256756508db8ead48224f6a1013bf03b596885 Mon Sep 17 00:00:00 2001 From: woocash2 Date: Thu, 3 Oct 2024 13:50:47 +0200 Subject: [PATCH 2/5] v2 amount utils --- amm/drink-tests/src/utils.rs | 136 +++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/amm/drink-tests/src/utils.rs b/amm/drink-tests/src/utils.rs index e7169142..c30071cb 100644 --- a/amm/drink-tests/src/utils.rs +++ b/amm/drink-tests/src/utils.rs @@ -1023,6 +1023,142 @@ pub mod stable_swap { } } +pub mod v2_amounts { + use primitive_types::U256; + use crate::pair_contract::{MathError, Pair as _, PairError}; + use super::*; + + const PAIR_TRADING_FEE_DENOM: u128 = 1000; + + struct PairParams { + token_0: AccountId, + token_1: AccountId, + reserve_0: u128, + reserve_1: u128, + fee: u128, + } + + pub fn get_amount_out( + session: &mut Session, + pair: AccountId, + token_in: AccountId, + token_out: AccountId, + amount_in: u128, + ) -> Result { + let PairParams { + token_0, + token_1, + reserve_0, + reserve_1, + fee, + } = get_pair_params(session, pair); + + let (reserve_in, reserve_out) = if token_0 == token_in && token_1 == token_out { + (reserve_0, reserve_1) + } else { + (reserve_1, reserve_0) + }; + + // Adjusts for fees paid in the `token_in`. + let amount_in_with_fee = casted_mul(amount_in, PAIR_TRADING_FEE_DENOM - fee); + + let numerator = amount_in_with_fee + .checked_mul(reserve_out.into()) + .ok_or(MathError::MulOverflow(13))?; + + let denominator = casted_mul(reserve_in, PAIR_TRADING_FEE_DENOM) + .checked_add(amount_in_with_fee) + .ok_or(MathError::AddOverflow(2))?; + + let amount_out: u128 = numerator + .checked_div(denominator) + .ok_or(MathError::DivByZero(7))? + .try_into() + .map_err(|_| MathError::CastOverflow(4))?; + + Ok(amount_out) + } + + pub fn get_amount_in( + session: &mut Session, + pair: AccountId, + token_in: AccountId, + token_out: AccountId, + amount_out: u128, + ) -> Result { + let PairParams { + token_0, + token_1, + reserve_0, + reserve_1, + fee, + } = get_pair_params(session, pair); + + let (reserve_in, reserve_out) = if token_0 == token_in && token_1 == token_out { + (reserve_0, reserve_1) + } else { + (reserve_1, reserve_0) + }; + + let numerator = casted_mul(reserve_in, amount_out) + .checked_mul(PAIR_TRADING_FEE_DENOM.into()) + .ok_or(MathError::MulOverflow(14))?; + + let denominator = casted_mul( + reserve_out + .checked_sub(amount_out) + .ok_or(MathError::SubUnderflow(15))?, + PAIR_TRADING_FEE_DENOM - fee, + ); + + let amount_in: u128 = numerator + .checked_div(denominator) + .ok_or(MathError::DivByZero(8))? + .checked_add(1.into()) + .ok_or(MathError::AddOverflow(3))? + .try_into() + .map_err(|_| MathError::CastOverflow(5))?; + + Ok(amount_in) + } + + fn get_pair_params(session: &mut Session, pair: AccountId) -> PairParams { + let contract = pair_contract::Instance::from(pair); + let token_0 = handle_contract_result( + session.query( + contract.get_token_0() + ).unwrap() + ); + let token_1 = handle_contract_result( + session.query( + contract.get_token_1() + ).unwrap() + ); + let (reserve_0, reserve_1, _) = handle_contract_result( + session.query( + contract.get_reserves() + ).unwrap() + ); + let fee = handle_contract_result( + session.query( + contract.get_fee() + ).unwrap() + ); + + PairParams { + token_0, + token_1, + reserve_0, + reserve_1, + fee: fee as u128, + } + } + + fn casted_mul(a: u128, b: u128) -> U256 { + U256::from(a) * U256::from(b) + } +} + pub fn get_timestamp(session: &mut Session) -> u64 { session.sandbox().get_timestamp() } From d2ff1c5fcae1e681658c67b6a3266cd5c4e2edc4 Mon Sep 17 00:00:00 2001 From: woocash2 Date: Thu, 3 Oct 2024 14:17:19 +0200 Subject: [PATCH 3/5] working first test --- amm/drink-tests/src/router_v2_tests.rs | 66 ++++++++++++++++++++++++++ amm/drink-tests/src/utils.rs | 8 ++-- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/amm/drink-tests/src/router_v2_tests.rs b/amm/drink-tests/src/router_v2_tests.rs index 65159a3f..b57b2b5b 100644 --- a/amm/drink-tests/src/router_v2_tests.rs +++ b/amm/drink-tests/src/router_v2_tests.rs @@ -478,6 +478,33 @@ fn test_psp22_swap(mut session: Session) { let swap_amount = 100 * TOKEN; + 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()); + router_v2::swap_exact_tokens_for_tokens( &mut session, router.into(), @@ -503,6 +530,39 @@ fn test_psp22_swap(mut session: Session) { ) .expect("Should swap"); + 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(); + router_v2::swap_tokens_for_exact_tokens( &mut session, router.into(), @@ -528,6 +588,12 @@ fn test_psp22_swap(mut session: Session) { ) .expect("Should swap"); + 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()), diff --git a/amm/drink-tests/src/utils.rs b/amm/drink-tests/src/utils.rs index c30071cb..370552a7 100644 --- a/amm/drink-tests/src/utils.rs +++ b/amm/drink-tests/src/utils.rs @@ -308,6 +308,7 @@ pub mod router_v2 { .unwrap() } + #[allow(dead_code)] pub fn remove_pair_liquidity( session: &mut Session, router: AccountId, @@ -979,6 +980,7 @@ pub mod stable_swap { ) } + #[allow(dead_code)] pub fn get_mint_liquidity_for_amounts( session: &mut Session, stable_pool: AccountId, @@ -994,7 +996,7 @@ pub mod stable_swap { ) } - fn get_swap_amount_out( + pub fn get_swap_amount_out( session: &mut Session, stable_pool: AccountId, token_in: AccountId, @@ -1008,7 +1010,7 @@ pub mod stable_swap { ) } - fn get_swap_amount_in( + pub fn get_swap_amount_in( session: &mut Session, stable_pool: AccountId, token_in: AccountId, @@ -1025,7 +1027,7 @@ pub mod stable_swap { pub mod v2_amounts { use primitive_types::U256; - use crate::pair_contract::{MathError, Pair as _, PairError}; + use crate::pair_contract::{MathError, Pair}; use super::*; const PAIR_TRADING_FEE_DENOM: u128 = 1000; From f5e09d30dfc3670e39d915958215e26d644c7bd1 Mon Sep 17 00:00:00 2001 From: woocash2 Date: Thu, 3 Oct 2024 15:06:12 +0200 Subject: [PATCH 4/5] fmt --- amm/drink-tests/src/lib.rs | 4 +- amm/drink-tests/src/router_v2_tests.rs | 18 ++++++--- amm/drink-tests/src/utils.rs | 53 ++++++++++++-------------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/amm/drink-tests/src/lib.rs b/amm/drink-tests/src/lib.rs index 2c59a5ed..551aa4b5 100644 --- a/amm/drink-tests/src/lib.rs +++ b/amm/drink-tests/src/lib.rs @@ -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; diff --git a/amm/drink-tests/src/router_v2_tests.rs b/amm/drink-tests/src/router_v2_tests.rs index b57b2b5b..56cb0f3b 100644 --- a/amm/drink-tests/src/router_v2_tests.rs +++ b/amm/drink-tests/src/router_v2_tests.rs @@ -484,7 +484,8 @@ fn test_psp22_swap(mut session: Session) { ice.into(), usdc.into(), swap_amount, - ).unwrap(); + ) + .unwrap(); let second_step_output = stable_swap::get_swap_amount_out( &mut session, @@ -492,7 +493,8 @@ fn test_psp22_swap(mut session: Session) { usdc.into(), usdt.into(), first_step_output, - ).unwrap(); + ) + .unwrap(); let third_step_output = v2_amounts::get_amount_out( &mut session, @@ -500,7 +502,8 @@ fn test_psp22_swap(mut session: Session) { usdt.into(), wood.into(), second_step_output.0, - ).unwrap(); + ) + .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()); @@ -545,7 +548,8 @@ fn test_psp22_swap(mut session: Session) { usdt.into(), wood.into(), swap_amount, - ).unwrap(); + ) + .unwrap(); let second_step_input = stable_swap::get_swap_amount_in( &mut session, @@ -553,7 +557,8 @@ fn test_psp22_swap(mut session: Session) { usdc.into(), usdt.into(), third_step_input, - ).unwrap(); + ) + .unwrap(); let first_step_input = v2_amounts::get_amount_in( &mut session, @@ -561,7 +566,8 @@ fn test_psp22_swap(mut session: Session) { ice.into(), usdc.into(), second_step_input.0, - ).unwrap(); + ) + .unwrap(); router_v2::swap_tokens_for_exact_tokens( &mut session, diff --git a/amm/drink-tests/src/utils.rs b/amm/drink-tests/src/utils.rs index 370552a7..fd438b16 100644 --- a/amm/drink-tests/src/utils.rs +++ b/amm/drink-tests/src/utils.rs @@ -1004,9 +1004,15 @@ pub mod stable_swap { token_in_amount: u128, ) -> Result<(u128, u128), StablePoolError> { handle_contract_result( - session.query( - stable_pool_contract::Instance::from(stable_pool).get_swap_amount_out(token_in, token_out, token_in_amount) - ).unwrap() + session + .query( + stable_pool_contract::Instance::from(stable_pool).get_swap_amount_out( + token_in, + token_out, + token_in_amount, + ), + ) + .unwrap(), ) } @@ -1018,17 +1024,23 @@ pub mod stable_swap { token_out_amount: u128, ) -> Result<(u128, u128), StablePoolError> { handle_contract_result( - session.query( - stable_pool_contract::Instance::from(stable_pool).get_swap_amount_in(token_in, token_out, token_out_amount) - ).unwrap() + session + .query( + stable_pool_contract::Instance::from(stable_pool).get_swap_amount_in( + token_in, + token_out, + token_out_amount, + ), + ) + .unwrap(), ) } } pub mod v2_amounts { - use primitive_types::U256; - use crate::pair_contract::{MathError, Pair}; use super::*; + use crate::pair_contract::{MathError, Pair}; + use primitive_types::U256; const PAIR_TRADING_FEE_DENOM: u128 = 1000; @@ -1126,26 +1138,11 @@ pub mod v2_amounts { fn get_pair_params(session: &mut Session, pair: AccountId) -> PairParams { let contract = pair_contract::Instance::from(pair); - let token_0 = handle_contract_result( - session.query( - contract.get_token_0() - ).unwrap() - ); - let token_1 = handle_contract_result( - session.query( - contract.get_token_1() - ).unwrap() - ); - let (reserve_0, reserve_1, _) = handle_contract_result( - session.query( - contract.get_reserves() - ).unwrap() - ); - let fee = handle_contract_result( - session.query( - contract.get_fee() - ).unwrap() - ); + let token_0 = handle_contract_result(session.query(contract.get_token_0()).unwrap()); + let token_1 = handle_contract_result(session.query(contract.get_token_1()).unwrap()); + let (reserve_0, reserve_1, _) = + handle_contract_result(session.query(contract.get_reserves()).unwrap()); + let fee = handle_contract_result(session.query(contract.get_fee()).unwrap()); PairParams { token_0, From 071ec00fdb6258dfb7d8e1667b329a0340f7f91d Mon Sep 17 00:00:00 2001 From: woocash2 Date: Thu, 3 Oct 2024 15:12:29 +0200 Subject: [PATCH 5/5] compare router outputs --- amm/drink-tests/src/router_v2_tests.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/amm/drink-tests/src/router_v2_tests.rs b/amm/drink-tests/src/router_v2_tests.rs index 56cb0f3b..fe291f99 100644 --- a/amm/drink-tests/src/router_v2_tests.rs +++ b/amm/drink-tests/src/router_v2_tests.rs @@ -508,7 +508,7 @@ fn test_psp22_swap(mut session: Session) { 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()); - router_v2::swap_exact_tokens_for_tokens( + let amounts = router_v2::swap_exact_tokens_for_tokens( &mut session, router.into(), swap_amount, @@ -533,6 +533,12 @@ fn test_psp22_swap(mut session: Session) { ) .expect("Should swap"); + 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()); @@ -569,7 +575,7 @@ fn test_psp22_swap(mut session: Session) { ) .unwrap(); - router_v2::swap_tokens_for_exact_tokens( + let amounts = router_v2::swap_tokens_for_exact_tokens( &mut session, router.into(), swap_amount, @@ -594,6 +600,12 @@ 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());