From 707247e9648237dcb0f8b912cb4cf4ed749bc01d Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Fri, 5 Apr 2024 00:56:12 +1100 Subject: [PATCH] Migrate fee payment from `Currency` to `fungible` (#2292) Part of https://github.com/paritytech/polkadot-sdk/issues/226 Related https://github.com/paritytech/polkadot-sdk/issues/1833 - Deprecate `CurrencyAdapter` and introduce `FungibleAdapter` - Deprecate `ToStakingPot` and replace usage with `ResolveTo` - Required creating a new `StakingPotAccountId` struct that implements `TypedGet` for the staking pot account ID - Update parachain common utils `DealWithFees`, `ToAuthor` and `AssetsToBlockAuthor` implementations to use `fungible` - Update runtime XCM Weight Traders to use `ResolveTo` instead of `ToStakingPot` - Update runtime Transaction Payment pallets to use `FungibleAdapter` instead of `CurrencyAdapter` - [x] Blocked by https://github.com/paritytech/polkadot-sdk/pull/1296, needs the `Unbalanced::decrease_balance` fix --- bridges/bin/runtime-common/src/mock.rs | 2 +- cumulus/pallets/collator-selection/src/lib.rs | 14 +++ cumulus/parachains/common/src/impls.rs | 45 ++++++--- .../assets/asset-hub-rococo/src/lib.rs | 2 +- .../assets/asset-hub-rococo/src/xcm_config.rs | 13 ++- .../assets/asset-hub-westend/src/lib.rs | 2 +- .../asset-hub-westend/src/xcm_config.rs | 13 ++- .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 2 +- .../bridge-hub-rococo/src/xcm_config.rs | 13 ++- .../bridge-hubs/bridge-hub-westend/src/lib.rs | 2 +- .../bridge-hub-westend/src/xcm_config.rs | 13 ++- .../collectives-westend/src/lib.rs | 2 +- .../collectives-westend/src/xcm_config.rs | 21 ++-- .../contracts/contracts-rococo/src/lib.rs | 2 +- .../coretime/coretime-rococo/src/lib.rs | 2 +- .../coretime-rococo/src/xcm_config.rs | 13 ++- .../coretime/coretime-westend/src/lib.rs | 2 +- .../coretime-westend/src/xcm_config.rs | 6 +- .../runtimes/people/people-rococo/src/lib.rs | 2 +- .../people/people-rococo/src/xcm_config.rs | 13 ++- .../runtimes/people/people-westend/src/lib.rs | 2 +- .../people/people-westend/src/xcm_config.rs | 13 ++- .../runtimes/testing/penpal/src/lib.rs | 2 +- .../testing/rococo-parachain/src/lib.rs | 2 +- cumulus/test/runtime/src/lib.rs | 2 +- polkadot/runtime/common/src/crowdloan/mod.rs | 4 - polkadot/runtime/common/src/impls.rs | 38 +++++--- polkadot/runtime/common/src/lib.rs | 3 + polkadot/runtime/rococo/src/lib.rs | 4 +- polkadot/runtime/test-runtime/src/lib.rs | 4 +- polkadot/runtime/westend/src/lib.rs | 4 +- polkadot/xcm/xcm-builder/src/weight.rs | 33 ++++--- prdoc/pr_2292.prdoc | 59 ++++++++++++ substrate/bin/node/runtime/src/lib.rs | 6 ++ substrate/frame/balances/src/tests/mod.rs | 4 +- substrate/frame/executive/src/tests.rs | 4 +- .../asset-conversion-tx-payment/src/mock.rs | 13 ++- .../asset-tx-payment/src/mock.rs | 4 +- .../frame/transaction-payment/src/mock.rs | 12 ++- .../frame/transaction-payment/src/payment.rs | 95 +++++++++++++++++-- substrate/frame/treasury/src/lib.rs | 14 +++ templates/minimal/runtime/src/lib.rs | 2 +- .../parachain/runtime/src/configs/mod.rs | 2 +- templates/solochain/runtime/src/lib.rs | 4 +- 44 files changed, 383 insertions(+), 131 deletions(-) create mode 100644 prdoc/pr_2292.prdoc diff --git a/bridges/bin/runtime-common/src/mock.rs b/bridges/bin/runtime-common/src/mock.rs index 8c4cb2233e17c..ad71cd0d456d8 100644 --- a/bridges/bin/runtime-common/src/mock.rs +++ b/bridges/bin/runtime-common/src/mock.rs @@ -166,7 +166,7 @@ impl pallet_balances::Config for TestRuntime { #[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)] impl pallet_transaction_payment::Config for TestRuntime { - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = IdentityFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/pallets/collator-selection/src/lib.rs b/cumulus/pallets/collator-selection/src/lib.rs index 84bde5c9fac9f..82cce385b4d46 100644 --- a/cumulus/pallets/collator-selection/src/lib.rs +++ b/cumulus/pallets/collator-selection/src/lib.rs @@ -81,6 +81,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +use core::marker::PhantomData; +use frame_support::traits::TypedGet; pub use pallet::*; #[cfg(test)] @@ -981,3 +983,15 @@ pub mod pallet { } } } + +/// [`TypedGet`] implementaion to get the AccountId of the StakingPot. +pub struct StakingPotAccountId(PhantomData); +impl TypedGet for StakingPotAccountId +where + R: crate::Config, +{ + type Type = ::AccountId; + fn get() -> Self::Type { + >::account_id() + } +} diff --git a/cumulus/parachains/common/src/impls.rs b/cumulus/parachains/common/src/impls.rs index 6a990740f0f19..d70fdfeb7095a 100644 --- a/cumulus/parachains/common/src/impls.rs +++ b/cumulus/parachains/common/src/impls.rs @@ -17,10 +17,11 @@ //! Taken from polkadot/runtime/common (at a21cd64) and adapted for parachains. use frame_support::traits::{ - fungibles::{self, Balanced, Credit}, - Contains, ContainsPair, Currency, Get, Imbalance, OnUnbalanced, OriginTrait, + fungible, fungibles, tokens::imbalance::ResolveTo, Contains, ContainsPair, Currency, Defensive, + Get, Imbalance, OnUnbalanced, OriginTrait, }; use pallet_asset_tx_payment::HandleCredit; +use pallet_collator_selection::StakingPotAccountId; use sp_runtime::traits::Zero; use sp_std::{marker::PhantomData, prelude::*}; use xcm::latest::{ @@ -29,16 +30,20 @@ use xcm::latest::{ }; use xcm_executor::traits::ConvertLocation; +/// Type alias to conveniently refer to `frame_system`'s `Config::AccountId`. +pub type AccountIdOf = ::AccountId; + /// Type alias to conveniently refer to the `Currency::NegativeImbalance` associated type. pub type NegativeImbalance = as Currency< ::AccountId, >>::NegativeImbalance; -/// Type alias to conveniently refer to `frame_system`'s `Config::AccountId`. -pub type AccountIdOf = ::AccountId; - /// Implementation of `OnUnbalanced` that deposits the fees into a staking pot for later payout. +#[deprecated( + note = "ToStakingPot is deprecated and will be removed after March 2024. Please use frame_support::traits::tokens::imbalance::ResolveTo instead." +)] pub struct ToStakingPot(PhantomData); +#[allow(deprecated)] impl OnUnbalanced> for ToStakingPot where R: pallet_balances::Config + pallet_collator_selection::Config, @@ -47,25 +52,30 @@ where { fn on_nonzero_unbalanced(amount: NegativeImbalance) { let staking_pot = >::account_id(); + // In case of error: Will drop the result triggering the `OnDrop` of the imbalance. >::resolve_creating(&staking_pot, amount); } } -/// Implementation of `OnUnbalanced` that deals with the fees by combining tip and fee and passing -/// the result on to `ToStakingPot`. +/// Fungible implementation of `OnUnbalanced` that deals with the fees by combining tip and fee and +/// passing the result on to `ToStakingPot`. pub struct DealWithFees(PhantomData); -impl OnUnbalanced> for DealWithFees +impl OnUnbalanced>> for DealWithFees where R: pallet_balances::Config + pallet_collator_selection::Config, AccountIdOf: From + Into, ::RuntimeEvent: From>, { - fn on_unbalanceds(mut fees_then_tips: impl Iterator>) { + fn on_unbalanceds( + mut fees_then_tips: impl Iterator< + Item = fungible::Credit>, + >, + ) { if let Some(mut fees) = fees_then_tips.next() { if let Some(tips) = fees_then_tips.next() { tips.merge_into(&mut fees); } - as OnUnbalanced<_>>::on_unbalanced(fees); + ResolveTo::, pallet_balances::Pallet>::on_unbalanced(fees) } } } @@ -79,10 +89,11 @@ where R: pallet_authorship::Config + pallet_assets::Config, AccountIdOf: From + Into, { - fn handle_credit(credit: Credit, pallet_assets::Pallet>) { + fn handle_credit(credit: fungibles::Credit, pallet_assets::Pallet>) { + use frame_support::traits::fungibles::Balanced; if let Some(author) = pallet_authorship::Pallet::::author() { // In case of error: Will drop the result triggering the `OnDrop` of the imbalance. - let _ = pallet_assets::Pallet::::resolve(&author, credit); + let _ = pallet_assets::Pallet::::resolve(&author, credit).defensive(); } } } @@ -313,8 +324,14 @@ mod tests { #[test] fn test_fees_and_tip_split() { new_test_ext().execute_with(|| { - let fee = Balances::issue(10); - let tip = Balances::issue(20); + let fee = + as frame_support::traits::fungible::Balanced< + AccountId, + >>::issue(10); + let tip = + as frame_support::traits::fungible::Balanced< + AccountId, + >>::issue(20); assert_eq!(Balances::free_balance(TEST_ACCOUNT), 0); diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs index 7edec45abfbb2..547d465802efb 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs @@ -226,7 +226,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = SlowAdjustingFeeUpdate; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs index f4ff985e2773f..f71b5e9c5e88b 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/xcm_config.rs @@ -27,14 +27,13 @@ use assets_common::{ use frame_support::{ parameter_types, traits::{ - tokens::imbalance::ResolveAssetTo, ConstU32, Contains, Equals, Everything, Nothing, - PalletInfoAccess, + tokens::imbalance::{ResolveAssetTo, ResolveTo}, + ConstU32, Contains, Equals, Everything, Nothing, PalletInfoAccess, }, }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, AssetFeeAsExistentialDepositMultiplier, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -569,7 +568,13 @@ impl xcm_executor::Config for XcmConfig { MaxInstructions, >; type Trader = ( - UsingComponents>, + UsingComponents< + WeightToFee, + TokenLocation, + AccountId, + Balances, + ResolveTo, + >, cumulus_primitives_utility::SwapFirstAssetTrader< TokenLocationV3, crate::AssetConversion, diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index d17d5a707579d..97928d0548436 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -210,7 +210,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = SlowAdjustingFeeUpdate; diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs index 7d3ed650e6bfc..360b1a7055b70 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/xcm_config.rs @@ -27,14 +27,13 @@ use assets_common::{ use frame_support::{ parameter_types, traits::{ - tokens::imbalance::ResolveAssetTo, ConstU32, Contains, Equals, Everything, Nothing, - PalletInfoAccess, + tokens::imbalance::{ResolveAssetTo, ResolveTo}, + ConstU32, Contains, Equals, Everything, Nothing, PalletInfoAccess, }, }; use frame_system::EnsureRoot; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, AssetFeeAsExistentialDepositMultiplier, ConcreteAssetFromSystem, RelayOrOtherSystemParachains, @@ -591,7 +590,13 @@ impl xcm_executor::Config for XcmConfig { MaxInstructions, >; type Trader = ( - UsingComponents>, + UsingComponents< + WeightToFee, + WestendLocation, + AccountId, + Balances, + ResolveTo, + >, cumulus_primitives_utility::SwapFirstAssetTrader< WestendLocationV3, crate::AssetConversion, diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 9796a77f994e4..6ffb7eb3c948e 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -317,7 +317,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs index 8934ff9b22729..8de7aa742fc68 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs @@ -33,13 +33,13 @@ use bp_relayers::{PayRewardFromAccount, RewardsAccountOwner, RewardsAccountParam use bp_runtime::ChainId; use frame_support::{ parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, StoragePrefixedMap, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -295,8 +295,13 @@ impl xcm_executor::Config for XcmConfig { RuntimeCall, MaxInstructions, >; - type Trader = - UsingComponents>; + type Trader = UsingComponents< + WeightToFee, + TokenLocation, + AccountId, + Balances, + ResolveTo, Balances>, + >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetLocker = (); diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs index 4318df8f15edb..9571cc68a1873 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/lib.rs @@ -291,7 +291,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs index 840d0c9af0e58..dcefeeaef7095 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/xcm_config.rs @@ -22,12 +22,12 @@ use super::{ use crate::bridge_common_config::{DeliveryRewardInBalance, RequiredStakeForStakeAndSlash}; use frame_support::{ parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -244,8 +244,13 @@ impl xcm_executor::Config for XcmConfig { RuntimeCall, MaxInstructions, >; - type Trader = - UsingComponents>; + type Trader = UsingComponents< + WeightToFee, + WestendLocation, + AccountId, + Balances, + ResolveTo, Balances>, + >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetLocker = (); diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs index 170b0a3960006..b7410071f128c 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs @@ -223,7 +223,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = SlowAdjustingFeeUpdate; diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs index b83106a582840..34c0deef534e1 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/xcm_config.rs @@ -20,17 +20,15 @@ use super::{ }; use frame_support::{ parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, weights::Weight, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; -use parachains_common::{ - impls::ToStakingPot, - xcm_config::{ - AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, - RelayOrOtherSystemParachains, - }, +use parachains_common::xcm_config::{ + AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, + RelayOrOtherSystemParachains, }; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_common::xcm_sender::ExponentialPrice; @@ -268,8 +266,13 @@ impl xcm_executor::Config for XcmConfig { type UniversalLocation = UniversalLocation; type Barrier = Barrier; type Weigher = FixedWeightBounds; - type Trader = - UsingComponents>; + type Trader = UsingComponents< + WeightToFee, + WndLocation, + AccountId, + Balances, + ResolveTo, Balances>, + >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetClaims = PolkadotXcm; diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs index 936d87a23caa0..1d2da68067523 100644 --- a/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/src/lib.rs @@ -232,7 +232,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type WeightToFee = WeightToFee; /// Relay Chain `TransactionByteFee` / 10 type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs index 27965aa204f98..7e489cdd06fea 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs @@ -244,7 +244,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs index 955f2eeba339c..afb68749f3b9b 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/xcm_config.rs @@ -22,12 +22,12 @@ use super::{ use frame_support::{ pallet_prelude::PalletInfoAccess, parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -237,8 +237,13 @@ impl xcm_executor::Config for XcmConfig { RuntimeCall, MaxInstructions, >; - type Trader = - UsingComponents>; + type Trader = UsingComponents< + WeightToFee, + RocRelayLocation, + AccountId, + Balances, + ResolveTo, Balances>, + >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetClaims = PolkadotXcm; diff --git a/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs b/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs index 8075ffb4f1c22..d54e1be44fc75 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-westend/src/lib.rs @@ -244,7 +244,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs index fc7d36a8ba185..eac07c97c0179 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-westend/src/xcm_config.rs @@ -22,12 +22,12 @@ use super::{ use frame_support::{ pallet_prelude::PalletInfoAccess, parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -249,7 +249,7 @@ impl xcm_executor::Config for XcmConfig { TokenRelayLocation, AccountId, Balances, - ToStakingPot, + ResolveTo, Balances>, >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs b/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs index 1b6499f5d6178..4232d9e41dd48 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/lib.rs @@ -225,7 +225,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/xcm_config.rs b/cumulus/parachains/runtimes/people/people-rococo/src/xcm_config.rs index a10333fdb6265..048728d271aa0 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/xcm_config.rs @@ -20,12 +20,12 @@ use super::{ use crate::{TransactionByteFee, CENTS}; use frame_support::{ parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -249,8 +249,13 @@ impl xcm_executor::Config for XcmConfig { RuntimeCall, MaxInstructions, >; - type Trader = - UsingComponents>; + type Trader = UsingComponents< + WeightToFee, + RelayLocation, + AccountId, + Balances, + ResolveTo, Balances>, + >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetClaims = PolkadotXcm; diff --git a/cumulus/parachains/runtimes/people/people-westend/src/lib.rs b/cumulus/parachains/runtimes/people/people-westend/src/lib.rs index 6ae53d641b0c7..25b17fd502e1f 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/lib.rs @@ -225,7 +225,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = - pallet_transaction_payment::CurrencyAdapter>; + pallet_transaction_payment::FungibleAdapter>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/cumulus/parachains/runtimes/people/people-westend/src/xcm_config.rs b/cumulus/parachains/runtimes/people/people-westend/src/xcm_config.rs index fee2f5684ac30..368b20e1c9e05 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/xcm_config.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/xcm_config.rs @@ -20,12 +20,12 @@ use super::{ use crate::{TransactionByteFee, CENTS}; use frame_support::{ parameter_types, - traits::{ConstU32, Contains, Equals, Everything, Nothing}, + traits::{tokens::imbalance::ResolveTo, ConstU32, Contains, Equals, Everything, Nothing}, }; use frame_system::EnsureRoot; +use pallet_collator_selection::StakingPotAccountId; use pallet_xcm::XcmPassthrough; use parachains_common::{ - impls::ToStakingPot, xcm_config::{ AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains, @@ -257,8 +257,13 @@ impl xcm_executor::Config for XcmConfig { RuntimeCall, MaxInstructions, >; - type Trader = - UsingComponents>; + type Trader = UsingComponents< + WeightToFee, + RelayLocation, + AccountId, + Balances, + ResolveTo, Balances>, + >; type ResponseHandler = PolkadotXcm; type AssetTrap = PolkadotXcm; type AssetClaims = PolkadotXcm; diff --git a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs index 0a55d2dcfe53e..c8c469b76fed3 100644 --- a/cumulus/parachains/runtimes/testing/penpal/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/penpal/src/lib.rs @@ -416,7 +416,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = SlowAdjustingFeeUpdate; diff --git a/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs b/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs index 034d16267d450..d896d7492b5a3 100644 --- a/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs +++ b/cumulus/parachains/runtimes/testing/rococo-parachain/src/lib.rs @@ -259,7 +259,7 @@ impl pallet_balances::Config for Runtime { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter; type WeightToFee = IdentityFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = (); diff --git a/cumulus/test/runtime/src/lib.rs b/cumulus/test/runtime/src/lib.rs index 5127b63f27155..350c945765b4a 100644 --- a/cumulus/test/runtime/src/lib.rs +++ b/cumulus/test/runtime/src/lib.rs @@ -241,7 +241,7 @@ impl pallet_balances::Config for Runtime { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter; type WeightToFee = IdentityFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = (); diff --git a/polkadot/runtime/common/src/crowdloan/mod.rs b/polkadot/runtime/common/src/crowdloan/mod.rs index d405278411b7e..a2143212d8c59 100644 --- a/polkadot/runtime/common/src/crowdloan/mod.rs +++ b/polkadot/runtime/common/src/crowdloan/mod.rs @@ -83,10 +83,6 @@ type CurrencyOf = <::Auctioneer as Auctioneer> type LeasePeriodOf = <::Auctioneer as Auctioneer>>::LeasePeriod; type BalanceOf = as Currency<::AccountId>>::Balance; -#[allow(dead_code)] -type NegativeImbalanceOf = - as Currency<::AccountId>>::NegativeImbalance; - type FundIndex = u32; pub trait WeightInfo { diff --git a/polkadot/runtime/common/src/impls.rs b/polkadot/runtime/common/src/impls.rs index acf5a701a62d0..cc1243790c2e5 100644 --- a/polkadot/runtime/common/src/impls.rs +++ b/polkadot/runtime/common/src/impls.rs @@ -16,8 +16,12 @@ //! Auxiliary `struct`/`enum`s for polkadot runtime. -use crate::NegativeImbalance; -use frame_support::traits::{Currency, Imbalance, OnUnbalanced}; +use frame_support::traits::{ + fungible::{Balanced, Credit}, + tokens::imbalance::ResolveTo, + Imbalance, OnUnbalanced, +}; +use pallet_treasury::TreasuryAccountId; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use primitives::Balance; use sp_runtime::{traits::TryConvert, Perquintill, RuntimeDebug}; @@ -25,28 +29,31 @@ use xcm::VersionedLocation; /// Logic for the author to get a portion of fees. pub struct ToAuthor(sp_std::marker::PhantomData); -impl OnUnbalanced> for ToAuthor +impl OnUnbalanced>> for ToAuthor where R: pallet_balances::Config + pallet_authorship::Config, ::AccountId: From, ::AccountId: Into, { - fn on_nonzero_unbalanced(amount: NegativeImbalance) { + fn on_nonzero_unbalanced( + amount: Credit<::AccountId, pallet_balances::Pallet>, + ) { if let Some(author) = >::author() { - >::resolve_creating(&author, amount); + let _ = >::resolve(&author, amount); } } } pub struct DealWithFees(sp_std::marker::PhantomData); -impl OnUnbalanced> for DealWithFees +impl OnUnbalanced>> for DealWithFees where - R: pallet_balances::Config + pallet_treasury::Config + pallet_authorship::Config, - pallet_treasury::Pallet: OnUnbalanced>, + R: pallet_balances::Config + pallet_authorship::Config + pallet_treasury::Config, ::AccountId: From, ::AccountId: Into, { - fn on_unbalanceds(mut fees_then_tips: impl Iterator>) { + fn on_unbalanceds( + mut fees_then_tips: impl Iterator>>, + ) { if let Some(fees) = fees_then_tips.next() { // for fees, 80% to treasury, 20% to author let mut split = fees.ration(80, 20); @@ -54,8 +61,7 @@ where // for tips, if any, 100% to author tips.merge_into(&mut split.1); } - use pallet_treasury::Pallet as Treasury; - as OnUnbalanced<_>>::on_unbalanced(split.0); + ResolveTo::, pallet_balances::Pallet>::on_unbalanced(split.0); as OnUnbalanced<_>>::on_unbalanced(split.1); } } @@ -366,8 +372,14 @@ mod tests { #[test] fn test_fees_and_tip_split() { new_test_ext().execute_with(|| { - let fee = Balances::issue(10); - let tip = Balances::issue(20); + let fee = + as frame_support::traits::fungible::Balanced< + AccountId, + >>::issue(10); + let tip = + as frame_support::traits::fungible::Balanced< + AccountId, + >>::issue(20); assert_eq!(Balances::free_balance(Treasury::account_id()), 0); assert_eq!(Balances::free_balance(TEST_ACCOUNT), 0); diff --git a/polkadot/runtime/common/src/lib.rs b/polkadot/runtime/common/src/lib.rs index bd49d3cccc9ca..65161764ccd7b 100644 --- a/polkadot/runtime/common/src/lib.rs +++ b/polkadot/runtime/common/src/lib.rs @@ -63,6 +63,9 @@ pub use sp_runtime::BuildStorage; /// Implementations of some helper traits passed into runtime modules as associated types. pub use impls::ToAuthor; +#[deprecated( + note = "Please use fungible::Credit instead. This type will be removed some time after March 2024." +)] pub type NegativeImbalance = as Currency< ::AccountId, >>::NegativeImbalance; diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 7d16d2dbf1655..471bb891e00ed 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -79,7 +79,7 @@ use frame_system::{EnsureRoot, EnsureSigned}; use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId}; use pallet_identity::legacy::IdentityInfo; use pallet_session::historical as session_historical; -use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo}; +use pallet_transaction_payment::{FeeDetails, FungibleAdapter, RuntimeDispatchInfo}; use sp_core::{ConstU128, OpaqueMetadata, H256}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, @@ -324,7 +324,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter>; + type OnChargeTransaction = FungibleAdapter>; type OperationalFeeMultiplier = OperationalFeeMultiplier; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/polkadot/runtime/test-runtime/src/lib.rs b/polkadot/runtime/test-runtime/src/lib.rs index 446cd101efffb..40aa4a1d49974 100644 --- a/polkadot/runtime/test-runtime/src/lib.rs +++ b/polkadot/runtime/test-runtime/src/lib.rs @@ -20,7 +20,7 @@ // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256. #![recursion_limit = "256"] -use pallet_transaction_payment::CurrencyAdapter; +use pallet_transaction_payment::FungibleAdapter; use parity_scale_codec::Encode; use sp_std::{collections::btree_map::BTreeMap, prelude::*}; @@ -233,7 +233,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter; + type OnChargeTransaction = FungibleAdapter; type OperationalFeeMultiplier = OperationalFeeMultiplier; type WeightToFee = WeightToFee; type LengthToFee = frame_support::weights::ConstantMultiplier; diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 9445e27f0e51c..4de1dbc16e095 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -42,7 +42,7 @@ use frame_system::{EnsureRoot, EnsureSigned}; use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId}; use pallet_identity::legacy::IdentityInfo; use pallet_session::historical as session_historical; -use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo}; +use pallet_transaction_payment::{FeeDetails, FungibleAdapter, RuntimeDispatchInfo}; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use primitives::{ slashing, AccountId, AccountIndex, ApprovalVotingParams, Balance, BlockNumber, CandidateEvent, @@ -377,7 +377,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter>; + type OnChargeTransaction = FungibleAdapter>; type OperationalFeeMultiplier = OperationalFeeMultiplier; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; diff --git a/polkadot/xcm/xcm-builder/src/weight.rs b/polkadot/xcm/xcm-builder/src/weight.rs index 6026218f55316..6141b0142eed0 100644 --- a/polkadot/xcm/xcm-builder/src/weight.rs +++ b/polkadot/xcm/xcm-builder/src/weight.rs @@ -16,7 +16,10 @@ use frame_support::{ dispatch::GetDispatchInfo, - traits::{tokens::currency::Currency as CurrencyT, Get, OnUnbalanced as OnUnbalancedT}, + traits::{ + fungible::{Balanced, Credit, Inspect}, + Get, OnUnbalanced as OnUnbalancedT, + }, weights::{ constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND}, WeightToFee as WeightToFeeT, @@ -193,23 +196,23 @@ impl, R: TakeRevenue> Drop for FixedRateOfFungible /// Weight trader which uses the configured `WeightToFee` to set the right price for weight and then /// places any weight bought into the right account. pub struct UsingComponents< - WeightToFee: WeightToFeeT, + WeightToFee: WeightToFeeT>::Balance>, AssetIdValue: Get, AccountId, - Currency: CurrencyT, - OnUnbalanced: OnUnbalancedT, + Fungible: Balanced + Inspect, + OnUnbalanced: OnUnbalancedT>, >( Weight, - Currency::Balance, - PhantomData<(WeightToFee, AssetIdValue, AccountId, Currency, OnUnbalanced)>, + Fungible::Balance, + PhantomData<(WeightToFee, AssetIdValue, AccountId, Fungible, OnUnbalanced)>, ); impl< - WeightToFee: WeightToFeeT, + WeightToFee: WeightToFeeT>::Balance>, AssetIdValue: Get, AccountId, - Currency: CurrencyT, - OnUnbalanced: OnUnbalancedT, - > WeightTrader for UsingComponents + Fungible: Balanced + Inspect, + OnUnbalanced: OnUnbalancedT>, + > WeightTrader for UsingComponents { fn new() -> Self { Self(Weight::zero(), Zero::zero(), PhantomData) @@ -247,14 +250,14 @@ impl< } } impl< - WeightToFee: WeightToFeeT, + WeightToFee: WeightToFeeT>::Balance>, AssetId: Get, AccountId, - Currency: CurrencyT, - OnUnbalanced: OnUnbalancedT, - > Drop for UsingComponents + Fungible: Balanced + Inspect, + OnUnbalanced: OnUnbalancedT>, + > Drop for UsingComponents { fn drop(&mut self) { - OnUnbalanced::on_unbalanced(Currency::issue(self.1)); + OnUnbalanced::on_unbalanced(Fungible::issue(self.1)); } } diff --git a/prdoc/pr_2292.prdoc b/prdoc/pr_2292.prdoc new file mode 100644 index 0000000000000..e4c50f7806bae --- /dev/null +++ b/prdoc/pr_2292.prdoc @@ -0,0 +1,59 @@ +title: Migrate Fee Payment from Currency to fungible traits + +doc: + - audience: Runtime Dev + description: | + Deprecates the `CurrencyAdapter` and introduces `FungibleAdapter` + Deprecates `ToStakingPot` and replaces usage with `ResolveTo` + Updated `DealWithFees`, `ToAuthor`, `AssetsToBlockAuthor` to all use `fungible` traits + Updated runtime XCM Weight Traders to use `ResolveTo` + Updated runtime TransactionPayment pallets to use `FungibleAdapter` instead of `CurrencyAdapter` + + Runtime Migration Guide: + - Replace usage of `CurrencyAdapter` with `FungibleAdapter` + - Replace usage of `ToStakingPot` with `ResolveTo, Balances>` + +crates: + - name: pallet-collator-selection + bump: minor + - name: parachains-common + bump: major + - name: asset-hub-rococo-runtime + bump: major + - name: asset-hub-westend-runtime + bump: major + - name: bridge-hub-westend-runtime + bump: major + - name: bridge-hub-rococo-runtime + bump: major + - name: collectives-westend-runtime + bump: major + - name: contracts-rococo-runtime + bump: major + - name: coretime-rococo-runtime + bump: major + - name: coretime-westend-runtime + bump: major + - name: people-westend-runtime + bump: major + - name: people-rococo-runtime + bump: major + - name: polkadot-runtime-common + bump: major + - name: westend-runtime + bump: major + - name: rococo-runtime + bump: major + - name: staging-xcm-builder + bump: major + - name: kitchensink-runtime + bump: major + - name: pallet-transaction-payment + bump: minor + - name: minimal-template-runtime + bump: major + - name: parachain-template-runtime + bump: major + - name: solochain-template-runtime + bump: major + diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 191244c3bb060..5ed5e7943f696 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -71,6 +71,9 @@ use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use pallet_nfts::PalletFeatures; use pallet_nis::WithMaximumOf; use pallet_session::historical as pallet_session_historical; +// Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles +// +#[allow(deprecated)] pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment}; use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; use pallet_tx_pause::RuntimeCallNameOf; @@ -549,6 +552,9 @@ parameter_types! { pub MaximumMultiplier: Multiplier = Bounded::max_value(); } +// Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles +// +#[allow(deprecated)] impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; type OnChargeTransaction = CurrencyAdapter; diff --git a/substrate/frame/balances/src/tests/mod.rs b/substrate/frame/balances/src/tests/mod.rs index f2f107d8bd64e..234fe6eaf2c32 100644 --- a/substrate/frame/balances/src/tests/mod.rs +++ b/substrate/frame/balances/src/tests/mod.rs @@ -32,7 +32,7 @@ use frame_support::{ weights::{IdentityFee, Weight}, }; use frame_system::{self as system, RawOrigin}; -use pallet_transaction_payment::{ChargeTransactionPayment, CurrencyAdapter, Multiplier}; +use pallet_transaction_payment::{ChargeTransactionPayment, FungibleAdapter, Multiplier}; use scale_info::TypeInfo; use sp_core::hexdisplay::HexDisplay; use sp_io; @@ -99,7 +99,7 @@ impl frame_system::Config for Test { #[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)] impl pallet_transaction_payment::Config for Test { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter, ()>; + type OnChargeTransaction = FungibleAdapter, ()>; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = IdentityFee; type LengthToFee = IdentityFee; diff --git a/substrate/frame/executive/src/tests.rs b/substrate/frame/executive/src/tests.rs index 204889a292fd7..e3721f7b6dcba 100644 --- a/substrate/frame/executive/src/tests.rs +++ b/substrate/frame/executive/src/tests.rs @@ -19,6 +19,7 @@ use super::*; +use pallet_transaction_payment::FungibleAdapter; use sp_core::H256; use sp_runtime::{ generic::{DigestItem, Era}, @@ -40,7 +41,6 @@ use frame_support::{ }; use frame_system::{pallet_prelude::*, ChainContext, LastRuntimeUpgrade, LastRuntimeUpgradeInfo}; use pallet_balances::Call as BalancesCall; -use pallet_transaction_payment::CurrencyAdapter; const TEST_KEY: &[u8] = b":test:key:"; @@ -338,7 +338,7 @@ parameter_types! { } impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter; + type OnChargeTransaction = FungibleAdapter; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = IdentityFee; type LengthToFee = ConstantMultiplier; diff --git a/substrate/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs b/substrate/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs index bd9565fe1887c..d61558cf53636 100644 --- a/substrate/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs +++ b/substrate/frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs @@ -24,6 +24,7 @@ use frame_support::{ pallet_prelude::*, parameter_types, traits::{ + fungible, tokens::{ fungible::{NativeFromLeft, NativeOrWithId, UnionOf}, imbalance::ResolveAssetTo, @@ -36,7 +37,7 @@ use frame_support::{ use frame_system as system; use frame_system::{EnsureRoot, EnsureSignedBy}; use pallet_asset_conversion::{Ascending, Chain, WithFirstAsset}; -use pallet_transaction_payment::CurrencyAdapter; +use pallet_transaction_payment::FungibleAdapter; use sp_core::H256; use sp_runtime::{ traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, SaturatedConversion}, @@ -155,9 +156,13 @@ parameter_types! { } pub struct DealWithFees; -impl OnUnbalanced> for DealWithFees { +impl OnUnbalanced::AccountId, Balances>> + for DealWithFees +{ fn on_unbalanceds( - mut fees_then_tips: impl Iterator>, + mut fees_then_tips: impl Iterator< + Item = fungible::Credit<::AccountId, Balances>, + >, ) { if let Some(fees) = fees_then_tips.next() { FeeUnbalancedAmount::mutate(|a| *a += fees.peek()); @@ -171,7 +176,7 @@ impl OnUnbalanced> for DealWithFees #[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)] impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter; + type OnChargeTransaction = FungibleAdapter; type WeightToFee = WeightToFee; type LengthToFee = TransactionByteFee; type FeeMultiplierUpdate = (); diff --git a/substrate/frame/transaction-payment/asset-tx-payment/src/mock.rs b/substrate/frame/transaction-payment/asset-tx-payment/src/mock.rs index 4387f319c3b54..b04d4ffd9e0b7 100644 --- a/substrate/frame/transaction-payment/asset-tx-payment/src/mock.rs +++ b/substrate/frame/transaction-payment/asset-tx-payment/src/mock.rs @@ -28,7 +28,7 @@ use frame_support::{ }; use frame_system as system; use frame_system::EnsureRoot; -use pallet_transaction_payment::CurrencyAdapter; +use pallet_transaction_payment::FungibleAdapter; use sp_core::H256; use sp_runtime::traits::{BlakeTwo256, ConvertInto, IdentityLookup, SaturatedConversion}; @@ -139,7 +139,7 @@ impl WeightToFeeT for TransactionByteFee { #[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)] impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter; + type OnChargeTransaction = FungibleAdapter; type WeightToFee = WeightToFee; type LengthToFee = TransactionByteFee; type FeeMultiplierUpdate = (); diff --git a/substrate/frame/transaction-payment/src/mock.rs b/substrate/frame/transaction-payment/src/mock.rs index 67ad1caa02ff8..c1bb05ab5c7eb 100644 --- a/substrate/frame/transaction-payment/src/mock.rs +++ b/substrate/frame/transaction-payment/src/mock.rs @@ -25,7 +25,7 @@ use frame_support::{ derive_impl, dispatch::DispatchClass, parameter_types, - traits::{ConstU32, ConstU64, Imbalance, OnUnbalanced}, + traits::{fungible, ConstU32, ConstU64, Imbalance, OnUnbalanced}, weights::{Weight, WeightToFee as WeightToFeeT}, }; use frame_system as system; @@ -137,9 +137,13 @@ parameter_types! { } pub struct DealWithFees; -impl OnUnbalanced> for DealWithFees { +impl OnUnbalanced::AccountId, Balances>> + for DealWithFees +{ fn on_unbalanceds( - mut fees_then_tips: impl Iterator>, + mut fees_then_tips: impl Iterator< + Item = fungible::Credit<::AccountId, Balances>, + >, ) { if let Some(fees) = fees_then_tips.next() { FeeUnbalancedAmount::mutate(|a| *a += fees.peek()); @@ -152,7 +156,7 @@ impl OnUnbalanced> for DealWithFees impl Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter; + type OnChargeTransaction = FungibleAdapter; type OperationalFeeMultiplier = OperationalFeeMultiplier; type WeightToFee = WeightToFee; type LengthToFee = TransactionByteFee; diff --git a/substrate/frame/transaction-payment/src/payment.rs b/substrate/frame/transaction-payment/src/payment.rs index 886683f2e0b88..0fe616782903c 100644 --- a/substrate/frame/transaction-payment/src/payment.rs +++ b/substrate/frame/transaction-payment/src/payment.rs @@ -25,7 +25,11 @@ use sp_runtime::{ }; use frame_support::{ - traits::{Currency, ExistenceRequirement, Imbalance, OnUnbalanced, WithdrawReasons}, + traits::{ + fungible::{Balanced, Credit, Debt, Inspect}, + tokens::Precision, + Currency, ExistenceRequirement, Imbalance, OnUnbalanced, WithdrawReasons, + }, unsigned::TransactionValidityError, }; @@ -66,18 +70,95 @@ pub trait OnChargeTransaction { ) -> Result<(), TransactionValidityError>; } -/// Implements the transaction payment for a pallet implementing the `Currency` +/// Implements transaction payment for a pallet implementing the [`frame_support::traits::fungible`] +/// trait (eg. pallet_balances) using an unbalance handler (implementing +/// [`OnUnbalanced`]). +/// +/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and +/// then `tip`. +pub struct FungibleAdapter(PhantomData<(F, OU)>); + +impl OnChargeTransaction for FungibleAdapter +where + T: Config, + F: Balanced, + OU: OnUnbalanced>, +{ + type LiquidityInfo = Option>; + type Balance = ::AccountId>>::Balance; + + fn withdraw_fee( + who: &::AccountId, + _call: &::RuntimeCall, + _dispatch_info: &DispatchInfoOf<::RuntimeCall>, + fee: Self::Balance, + _tip: Self::Balance, + ) -> Result { + if fee.is_zero() { + return Ok(None) + } + + match F::withdraw( + who, + fee, + Precision::Exact, + frame_support::traits::tokens::Preservation::Preserve, + frame_support::traits::tokens::Fortitude::Polite, + ) { + Ok(imbalance) => Ok(Some(imbalance)), + Err(_) => Err(InvalidTransaction::Payment.into()), + } + } + + fn correct_and_deposit_fee( + who: &::AccountId, + _dispatch_info: &DispatchInfoOf<::RuntimeCall>, + _post_info: &PostDispatchInfoOf<::RuntimeCall>, + corrected_fee: Self::Balance, + tip: Self::Balance, + already_withdrawn: Self::LiquidityInfo, + ) -> Result<(), TransactionValidityError> { + if let Some(paid) = already_withdrawn { + // Calculate how much refund we should return + let refund_amount = paid.peek().saturating_sub(corrected_fee); + // refund to the the account that paid the fees if it exists. otherwise, don't refind + // anything. + let refund_imbalance = if F::total_balance(who) > F::Balance::zero() { + F::deposit(who, refund_amount, Precision::BestEffort) + .unwrap_or_else(|_| Debt::::zero()) + } else { + Debt::::zero() + }; + // merge the imbalance caused by paying the fees and refunding parts of it again. + let adjusted_paid: Credit = paid + .offset(refund_imbalance) + .same() + .map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?; + // Call someone else to handle the imbalance (fee and tip separately) + let (tip, fee) = adjusted_paid.split(tip); + OU::on_unbalanceds(Some(fee).into_iter().chain(Some(tip))); + } + + Ok(()) + } +} + +/// Implements the transaction payment for a pallet implementing the [`Currency`] /// trait (eg. the pallet_balances) using an unbalance handler (implementing -/// `OnUnbalanced`). +/// [`OnUnbalanced`]). /// -/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: fee and -/// then tip. +/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and +/// then `tip`. +#[deprecated( + note = "Please use the fungible trait and FungibleAdapter. This struct will be removed some time after March 2024." +)] pub struct CurrencyAdapter(PhantomData<(C, OU)>); /// Default implementation for a Currency and an OnUnbalanced handler. /// -/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: fee and -/// then tip. +/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and +/// then `tip`. +#[allow(deprecated)] impl OnChargeTransaction for CurrencyAdapter where T: Config, diff --git a/substrate/frame/treasury/src/lib.rs b/substrate/frame/treasury/src/lib.rs index d569ae406ea39..2723c4289d928 100644 --- a/substrate/frame/treasury/src/lib.rs +++ b/substrate/frame/treasury/src/lib.rs @@ -76,6 +76,8 @@ mod benchmarking; #[cfg(test)] mod tests; pub mod weights; +use core::marker::PhantomData; + #[cfg(feature = "runtime-benchmarks")] pub use benchmarking::ArgumentsFactory; @@ -1120,3 +1122,15 @@ impl, I: 'static> OnUnbalanced> for Palle Self::deposit_event(Event::Deposit { value: numeric_amount }); } } + +/// TypedGet implementaion to get the AccountId of the Treasury. +pub struct TreasuryAccountId(PhantomData); +impl sp_runtime::traits::TypedGet for TreasuryAccountId +where + R: crate::Config, +{ + type Type = ::AccountId; + fn get() -> Self::Type { + >::account_id() + } +} diff --git a/templates/minimal/runtime/src/lib.rs b/templates/minimal/runtime/src/lib.rs index 00fcaf1cec76a..f386169130cec 100644 --- a/templates/minimal/runtime/src/lib.rs +++ b/templates/minimal/runtime/src/lib.rs @@ -104,7 +104,7 @@ impl pallet_timestamp::Config for Runtime {} #[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)] impl pallet_transaction_payment::Config for Runtime { - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter; type WeightToFee = NoFee<::Balance>; type LengthToFee = FixedFee<1, ::Balance>; } diff --git a/templates/parachain/runtime/src/configs/mod.rs b/templates/parachain/runtime/src/configs/mod.rs index e2c51e07d37b9..f1aea481ee277 100644 --- a/templates/parachain/runtime/src/configs/mod.rs +++ b/templates/parachain/runtime/src/configs/mod.rs @@ -166,7 +166,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter; type WeightToFee = WeightToFee; type LengthToFee = ConstantMultiplier; type FeeMultiplierUpdate = SlowAdjustingFeeUpdate; diff --git a/templates/solochain/runtime/src/lib.rs b/templates/solochain/runtime/src/lib.rs index 5a97bd2f39120..f3353d19be6ac 100644 --- a/templates/solochain/runtime/src/lib.rs +++ b/templates/solochain/runtime/src/lib.rs @@ -36,7 +36,7 @@ pub use frame_support::{ pub use frame_system::Call as SystemCall; pub use pallet_balances::Call as BalancesCall; pub use pallet_timestamp::Call as TimestampCall; -use pallet_transaction_payment::{ConstFeeMultiplier, CurrencyAdapter, Multiplier}; +use pallet_transaction_payment::{ConstFeeMultiplier, FungibleAdapter, Multiplier}; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; pub use sp_runtime::{Perbill, Permill}; @@ -230,7 +230,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = CurrencyAdapter; + type OnChargeTransaction = FungibleAdapter; type OperationalFeeMultiplier = ConstU8<5>; type WeightToFee = IdentityFee; type LengthToFee = IdentityFee;