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
This commit is contained in:
Liam Aharon
2024-04-05 00:56:12 +11:00
committed by GitHub
parent c130ea9939
commit bda4e75ac4
44 changed files with 384 additions and 132 deletions
@@ -83,10 +83,6 @@ type CurrencyOf<T> = <<T as Config>::Auctioneer as Auctioneer<BlockNumberFor<T>>
type LeasePeriodOf<T> = <<T as Config>::Auctioneer as Auctioneer<BlockNumberFor<T>>>::LeasePeriod;
type BalanceOf<T> = <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[allow(dead_code)]
type NegativeImbalanceOf<T> =
<CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;
type FundIndex = u32;
pub trait WeightInfo {
+25 -13
View File
@@ -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<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for ToAuthor<R>
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for ToAuthor<R>
where
R: pallet_balances::Config + pallet_authorship::Config,
<R as frame_system::Config>::AccountId: From<primitives::AccountId>,
<R as frame_system::Config>::AccountId: Into<primitives::AccountId>,
{
fn on_nonzero_unbalanced(amount: NegativeImbalance<R>) {
fn on_nonzero_unbalanced(
amount: Credit<<R as frame_system::Config>::AccountId, pallet_balances::Pallet<R>>,
) {
if let Some(author) = <pallet_authorship::Pallet<R>>::author() {
<pallet_balances::Pallet<R>>::resolve_creating(&author, amount);
let _ = <pallet_balances::Pallet<R>>::resolve(&author, amount);
}
}
}
pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for DealWithFees<R>
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for DealWithFees<R>
where
R: pallet_balances::Config + pallet_treasury::Config + pallet_authorship::Config,
pallet_treasury::Pallet<R>: OnUnbalanced<NegativeImbalance<R>>,
R: pallet_balances::Config + pallet_authorship::Config + pallet_treasury::Config,
<R as frame_system::Config>::AccountId: From<primitives::AccountId>,
<R as frame_system::Config>::AccountId: Into<primitives::AccountId>,
{
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance<R>>) {
fn on_unbalanceds<B>(
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
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;
<Treasury<R> as OnUnbalanced<_>>::on_unbalanced(split.0);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(split.0);
<ToAuthor<R> 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 =
<pallet_balances::Pallet<Test> as frame_support::traits::fungible::Balanced<
AccountId,
>>::issue(10);
let tip =
<pallet_balances::Pallet<Test> 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);
+3
View File
@@ -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<T> = <pallet_balances::Pallet<T> as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
+2 -2
View File
@@ -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<Balances, ToAuthor<Runtime>>;
type OnChargeTransaction = FungibleAdapter<Balances, ToAuthor<Runtime>>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = WeightToFee;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
+2 -2
View File
@@ -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<Balances, ()>;
type OnChargeTransaction = FungibleAdapter<Balances, ()>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = WeightToFee;
type LengthToFee = frame_support::weights::ConstantMultiplier<Balance, TransactionByteFee>;
+2 -2
View File
@@ -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<Balances, ToAuthor<Runtime>>;
type OnChargeTransaction = FungibleAdapter<Balances, ToAuthor<Runtime>>;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = WeightToFee;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;