mirror of
https://github.com/serai-dex/serai.git
synced 2025-03-12 09:26:51 +00:00
bug fixes
This commit is contained in:
parent
457638f776
commit
eea7cc06aa
3 changed files with 38 additions and 15 deletions
|
@ -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
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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::<T>::balance(pool_account, coin).0;
|
||||
let burn_percent =
|
||||
|
|
|
@ -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<T> = coins_pallet::Pallet<T, coins::Instance1>;
|
||||
type LiquidityTokensError<T> = coins_pallet::Error<T, coins::Instance1>;
|
||||
|
@ -60,6 +61,20 @@ fn pool_balance(owner: PublicKey, token_id: Coin) -> u64 {
|
|||
LiquidityTokens::<Test>::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(<<Test as Config>::LPFee as Get<u32>>::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::<Test>::mint(user, Balance { coin: coin2, amount: Amount(base2) }));
|
||||
assert_ok!(CoinsPallet::<Test>::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);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue