From 080caf688029cb5d7285401c7a3137532d7d03be Mon Sep 17 00:00:00 2001 From: zqhxuyuan Date: Tue, 21 Jun 2022 00:15:46 +0800 Subject: [PATCH] Add TransactionFeePaid event (#2218) * add TransactionFeePaid event * more event test * fix comment --- modules/transaction-payment/src/lib.rs | 35 +++++++---- modules/transaction-payment/src/tests.rs | 75 +++++++++++++++++++++++- 2 files changed, 99 insertions(+), 11 deletions(-) diff --git a/modules/transaction-payment/src/lib.rs b/modules/transaction-payment/src/lib.rs index 9387dcae86..a947ebf09f 100644 --- a/modules/transaction-payment/src/lib.rs +++ b/modules/transaction-payment/src/lib.rs @@ -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, + actual_tip: PalletBalanceOf, + actual_surplus: PalletBalanceOf, + }, } /// The next fee multiplier. @@ -1134,7 +1143,7 @@ where ( PalletBalanceOf, Option>, - Option>, + PalletBalanceOf, T::AccountId, ), TransactionValidityError, @@ -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() { @@ -1158,7 +1167,7 @@ where // withdraw native currency as fee, also consider surplus when swap from dex or pool. match ::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()), } } @@ -1255,8 +1264,8 @@ where PalletBalanceOf, Self::AccountId, Option>, - PalletBalanceOf, // fee includes surplus - Option>, // surplus + PalletBalanceOf, // fee includes surplus + PalletBalanceOf, // surplus ); fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> { @@ -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 ::Currency::deposit_into_existing(&who, refund) { Ok(refund_imbalance) => { // The refund cannot be larger than the up front payed max weight. @@ -1335,6 +1343,13 @@ where // distribute fee ::OnTransactionPayment::on_unbalanceds(Some(fee).into_iter().chain(Some(tip))); + + Pallet::::deposit_event(Event::::TransactionFeePaid { + who, + actual_fee, + actual_tip, + actual_surplus, + }); } Ok(()) } diff --git a/modules/transaction-payment/src/tests.rs b/modules/transaction-payment/src/tests.rs index b93abd5d64..7ea86a12d6 100644 --- a/modules/transaction-payment/src/tests.rs +++ b/modules/transaction-payment/src/tests.rs @@ -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); @@ -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, + }, + )); }); } @@ -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); @@ -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, + }, + )); }); } @@ -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::::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, + }, + )); }); } @@ -461,6 +507,15 @@ fn refund_tip_according_to_actual_when_post_dispatch_and_native_currency_is_enou assert!(ChargeTransactionPayment::::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; @@ -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, + }, + )); }); } @@ -497,6 +561,15 @@ fn refund_should_not_works() { assert!(ChargeTransactionPayment::::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, + }, + )); }); }