Skip to content

Commit f520adb

Browse files
Zero refund check for FungibleAdapter (#6506)
`FungibleAdapter` will now check if the _refund amount_ is zero before calling deposit & emitting an event. Fixes #6469. --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 1c0b610 commit f520adb

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

prdoc/pr_6506.prdoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
title: Zero refund check for FungibleAdapter
2+
doc:
3+
- audience: Runtime User
4+
description: |-
5+
`FungibleAdapter` will now check if the _refund amount_ is zero before calling deposit & emitting an event.
6+
7+
Fixes https://github.com/paritytech/polkadot-sdk/issues/6469.
8+
crates:
9+
- name: pallet-transaction-payment
10+
bump: patch

substrate/frame/transaction-payment/src/payment.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,15 @@ where
155155
if let Some(paid) = already_withdrawn {
156156
// Calculate how much refund we should return
157157
let refund_amount = paid.peek().saturating_sub(corrected_fee);
158-
// refund to the the account that paid the fees if it exists. otherwise, don't refind
159-
// anything.
160-
let refund_imbalance = if F::total_balance(who) > F::Balance::zero() {
161-
F::deposit(who, refund_amount, Precision::BestEffort)
162-
.unwrap_or_else(|_| Debt::<T::AccountId, F>::zero())
163-
} else {
164-
Debt::<T::AccountId, F>::zero()
165-
};
158+
// Refund to the the account that paid the fees if it exists & refund is non-zero.
159+
// Otherwise, don't refund anything.
160+
let refund_imbalance =
161+
if refund_amount > Zero::zero() && F::total_balance(who) > F::Balance::zero() {
162+
F::deposit(who, refund_amount, Precision::BestEffort)
163+
.unwrap_or_else(|_| Debt::<T::AccountId, F>::zero())
164+
} else {
165+
Debt::<T::AccountId, F>::zero()
166+
};
166167
// merge the imbalance caused by paying the fees and refunding parts of it again.
167168
let adjusted_paid: Credit<T::AccountId, F> = paid
168169
.offset(refund_imbalance)

substrate/frame/transaction-payment/src/tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,40 @@ fn no_fee_and_no_weight_for_other_origins() {
877877
assert_eq!(post_info.actual_weight, Some(info.call_weight));
878878
})
879879
}
880+
881+
#[test]
882+
fn fungible_adapter_no_zero_refund_action() {
883+
type FungibleAdapterT = payment::FungibleAdapter<Balances, DealWithFees>;
884+
885+
ExtBuilder::default().balance_factor(10).build().execute_with(|| {
886+
System::set_block_number(10);
887+
888+
let dummy_acc = 1;
889+
let (actual_fee, no_tip) = (10, 0);
890+
let already_paid = <FungibleAdapterT as OnChargeTransaction<Runtime>>::withdraw_fee(
891+
&dummy_acc,
892+
CALL,
893+
&CALL.get_dispatch_info(),
894+
actual_fee,
895+
no_tip,
896+
).expect("Account must have enough funds.");
897+
898+
// Correction action with no expected side effect.
899+
assert!(<FungibleAdapterT as OnChargeTransaction<Runtime>>::correct_and_deposit_fee(
900+
&dummy_acc,
901+
&CALL.get_dispatch_info(),
902+
&default_post_info(),
903+
actual_fee,
904+
no_tip,
905+
already_paid,
906+
).is_ok());
907+
908+
// Ensure no zero amount deposit event is emitted.
909+
let events = System::events();
910+
assert!(!events
911+
.iter()
912+
.any(|record| matches!(record.event, RuntimeEvent::Balances(pallet_balances::Event::Deposit { amount, .. }) if amount.is_zero())),
913+
"No zero amount deposit amount event should be emitted.",
914+
);
915+
});
916+
}

0 commit comments

Comments
 (0)