set correct swap fee, burn half

This commit is contained in:
akildemir 2024-09-02 16:40:32 +03:00
parent 4e834873d3
commit 457638f776
3 changed files with 38 additions and 13 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: 17260886638951
}]
);
})
@ -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: 110557340473,
lp_token_minted: 1054092553386
}]
);
})
@ -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: 1487256912435088
}]
);
}
@ -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: 1760904169
}]
);
}

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)?, *coin2)?;
}
}
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.into());
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

@ -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