Skip to content

Commit

Permalink
Update XCMP Fees (#144)
Browse files Browse the repository at this point in the history
* Updating xcmp fees

* fmt

* removed unused code
  • Loading branch information
Ryan Huttman authored May 17, 2022
1 parent 84be538 commit 956f808
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 106 deletions.
6 changes: 3 additions & 3 deletions primitives/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ pub trait TokenInfo {
let decimals = self.get_decimals();
10_u128.pow(decimals)
}

fn cent(&self) -> Balance {
self.dollar() / 100
}

fn millicent(&self) -> Balance {
self.cent() / 1_000
}
Expand All @@ -48,4 +48,4 @@ pub fn convert_to_token<C: TokenInfo>(source: C, target: C, amount: Balance) ->
let diff = target_decimals - current_decimals;
return amount * 10_u128.pow(diff)
}
}
}
36 changes: 5 additions & 31 deletions runtime/neumann/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use smallvec::smallvec;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
Expand All @@ -46,8 +45,7 @@ use frame_support::{
traits::{ConstU128, ConstU32, Contains, EnsureOneOf, Imbalance, OnUnbalanced, PrivilegeCmp},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
ConstantMultiplier, DispatchClass, Weight,
},
PalletId,
};
Expand Down Expand Up @@ -75,8 +73,8 @@ use orml_traits::parameter_type_with_key;

// Common imports
use primitives::{
AccountId, Address, Amount, AuraId, Balance, BlockNumber, Hash, Header, Index, Signature,
tokens::TokenInfo,
tokens::TokenInfo, AccountId, Address, Amount, AuraId, Balance, BlockNumber, Hash, Header,
Index, Signature,
};

// Custom pallet imports
Expand Down Expand Up @@ -117,31 +115,6 @@ pub type Executive = frame_executive::Executive<
AllPalletsWithSystem,
>;

/// 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> {
let weight_to_fee_scalar: Balance = 6;

smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::zero(),
coeff_integer: weight_to_fee_scalar,
}]
}
}

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
Expand Down Expand Up @@ -440,6 +413,7 @@ parameter_types! {
/// See `multiplier_can_grow_from_zero`.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128);
pub const TransactionByteFee: Balance = 0;
pub const WeightToFeeScalar: Balance = 6;
pub const OperationalFeeMultiplier: u8 = 5;
}

Expand Down Expand Up @@ -480,7 +454,7 @@ where
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction =
pallet_transaction_payment::CurrencyAdapter<Balances, DealWithInclusionFees<Runtime>>;
type WeightToFee = WeightToFee;
type WeightToFee = ConstantMultiplier<Balance, WeightToFeeScalar>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
Expand Down
49 changes: 34 additions & 15 deletions runtime/neumann/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use frame_support::{
weights::Weight,
};
use frame_system::EnsureRoot;
use sp_runtime::traits::Convert;
use sp_runtime::{traits::Convert, Percent};

// Polkadot Imports
use pallet_xcm::XcmPassthrough;
Expand Down Expand Up @@ -204,25 +204,44 @@ parameter_types! {
pub RocPerSecond: (AssetId, u128) = (MultiLocation::parent().into(), roc_per_second());
}

pub struct ToTreasury;
impl TakeRevenue for ToTreasury {
fn take_revenue(revenue: MultiAsset) {
if let MultiAsset {
id: AssetId::Concrete(id),
fun: Fungibility::Fungible(amount),
} = revenue
{
if let Some(currency_id) = CurrencyIdConvert::convert(id) {
// Ensure TreasuryAccount have ed requirement for native asset, but don't need
pub struct ToNativeTreasury;
impl TakeRevenue for ToNativeTreasury {
fn take_revenue(revenue: MultiAsset) {
if let MultiAsset { id: AssetId::Concrete(id), fun: Fungibility::Fungible(amount) } =
revenue
{
if let Some(currency_id) = CurrencyIdConvert::convert(id) {
// 80% burned, 20% to the treasury
let to_treasury = Percent::from_percent(20).mul_floor(amount);
// Due to the way XCM works the amount has already been taken off the total allocation balance.
// Thus whatever we deposit here gets added back to the total allocation, and the rest is burned.
let _ = Currencies::deposit(currency_id, &TreasuryAccount::get(), to_treasury);
}
}
}
}

pub struct ToForeignTreasury;
impl TakeRevenue for ToForeignTreasury {
fn take_revenue(revenue: MultiAsset) {
if let MultiAsset { id: AssetId::Concrete(id), fun: Fungibility::Fungible(amount) } =
revenue
{
if let Some(currency_id) = CurrencyIdConvert::convert(id) {
// Ensure TreasuryAccount have ed requirement for native asset, but don't need
// ed requirement for cross-chain asset because it's one of whitelist accounts.
// Ignore the result.
let _ = Currencies::deposit(currency_id, &TreasuryAccount::get(), amount);
}
}
}
}
}
}
}

pub type Trader = (FixedRateOfFungible<NeuPerSecond, ToTreasury>, FixedRateOfFungible<NeuCanonicalPerSecond, ()>, FixedRateOfFungible<RocPerSecond, ToTreasury>);
pub type Trader = (
FixedRateOfFungible<NeuPerSecond, ToNativeTreasury>,
FixedRateOfFungible<NeuCanonicalPerSecond, ToNativeTreasury>,
FixedRateOfFungible<RocPerSecond, ToForeignTreasury>,
);

pub struct XcmConfig;
impl Config for XcmConfig {
Expand Down
42 changes: 10 additions & 32 deletions runtime/turing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::{Decode, Encode, MaxEncodedLen};
use hex_literal::hex;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use smallvec::smallvec;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
Expand All @@ -46,8 +46,7 @@ use frame_support::{
traits::{ConstU128, ConstU32, Contains, EnsureOneOf, Imbalance, OnUnbalanced, PrivilegeCmp},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
ConstantMultiplier, DispatchClass, Weight,
},
PalletId,
};
Expand Down Expand Up @@ -75,8 +74,8 @@ use orml_traits::parameter_type_with_key;

// Common imports
use primitives::{
AccountId, Address, Amount, AuraId, Balance, BlockNumber, Hash, Header, Index, Signature,
tokens::TokenInfo,
tokens::TokenInfo, AccountId, Address, Amount, AuraId, Balance, BlockNumber, Hash, Header,
Index, Signature,
};

// Custom pallet imports
Expand Down Expand Up @@ -117,31 +116,6 @@ pub type Executive = frame_executive::Executive<
AllPalletsWithSystem,
>;

/// 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> {
let weight_to_fee_scalar: Balance = 6;

smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::zero(),
coeff_integer: weight_to_fee_scalar,
}]
}
}

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
Expand Down Expand Up @@ -356,6 +330,9 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
pub TreasuryAccount: AccountId = TreasuryPalletId::get().into_account();
// Until we can codify how to handle forgien tokens that we collect in XCMP fees
// we will send the tokens to a special account to be dealt with.
pub TemporaryForeignTreasuryAccount: AccountId = hex!["8acc2955e592588af0eeec40384bf3b498335ecc90df5e6980f0141e1314eb37"].into();
}

