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
30 changes: 4 additions & 26 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ use frame_support::{
},
weights::{
constants::{RocksDbWeight, WEIGHT_PER_SECOND},
DispatchClass, GetDispatchInfo, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
ConstantMultiplier, DispatchClass, GetDispatchInfo, Weight, WeightToFeeCoefficient,
WeightToFeeCoefficients, WeightToFeePolynomial,
},
PalletId,
};
Expand Down Expand Up @@ -300,28 +300,6 @@ where
}
}

pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;

/// Return a vec of coefficients. Here we just use one coefficient and reduce it to a constant
/// modifier in order to closely match Ethereum-based fees.
///
/// Calculation, per the documentation in `frame_support`:
///
/// ```ignore
/// coeff_integer * x^(degree) + coeff_frac * x^(degree)
/// ```
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: currency::WEIGHT_FEE,
negative: false,
}]
}
}

pub struct LengthToFee;
impl WeightToFeePolynomial for LengthToFee {
type Balance = Balance;
Expand All @@ -337,7 +315,7 @@ impl WeightToFeePolynomial for LengthToFee {
WeightToFeeCoefficient {
degree: 3,
coeff_frac: Perbill::zero(),
coeff_integer: 1,
coeff_integer: 1 * currency::SUPPLY_FACTOR,
negative: false,
},
]
Expand All @@ -347,7 +325,7 @@ impl WeightToFeePolynomial for LengthToFee {
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees<Runtime>>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = WeightToFee;
type WeightToFee = ConstantMultiplier<Balance, ConstU128<{ currency::WEIGHT_FEE }>>;
type LengthToFee = LengthToFee;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime>;
}
Expand Down
12 changes: 9 additions & 3 deletions runtime/moonbase/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

use super::{
AccountId, AssetId, AssetManager, Assets, Balance, Balances, Call, DealWithFees, Event,
LocalAssets, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, Treasury,
WeightToFee, XcmpQueue, FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
LocalAssets, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, Treasury, XcmpQueue,
FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
};

use pallet_evm_precompile_assets_erc20::AccountIdAssetIdConversion;
Expand Down Expand Up @@ -272,7 +272,13 @@ impl xcm_executor::Config for XcmExecutorConfig {
// When we receive a non-reserve asset, we use AssetManager to fetch how many
// units per second we should charge
type Trader = (
UsingComponents<WeightToFee, SelfReserve, AccountId, Balances, DealWithFees<Runtime>>,
UsingComponents<
<Runtime as pallet_transaction_payment::Config>::WeightToFee,
SelfReserve,
AccountId,
Balances,
DealWithFees<Runtime>,
>,
FirstAssetTrader<AssetType, AssetManager, XcmFeesToAccount>,
);
type ResponseHandler = PolkadotXcm;
Expand Down
17 changes: 0 additions & 17 deletions runtime/moonbase/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1919,23 +1919,6 @@ where
fn length_fee_is_sensible() {
use sp_runtime::testing::TestXt;

ExtBuilder::default()
.with_balances(vec![
(AccountId::from(ALICE), (1 * UNIT) + (1 * WEI)),
(AccountId::from(BOB), 0),
])
.build()
.execute_with(|| {
// Substrate transfer
assert_ok!(Balances::transfer(
origin_of(AccountId::from(ALICE)),
AccountId::from(BOB),
1 * UNIT,
));
// 1 WEI is left in the account
assert_eq!(Balances::free_balance(AccountId::from(ALICE)), 1 * WEI);
});

// tests that length fee is sensible for a few hypothetical transactions
ExtBuilder::default().build().execute_with(|| {
let call = frame_system::Call::remark::<Runtime> { remark: vec![] };
Expand Down
34 changes: 27 additions & 7 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ use frame_support::{
},
weights::{
constants::{RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, GetDispatchInfo, IdentityFee, Weight,
ConstantMultiplier, DispatchClass, GetDispatchInfo, Weight, WeightToFeeCoefficient,
WeightToFeeCoefficients, WeightToFeePolynomial,
},
PalletId,
};
Expand All @@ -68,6 +69,7 @@ use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustm
pub use parachain_staking::{InflationInfo, Range};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use smallvec::smallvec;
use sp_api::impl_runtime_apis;
use sp_core::{OpaqueMetadata, H160, H256, U256};
use sp_runtime::{
Expand Down Expand Up @@ -122,7 +124,7 @@ pub mod currency {
pub const GLMR: Balance = 1_000_000_000_000_000_000;
pub const KILOGLMR: Balance = 1_000_000_000_000_000_000_000;

pub const TRANSACTION_BYTE_FEE: Balance = 10 * MICROGLMR * SUPPLY_FACTOR;
pub const TRANSACTION_BYTE_FEE: Balance = 1 * GIGAWEI * SUPPLY_FACTOR;
pub const STORAGE_BYTE_FEE: Balance = 100 * MICROGLMR * SUPPLY_FACTOR;
pub const WEIGHT_FEE: Balance = 100 * KILOWEI * SUPPLY_FACTOR;

Expand Down Expand Up @@ -294,15 +296,33 @@ where
}
}

parameter_types! {
pub const TransactionByteFee: Balance = currency::TRANSACTION_BYTE_FEE;
pub struct LengthToFee;
impl WeightToFeePolynomial for LengthToFee {
type Balance = Balance;

fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![
WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: currency::TRANSACTION_BYTE_FEE,
negative: false,
},
WeightToFeeCoefficient {
degree: 3,
coeff_frac: Perbill::zero(),
coeff_integer: 1 * currency::SUPPLY_FACTOR,
negative: false,
},
]
}
}

impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees<Runtime>>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type WeightToFee = ConstantMultiplier<Balance, ConstU128<{ currency::WEIGHT_FEE }>>;
type LengthToFee = LengthToFee;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime>;
}

Expand Down Expand Up @@ -1319,7 +1339,7 @@ mod tests {
assert_eq!(SUPPLY_FACTOR, 100);

// txn fees
assert_eq!(TRANSACTION_BYTE_FEE, Balance::from(1 * MILLIGLMR));
assert_eq!(TRANSACTION_BYTE_FEE, Balance::from(100 * GIGAWEI));
assert_eq!(
get!(pallet_transaction_payment, OperationalFeeMultiplier, u8),
5_u8
Expand Down
4 changes: 2 additions & 2 deletions runtime/moonbeam/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sp_runtime::traits::Hash as THash;
use frame_support::{
parameter_types,
traits::{Everything, Nothing, PalletInfoAccess},
weights::{IdentityFee, Weight},
weights::Weight,
};

use frame_system::EnsureRoot;
Expand Down Expand Up @@ -261,7 +261,7 @@ impl xcm_executor::Config for XcmExecutorConfig {
// units per second we should charge
type Trader = (
UsingComponents<
IdentityFee<Balance>,
<Runtime as pallet_transaction_payment::Config>::WeightToFee,
SelfReserve,
AccountId,
Balances,
Expand Down
35 changes: 35 additions & 0 deletions runtime/moonbeam/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,41 @@ where
});
}

#[test]
#[rustfmt::skip]
fn length_fee_is_sensible() {
use sp_runtime::testing::TestXt;

// tests that length fee is sensible for a few hypothetical transactions
ExtBuilder::default().build().execute_with(|| {
let call = frame_system::Call::remark::<Runtime> { remark: vec![] };
let uxt: TestXt<_, ()> = TestXt::new(call, Some((1u64, ())));

let calc_fee = |len: u32| -> Balance {
moonbeam_runtime::TransactionPayment::query_fee_details(uxt.clone(), len)
.inclusion_fee
.expect("fee should be calculated")
.len_fee
};

// editorconfig-checker-disable
// left: cost of length fee, right: size in bytes
// /------------- proportional component: O(N * 1B)
// | /- exponential component: O(N ** 3)
// | |
assert_eq!( 100_000_000_100, calc_fee(1));
assert_eq!( 1_000_000_100_000, calc_fee(10));
assert_eq!( 10_000_100_000_000, calc_fee(100));
assert_eq!( 100_100_000_000_000, calc_fee(1_000));
assert_eq!( 1_100_000_000_000_000, calc_fee(10_000)); // inflection point
assert_eq!( 110_000_000_000_000_000, calc_fee(100_000));
assert_eq!( 100_100_000_000_000_000_000, calc_fee(1_000_000)); // 100 GLMR, ~ 1MB
assert_eq!( 100_001_000_000_000_000_000_000, calc_fee(10_000_000));
assert_eq!(100_000_010_000_000_000_000_000_000, calc_fee(100_000_000));
// editorconfig-checker-enable
});
}

#[test]
fn multiplier_can_grow_from_zero() {
let minimum_multiplier = moonbeam_runtime::MinimumMultiplier::get();
Expand Down
48 changes: 22 additions & 26 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ use frame_support::{
},
weights::{
constants::{RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, GetDispatchInfo, IdentityFee, Weight,
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
ConstantMultiplier, DispatchClass, GetDispatchInfo, Weight, WeightToFeeCoefficient,
WeightToFeeCoefficients, WeightToFeePolynomial,
},
PalletId,
};
Expand Down Expand Up @@ -123,7 +123,7 @@ pub mod currency {
pub const MOVR: Balance = 1_000_000_000_000_000_000;
pub const KILOMOVR: Balance = 1_000_000_000_000_000_000_000;

pub const TRANSACTION_BYTE_FEE: Balance = 10 * MICROMOVR * SUPPLY_FACTOR;
pub const TRANSACTION_BYTE_FEE: Balance = 1 * GIGAWEI * SUPPLY_FACTOR;
pub const STORAGE_BYTE_FEE: Balance = 100 * MICROMOVR * SUPPLY_FACTOR;
pub const WEIGHT_FEE: Balance = 100 * KILOWEI * SUPPLY_FACTOR;

Expand Down Expand Up @@ -294,37 +294,33 @@ where
}
}

pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
pub struct LengthToFee;
impl WeightToFeePolynomial for LengthToFee {
type Balance = Balance;

/// Return a vec of coefficients. Here we just use one coefficient and reduce it to a constant
/// modifier in order to closely match Ethereum-based fees.
///
/// Calculation, per the documentation in `frame_support`:
///
/// ```ignore
/// coeff_integer * x^(degree) + coeff_frac * x^(degree)
/// ```
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
smallvec![WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: currency::WEIGHT_FEE,
negative: false,
}]
smallvec![
WeightToFeeCoefficient {
degree: 1,
coeff_frac: Perbill::zero(),
coeff_integer: currency::TRANSACTION_BYTE_FEE,
negative: false,
},
WeightToFeeCoefficient {
degree: 3,
coeff_frac: Perbill::zero(),
coeff_integer: 1 * currency::SUPPLY_FACTOR,
negative: false,
},
]
}
}

parameter_types! {
pub const TransactionByteFee: Balance = currency::TRANSACTION_BYTE_FEE;
}

impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees<Runtime>>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = IdentityFee<Balance>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type WeightToFee = ConstantMultiplier<Balance, ConstU128<{ currency::WEIGHT_FEE }>>;
type LengthToFee = LengthToFee;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Runtime>;
}

Expand Down Expand Up @@ -1348,7 +1344,7 @@ mod tests {
assert_eq!(SUPPLY_FACTOR, 1);

// txn fees
assert_eq!(TRANSACTION_BYTE_FEE, Balance::from(10 * MICROMOVR));
assert_eq!(TRANSACTION_BYTE_FEE, Balance::from(1 * GIGAWEI));
assert_eq!(
get!(pallet_transaction_payment, OperationalFeeMultiplier, u8),
5_u8
Expand Down
12 changes: 9 additions & 3 deletions runtime/moonriver/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

use super::{
AccountId, AssetId, AssetManager, Assets, Balance, Balances, Call, DealWithFees, Event,
LocalAssets, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, Treasury,
WeightToFee, XcmpQueue, FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
LocalAssets, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, Treasury, XcmpQueue,
FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
};

use pallet_evm_precompile_assets_erc20::AccountIdAssetIdConversion;
Expand Down Expand Up @@ -267,7 +267,13 @@ impl xcm_executor::Config for XcmExecutorConfig {
// When we receive a non-reserve asset, we use AssetManager to fetch how many
// units per second we should charge
type Trader = (
UsingComponents<WeightToFee, SelfReserve, AccountId, Balances, DealWithFees<Runtime>>,
UsingComponents<
<Runtime as pallet_transaction_payment::Config>::WeightToFee,
SelfReserve,
AccountId,
Balances,
DealWithFees<Runtime>,
>,
FirstAssetTrader<AssetType, AssetManager, XcmFeesToAccount>,
);
type ResponseHandler = PolkadotXcm;
Expand Down
35 changes: 35 additions & 0 deletions runtime/moonriver/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,41 @@ where
});
}

#[test]
#[rustfmt::skip]
fn length_fee_is_sensible() {
use sp_runtime::testing::TestXt;

// tests that length fee is sensible for a few hypothetical transactions
ExtBuilder::default().build().execute_with(|| {
let call = frame_system::Call::remark::<Runtime> { remark: vec![] };
let uxt: TestXt<_, ()> = TestXt::new(call, Some((1u64, ())));

let calc_fee = |len: u32| -> Balance {
moonriver_runtime::TransactionPayment::query_fee_details(uxt.clone(), len)
.inclusion_fee
.expect("fee should be calculated")
.len_fee
};

// editorconfig-checker-disable
// left: cost of length fee, right: size in bytes
// /------------- proportional component: O(N * 1B)
// | /- exponential component: O(N ** 3)
// | |
assert_eq!( 1_000_000_001, calc_fee(1));
assert_eq!( 10_000_001_000, calc_fee(10));
assert_eq!( 100_001_000_000, calc_fee(100));
assert_eq!( 1_001_000_000_000, calc_fee(1_000));
assert_eq!( 11_000_000_000_000, calc_fee(10_000)); // inflection point
assert_eq!( 1_100_000_000_000_000, calc_fee(100_000));
assert_eq!( 1_001_000_000_000_000_000, calc_fee(1_000_000)); // one MOVR, ~ 1MB
assert_eq!( 1_000_010_000_000_000_000_000, calc_fee(10_000_000));
assert_eq!(1_000_000_100_000_000_000_000_000, calc_fee(100_000_000));
// editorconfig-checker-enable
});
}

#[test]
fn multiplier_can_grow_from_zero() {
let minimum_multiplier = moonriver_runtime::MinimumMultiplier::get();
Expand Down
Loading