Skip to content

Commit

Permalink
feat: InvArch - treasury + DealWithFees
Browse files Browse the repository at this point in the history
  • Loading branch information
arrudagates committed Nov 10, 2023
1 parent b769fe5 commit bfb3d7e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 52 deletions.
1 change: 1 addition & 0 deletions invarch/Cargo.lock

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

1 change: 1 addition & 0 deletions invarch/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pallet-sudo = { git = "https://github.com/paritytech/substrate", default-feature
pallet-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
pallet-treasury = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
sp-block-builder = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.43" }
Expand Down
102 changes: 99 additions & 3 deletions invarch/runtime/src/balances.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use crate::{
AccountId, Balance, Balances, Runtime, RuntimeEvent, System, EXISTENTIAL_DEPOSIT, UNIT,
AccountId, Balance, Balances, BlockNumber, ExtrinsicBaseWeight, Runtime, RuntimeEvent, System,
Treasury, DAYS, EXISTENTIAL_DEPOSIT, MICROUNIT, MILLIUNIT, UNIT,
};
use frame_support::{pallet_prelude::ConstU32, parameter_types, traits::SortedMembers};
use frame_system::EnsureSignedBy;
use frame_support::{
pallet_prelude::ConstU32,
parameter_types,
traits::{Currency, Imbalance, OnUnbalanced, SortedMembers},
weights::{
ConstantMultiplier, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
},
PalletId,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use polkadot_runtime_common::SlowAdjustingFeeUpdate;
use sp_runtime::{traits::AccountIdConversion, Perbill, Permill};
use sp_std::vec::Vec;

parameter_types! {
Expand All @@ -28,6 +39,63 @@ impl pallet_balances::Config for Runtime {
type HoldIdentifier = [u8; 8];
}

parameter_types! {
// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = 10 * MICROUNIT;
pub const OperationalFeeMultiplier: u8 = 5;
}

pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
let p = MILLIUNIT;
let q = 100 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;

pub struct ToCollatorPot;
impl OnUnbalanced<NegativeImbalance> for ToCollatorPot {
fn on_nonzero_unbalanced(amount: NegativeImbalance) {
let collator_pot =
<Runtime as pallet_collator_selection::Config>::PotId::get().into_account_truncating();
Balances::resolve_creating(&collator_pot, amount);
}
}

pub struct DealWithFees;
impl OnUnbalanced<NegativeImbalance> for DealWithFees {
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance>) {
if let Some(mut fees) = fees_then_tips.next() {
if let Some(tips) = fees_then_tips.next() {
tips.merge_into(&mut fees);
}

let (to_treasury, to_collators) = fees.ration(50, 50);

ToCollatorPot::on_unbalanced(to_collators);
Treasury::on_unbalanced(to_treasury)
}
}
}

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, DealWithFees>;
type WeightToFee = WeightToFee;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
}

parameter_types! {
pub const MinVestedTransfer: Balance = UNIT;
pub const MaxVestingSchedules: u32 = 50u32;
Expand Down Expand Up @@ -60,3 +128,31 @@ impl orml_vesting::Config for Runtime {
// Relay chain block number provider (6 seconds)
type BlockNumberProvider = cumulus_pallet_parachain_system::RelaychainDataProvider<Runtime>;
}

parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(1);
pub const ProposalBondMinimum: Balance = 100 * UNIT;
pub const SpendPeriod: BlockNumber = 30 * DAYS;
pub const Burn: Permill = Permill::from_percent(1);
pub const TreasuryPalletId: PalletId = PalletId(*b"ia/trsry");
pub const MaxApprovals: u32 = 100;
}

impl pallet_treasury::Config for Runtime {
type PalletId = TreasuryPalletId;
type Currency = Balances;
type ApproveOrigin = EnsureRoot<AccountId>;
type RejectOrigin = EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
type OnSlash = ();
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ProposalBondMinimum;
type SpendPeriod = SpendPeriod;
type Burn = ();
type BurnDestination = ();
type SpendFunds = ();
type WeightInfo = pallet_treasury::weights::SubstrateWeight<Runtime>;
type MaxApprovals = MaxApprovals;
type ProposalBondMaximum = ();
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<Balance>;
}
54 changes: 5 additions & 49 deletions invarch/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod xcm_config;
pub use balances::*;

use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
use smallvec::smallvec;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
Expand All @@ -32,11 +31,8 @@ use frame_support::{
construct_runtime,
dispatch::DispatchClass,
parameter_types,
traits::{ConstU32, ConstU64, ConstU8, Everything},
weights::{
constants::WEIGHT_REF_TIME_PER_SECOND, ConstantMultiplier, Weight, WeightToFeeCoefficient,
WeightToFeeCoefficients, WeightToFeePolynomial,
},
traits::{ConstU32, ConstU64, Everything},
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
PalletId,
};
use frame_system::{
Expand All @@ -50,7 +46,7 @@ pub use sp_runtime::{MultiAddress, Perbill, Permill};
pub use sp_runtime::BuildStorage;

// Polkadot imports
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use polkadot_runtime_common::BlockHashCount;

use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

Expand Down Expand Up @@ -116,33 +112,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> {
// in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
// in our template, we map to 1/10 of that, or 1/10 MILLIUNIT
let p = MILLIUNIT / 10;
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,
}]
}
}

/// 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 @@ -321,20 +290,6 @@ impl pallet_authorship::Config for Runtime {
type EventHandler = (CollatorSelection,);
}

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = 10 * MICROUNIT;
}

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, ()>;
type WeightToFee = WeightToFee;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
type OperationalFeeMultiplier = ConstU8<5>;
}

parameter_types! {
pub const ReservedXcmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
Expand Down Expand Up @@ -433,7 +388,8 @@ construct_runtime!(
// Monetary stuff.
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>} = 10,
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>} = 11,
Vesting: orml_vesting::{Pallet, Call, Storage, Event<T>} = 12,
Treasury: pallet_treasury::{Pallet, Call, Storage, Event<T>} = 12,
Vesting: orml_vesting::{Pallet, Call, Storage, Event<T>} = 13,

// Collator support. The order of these 4 are important and shall not change.
Authorship: pallet_authorship::{Pallet, Storage} = 20,
Expand Down

0 comments on commit bfb3d7e

Please sign in to comment.