bug fixes

This commit is contained in:
akildemir
2024-10-18 09:50:49 +03:00
parent a116bdfea2
commit be46ac3ee1
4 changed files with 24 additions and 26 deletions

View File

@@ -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::<T>::balance(pool_account, coin).0;
let burn_percent =
HigherPrecisionBalance::from(T::LPFee::get()).checked_div(2).ok_or(Error::<T>::Overflow)?;
let burn_amount = HigherPrecisionBalance::from(balance)
.checked_mul(burn_percent)
.ok_or(Error::<T>::Overflow)?
.checked_div(1000)
.ok_or(Error::<T>::Overflow)?;
// half of the taken fee
let burn_percent = T::LPFee::get().checked_div(2).ok_or(Error::<T>::Overflow)?;
let burn_amount = Self::mul_div(amount, burn_percent.into(), 1000)?;
Coins::<T>::burn(
origin.into(),
RawOrigin::Signed(pool_account).into(),
Balance { coin, amount: Amount(burn_amount.try_into().map_err(|_| Error::<T>::Overflow)?) },
)?;
Ok(())

View File

@@ -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();