Skip to content

Commit

Permalink
set correct swap fee, burn half
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Sep 2, 2024
1 parent 4e83487 commit 457638f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
16 changes: 8 additions & 8 deletions substrate/client/tests/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 16633299966633
amount_out: 16599866399465
}]
);

Expand All @@ -131,7 +131,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 17254428681101
amount_out: 17260886638951
}]
);
})
Expand Down Expand Up @@ -191,7 +191,7 @@ serai_test!(
send_to: pair.public().into(),
path,
amount_in: amount_in.0,
amount_out: 12453103964435,
amount_out: 12406166091918,
}]
);
})
Expand Down Expand Up @@ -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
}]
);
})
Expand Down Expand Up @@ -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
}]
);
}
Expand Down Expand Up @@ -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
}]
);
}
Expand Down Expand Up @@ -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
}]
);
}
Expand Down
32 changes: 28 additions & 4 deletions substrate/dex/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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};

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 457638f

Please sign in to comment.