mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 19:21:13 +00:00
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:
@@ -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<Pallet<Test>, ()>;
|
||||
type OnChargeTransaction = FungibleAdapter<Pallet<Test>, ()>;
|
||||
type OperationalFeeMultiplier = ConstU8<5>;
|
||||
type WeightToFee = IdentityFee<u64>;
|
||||
type LengthToFee = IdentityFee<u64>;
|
||||
|
||||
@@ -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<Balances, ()>;
|
||||
type OnChargeTransaction = FungibleAdapter<Balances, ()>;
|
||||
type OperationalFeeMultiplier = ConstU8<5>;
|
||||
type WeightToFee = IdentityFee<Balance>;
|
||||
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
|
||||
|
||||
@@ -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<pallet_balances::NegativeImbalance<Runtime>> for DealWithFees {
|
||||
impl OnUnbalanced<fungible::Credit<<Runtime as frame_system::Config>::AccountId, Balances>>
|
||||
for DealWithFees
|
||||
{
|
||||
fn on_unbalanceds<B>(
|
||||
mut fees_then_tips: impl Iterator<Item = pallet_balances::NegativeImbalance<Runtime>>,
|
||||
mut fees_then_tips: impl Iterator<
|
||||
Item = fungible::Credit<<Runtime as frame_system::Config>::AccountId, Balances>,
|
||||
>,
|
||||
) {
|
||||
if let Some(fees) = fees_then_tips.next() {
|
||||
FeeUnbalancedAmount::mutate(|a| *a += fees.peek());
|
||||
@@ -171,7 +176,7 @@ impl OnUnbalanced<pallet_balances::NegativeImbalance<Runtime>> for DealWithFees
|
||||
#[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)]
|
||||
impl pallet_transaction_payment::Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
|
||||
type OnChargeTransaction = FungibleAdapter<Balances, DealWithFees>;
|
||||
type WeightToFee = WeightToFee;
|
||||
type LengthToFee = TransactionByteFee;
|
||||
type FeeMultiplierUpdate = ();
|
||||
|
||||
@@ -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<Balances, ()>;
|
||||
type OnChargeTransaction = FungibleAdapter<Balances, ()>;
|
||||
type WeightToFee = WeightToFee;
|
||||
type LengthToFee = TransactionByteFee;
|
||||
type FeeMultiplierUpdate = ();
|
||||
|
||||
@@ -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<pallet_balances::NegativeImbalance<Runtime>> for DealWithFees {
|
||||
impl OnUnbalanced<fungible::Credit<<Runtime as frame_system::Config>::AccountId, Balances>>
|
||||
for DealWithFees
|
||||
{
|
||||
fn on_unbalanceds<B>(
|
||||
mut fees_then_tips: impl Iterator<Item = pallet_balances::NegativeImbalance<Runtime>>,
|
||||
mut fees_then_tips: impl Iterator<
|
||||
Item = fungible::Credit<<Runtime as frame_system::Config>::AccountId, Balances>,
|
||||
>,
|
||||
) {
|
||||
if let Some(fees) = fees_then_tips.next() {
|
||||
FeeUnbalancedAmount::mutate(|a| *a += fees.peek());
|
||||
@@ -152,7 +156,7 @@ impl OnUnbalanced<pallet_balances::NegativeImbalance<Runtime>> for DealWithFees
|
||||
|
||||
impl Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
|
||||
type OnChargeTransaction = FungibleAdapter<Balances, DealWithFees>;
|
||||
type OperationalFeeMultiplier = OperationalFeeMultiplier;
|
||||
type WeightToFee = WeightToFee;
|
||||
type LengthToFee = TransactionByteFee;
|
||||
|
||||
@@ -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<T: Config> {
|
||||
) -> Result<(), TransactionValidityError>;
|
||||
}
|
||||
|
||||
/// Implements the transaction payment for a pallet implementing the `Currency`
|
||||
/// trait (eg. the pallet_balances) using an unbalance handler (implementing
|
||||
/// `OnUnbalanced`).
|
||||
/// 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.
|
||||
/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and
|
||||
/// then `tip`.
|
||||
pub struct FungibleAdapter<F, OU>(PhantomData<(F, OU)>);
|
||||
|
||||
impl<T, F, OU> OnChargeTransaction<T> for FungibleAdapter<F, OU>
|
||||
where
|
||||
T: Config,
|
||||
F: Balanced<T::AccountId>,
|
||||
OU: OnUnbalanced<Credit<T::AccountId, F>>,
|
||||
{
|
||||
type LiquidityInfo = Option<Credit<T::AccountId, F>>;
|
||||
type Balance = <F as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
|
||||
|
||||
fn withdraw_fee(
|
||||
who: &<T>::AccountId,
|
||||
_call: &<T>::RuntimeCall,
|
||||
_dispatch_info: &DispatchInfoOf<<T>::RuntimeCall>,
|
||||
fee: Self::Balance,
|
||||
_tip: Self::Balance,
|
||||
) -> Result<Self::LiquidityInfo, TransactionValidityError> {
|
||||
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: &<T>::AccountId,
|
||||
_dispatch_info: &DispatchInfoOf<<T>::RuntimeCall>,
|
||||
_post_info: &PostDispatchInfoOf<<T>::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::<T::AccountId, F>::zero())
|
||||
} else {
|
||||
Debt::<T::AccountId, F>::zero()
|
||||
};
|
||||
// merge the imbalance caused by paying the fees and refunding parts of it again.
|
||||
let adjusted_paid: Credit<T::AccountId, F> = 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`]).
|
||||
///
|
||||
/// 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<C, OU>(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<T, C, OU> OnChargeTransaction<T> for CurrencyAdapter<C, OU>
|
||||
where
|
||||
T: Config,
|
||||
|
||||
@@ -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<T: Config<I>, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>> for Palle
|
||||
Self::deposit_event(Event::Deposit { value: numeric_amount });
|
||||
}
|
||||
}
|
||||
|
||||
/// TypedGet implementaion to get the AccountId of the Treasury.
|
||||
pub struct TreasuryAccountId<R>(PhantomData<R>);
|
||||
impl<R> sp_runtime::traits::TypedGet for TreasuryAccountId<R>
|
||||
where
|
||||
R: crate::Config,
|
||||
{
|
||||
type Type = <R as frame_system::Config>::AccountId;
|
||||
fn get() -> Self::Type {
|
||||
<crate::Pallet<R>>::account_id()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user