diff --git a/modules/transaction-payment/src/lib.rs b/modules/transaction-payment/src/lib.rs index d614539b8..81fd4d1e0 100644 --- a/modules/transaction-payment/src/lib.rs +++ b/modules/transaction-payment/src/lib.rs @@ -218,6 +218,7 @@ pub mod module { use super::*; pub const RESERVE_ID: ReserveIdentifier = ReserveIdentifier::TransactionPayment; + pub const DEPOSIT_ID: ReserveIdentifier = ReserveIdentifier::TransactionPaymentDeposit; #[pallet::config] pub trait Config: frame_system::Config { @@ -281,6 +282,10 @@ pub mod module { #[pallet::constant] type MaxTipsOfPriority: Get>; + /// Deposit for setting an Alternative fee swap + #[pallet::constant] + type AlternativeFeeSwapDeposit: Get>; + /// Convert a weight value into a deductible fee based on the currency /// type. type WeightToFee: WeightToFeePolynomial>; @@ -419,8 +424,10 @@ pub mod module { Error::::InvalidSwapPath ); AlternativeFeeSwapPath::::insert(&who, &path); + T::Currency::ensure_reserved_named(&DEPOSIT_ID, &who, T::AlternativeFeeSwapDeposit::get())?; } else { AlternativeFeeSwapPath::::remove(&who); + T::Currency::unreserve_all_named(&DEPOSIT_ID, &who); } Ok(()) } diff --git a/modules/transaction-payment/src/mock.rs b/modules/transaction-payment/src/mock.rs index cfbbeed38..c795dd2f4 100644 --- a/modules/transaction-payment/src/mock.rs +++ b/modules/transaction-payment/src/mock.rs @@ -183,6 +183,7 @@ parameter_types! { pub static TipPerWeightStep: u128 = 1; pub MaxTipsOfPriority: u128 = 1000; pub DefaultFeeSwapPathList: Vec> = vec![vec![AUSD, ACA], vec![DOT, AUSD, ACA]]; + pub AlternativeFeeSwapDeposit: Balance = 1000; } thread_local! { @@ -225,6 +226,7 @@ impl PriceProvider for MockPriceSource { impl Config for Runtime { type NativeCurrencyId = GetNativeCurrencyId; type DefaultFeeSwapPathList = DefaultFeeSwapPathList; + type AlternativeFeeSwapDeposit = AlternativeFeeSwapDeposit; type Currency = PalletBalances; type MultiCurrency = Currencies; type OnTransactionPayment = DealWithFees; diff --git a/modules/transaction-payment/src/tests.rs b/modules/transaction-payment/src/tests.rs index 2abc7a32f..90fc427d9 100644 --- a/modules/transaction-payment/src/tests.rs +++ b/modules/transaction-payment/src/tests.rs @@ -26,8 +26,9 @@ use frame_support::{ weights::{DispatchClass, DispatchInfo, Pays}, }; use mock::{ - AccountId, BlockWeights, Call, Currencies, DEXModule, ExtBuilder, MockPriceSource, Origin, Runtime, - TransactionPayment, ACA, ALICE, AUSD, BOB, CHARLIE, DOT, FEE_UNBALANCED_AMOUNT, TIP_UNBALANCED_AMOUNT, + AccountId, AlternativeFeeSwapDeposit, BlockWeights, Call, Currencies, DEXModule, ExtBuilder, MockPriceSource, + Origin, Runtime, TransactionPayment, ACA, ALICE, AUSD, BOB, CHARLIE, DOT, FEE_UNBALANCED_AMOUNT, + TIP_UNBALANCED_AMOUNT, }; use orml_traits::MultiCurrency; use sp_runtime::{testing::TestXt, traits::One}; @@ -350,37 +351,40 @@ fn charges_fee_failed_by_slippage_limit() { #[test] fn set_alternative_fee_swap_path_work() { - ExtBuilder::default().build().execute_with(|| { - assert_eq!(TransactionPayment::alternative_fee_swap_path(&ALICE), None); - assert_ok!(TransactionPayment::set_alternative_fee_swap_path( - Origin::signed(ALICE), - Some(vec![AUSD, ACA]) - )); - assert_eq!( - TransactionPayment::alternative_fee_swap_path(&ALICE).unwrap(), - vec![AUSD, ACA] - ); - assert_ok!(TransactionPayment::set_alternative_fee_swap_path( - Origin::signed(ALICE), - None - )); - assert_eq!(TransactionPayment::alternative_fee_swap_path(&ALICE), None); + ExtBuilder::default() + .one_hundred_thousand_for_alice_n_charlie() + .build() + .execute_with(|| { + assert_eq!(TransactionPayment::alternative_fee_swap_path(&ALICE), None); + assert_ok!(TransactionPayment::set_alternative_fee_swap_path( + Origin::signed(ALICE), + Some(vec![AUSD, ACA]) + )); + assert_eq!( + TransactionPayment::alternative_fee_swap_path(&ALICE).unwrap(), + vec![AUSD, ACA] + ); + assert_ok!(TransactionPayment::set_alternative_fee_swap_path( + Origin::signed(ALICE), + None + )); + assert_eq!(TransactionPayment::alternative_fee_swap_path(&ALICE), None); - assert_noop!( - TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA])), - Error::::InvalidSwapPath - ); + assert_noop!( + TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA])), + Error::::InvalidSwapPath + ); - assert_noop!( - TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![AUSD, DOT])), - Error::::InvalidSwapPath - ); + assert_noop!( + TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![AUSD, DOT])), + Error::::InvalidSwapPath + ); - assert_noop!( - TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA, ACA])), - Error::::InvalidSwapPath - ); - }); + assert_noop!( + TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA, ACA])), + Error::::InvalidSwapPath + ); + }); } #[test] @@ -410,6 +414,12 @@ fn charge_fee_by_default_swap_path() { )); assert_eq!(DEXModule::get_liquidity_pool(ACA, AUSD), (10000, 1000)); assert_eq!(DEXModule::get_liquidity_pool(DOT, AUSD), (100, 1000)); + assert_ok!(Currencies::update_balance( + Origin::root(), + BOB, + ACA, + AlternativeFeeSwapDeposit::get().try_into().unwrap(), + )); assert_ok!(TransactionPayment::set_alternative_fee_swap_path( Origin::signed(BOB), Some(vec![DOT, ACA]) @@ -431,11 +441,11 @@ fn charge_fee_by_default_swap_path() { 1 ); - assert_eq!(Currencies::free_balance(ACA, &BOB), Currencies::minimum_balance(ACA)); + assert_eq!(Currencies::free_balance(ACA, &BOB), 0); assert_eq!(Currencies::free_balance(AUSD, &BOB), 0); assert_eq!(Currencies::free_balance(DOT, &BOB), 100 - 34); - assert_eq!(DEXModule::get_liquidity_pool(ACA, AUSD), (10000 - 2000 - 10, 1252)); - assert_eq!(DEXModule::get_liquidity_pool(DOT, AUSD), (100 + 34, 1000 - 252)); + assert_eq!(DEXModule::get_liquidity_pool(ACA, AUSD), (10000 - 2000, 1251)); + assert_eq!(DEXModule::get_liquidity_pool(DOT, AUSD), (100 + 34, 1000 - 251)); }); } diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index e9fec1ae2..6a2e6fde2 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -169,6 +169,7 @@ pub enum ReserveIdentifier { Honzon, Nft, TransactionPayment, + TransactionPaymentDeposit, // always the last, indicate number of variants Count, diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index 03ddcc3b6..12ed320de 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -1117,6 +1117,7 @@ impl module_transaction_payment::Config for Runtime { type Currency = Balances; type MultiCurrency = Currencies; type OnTransactionPayment = DealWithFees; + type AlternativeFeeSwapDeposit = NativeTokenExistentialDeposit; type TransactionByteFee = TransactionByteFee; type OperationalFeeMultiplier = OperationalFeeMultiplier; type TipPerWeightStep = TipPerWeightStep; diff --git a/runtime/acala/src/weights/module_transaction_payment.rs b/runtime/acala/src/weights/module_transaction_payment.rs index fdbe91091..cd035de3b 100644 --- a/runtime/acala/src/weights/module_transaction_payment.rs +++ b/runtime/acala/src/weights/module_transaction_payment.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_transaction_payment //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-12-24, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-latest"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=acala-latest // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module_transaction_payment // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -36,7 +36,6 @@ // --template=./templates/runtime-weight-template.hbs // --output=./runtime/acala/src/weights/ - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -48,11 +47,12 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_transaction_payment::WeightInfo for WeightInfo { fn set_alternative_fee_swap_path() -> Weight { - (4_686_000 as Weight) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) + (44_246_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn on_finalize() -> Weight { - (13_644_000 as Weight) + (13_404_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } diff --git a/runtime/common/src/precompile/mock.rs b/runtime/common/src/precompile/mock.rs index 88a2ca1c3..bc5eb638c 100644 --- a/runtime/common/src/precompile/mock.rs +++ b/runtime/common/src/precompile/mock.rs @@ -256,6 +256,7 @@ impl module_transaction_payment::Config for Test { type Currency = Balances; type MultiCurrency = Currencies; type OnTransactionPayment = (); + type AlternativeFeeSwapDeposit = ExistentialDeposit; type TransactionByteFee = TransactionByteFee; type OperationalFeeMultiplier = OperationalFeeMultiplier; type TipPerWeightStep = TipPerWeightStep; diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index 4f222015c..45d88c716 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1132,6 +1132,7 @@ impl module_transaction_payment::Config for Runtime { type Currency = Balances; type MultiCurrency = Currencies; type OnTransactionPayment = DealWithFees; + type AlternativeFeeSwapDeposit = NativeTokenExistentialDeposit; type TransactionByteFee = TransactionByteFee; type OperationalFeeMultiplier = OperationalFeeMultiplier; type TipPerWeightStep = TipPerWeightStep; diff --git a/runtime/karura/src/weights/module_transaction_payment.rs b/runtime/karura/src/weights/module_transaction_payment.rs index f76ca732d..0ed8b7731 100644 --- a/runtime/karura/src/weights/module_transaction_payment.rs +++ b/runtime/karura/src/weights/module_transaction_payment.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_transaction_payment //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-12-24, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=karura-dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module_transaction_payment // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -36,7 +36,6 @@ // --template=./templates/runtime-weight-template.hbs // --output=./runtime/karura/src/weights/ - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -48,11 +47,12 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_transaction_payment::WeightInfo for WeightInfo { fn set_alternative_fee_swap_path() -> Weight { - (4_707_000 as Weight) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) + (41_748_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn on_finalize() -> Weight { - (13_019_000 as Weight) + (12_563_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } diff --git a/runtime/mandala/src/benchmarking/transaction_payment.rs b/runtime/mandala/src/benchmarking/transaction_payment.rs index 41234aa8c..f1d410a6f 100644 --- a/runtime/mandala/src/benchmarking/transaction_payment.rs +++ b/runtime/mandala/src/benchmarking/transaction_payment.rs @@ -16,7 +16,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{AccountId, CurrencyId, GetNativeCurrencyId, GetStableCurrencyId, Runtime, System, TransactionPayment}; +use super::utils::set_balance; +use crate::{ + AccountId, CurrencyId, GetNativeCurrencyId, GetStableCurrencyId, NativeTokenExistentialDeposit, Runtime, System, + TransactionPayment, +}; use frame_benchmarking::whitelisted_caller; use frame_support::traits::OnFinalize; use frame_system::RawOrigin; @@ -31,6 +35,7 @@ runtime_benchmarks! { set_alternative_fee_swap_path { let caller: AccountId = whitelisted_caller(); + set_balance(NATIVECOIN, &caller, NativeTokenExistentialDeposit::get()); }: _(RawOrigin::Signed(caller.clone()), Some(vec![STABLECOIN, NATIVECOIN])) verify { assert_eq!(TransactionPayment::alternative_fee_swap_path(&caller).unwrap().into_inner(), vec![STABLECOIN, NATIVECOIN]); diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index 059b3dfb9..43522c934 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1153,6 +1153,7 @@ impl module_transaction_payment::Config for Runtime { type Currency = Balances; type MultiCurrency = Currencies; type OnTransactionPayment = DealWithFees; + type AlternativeFeeSwapDeposit = NativeTokenExistentialDeposit; type TransactionByteFee = TransactionByteFee; type OperationalFeeMultiplier = OperationalFeeMultiplier; type TipPerWeightStep = TipPerWeightStep; diff --git a/runtime/mandala/src/weights/module_transaction_payment.rs b/runtime/mandala/src/weights/module_transaction_payment.rs index 7264c3b56..3b193312f 100644 --- a/runtime/mandala/src/weights/module_transaction_payment.rs +++ b/runtime/mandala/src/weights/module_transaction_payment.rs @@ -18,8 +18,8 @@ //! Autogenerated weights for module_transaction_payment //! -//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0 -//! DATE: 2021-07-19, STEPS: `[50, ]`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2021-12-24, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module_transaction_payment // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -36,7 +36,6 @@ // --template=./templates/runtime-weight-template.hbs // --output=./runtime/mandala/src/weights/ - #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -48,11 +47,12 @@ use sp_std::marker::PhantomData; pub struct WeightInfo(PhantomData); impl module_transaction_payment::WeightInfo for WeightInfo { fn set_alternative_fee_swap_path() -> Weight { - (4_730_000 as Weight) - .saturating_add(T::DbWeight::get().writes(1 as Weight)) + (52_625_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) } fn on_finalize() -> Weight { - (15_104_000 as Weight) + (23_555_000 as Weight) .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) }