diff --git a/substrate/client/tests/dex.rs b/substrate/client/tests/dex.rs index 21256e29..78e42783 100644 --- a/substrate/client/tests/dex.rs +++ b/substrate/client/tests/dex.rs @@ -131,7 +131,7 @@ serai_test!( send_to: pair.public().into(), path, amount_in: amount_in.0, - amount_out: 17260886638951 + amount_out: 17166744497317 }] ); }) @@ -246,8 +246,8 @@ serai_test!( mint_to: pair.public().into(), pool_id: Coin::Bitcoin, coin_amount: 10_000_000_000_000, // half of sent amount - sri_amount: 110557340473, - lp_token_minted: 1054092553386 + sri_amount: 111669009482, + lp_token_minted: 1055147701082 }] ); }) @@ -372,7 +372,7 @@ serai_test!( send_to: out_address.as_native().unwrap(), path, amount_in: 200_000_000_000, - amount_out: 1487256912435088 + amount_out: 1473437558561637 }] ); } @@ -410,7 +410,7 @@ serai_test!( send_to: out_address.as_native().unwrap(), path, amount_in: 100_000_000_000_000, - amount_out: 1760904169 + amount_out: 1751430396 }] ); } diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index 1974c8f7..f69a0c67 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -981,7 +981,7 @@ pub mod pallet { amounts.push(amount_out); // now that we got swap fee from the user, burn half of it. - Self::burn_half_of_swap_fee(Self::get_pool_id(*coin1, *coin2)?, *coin2)?; + Self::burn_half_of_swap_fee(Self::get_pool_id(*coin1, *coin2)?, *coin1)?; } } @@ -990,7 +990,7 @@ pub mod pallet { fn burn_half_of_swap_fee(pool: PoolId, coin: Coin) -> Result<(), DispatchError> { let pool_account = Self::get_pool_account(pool); - let origin = RawOrigin::Signed(pool_account.into()); + let origin = RawOrigin::Signed(pool_account); let balance = Coins::::balance(pool_account, coin).0; let burn_percent = diff --git a/substrate/dex/pallet/src/tests.rs b/substrate/dex/pallet/src/tests.rs index ee714471..e7c367c3 100644 --- a/substrate/dex/pallet/src/tests.rs +++ b/substrate/dex/pallet/src/tests.rs @@ -26,6 +26,7 @@ pub use coins_pallet as coins; use coins::Pallet as CoinsPallet; use serai_primitives::{Balance, COINS, PublicKey, system_address, Amount}; +use sp_core::Get; type LiquidityTokens = coins_pallet::Pallet; type LiquidityTokensError = coins_pallet::Error; @@ -60,6 +61,20 @@ fn pool_balance(owner: PublicKey, token_id: Coin) -> u64 { LiquidityTokens::::balance(owner, token_id).0 } +fn get_burn_amount(liquidity: u64) -> u64 { + // burn half of the swap fee left in the pool + let burn_percent = HigherPrecisionBalance::from(<::LPFee as Get>::get()) + .checked_div(2) + .unwrap(); + + let burn_amount = HigherPrecisionBalance::from(liquidity) + .checked_mul(burn_percent) + .unwrap() + .checked_div(1000) + .unwrap(); + u64::try_from(burn_amount).unwrap() +} + macro_rules! bvec { ($( $x:tt )*) => { vec![$( $x )*].try_into().unwrap() @@ -1013,15 +1028,16 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() { assert_ok!(CoinsPallet::::mint(user, Balance { coin: coin2, amount: Amount(base2) })); assert_ok!(CoinsPallet::::mint(user, Balance { coin: coin3, amount: Amount(base2) })); - let liquidity1 = 10000; - let liquidity2 = 200; + let liquidity1_pool1 = 10000; + let mut liquidity1_pool2 = liquidity1_pool1; + let mut liquidity2 = 200; let liquidity3 = 2000; assert_ok!(Dex::add_liquidity( RuntimeOrigin::signed(user), coin2, liquidity2, - liquidity1, + liquidity1_pool1, 1, 1, user, @@ -1030,15 +1046,15 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() { RuntimeOrigin::signed(user), coin3, liquidity3, - liquidity1, + liquidity1_pool2, 1, 1, user, )); let input_amount = 500; - let expect_out2 = Dex::get_amount_out(input_amount, liquidity2, liquidity1).ok().unwrap(); - let expect_out3 = Dex::get_amount_out(expect_out2, liquidity1, liquidity3).ok().unwrap(); + let expect_out2 = Dex::get_amount_out(input_amount, liquidity2, liquidity1_pool1).ok().unwrap(); + let expect_out3 = Dex::get_amount_out(expect_out2, liquidity1_pool2, liquidity3).ok().unwrap(); assert_noop!( Dex::swap_exact_tokens_for_tokens( @@ -1070,6 +1086,13 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() { user, )); + // burn half of the taken fees + let burn_amount = get_burn_amount(liquidity2); + liquidity2 -= burn_amount; + + let burn_amount = get_burn_amount(liquidity1_pool2); + liquidity1_pool2 -= burn_amount; + let pool_id1 = Dex::get_pool_id(coin1, coin2).unwrap(); let pool_id2 = Dex::get_pool_id(coin1, coin3).unwrap(); let pallet_account1 = Dex::get_pool_account(pool_id1); @@ -1077,8 +1100,8 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() { assert_eq!(balance(user, coin2), base2 - liquidity2 - input_amount); assert_eq!(balance(pallet_account1, coin2), liquidity2 + input_amount); - assert_eq!(balance(pallet_account1, coin1), liquidity1 - expect_out2); - assert_eq!(balance(pallet_account2, coin1), liquidity1 + expect_out2); + assert_eq!(balance(pallet_account1, coin1), liquidity1_pool1 - expect_out2); + assert_eq!(balance(pallet_account2, coin1), liquidity1_pool2 + expect_out2); assert_eq!(balance(pallet_account2, coin3), liquidity3 - expect_out3); assert_eq!(balance(user, coin3), 10000 - liquidity3 + expect_out3); });