Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
changed LPFee to Permill type
Browse files Browse the repository at this point in the history
  • Loading branch information
PatricioNapoli committed Aug 24, 2023
1 parent b7c18f1 commit f421eb7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ parameter_types! {
pub const PoolSetupFee: Balance = 1 * DOLLARS; // should be more or equal to the existential deposit
pub const MintMinLiquidity: Balance = 100; // 100 is good enough when the main currency has 10-12 decimals.
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero.
pub const LiquidityPoolFee: Permill = Permill::from_parts(3000); // 0.3%
}

impl pallet_asset_conversion::Config for Runtime {
Expand All @@ -1571,7 +1572,7 @@ impl pallet_asset_conversion::Config for Runtime {
type MultiAssetId = NativeOrAssetId<u32>;
type PoolAssetId = <Self as pallet_assets::Config<Instance2>>::AssetId;
type PalletId = AssetConversionPalletId;
type LPFee = ConstU32<3>; // means 0.3%
type LPFee = LiquidityPoolFee;
type PoolSetupFee = PoolSetupFee;
type PoolSetupFeeReceiver = AssetConversionOrigin;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
Expand Down
13 changes: 8 additions & 5 deletions frame/asset-conversion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub mod pallet {

/// A % the liquidity providers will take of every swap. Represents 10ths of a percent.
#[pallet::constant]
type LPFee: Get<u32>;
type LPFee: Get<Permill>;

/// A one-time fee to setup the pool.
#[pallet::constant]
Expand Down Expand Up @@ -1124,9 +1124,10 @@ pub mod pallet {
return Err(Error::<T>::ZeroLiquidity.into())
}

let amount_in_with_fee = amount_in
.checked_mul(&(T::HigherPrecisionBalance::from(1000u32) - (T::LPFee::get().into())))
.ok_or(Error::<T>::Overflow)?;
let fee_mult = T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get() * 1_u32);

let amount_in_with_fee =
amount_in.checked_mul(&fee_mult).ok_or(Error::<T>::Overflow)?;

let numerator =
amount_in_with_fee.checked_mul(&reserve_out).ok_or(Error::<T>::Overflow)?;
Expand Down Expand Up @@ -1169,10 +1170,12 @@ pub mod pallet {
.checked_mul(&1000u32.into())
.ok_or(Error::<T>::Overflow)?;

let fee_mult = T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get() * 1_u32);

let denominator = reserve_out
.checked_sub(&amount_out)
.ok_or(Error::<T>::Overflow)?
.checked_mul(&(T::HigherPrecisionBalance::from(1000u32) - T::LPFee::get().into()))
.checked_mul(&fee_mult)
.ok_or(Error::<T>::Overflow)?;

let result = numerator
Expand Down
3 changes: 2 additions & 1 deletion frame/asset-conversion/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub storage AllowMultiAssetPools: bool = true;
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub LiquidityPoolFee: Permill = Permill::from_parts(3000); // 0.3%
}

ord_parameter_types! {
Expand All @@ -159,7 +160,7 @@ impl Config for Test {
type PoolAssets = PoolAssets;
type PalletId = AssetConversionPalletId;
type WeightInfo = ();
type LPFee = ConstU32<3>; // means 0.3%
type LPFee = LiquidityPoolFee;
type PoolSetupFee = ConstU128<100>; // should be more or equal to the existential deposit
type PoolSetupFeeReceiver = AssetConversionOrigin;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use pallet_transaction_payment::CurrencyAdapter;
use sp_core::H256;
use sp_runtime::{
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, SaturatedConversion},
Permill,
Percent, Permill,
};

type Block = frame_system::mocking::MockBlock<Runtime>;
Expand Down Expand Up @@ -225,6 +225,7 @@ parameter_types! {
pub storage AllowMultiAssetPools: bool = false;
// should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
pub LiquidityPoolFee: Permill = Permill::from_parts(3000); // 0.3%
pub const MaxSwapPathLength: u32 = 4;
}

Expand All @@ -242,7 +243,7 @@ impl pallet_asset_conversion::Config for Runtime {
type PoolAssets = PoolAssets;
type PalletId = AssetConversionPalletId;
type WeightInfo = ();
type LPFee = ConstU32<3>; // means 0.3%
type LPFee = LiquidityPoolFee;
type PoolSetupFee = ConstU64<100>; // should be more or equal to the existential deposit
type PoolSetupFeeReceiver = AssetConversionOrigin;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
Expand Down

0 comments on commit f421eb7

Please sign in to comment.