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

@ -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
}]
);
}

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

View file

@ -215,8 +215,8 @@ impl coins::Config<coins::Instance1> 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