Skip to content

Commit

Permalink
fix: fee and tip handling logic for clarity and correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Jan 7, 2025
1 parent ccd3df9 commit 0b3adaf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
27 changes: 24 additions & 3 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,11 @@ impl pallet_balances::Config for Runtime {

/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
pub struct DealWithSubstrateFeesAndTip<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithSubstrateFeesAndTip<R>
impl<R> DealWithSubstrateFeesAndTip<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
{
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
fn deal_with_fees(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let treasury_proportion =
Expand All @@ -356,6 +355,28 @@ where
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}

fn deal_with_tip(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// same as fees
Self::deal_with_fees(amount)
}
}

impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithSubstrateFeesAndTip<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
{
fn on_unbalanceds(
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
Self::deal_with_fees(fees);
if let Some(tip) = fees_then_tips.next() {
Self::deal_with_tip(tip);
}
}
}
}

/// Deal with ethereum based fees. To handle tips/priority fees, use DealWithEthereumPriorityFees.
Expand Down
3 changes: 1 addition & 2 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ impl pallet_balances::Config for Runtime {

/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
pub struct DealWithSubstrateFeesAndTip<R>(sp_std::marker::PhantomData<R>);

impl<R> DealWithSubstrateFeesAndTip<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
Expand All @@ -358,7 +357,7 @@ where
}

impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithSubstrateFeesAndTip<R>
for DealWithSubstrateFeesAndTip<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
{
Expand Down
27 changes: 24 additions & 3 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,11 @@ impl pallet_balances::Config for Runtime {

/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
pub struct DealWithSubstrateFeesAndTip<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithSubstrateFeesAndTip<R>
impl<R> DealWithSubstrateFeesAndTip<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
{
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
fn deal_with_fees(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let treasury_proportion =
Expand All @@ -352,6 +351,28 @@ where
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}

fn deal_with_tip(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// same as fees
Self::deal_with_fees(amount)
}
}

impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithSubstrateFeesAndTip<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
{
fn on_unbalanceds(
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
Self::deal_with_fees(fees);
if let Some(tip) = fees_then_tips.next() {
Self::deal_with_tip(tip);
}
}
}
}

/// Deal with ethereum based fees. To handle tips/priority fees, use DealWithEthereumPriorityFees.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ describeSuite({
const treasuryIncrease = feeWithTip - issuanceDecrease;
return treasuryIncrease;
};
const calcIssuanceDecrease = (feeWithTip: bigint, tip?: bigint): bigint => {
const feeWithTipBN = new BN(feeWithTip.toString());
const { burnt } = calculateFeePortions(treasuryPerbill, feeWithTipBN);
const calcIssuanceDecrease = (feeWithTip: bigint, maybeTip?: bigint): bigint => {
const tip = maybeTip ?? 0n;
const feeWithoutTip = feeWithTip - tip;
const { burnt: feeBurnt } = calculateFeePortions(treasuryPerbill, feeWithoutTip);
const { burnt: tipBurnt } = calculateFeePortions(treasuryPerbill, tip);

return BigInt(burnt.toString());
return feeBurnt + tipBurnt;
};

for (const txnType of TransactionTypes) {
Expand Down

0 comments on commit 0b3adaf

Please sign in to comment.