From da40ff14e082aff9f606227ec18db4906379ed01 Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Thu, 2 Feb 2023 07:47:49 +0100 Subject: [PATCH] Fee and tip represented as asset ID inside `AssetTxFeePaid` (#13083) * fee & tip in the asset ID Balance type * docs * rewrite * update runtime config * docs --- .../asset-tx-payment/src/lib.rs | 26 ++++++++++--------- .../asset-tx-payment/src/payment.rs | 17 ++++++++---- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/frame/transaction-payment/asset-tx-payment/src/lib.rs b/frame/transaction-payment/asset-tx-payment/src/lib.rs index 230b307317f8b..645ebb1e123be 100644 --- a/frame/transaction-payment/asset-tx-payment/src/lib.rs +++ b/frame/transaction-payment/asset-tx-payment/src/lib.rs @@ -134,8 +134,8 @@ pub mod pallet { /// has been paid by `who` in an asset `asset_id`. AssetTxFeePaid { who: T::AccountId, - actual_fee: BalanceOf, - tip: BalanceOf, + actual_fee: AssetBalanceOf, + tip: AssetBalanceOf, asset_id: Option>, }, } @@ -284,18 +284,20 @@ where let actual_fee = pallet_transaction_payment::Pallet::::compute_actual_fee( len as u32, info, post_info, tip, ); - T::OnChargeAssetTransaction::correct_and_deposit_fee( - &who, - info, - post_info, - actual_fee.into(), - tip.into(), - already_withdrawn.into(), - )?; + + let (converted_fee, converted_tip) = + T::OnChargeAssetTransaction::correct_and_deposit_fee( + &who, + info, + post_info, + actual_fee.into(), + tip.into(), + already_withdrawn.into(), + )?; Pallet::::deposit_event(Event::::AssetTxFeePaid { who, - actual_fee, - tip, + actual_fee: converted_fee, + tip: converted_tip, asset_id, }); }, diff --git a/frame/transaction-payment/asset-tx-payment/src/payment.rs b/frame/transaction-payment/asset-tx-payment/src/payment.rs index 85d1bec4b275c..ae8253b6e7d3e 100644 --- a/frame/transaction-payment/asset-tx-payment/src/payment.rs +++ b/frame/transaction-payment/asset-tx-payment/src/payment.rs @@ -58,6 +58,8 @@ pub trait OnChargeAssetTransaction { /// the corrected amount. /// /// Note: The `fee` already includes the `tip`. + /// + /// Returns the fee and tip in the asset used for payment as (fee, tip). fn correct_and_deposit_fee( who: &T::AccountId, dispatch_info: &DispatchInfoOf, @@ -65,7 +67,7 @@ pub trait OnChargeAssetTransaction { corrected_fee: Self::Balance, tip: Self::Balance, already_withdrawn: Self::LiquidityInfo, - ) -> Result<(), TransactionValidityError>; + ) -> Result<(AssetBalanceOf, AssetBalanceOf), TransactionValidityError>; } /// Allows specifying what to do with the withdrawn asset fees. @@ -132,19 +134,24 @@ where /// Since the predicted fee might have been too high, parts of the fee may be refunded. /// /// Note: The `corrected_fee` already includes the `tip`. + /// + /// Returns the fee and tip in the asset used for payment as (fee, tip). fn correct_and_deposit_fee( who: &T::AccountId, _dispatch_info: &DispatchInfoOf, _post_info: &PostDispatchInfoOf, corrected_fee: Self::Balance, - _tip: Self::Balance, + tip: Self::Balance, paid: Self::LiquidityInfo, - ) -> Result<(), TransactionValidityError> { + ) -> Result<(AssetBalanceOf, AssetBalanceOf), TransactionValidityError> { let min_converted_fee = if corrected_fee.is_zero() { Zero::zero() } else { One::one() }; - // Convert the corrected fee into the asset used for payment. + // Convert the corrected fee and tip into the asset used for payment. let converted_fee = CON::to_asset_balance(corrected_fee, paid.asset()) .map_err(|_| -> TransactionValidityError { InvalidTransaction::Payment.into() })? .max(min_converted_fee); + let converted_tip = CON::to_asset_balance(tip, paid.asset()) + .map_err(|_| -> TransactionValidityError { InvalidTransaction::Payment.into() })?; + // Calculate how much refund we should return. let (final_fee, refund) = paid.split(converted_fee); // Refund to the account that paid the fees. If this fails, the account might have dropped @@ -152,6 +159,6 @@ where let _ = >::resolve(who, refund); // Handle the final fee, e.g. by transferring to the block author or burning. HC::handle_credit(final_fee); - Ok(()) + Ok((converted_fee, converted_tip)) } }