Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
refactor: use weightToFee for xcm Trader (paritytech#752)
Browse files Browse the repository at this point in the history
* refactor: use weightToFee for xcm Trader

* fix format
  • Loading branch information
yrong authored Jan 17, 2023
1 parent a0e10c3 commit 3df6afc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
2 changes: 2 additions & 0 deletions parachain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion parachain/runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
repository = "https://github.com/Snowfork/snowbridge"

[dependencies]
smallvec = "1.8.0"
frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30", default-features = false }
Expand All @@ -16,6 +17,8 @@ sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "po
snowbridge-core = { path = "../../primitives/core", package = "snowbridge-core", default-features = false }
basic-channel = { path = "../../pallets/basic-channel", package = "snowbridge-basic-channel", default-features = false }
incentivized-channel = { path = "../../pallets/incentivized-channel", package = "snowbridge-incentivized-channel", default-features = false }
runtime-primitives = { path = "../../primitives/runtime", default-features = false, package = "snowbridge-runtime-primitives" }


[features]
default = [ "std" ]
Expand All @@ -27,5 +30,6 @@ std = [
"sp-runtime/std",
"snowbridge-core/std",
"basic-channel/std",
"incentivized-channel/std"
"incentivized-channel/std",
"runtime-primitives/std"
]
67 changes: 67 additions & 0 deletions parachain/runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,70 @@ parameter_types! {
pub const TreasuryPalletId: PalletId = PalletId(*b"s/treasy");
pub const DotPalletId: PalletId = PalletId(*b"s/dotapp");
}

/// Money matters.
pub mod currency {
use runtime_primitives::Balance;

pub const UNITS: Balance = 1_000_000_000_000;
pub const CENTS: Balance = UNITS / 100;
pub const MILLICENTS: Balance = CENTS / 1_000;
}

/// Fee-related.
pub mod fee {
use frame_support::weights::{
constants::ExtrinsicBaseWeight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
};
use runtime_primitives::Balance;
use smallvec::smallvec;
pub use sp_runtime::Perbill;

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
/// This should typically create a mapping between the following ranges:
/// - [0, MAXIMUM_BLOCK_WEIGHT]
/// - [Balance::min, Balance::max]
///
/// Yet, it can be used for any other sort of change to weight-fee. Some examples being:
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// extrinsic base weight map to 1/100 CENT
let p = super::currency::CENTS;
let q = 100 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}
}

#[cfg(test)]
mod tests {
use super::{
currency::{CENTS, MILLICENTS},
fee::WeightToFee,
};
use frame_support::weights::{constants::ExtrinsicBaseWeight, WeightToFee as WeightToFeeT};

#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/100 of a CENT
println!("Base Weight: {}", ExtrinsicBaseWeight::get());
let x = WeightToFee::weight_to_fee(&ExtrinsicBaseWeight::get());
println!("Base Fee: {}", x);
let y = CENTS / 100;
println!("CENTS: {}", y);
assert!(x.max(y) - x.min(y) < MILLICENTS);
}
}
5 changes: 3 additions & 2 deletions parachain/runtime/snowbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ use xcm_builder::{
use xcm_executor::{traits::JustTry, Config, XcmExecutor};

use runtime_common::{
DotPalletId, MaxMessagePayloadSize, MaxMessagesPerCommit, OutboundRouter, TreasuryPalletId,
fee::WeightToFee, DotPalletId, MaxMessagePayloadSize, MaxMessagesPerCommit, OutboundRouter,
TreasuryPalletId,
};

pub use runtime_primitives::{AccountId, Address, Balance, BlockNumber, Hash, Index, Signature};
Expand Down Expand Up @@ -366,7 +367,7 @@ impl Config for XcmConfig {
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Trader = UsingComponents<IdentityFee<Balance>, RococoLocation, AccountId, Balances, ()>;
type Trader = UsingComponents<WeightToFee, RococoLocation, AccountId, Balances, ()>;
type ResponseHandler = (); // Don't handle responses for now.
type SubscriptionService = PolkadotXcm;
type AssetTrap = (); // Don't handle asset trap.
Expand Down
5 changes: 3 additions & 2 deletions parachain/runtime/snowblink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ use xcm_builder::{
use xcm_executor::{traits::JustTry, Config, XcmExecutor};

use runtime_common::{
DotPalletId, MaxMessagePayloadSize, MaxMessagesPerCommit, OutboundRouter, TreasuryPalletId,
fee::WeightToFee, DotPalletId, MaxMessagePayloadSize, MaxMessagesPerCommit, OutboundRouter,
TreasuryPalletId,
};

pub use runtime_primitives::{AccountId, Address, Balance, BlockNumber, Hash, Index, Signature};
Expand Down Expand Up @@ -364,7 +365,7 @@ impl Config for XcmConfig {
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Trader = UsingComponents<IdentityFee<Balance>, RococoLocation, AccountId, Balances, ()>;
type Trader = UsingComponents<WeightToFee, RococoLocation, AccountId, Balances, ()>;
type ResponseHandler = (); // Don't handle responses for now.
type SubscriptionService = PolkadotXcm;
type AssetTrap = (); // Don't handle asset trap.
Expand Down
5 changes: 3 additions & 2 deletions parachain/runtime/snowbridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ use xcm_builder::{
use xcm_executor::{traits::JustTry, Config, XcmExecutor};

use runtime_common::{
DotPalletId, MaxMessagePayloadSize, MaxMessagesPerCommit, OutboundRouter, TreasuryPalletId,
fee::WeightToFee, DotPalletId, MaxMessagePayloadSize, MaxMessagesPerCommit, OutboundRouter,
TreasuryPalletId,
};

pub use runtime_primitives::{AccountId, Address, Balance, BlockNumber, Hash, Index, Signature};
Expand Down Expand Up @@ -364,7 +365,7 @@ impl Config for XcmConfig {
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Trader = UsingComponents<IdentityFee<Balance>, RococoLocation, AccountId, Balances, ()>;
type Trader = UsingComponents<WeightToFee, RococoLocation, AccountId, Balances, ()>;
type ResponseHandler = (); // Don't handle responses for now.
type SubscriptionService = PolkadotXcm;
type AssetTrap = (); // Don't handle asset trap.
Expand Down

0 comments on commit 3df6afc

Please sign in to comment.