Skip to content

Commit

Permalink
Fee and tip represented as asset ID inside AssetTxFeePaid (parityte…
Browse files Browse the repository at this point in the history
…ch#13083)

* fee & tip in the asset ID Balance type

* docs

* rewrite

* update runtime config

* docs
  • Loading branch information
Szegoo authored and nathanwhit committed Jul 19, 2023
1 parent 82d9504 commit 4296ebf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
26 changes: 14 additions & 12 deletions frame/transaction-payment/asset-tx-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ pub mod pallet {
/// has been paid by `who` in an asset `asset_id`.
AssetTxFeePaid {
who: T::AccountId,
actual_fee: BalanceOf<T>,
tip: BalanceOf<T>,
actual_fee: AssetBalanceOf<T>,
tip: AssetBalanceOf<T>,
asset_id: Option<ChargeAssetIdOf<T>>,
},
}
Expand Down Expand Up @@ -284,18 +284,20 @@ where
let actual_fee = pallet_transaction_payment::Pallet::<T>::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::<T>::deposit_event(Event::<T>::AssetTxFeePaid {
who,
actual_fee,
tip,
actual_fee: converted_fee,
tip: converted_tip,
asset_id,
});
},
Expand Down
17 changes: 12 additions & 5 deletions frame/transaction-payment/asset-tx-payment/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ pub trait OnChargeAssetTransaction<T: Config> {
/// 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<T::RuntimeCall>,
post_info: &PostDispatchInfoOf<T::RuntimeCall>,
corrected_fee: Self::Balance,
tip: Self::Balance,
already_withdrawn: Self::LiquidityInfo,
) -> Result<(), TransactionValidityError>;
) -> Result<(AssetBalanceOf<T>, AssetBalanceOf<T>), TransactionValidityError>;
}

/// Allows specifying what to do with the withdrawn asset fees.
Expand Down Expand Up @@ -132,26 +134,31 @@ 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<T::RuntimeCall>,
_post_info: &PostDispatchInfoOf<T::RuntimeCall>,
corrected_fee: Self::Balance,
_tip: Self::Balance,
tip: Self::Balance,
paid: Self::LiquidityInfo,
) -> Result<(), TransactionValidityError> {
) -> Result<(AssetBalanceOf<T>, AssetBalanceOf<T>), 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
// below the existential balance. In that case we don't refund anything.
let _ = <T::Fungibles as Balanced<T::AccountId>>::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))
}
}

0 comments on commit 4296ebf

Please sign in to comment.