Skip to content

Commit

Permalink
Add TransactionFeePaid event (#2218)
Browse files Browse the repository at this point in the history
* add TransactionFeePaid event

* more event test

* fix comment
  • Loading branch information
zqhxuyuan authored Jun 20, 2022
1 parent 8196ef9 commit 080caf6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 11 deletions.
35 changes: 25 additions & 10 deletions modules/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ pub mod module {
foreign_amount: Balance,
native_amount: Balance,
},
/// A transaction `actual_fee`, of which `actual_tip` was added to the minimum inclusion
/// fee, has been paid by `who`. `actual_surplus` indicate extra amount when paid by none
/// native token.
TransactionFeePaid {
who: T::AccountId,
actual_fee: PalletBalanceOf<T>,
actual_tip: PalletBalanceOf<T>,
actual_surplus: PalletBalanceOf<T>,
},
}

/// The next fee multiplier.
Expand Down Expand Up @@ -1134,7 +1143,7 @@ where
(
PalletBalanceOf<T>,
Option<NegativeImbalanceOf<T>>,
Option<PalletBalanceOf<T>>,
PalletBalanceOf<T>,
T::AccountId,
),
TransactionValidityError,
Expand All @@ -1144,7 +1153,7 @@ where

// Only mess with balances if fee is not zero.
if fee.is_zero() {
return Ok((fee, None, None, who.clone()));
return Ok((fee, None, 0, who.clone()));
}

let reason = if tip.is_zero() {
Expand All @@ -1158,7 +1167,7 @@ where

// withdraw native currency as fee, also consider surplus when swap from dex or pool.
match <T as Config>::Currency::withdraw(&payer, fee + fee_surplus, reason, ExistenceRequirement::KeepAlive) {
Ok(imbalance) => Ok((fee + fee_surplus, Some(imbalance), Some(fee_surplus), payer)),
Ok(imbalance) => Ok((fee + fee_surplus, Some(imbalance), fee_surplus, payer)),
Err(_) => Err(InvalidTransaction::Payment.into()),
}
}
Expand Down Expand Up @@ -1255,8 +1264,8 @@ where
PalletBalanceOf<T>,
Self::AccountId,
Option<NegativeImbalanceOf<T>>,
PalletBalanceOf<T>, // fee includes surplus
Option<PalletBalanceOf<T>>, // surplus
PalletBalanceOf<T>, // fee includes surplus
PalletBalanceOf<T>, // surplus
);

fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {
Expand Down Expand Up @@ -1312,11 +1321,10 @@ where
actual_tip = tip.saturating_sub(refund_tip);
}
// the refund surplus also need to return back to user
if let Some(surplus) = surplus {
let percent = Percent::from_rational(surplus, fee.saturating_sub(surplus));
let actual_surplus = percent.mul_ceil(actual_fee);
refund = refund.saturating_sub(actual_surplus);
}
let percent = Percent::from_rational(surplus, fee.saturating_sub(surplus));
let actual_surplus = percent.mul_ceil(actual_fee);
refund = refund.saturating_sub(actual_surplus);

let actual_payment = match <T as Config>::Currency::deposit_into_existing(&who, refund) {
Ok(refund_imbalance) => {
// The refund cannot be larger than the up front payed max weight.
Expand All @@ -1335,6 +1343,13 @@ where

// distribute fee
<T as Config>::OnTransactionPayment::on_unbalanceds(Some(fee).into_iter().chain(Some(tip)));

Pallet::<T>::deposit_event(Event::<T>::TransactionFeePaid {
who,
actual_fee,
actual_tip,
actual_surplus,
});
}
Ok(())
}
Expand Down
75 changes: 74 additions & 1 deletion modules/transaction-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ fn pre_post_dispatch_and_refund_native_is_enough() {
assert_eq!(FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow()), fee - refund);
assert_eq!(TIP_UNBALANCED_AMOUNT.with(|a| *a.borrow()), 0);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: ALICE,
actual_fee,
actual_tip: 0,
actual_surplus: 0,
},
));

