From 7ff5538dee86865458a02772b7b3882d5a9b4adf Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 17 Nov 2022 11:11:12 +0100 Subject: [PATCH 1/6] check liqui in swaps --- contracts/simple_dex/lib.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/contracts/simple_dex/lib.rs b/contracts/simple_dex/lib.rs index c5a2df20f1..4652e1da19 100644 --- a/contracts/simple_dex/lib.rs +++ b/contracts/simple_dex/lib.rs @@ -153,6 +153,12 @@ mod simple_dex { let this = self.env().account_id(); let caller = self.env().caller(); + let balance_token_out = self.balance_of(token_out, this)?; + if balance_token_out < min_amount_token_out { + // throw early if we cannot support this swap anyway due to liquidity being too low + return Err(DexError::NotEnoughLiquidityOf(token_out)); + } + let swap_pair = SwapPair::new(token_in, token_out); if !self.swap_pairs.contains(&swap_pair) { return Err(DexError::UnsupportedSwapPair(swap_pair)); @@ -163,12 +169,7 @@ mod simple_dex { return Err(DexError::InsufficientAllowanceOf(token_in)); } - let amount_token_out = self.out_given_in( - token_in, - token_out, - amount_token_in, - Some(min_amount_token_out), - )?; + let amount_token_out = self.out_given_in(token_in, token_out, amount_token_in)?; if amount_token_out < min_amount_token_out { // thrown if too much slippage occured before this tx gets executed @@ -368,17 +369,11 @@ mod simple_dex { token_in: AccountId, token_out: AccountId, amount_token_in: Balance, - min_amount_token_out: Option, ) -> Result { let this = self.env().account_id(); let balance_token_in = self.balance_of(token_in, this)?; let balance_token_out = self.balance_of(token_out, this)?; - if min_amount_token_out.map_or(false, |x| balance_token_out < x) { - // throw early if we cannot support this swap anyway due to liquidity being too low - return Err(DexError::NotEnoughLiquidityOf(token_out)); - } - let op0 = amount_token_in .checked_mul(self.swap_fee_percentage) .ok_or(DexError::Arithmethic)?; From f7dc88614274fa92ff5eab2427b2b093fdd433a0 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 17 Nov 2022 12:23:13 +0100 Subject: [PATCH 2/6] refactor tests too --- contracts/simple_dex/lib.rs | 5 +++-- e2e-tests/src/test/button_game/contracts.rs | 2 -- e2e-tests/src/test/button_game/mod.rs | 11 ++++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/contracts/simple_dex/lib.rs b/contracts/simple_dex/lib.rs index 4652e1da19..c848b08eb1 100644 --- a/contracts/simple_dex/lib.rs +++ b/contracts/simple_dex/lib.rs @@ -361,8 +361,9 @@ mod simple_dex { /// Swap trade output given a curve with equal token weights /// - /// swap_fee_percentage (integer) is a percentage of the trade that goes towards the pool - /// B_0 - (100 * B_0 * B_i) / (100 * (B_i + A_i) -A_i * fee) + /// B_0 - (100 * B_0 * B_i) / (100 * (B_i + A_i) - A_i * swap_fee) + /// where swap_fee (integer) is a percentage of the trade that goes towards the pool + /// and is used to pay the liquidity providers #[ink(message)] pub fn out_given_in( &self, diff --git a/e2e-tests/src/test/button_game/contracts.rs b/e2e-tests/src/test/button_game/contracts.rs index 8dc6188840..247945ce4a 100644 --- a/e2e-tests/src/test/button_game/contracts.rs +++ b/e2e-tests/src/test/button_game/contracts.rs @@ -78,7 +78,6 @@ impl SimpleDexInstance { token_in: &PSP22TokenInstance, token_out: &PSP22TokenInstance, amount_token_in: Balance, - min_amount_token_out: Option, ) -> Result { let token_in: AccountId = token_in.into(); let token_out: AccountId = token_out.into(); @@ -91,7 +90,6 @@ impl SimpleDexInstance { token_in.to_string(), token_out.to_string(), amount_token_in.to_string(), - min_amount_token_out.map_or("None".to_string(), |x| format!("Some({:})", x)), ], )? .try_into()? diff --git a/e2e-tests/src/test/button_game/mod.rs b/e2e-tests/src/test/button_game/mod.rs index 38919df53f..8d059aef42 100644 --- a/e2e-tests/src/test/button_game/mod.rs +++ b/e2e-tests/src/test/button_game/mod.rs @@ -71,14 +71,19 @@ pub fn simple_dex(config: &Config) -> Result<()> { ], )?; + assert!( + dex.out_given_in(account_conn, token1, token2, 100).is_ok(), + "out_given_in should always return" + ); + let more_than_liquidity = mega(1_000_000); assert!(dex - .out_given_in(account_conn, token1, token2, 100, Some(more_than_liquidity)) + .swap(account_conn, token1, 100, token2, more_than_liquidity) .is_err()); let initial_amount = mega(100); token1.mint(authority_conn, &account.public().into(), initial_amount)?; - let expected_output = dex.out_given_in(account_conn, token1, token2, initial_amount, None)?; + let expected_output = dex.out_given_in(account_conn, token1, token2, initial_amount)?; assert!(expected_output > 0); let at_most_10_percent_slippage = expected_output * 9 / 10; @@ -100,7 +105,7 @@ pub fn simple_dex(config: &Config) -> Result<()> { let balance_after = token1.balance_of(&conn, &account.public().into())?; assert!(initial_amount.abs_diff(balance_after) <= 1); assert!( - dex.out_given_in(account_conn, token1, token2, initial_amount, None)? + dex.out_given_in(account_conn, token1, token2, initial_amount)? .abs_diff(expected_output) <= 1 ); From cf8057100a960f4bc9d95afd47c44ba995938aa7 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 17 Nov 2022 12:48:07 +0100 Subject: [PATCH 3/6] cleanup --- e2e-tests/src/test/button_game/mod.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/e2e-tests/src/test/button_game/mod.rs b/e2e-tests/src/test/button_game/mod.rs index 8d059aef42..b06afb2763 100644 --- a/e2e-tests/src/test/button_game/mod.rs +++ b/e2e-tests/src/test/button_game/mod.rs @@ -76,11 +76,6 @@ pub fn simple_dex(config: &Config) -> Result<()> { "out_given_in should always return" ); - let more_than_liquidity = mega(1_000_000); - assert!(dex - .swap(account_conn, token1, 100, token2, more_than_liquidity) - .is_err()); - let initial_amount = mega(100); token1.mint(authority_conn, &account.public().into(), initial_amount)?; let expected_output = dex.out_given_in(account_conn, token1, token2, initial_amount)?; From 00c6699dab774157ed43fd34500250c3755af1bf Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 17 Nov 2022 12:53:10 +0100 Subject: [PATCH 4/6] check swap fail due to not enoigh liqui --- e2e-tests/src/test/button_game/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/e2e-tests/src/test/button_game/mod.rs b/e2e-tests/src/test/button_game/mod.rs index b06afb2763..22123c540e 100644 --- a/e2e-tests/src/test/button_game/mod.rs +++ b/e2e-tests/src/test/button_game/mod.rs @@ -76,6 +76,10 @@ pub fn simple_dex(config: &Config) -> Result<()> { "out_given_in should always return" ); + let more_than_liquidity = mega(1_000_000); + dex.swap(account_conn, token1, 100, token2, more_than_liquidity)?; + refute_recv_id(&mut events, "Swapped"); + let initial_amount = mega(100); token1.mint(authority_conn, &account.public().into(), initial_amount)?; let expected_output = dex.out_given_in(account_conn, token1, token2, initial_amount)?; From afc867631dad21477f2d3467d19a14a0c046ef16 Mon Sep 17 00:00:00 2001 From: Filip Bielejec Date: Thu, 17 Nov 2022 13:00:04 +0100 Subject: [PATCH 5/6] Update e2e-tests/src/test/button_game/mod.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Paweł Obrok --- e2e-tests/src/test/button_game/mod.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/e2e-tests/src/test/button_game/mod.rs b/e2e-tests/src/test/button_game/mod.rs index 22123c540e..54ea61c945 100644 --- a/e2e-tests/src/test/button_game/mod.rs +++ b/e2e-tests/src/test/button_game/mod.rs @@ -71,10 +71,6 @@ pub fn simple_dex(config: &Config) -> Result<()> { ], )?; - assert!( - dex.out_given_in(account_conn, token1, token2, 100).is_ok(), - "out_given_in should always return" - ); let more_than_liquidity = mega(1_000_000); dex.swap(account_conn, token1, 100, token2, more_than_liquidity)?; From 7fde1d59feb579c9ba5e15a44c65bc025cd58c44 Mon Sep 17 00:00:00 2001 From: filip Date: Thu, 17 Nov 2022 14:17:07 +0100 Subject: [PATCH 6/6] ugh fmt --- e2e-tests/src/test/button_game/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/e2e-tests/src/test/button_game/mod.rs b/e2e-tests/src/test/button_game/mod.rs index 54ea61c945..4ffc399750 100644 --- a/e2e-tests/src/test/button_game/mod.rs +++ b/e2e-tests/src/test/button_game/mod.rs @@ -71,7 +71,6 @@ pub fn simple_dex(config: &Config) -> Result<()> { ], )?; - let more_than_liquidity = mega(1_000_000); dex.swap(account_conn, token1, 100, token2, more_than_liquidity)?; refute_recv_id(&mut events, "Swapped");