2 Commits

Author SHA1 Message Date
akildemir
eea7cc06aa bug fixes 2024-09-03 13:42:30 +03:00
akildemir
457638f776 set correct swap fee, burn half 2024-09-02 16:40:32 +03:00
4 changed files with 69 additions and 21 deletions

View File

@@ -111,7 +111,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 16633299966633
amount_out: 16599866399465
}]
);
@@ -131,7 +131,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 17254428681101
amount_out: 17166744497317
}]
);
})
@@ -191,7 +191,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 12453103964435,
amount_out: 12406166091918,
}]
);
})
@@ -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: 111_333_778_668,
lp_token_minted: 1_054_092_553_383
sri_amount: 111669009482,
lp_token_minted: 1055147701082
}]
);
})
@@ -333,7 +333,7 @@ serai_test!(
send_to: IN_INSTRUCTION_EXECUTOR,
path,
amount_in: 200_000_000_000_000,
amount_out: 19_044_944_233
amount_out: 18933113030
}]
);
}
@@ -372,7 +372,7 @@ serai_test!(
send_to: out_address.as_native().unwrap(),
path,
amount_in: 200_000_000_000,
amount_out: 1487294253782353
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: 1_762_662_819
amount_out: 1751430396
}]
);
}

View File

@@ -81,7 +81,7 @@ mod mock;
use frame_support::ensure;
use frame_system::{
pallet_prelude::{BlockNumberFor, OriginFor},
ensure_signed,
ensure_signed, RawOrigin,
};
pub use pallet::*;
@@ -108,7 +108,7 @@ pub mod pallet {
use sp_core::sr25519::Public;
use sp_runtime::traits::IntegerSquareRoot;
use coins_pallet::{Pallet as CoinsPallet, Config as CoinsConfig};
use coins_pallet::{Pallet as Coins, Config as CoinsConfig};
use serai_primitives::{Coin, Amount, Balance, SubstrateAmount, reverse_lexicographic_order};
@@ -812,7 +812,7 @@ pub mod pallet {
to: &T::AccountId,
balance: Balance,
) -> Result<Amount, DispatchError> {
CoinsPallet::<T>::transfer_internal(*from, *to, balance)?;
Coins::<T>::transfer_internal(*from, *to, balance)?;
Ok(balance.amount)
}
@@ -911,7 +911,7 @@ pub mod pallet {
/// Get the `owner`'s balance of `coin`, which could be the chain's native coin or another
/// fungible. Returns a value in the form of an `Amount`.
fn get_balance(owner: &T::AccountId, coin: Coin) -> SubstrateAmount {
CoinsPallet::<T>::balance(*owner, coin).0
Coins::<T>::balance(*owner, coin).0
}
/// Returns a pool id constructed from 2 coins.
@@ -979,12 +979,36 @@ pub mod pallet {
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)?;
}
}
Ok(amounts)
}
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);
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)?;
Coins::<T>::burn(
origin.into(),
Balance { coin, amount: Amount(burn_amount.try_into().map_err(|_| Error::<T>::Overflow)?) },
)?;
Ok(())
}
/// Used by the RPC service to provide current prices.
pub fn quote_price_exact_tokens_for_tokens(
coin1: Coin,

View File

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

View File

@@ -214,7 +214,8 @@ impl coins::Config<coins::Instance1> for Runtime {
impl dex::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type LPFee = ConstU32<3>; // 0.3%
// 0.6% in total but only half will go to LPs(0.3%) other half to be burned.
type LPFee = ConstU32<6>;
type MintMinLiquidity = ConstU64<10000>;
type MaxSwapPathLength = ConstU32<3>; // coin1 -> SRI -> coin2