// reset and test refund with tip
FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow_mut() = 0);

Expand All @@ -323,6 +332,15 @@ fn pre_post_dispatch_and_refund_native_is_enough() {
assert_eq!(Currencies::free_balance(ACA, &CHARLIE), 100000 - fee - tip + refund);
assert_eq!(FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow()), fee - refund);
assert_eq!(TIP_UNBALANCED_AMOUNT.with(|a| *a.borrow()), tip);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: CHARLIE,
actual_fee,
actual_tip: tip,
actual_surplus: 0,
},
));
});
}

Expand Down Expand Up @@ -363,16 +381,26 @@ fn pre_post_dispatch_and_refund_with_fee_path_call() {

let refund = 200; // 1000 - 800
let refund_surplus = 100;
let actual_surplus = surplus - refund_surplus;
assert_eq!(
Currencies::free_balance(ACA, &ALICE),
aca_init + refund + refund_surplus
);
assert_eq!(
FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow()),
fee - refund + surplus - refund_surplus
fee - refund + actual_surplus
);
assert_eq!(TIP_UNBALANCED_AMOUNT.with(|a| *a.borrow()), 0);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: ALICE,
actual_fee,
actual_tip: 0,
actual_surplus,
},
));

// reset and test refund with tip
FEE_UNBALANCED_AMOUNT.with(|a| *a.borrow_mut() = 0);

Expand Down Expand Up @@ -418,6 +446,15 @@ fn pre_post_dispatch_and_refund_with_fee_path_call() {
fee - refund + surplus - refund_surplus
);
assert_eq!(TIP_UNBALANCED_AMOUNT.with(|a| *a.borrow()), tip);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: CHARLIE,
actual_fee,
actual_tip: tip,
actual_surplus: surplus - refund_surplus,
},
));
});
}

Expand All @@ -444,6 +481,15 @@ fn refund_fee_according_to_actual_when_post_dispatch_and_native_currency_is_enou
let refund = 200; // 1000 - 800
assert!(ChargeTransactionPayment::<Runtime>::post_dispatch(Some(pre), &INFO, &POST_INFO, 23, &Ok(())).is_ok());
assert_eq!(Currencies::free_balance(ACA, &ALICE), 100000 - fee + refund);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: ALICE,
actual_fee: fee - refund,
actual_tip: 0,
actual_surplus: 0,
},
));
});
}

Expand All @@ -461,6 +507,15 @@ fn refund_tip_according_to_actual_when_post_dispatch_and_native_currency_is_enou
assert!(ChargeTransactionPayment::<Runtime>::post_dispatch(Some(pre), &INFO, &POST_INFO, 23, &Ok(())).is_ok());
assert_eq!(Currencies::free_balance(ACA, &ALICE), 100000 - fee + refund);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: ALICE,
actual_fee: fee - refund,
actual_tip: 0,
actual_surplus: 0,
},
));

// tip = 1000
let fee = 23 * 2 + 1000; // len * byte + weight
let tip = 1000;
Expand All @@ -476,6 +531,15 @@ fn refund_tip_according_to_actual_when_post_dispatch_and_native_currency_is_enou
Currencies::free_balance(ACA, &CHARLIE),
100000 - fee - tip + refund_fee + refund_tip
);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: CHARLIE,
actual_fee: fee - refund_fee + tip,
actual_tip: tip - refund_tip,
actual_surplus: 0,
},
));
});
}

Expand All @@ -497,6 +561,15 @@ fn refund_should_not_works() {

assert!(ChargeTransactionPayment::<Runtime>::post_dispatch(Some(pre), &INFO, &POST_INFO, 23, &Ok(())).is_ok());
assert_eq!(Currencies::free_balance(ACA, &ALICE), 100000 - fee - tip);

System::assert_has_event(crate::mock::Event::TransactionPayment(
crate::Event::TransactionFeePaid {
who: ALICE,
actual_fee: fee + tip,
actual_tip: tip,
actual_surplus: 0,
},
));
});
}

Expand Down

0 comments on commit 080caf6

Please sign in to comment.