From f421eb799b590fd293a45311ec12a894b8b046cc Mon Sep 17 00:00:00 2001 From: Patricio Napoli Date: Thu, 24 Aug 2023 02:36:50 -0300 Subject: [PATCH 1/3] changed LPFee to Permill type --- bin/node/runtime/src/lib.rs | 3 ++- frame/asset-conversion/src/lib.rs | 13 ++++++++----- frame/asset-conversion/src/mock.rs | 3 ++- .../asset-conversion-tx-payment/src/mock.rs | 5 +++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b536b9547b71f..fd006bb2de2c5 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -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 { @@ -1571,7 +1572,7 @@ impl pallet_asset_conversion::Config for Runtime { type MultiAssetId = NativeOrAssetId; type PoolAssetId = >::AssetId; type PalletId = AssetConversionPalletId; - type LPFee = ConstU32<3>; // means 0.3% + type LPFee = LiquidityPoolFee; type PoolSetupFee = PoolSetupFee; type PoolSetupFeeReceiver = AssetConversionOrigin; type LiquidityWithdrawalFee = LiquidityWithdrawalFee; diff --git a/frame/asset-conversion/src/lib.rs b/frame/asset-conversion/src/lib.rs index d1d68f3e10fbb..f5535952666f1 100644 --- a/frame/asset-conversion/src/lib.rs +++ b/frame/asset-conversion/src/lib.rs @@ -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; + type LPFee: Get; /// A one-time fee to setup the pool. #[pallet::constant] @@ -1124,9 +1124,10 @@ pub mod pallet { return Err(Error::::ZeroLiquidity.into()) } - let amount_in_with_fee = amount_in - .checked_mul(&(T::HigherPrecisionBalance::from(1000u32) - (T::LPFee::get().into()))) - .ok_or(Error::::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::::Overflow)?; let numerator = amount_in_with_fee.checked_mul(&reserve_out).ok_or(Error::::Overflow)?; @@ -1169,10 +1170,12 @@ pub mod pallet { .checked_mul(&1000u32.into()) .ok_or(Error::::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::::Overflow)? - .checked_mul(&(T::HigherPrecisionBalance::from(1000u32) - T::LPFee::get().into())) + .checked_mul(&fee_mult) .ok_or(Error::::Overflow)?; let result = numerator diff --git a/frame/asset-conversion/src/mock.rs b/frame/asset-conversion/src/mock.rs index 7fe81b814047d..3b9698b0ceaf4 100644 --- a/frame/asset-conversion/src/mock.rs +++ b/frame/asset-conversion/src/mock.rs @@ -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! { @@ -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; diff --git a/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs b/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs index bfbe8b4178cee..ba91436b1e13d 100644 --- a/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs +++ b/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs @@ -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; @@ -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; } @@ -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; From fb6ea183b8c5fd90f1003526efded1871697090c Mon Sep 17 00:00:00 2001 From: Patricio Napoli Date: Thu, 24 Aug 2023 02:51:17 -0300 Subject: [PATCH 2/3] moved to .deconstruct() --- frame/asset-conversion/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frame/asset-conversion/src/lib.rs b/frame/asset-conversion/src/lib.rs index f5535952666f1..a7fd5174f6cd8 100644 --- a/frame/asset-conversion/src/lib.rs +++ b/frame/asset-conversion/src/lib.rs @@ -1124,7 +1124,8 @@ pub mod pallet { return Err(Error::::ZeroLiquidity.into()) } - let fee_mult = T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get() * 1_u32); + 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::::Overflow)?; @@ -1170,7 +1171,8 @@ pub mod pallet { .checked_mul(&1000u32.into()) .ok_or(Error::::Overflow)?; - let fee_mult = T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get() * 1_u32); + let fee_mult = + T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get().deconstruct()); let denominator = reserve_out .checked_sub(&amount_out) From 8add6c3deeabfc9cbf1778a249dc71f6d0cf9b87 Mon Sep 17 00:00:00 2001 From: Patricio Napoli Date: Thu, 24 Aug 2023 03:01:02 -0300 Subject: [PATCH 3/3] fixed to thousandth --- frame/asset-conversion/src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/frame/asset-conversion/src/lib.rs b/frame/asset-conversion/src/lib.rs index a7fd5174f6cd8..12a388835b0ae 100644 --- a/frame/asset-conversion/src/lib.rs +++ b/frame/asset-conversion/src/lib.rs @@ -1124,8 +1124,9 @@ pub mod pallet { return Err(Error::::ZeroLiquidity.into()) } - let fee_mult = - T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get().deconstruct()); + let fee_mult = T::HigherPrecisionBalance::from( + (1_000_000u32 - T::LPFee::get().deconstruct()) / 1000, // Reduce to thousand + ); let amount_in_with_fee = amount_in.checked_mul(&fee_mult).ok_or(Error::::Overflow)?; @@ -1171,8 +1172,9 @@ pub mod pallet { .checked_mul(&1000u32.into()) .ok_or(Error::::Overflow)?; - let fee_mult = - T::HigherPrecisionBalance::from(1_000_000u32 - T::LPFee::get().deconstruct()); + let fee_mult = T::HigherPrecisionBalance::from( + (1_000_000u32 - T::LPFee::get().deconstruct()) / 1000, // Reduce to thousand + ); let denominator = reserve_out .checked_sub(&amount_out)