parameter_type_with_key! {
Expand Down Expand Up @@ -406,7 +383,7 @@ impl TokenInfo for CurrencyId {
pub struct DustRemovalWhitelist;
impl Contains<AccountId> for DustRemovalWhitelist {
fn contains(a: &AccountId) -> bool {
*a == TreasuryAccount::get()
*a == TreasuryAccount::get() || *a == TemporaryForeignTreasuryAccount::get()
}
}

Expand Down Expand Up @@ -448,6 +425,7 @@ parameter_types! {
/// See `multiplier_can_grow_from_zero`.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128);
pub const TransactionByteFee: Balance = 0;
pub const WeightToFeeScalar: Balance = 6;
pub const OperationalFeeMultiplier: u8 = 5;
}

Expand Down Expand Up @@ -488,7 +466,7 @@ where
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction =
pallet_transaction_payment::CurrencyAdapter<Balances, DealWithInclusionFees<Runtime>>;
type WeightToFee = WeightToFee;
type WeightToFee = ConstantMultiplier<Balance, WeightToFeeScalar>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
Expand Down
66 changes: 41 additions & 25 deletions runtime/turing/src/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
AccountId, Balance, Call, Currencies, CurrencyId, Event, Origin, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, TreasuryAccount, UnknownTokens, XcmpQueue,
MAXIMUM_BLOCK_WEIGHT,
ParachainSystem, PolkadotXcm, Runtime, TemporaryForeignTreasuryAccount, TreasuryAccount,
UnknownTokens, XcmpQueue, MAXIMUM_BLOCK_WEIGHT,
};

use core::marker::PhantomData;
Expand All @@ -11,7 +11,7 @@ use frame_support::{
weights::Weight,
};
use frame_system::EnsureRoot;
use sp_runtime::traits::Convert;
use sp_runtime::{traits::Convert, Percent};

// Polkadot Imports
use pallet_xcm::XcmPassthrough;
Expand Down Expand Up @@ -232,31 +232,47 @@ parameter_types! {
);
}

pub struct ToTreasury;
impl TakeRevenue for ToTreasury {
fn take_revenue(revenue: MultiAsset) {
if let MultiAsset {
id: AssetId::Concrete(id),
fun: Fungibility::Fungible(amount),
} = revenue
{
if let Some(currency_id) = CurrencyIdConvert::convert(id) {
// Ensure TreasuryAccount have ed requirement for native asset, but don't need
// ed requirement for cross-chain asset because it's one of whitelist accounts.
// Ignore the result.
let _ = Currencies::deposit(currency_id, &TreasuryAccount::get(), amount);
}
}
}
pub struct ToNativeTreasury;
impl TakeRevenue for ToNativeTreasury {
fn take_revenue(revenue: MultiAsset) {
if let MultiAsset { id: AssetId::Concrete(id), fun: Fungibility::Fungible(amount) } =
revenue
{
if let Some(currency_id) = CurrencyIdConvert::convert(id) {
// 80% burned, 20% to the treasury
let to_treasury = Percent::from_percent(20).mul_floor(amount);
// Due to the way XCM works the amount has already been taken off the total allocation balance.
// Thus whatever we deposit here gets added back to the total allocation, and the rest is burned.
let _ = Currencies::deposit(currency_id, &TreasuryAccount::get(), to_treasury);
}
}
}
}

pub struct ToForeignTreasury;
impl TakeRevenue for ToForeignTreasury {
fn take_revenue(revenue: MultiAsset) {
if let MultiAsset { id: AssetId::Concrete(id), fun: Fungibility::Fungible(amount) } =
revenue
{
if let Some(currency_id) = CurrencyIdConvert::convert(id) {
let _ = Currencies::deposit(
currency_id,
&TemporaryForeignTreasuryAccount::get(),
amount,
);
}
}
}
}

pub type Trader = (
FixedRateOfFungible<TurPerSecond, ToTreasury>,
FixedRateOfFungible<TurCanonicalPerSecond, ToTreasury>,
FixedRateOfFungible<KsmPerSecond, ToTreasury>,
FixedRateOfFungible<KarPerSecond, ToTreasury>,
FixedRateOfFungible<KusdPerSecond, ToTreasury>,
FixedRateOfFungible<LksmPerSecond, ToTreasury>,
FixedRateOfFungible<TurPerSecond, ToNativeTreasury>,
FixedRateOfFungible<TurCanonicalPerSecond, ToNativeTreasury>,
FixedRateOfFungible<KsmPerSecond, ToForeignTreasury>,
FixedRateOfFungible<KarPerSecond, ToForeignTreasury>,
FixedRateOfFungible<KusdPerSecond, ToForeignTreasury>,
FixedRateOfFungible<LksmPerSecond, ToForeignTreasury>,
);

pub struct XcmConfig;
Expand Down

0 comments on commit 956f808

Please sign in to comment.