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

Changed LPFee to Permill type in Asset Conversion Pallet #14823

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 10 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,11 @@ 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().deconstruct());

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 +1171,13 @@ pub mod pallet {
.checked_mul(&1000u32.into())
.ok_or(Error::<T>::Overflow)?;

let fee_mult =
T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get().deconstruct());
Copy link
Contributor

@xlc xlc Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just confusing and not a good usage of Permill type


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