diff --git a/substrate/client/tests/dex.rs b/substrate/client/tests/dex.rs index d02d52602..21256e290 100644 --- a/substrate/client/tests/dex.rs +++ b/substrate/client/tests/dex.rs @@ -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 }] ); } diff --git a/substrate/dex/pallet/src/lib.rs b/substrate/dex/pallet/src/lib.rs index 60a38926b..1974c8f7d 100644 --- a/substrate/dex/pallet/src/lib.rs +++ b/substrate/dex/pallet/src/lib.rs @@ -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 { - CoinsPallet::::transfer_internal(*from, *to, balance)?; + Coins::::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::::balance(*owner, coin).0 + Coins::::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::::balance(pool_account, coin).0; + let burn_percent = + HigherPrecisionBalance::from(T::LPFee::get()).checked_div(2).ok_or(Error::::Overflow)?; + + let burn_amount = HigherPrecisionBalance::from(balance) + .checked_mul(burn_percent) + .ok_or(Error::::Overflow)? + .checked_div(1000) + .ok_or(Error::::Overflow)?; + + Coins::::burn( + origin.into(), + Balance { coin, amount: Amount(burn_amount.try_into().map_err(|_| Error::::Overflow)?) }, + )?; + Ok(()) + } + /// Used by the RPC service to provide current prices. pub fn quote_price_exact_tokens_for_tokens( coin1: Coin, diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index e55270cb9..21898ebab 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -214,7 +214,8 @@ impl coins::Config 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