Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Runtime: Treasury spends various asset kinds #7427

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
10 changes: 9 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "
pallet-vesting = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-treasury = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-asset-rate = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

Expand All @@ -49,6 +50,7 @@ runtime-parachains = { package = "polkadot-runtime-parachains", path = "../parac

slot-range-helper = { path = "slot_range_helper", default-features = false }
xcm = { path = "../../xcm", default-features = false }
xcm-builder = { path = "../../xcm/xcm-builder", default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down Expand Up @@ -97,7 +99,9 @@ std = [
"libsecp256k1/std",
"runtime-parachains/std",
"xcm/std",
"xcm-builder/std",
"sp-npos-elections/std",
"pallet-asset-rate/std",
]
runtime-benchmarks = [
"libsecp256k1/hmac",
Expand All @@ -107,7 +111,10 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"runtime-parachains/runtime-benchmarks",
"pallet-babe/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks"
"pallet-fast-unstake/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"pallet-asset-rate/runtime-benchmarks",
]
try-runtime = [
"runtime-parachains/try-runtime",
Expand All @@ -120,4 +127,5 @@ try-runtime = [
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
"pallet-fast-unstake/try-runtime",
"pallet-asset-rate/try-runtime",
]
52 changes: 51 additions & 1 deletion runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,54 @@ pub fn era_payout(
(staking_payout, rest)
}

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks {
use pallet_asset_rate::AssetKindFactory;
use pallet_treasury::ArgumentsFactory as TreasuryArgumentsFactory;
use xcm::prelude::*;
use xcm_builder::LocatableAssetId;

/// Implements the [`pallet_asset_rate::AssetKindFactory`] trait to provide factory method for benchmarks
/// that require the `AssetKind` of [`LocatableAssetId`].
/// The location of the asset is determined as a Parachain with an ID equal to the passed seed.
pub struct LocatableAssetFactory;
impl AssetKindFactory<LocatableAssetId> for LocatableAssetFactory {
fn create_asset_kind(seed: u32) -> LocatableAssetId {
LocatableAssetId {
asset_id: MultiLocation::new(
0,
X2(PalletInstance(seed.try_into().unwrap()), GeneralIndex(seed.into())),
)
.into(),
location: MultiLocation::new(0, X1(Parachain(seed))),
}
}
}

/// Implements the [`pallet_treasury::ArgumentsFactory`] trait to provide factory methods for benchmarks
/// that require the `AssetKind` of [`LocatableAssetId`] and the `Beneficiary` of [`MultiLocation`].
/// The location of the asset is determined as a Parachain with an ID equal to the passed seed.
pub struct TreasuryArguments;
impl TreasuryArgumentsFactory<LocatableAssetId, MultiLocation> for TreasuryArguments {
fn create_asset_kind(seed: u32) -> LocatableAssetId {
LocatableAssetFactory::create_asset_kind(seed)
}
fn create_beneficiary(seed: [u8; 32]) -> MultiLocation {
MultiLocation::new(0, X1(AccountId32 { network: None, id: seed }))
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use frame_support::{
dispatch::DispatchClass,
parameter_types,
traits::{ConstU32, FindAuthor},
traits::{
tokens::{PayFromAccount, UnityAssetBalanceConversion},
ConstU32, FindAuthor,
},
weights::Weight,
PalletId,
};
Expand Down Expand Up @@ -195,6 +236,7 @@ mod tests {
parameter_types! {
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
pub const MaxApprovals: u32 = 100;
pub TreasuryAccount: AccountId = Treasury::account_id();
}

impl pallet_treasury::Config for Test {
Expand All @@ -214,6 +256,14 @@ mod tests {
type MaxApprovals = MaxApprovals;
type WeightInfo = ();
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u64>;
type AssetKind = ();
type Beneficiary = Self::AccountId;
type BeneficiaryLookup = IdentityLookup<Self::AccountId>;
type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
type BalanceConverter = UnityAssetBalanceConversion;
type PayoutPeriod = ConstU64<0>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

pub struct OneAuthor;
Expand Down
4 changes: 4 additions & 0 deletions runtime/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pallet-whitelist = { git = "https://github.com/paritytech/substrate", branch = "
pallet-xcm = { path = "../../xcm/pallet-xcm", default-features = false }
pallet-xcm-benchmarks = { path = "../../xcm/pallet-xcm-benchmarks", default-features = false, optional = true }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-asset-rate = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
frame-try-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
Expand Down Expand Up @@ -204,6 +205,7 @@ std = [
"xcm-executor/std",
"xcm-builder/std",
"frame-election-provider-support/std",
"pallet-asset-rate/std",
]
runtime-benchmarks = [
"runtime-common/runtime-benchmarks",
Expand Down Expand Up @@ -255,6 +257,7 @@ runtime-benchmarks = [
"pallet-bags-list/runtime-benchmarks",
"runtime-parachains/runtime-benchmarks",
"pallet-xcm-benchmarks/runtime-benchmarks",
"pallet-asset-rate/runtime-benchmarks",
]
try-runtime = [
"frame-executive/try-runtime",
Expand Down Expand Up @@ -302,6 +305,7 @@ try-runtime = [
"pallet-babe/try-runtime",
"pallet-xcm/try-runtime",
"runtime-common/try-runtime",
"pallet-asset-rate/try-runtime",
]
# When enabled, the runtime API will not be build.
#
Expand Down
46 changes: 43 additions & 3 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ use sp_mmr_primitives as mmr;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{
AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto, Extrinsic as ExtrinsicT,
OpaqueKeys, SaturatedConversion, Verify,
AccountIdLookup, BlakeTwo256, Block as BlockT, CloneIdentity, ConvertInto,
Extrinsic as ExtrinsicT, Identity as IdentityConvert, IdentityLookup, OpaqueKeys,
SaturatedConversion, Verify,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedU128, KeyTypeId, Perbill, Percent, Permill,
Expand All @@ -84,7 +85,8 @@ use sp_staking::SessionIndex;
#[cfg(any(feature = "std", test))]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use xcm::latest::Junction;
use xcm::latest::{InteriorMultiLocation, Junction, Junction::PalletInstance, MultiLocation};
use xcm_builder::{LocatableAssetId, PayOverXcm};

pub use frame_system::Call as SystemCall;
pub use pallet_balances::Call as BalancesCall;
Expand Down Expand Up @@ -613,6 +615,10 @@ parameter_types! {
pub const SpendPeriod: BlockNumber = 6 * DAYS;
pub const Burn: Permill = Permill::from_perthousand(2);
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
pub const PayoutSpendPeriod: BlockNumber = 30 * DAYS;
// The asset's interior location for the paying account. This is the Treasury
// pallet instance (which sits at index 18).
pub TreasuryInteriorLocation: InteriorMultiLocation = PalletInstance(18).into();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hardcoding 18 here, I propose that we fetch it programmatically using the PalletInfo trait.


pub const TipCountdown: BlockNumber = 1 * DAYS;
pub const TipFindersFee: Percent = Percent::from_percent(20);
Expand Down Expand Up @@ -641,6 +647,23 @@ impl pallet_treasury::Config for Runtime {
type WeightInfo = weights::pallet_treasury::WeightInfo<Runtime>;
type SpendFunds = Bounties;
type SpendOrigin = TreasurySpender;
type AssetKind = LocatableAssetId;
type Beneficiary = MultiLocation;
type BeneficiaryLookup = IdentityLookup<Self::Beneficiary>;
type Paymaster = PayOverXcm<
TreasuryInteriorLocation,
crate::xcm_config::XcmRouter,
crate::XcmPallet,
ConstU32<{ 6 * HOURS }>,
Self::Beneficiary,
Self::AssetKind,
IdentityConvert,
CloneIdentity,
>;
type BalanceConverter = AssetRate;
type PayoutPeriod = PayoutSpendPeriod;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = runtime_common::impls::benchmarks::TreasuryArguments;
}

parameter_types! {
Expand Down Expand Up @@ -1342,6 +1365,19 @@ impl pallet_nomination_pools::Config for Runtime {
type MaxPointsToBalance = MaxPointsToBalance;
}

impl pallet_asset_rate::Config for Runtime {
type WeightInfo = weights::pallet_asset_rate::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
type CreateOrigin = EitherOfDiverse<EnsureRoot<AccountId>, Treasurer>;
type RemoveOrigin = EitherOfDiverse<EnsureRoot<AccountId>, Treasurer>;
type UpdateOrigin = EitherOfDiverse<EnsureRoot<AccountId>, Treasurer>;
type Balance = Balance;
type Currency = Balances;
type AssetId = <Runtime as pallet_treasury::Config>::AssetKind;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = runtime_common::impls::benchmarks::LocatableAssetFactory;
}

parameter_types! {
// The deposit configuration for the singed migration. Specially if you want to allow any signed account to do the migration (see `SignedFilter`, these deposits should be high)
pub const MigrationSignedDepositPerItem: Balance = 1 * CENTS;
Expand Down Expand Up @@ -1457,6 +1493,9 @@ construct_runtime! {
// Fast unstake pallet: extension to staking.
FastUnstake: pallet_fast_unstake = 42,

// Asset rate.
AssetRate: pallet_asset_rate::{Pallet, Call, Storage, Event<T>} = 46,

// Parachains pallets. Start indices at 50 to leave room.
ParachainsOrigin: parachains_origin::{Pallet, Origin} = 50,
Configuration: parachains_configuration::{Pallet, Call, Storage, Config<T>} = 51,
Expand Down Expand Up @@ -1675,6 +1714,7 @@ mod benches {
[pallet_utility, Utility]
[pallet_vesting, Vesting]
[pallet_whitelist, Whitelist]
[pallet_asset_rate, AssetRate]
// XCM
[pallet_xcm, XcmPallet]
[pallet_xcm_benchmarks::fungible, pallet_xcm_benchmarks::fungible::Pallet::<Runtime>]
Expand Down
1 change: 1 addition & 0 deletions runtime/kusama/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

pub mod frame_election_provider_support;
pub mod frame_system;
pub mod pallet_asset_rate;
pub mod pallet_bags_list;
pub mod pallet_balances;
pub mod pallet_balances_nis_counterpart_balances;
Expand Down
86 changes: 86 additions & 0 deletions runtime/kusama/src/weights/pallet_asset_rate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! Autogenerated weights for `pallet_asset_rate`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-03, STEPS: `50`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `cob`, CPU: `<UNKNOWN>`
//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("kusama-dev"), DB CACHE: 1024

// Executed Command:
// ./target/debug/polkadot
// benchmark
// pallet
// --chain=kusama-dev
// --steps=50
// --repeat=2
// --pallet=pallet_asset_rate
// --extrinsic=*
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./runtime/kusama/src/weights/
// --header=./file_header.txt

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::Weight};
use core::marker::PhantomData;

/// Weight functions for `pallet_asset_rate`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_asset_rate::WeightInfo for WeightInfo<T> {
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(1237), added: 3712, mode: MaxEncodedLen)
fn create() -> Weight {
// Proof Size summary in bytes:
// Measured: `42`
// Estimated: `4702`
// Minimum execution time: 53_000_000 picoseconds.
Weight::from_parts(55_000_000, 0)
.saturating_add(Weight::from_parts(0, 4702))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(1237), added: 3712, mode: MaxEncodedLen)
fn update() -> Weight {
// Proof Size summary in bytes:
// Measured: `110`
// Estimated: `4702`
// Minimum execution time: 60_000_000 picoseconds.
Weight::from_parts(60_000_000, 0)
.saturating_add(Weight::from_parts(0, 4702))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: AssetRate ConversionRateToNative (r:1 w:1)
/// Proof: AssetRate ConversionRateToNative (max_values: None, max_size: Some(1237), added: 3712, mode: MaxEncodedLen)
fn remove() -> Weight {
// Proof Size summary in bytes:
// Measured: `110`
// Estimated: `4702`
// Minimum execution time: 66_000_000 picoseconds.
Weight::from_parts(74_000_000, 0)
.saturating_add(Weight::from_parts(0, 4702))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
}
Loading