From be46ac3ee155e90f09fd428284aa267634accf21 Mon Sep 17 00:00:00 2001 From: akildemir Date: Fri, 18 Oct 2024 09:50:49 +0300 Subject: [PATCH] bug fixes --- substrate/client/tests/dex.rs | 16 ++++++++-------- substrate/dex/pallet/src/lib.rs | 26 ++++++++++++-------------- substrate/dex/pallet/src/tests.rs | 4 ++-- substrate/runtime/src/lib.rs | 4 ++-- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/substrate/client/tests/dex.rs b/substrate/client/tests/dex.rs index f9e6f289..2503cc97 100644 --- a/substrate/client/tests/dex.rs +++ b/substrate/client/tests/dex.rs @@ -116,7 +116,7 @@ serai_test!( send_to: pair.public().into(), path, amount_in: amount_in.0, - amount_out: 16599866399465 + amount_out: 16611018363939 }] ); @@ -144,7 +144,7 @@ serai_test!( send_to: pair.public().into(), path, amount_in: amount_in.0, - amount_out: 17166744497317 + amount_out: 17207430166736 }] ); }) @@ -210,7 +210,7 @@ serai_test!( send_to: pair.public().into(), path, amount_in: amount_in.0, - amount_out: 12406166091918, + amount_out: 12421816676180, }] ); }) @@ -264,8 +264,8 @@ serai_test!( mint_to: pair.public().into(), pool_id: coin, coin_amount: 10_000_000_000_000, // half of sent amount - sri_amount: 111669009482, - lp_token_minted: 1055147701082 + sri_amount: 111631562261, + lp_token_minted: 1055499886564 }] ); }) @@ -349,7 +349,7 @@ serai_test!( send_to: IN_INSTRUCTION_EXECUTOR, path, amount_in: 200_000_000_000_000, - amount_out: 18933113030 + amount_out: 18970355346 }] ); } @@ -388,7 +388,7 @@ serai_test!( send_to: out_address.as_native().unwrap(), path, amount_in: 200_000_000_000, - amount_out: 1473437558561637 + amount_out: 1482888317565764 }] ); } @@ -426,7 +426,7 @@ serai_test!( send_to: out_address.as_native().unwrap(), path, amount_in: 100_000_000_000_000, - amount_out: 1751430396 + amount_out: 1755477054 }] ); } diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index 0b0d3d1f..30031a16 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -992,32 +992,30 @@ pub mod pallet { let (reserve_in, reserve_out) = Self::get_reserves(coin1, coin2)?; let prev_amount = amounts.last().expect("Always has at least one element"); let amount_out = Self::get_amount_out(*prev_amount, reserve_in, reserve_out)?; - 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)?, *coin1)?; + Self::burn_half_of_swap_fee(Self::get_pool_id(*coin1, *coin2)?, *coin1, *prev_amount)?; + + amounts.push(amount_out); } } Ok(amounts) } - fn burn_half_of_swap_fee(pool: PoolId, coin: Coin) -> Result<(), DispatchError> { + fn burn_half_of_swap_fee( + pool: PoolId, + coin: Coin, + amount: SubstrateAmount, + ) -> Result<(), DispatchError> { let pool_account = Self::get_pool_account(pool); - let origin = RawOrigin::Signed(pool_account); - let balance = Coins::::balance(pool_account, coin).0; - let burn_percent = - HigherPrecisionBalance::from(T::LPFee::get()).checked_div(2).ok_or(Error::::Overflow)?; - - let burn_amount = HigherPrecisionBalance::from(balance) - .checked_mul(burn_percent) - .ok_or(Error::::Overflow)? - .checked_div(1000) - .ok_or(Error::::Overflow)?; + // half of the taken fee + let burn_percent = T::LPFee::get().checked_div(2).ok_or(Error::::Overflow)?; + let burn_amount = Self::mul_div(amount, burn_percent.into(), 1000)?; Coins::::burn( - origin.into(), + RawOrigin::Signed(pool_account).into(), Balance { coin, amount: Amount(burn_amount.try_into().map_err(|_| Error::::Overflow)?) }, )?; Ok(()) diff --git a/substrate/dex/pallet/src/tests.rs b/substrate/dex/pallet/src/tests.rs index 4c3030b9..82fa0a1b 100644 --- a/substrate/dex/pallet/src/tests.rs +++ b/substrate/dex/pallet/src/tests.rs @@ -1157,10 +1157,10 @@ fn swap_exact_tokens_for_tokens_in_multi_hops() { )); // burn half of the taken fees - let burn_amount = get_burn_amount(liquidity2); + let burn_amount = get_burn_amount(input_amount); liquidity2 -= burn_amount; - let burn_amount = get_burn_amount(liquidity1_pool2); + let burn_amount = get_burn_amount(expect_out2); liquidity1_pool2 -= burn_amount; let pool_id1 = Dex::get_pool_id(coin1, coin2).unwrap(); diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index 9b76a08f..048c7b9e 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -215,8 +215,8 @@ impl coins::Config for Runtime { impl dex::Config for Runtime { type RuntimeEvent = RuntimeEvent; - // 0.6% in total but only half will go to LPs(0.3%) other half to be burned. - type LPFee = ConstU32<6>; + // 0.5% in total but only half will go to LPs(0.25%) other half to be burned. + type LPFee = ConstU32<5>; type MintMinLiquidity = ConstU64<10000>; type MaxSwapPathLength = ConstU32<3>; // coin1 -> SRI -> coin2