Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions modules/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -281,6 +282,10 @@ pub mod module {
#[pallet::constant]
type MaxTipsOfPriority: Get<PalletBalanceOf<Self>>;

/// Deposit for setting an Alternative fee swap
#[pallet::constant]
type AlternativeFeeSwapDeposit: Get<PalletBalanceOf<Self>>;

/// Convert a weight value into a deductible fee based on the currency
/// type.
type WeightToFee: WeightToFeePolynomial<Balance = PalletBalanceOf<Self>>;
Expand Down Expand Up @@ -419,8 +424,10 @@ pub mod module {
Error::<T>::InvalidSwapPath
);
AlternativeFeeSwapPath::<T>::insert(&who, &path);
T::Currency::ensure_reserved_named(&DEPOSIT_ID, &who, T::AlternativeFeeSwapDeposit::get())?;
} else {
AlternativeFeeSwapPath::<T>::remove(&who);
T::Currency::unreserve_all_named(&DEPOSIT_ID, &who);
}
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions modules/transaction-payment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ parameter_types! {
pub static TipPerWeightStep: u128 = 1;
pub MaxTipsOfPriority: u128 = 1000;
pub DefaultFeeSwapPathList: Vec<Vec<CurrencyId>> = vec![vec![AUSD, ACA], vec![DOT, AUSD, ACA]];
pub AlternativeFeeSwapDeposit: Balance = 1000;
}

thread_local! {
Expand Down Expand Up @@ -225,6 +226,7 @@ impl PriceProvider<CurrencyId> for MockPriceSource {
impl Config for Runtime {
type NativeCurrencyId = GetNativeCurrencyId;
type DefaultFeeSwapPathList = DefaultFeeSwapPathList;
type AlternativeFeeSwapDeposit = AlternativeFeeSwapDeposit;
type Currency = PalletBalances;
type MultiCurrency = Currencies;
type OnTransactionPayment = DealWithFees;
Expand Down
76 changes: 43 additions & 33 deletions modules/transaction-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<Runtime>::InvalidSwapPath
);
assert_noop!(
TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA])),
Error::<Runtime>::InvalidSwapPath
);

assert_noop!(
TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![AUSD, DOT])),
Error::<Runtime>::InvalidSwapPath
);
assert_noop!(
TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![AUSD, DOT])),
Error::<Runtime>::InvalidSwapPath
);

assert_noop!(
TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA, ACA])),
Error::<Runtime>::InvalidSwapPath
);
});
assert_noop!(
TransactionPayment::set_alternative_fee_swap_path(Origin::signed(ALICE), Some(vec![ACA, ACA])),
Error::<Runtime>::InvalidSwapPath
);
});
}

#[test]
Expand Down Expand Up @@ -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])
Expand All @@ -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));
});
}

Expand Down
1 change: 1 addition & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ pub enum ReserveIdentifier {
Honzon,
Nft,
TransactionPayment,
TransactionPaymentDeposit,

// always the last, indicate number of variants
Count,
Expand Down
1 change: 1 addition & 0 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions runtime/acala/src/weights/module_transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,15 +28,14 @@
// --chain=acala-latest
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=module_transaction_payment
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --template=./templates/runtime-weight-template.hbs
// --output=./runtime/acala/src/weights/


#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
Expand All @@ -48,11 +47,12 @@ use sp_std::marker::PhantomData;
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> module_transaction_payment::WeightInfo for WeightInfo<T> {
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))
}
Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/precompile/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions runtime/karura/src/weights/module_transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,15 +28,14 @@
// --chain=karura-dev
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=module_transaction_payment
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --template=./templates/runtime-weight-template.hbs
// --output=./runtime/karura/src/weights/


#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
Expand All @@ -48,11 +47,12 @@ use sp_std::marker::PhantomData;
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> module_transaction_payment::WeightInfo for WeightInfo<T> {
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))
}
Expand Down
7 changes: 6 additions & 1 deletion runtime/mandala/src/benchmarking/transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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;
Expand All @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions runtime/mandala/src/weights/module_transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,15 +28,14 @@
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=*
// --pallet=module_transaction_payment
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --template=./templates/runtime-weight-template.hbs
// --output=./runtime/mandala/src/weights/


#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
Expand All @@ -48,11 +47,12 @@ use sp_std::marker::PhantomData;
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> module_transaction_payment::WeightInfo for WeightInfo<T> {
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))
}
Expand Down