Skip to content

Commit 551688f

Browse files
[PassKey] #2051 Enhancement: use underlying trait implementation from tx payment pallet (#2053)
Go after: #2047 # Goal The goal of this PR is to reduce redundancy in pre_dispatch and validate by calling tx payment pallet underlying implementation Closes #2051 # Discussion <!-- List discussion items --> # Checklist - [ ] Chain spec updated - [ ] Custom RPC OR Runtime API added/changed? Updated js/api-augment. - [ ] Design doc(s) updated - [ ] Tests added - [ ] Benchmarks added - [ ] Weights updated
1 parent 4978d76 commit 551688f

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

pallets/passkey/src/benchmarking.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ fn generate_payload<T: Config>() -> PasskeyPayload<T> {
6868
}
6969

7070
benchmarks! {
71-
where_clause { where <T as frame_system::Config>::RuntimeCall: From<Call<T>> + Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo> }
71+
where_clause { where
72+
BalanceOf<T>: From<u64>,
73+
<T as frame_system::Config>::RuntimeCall: From<Call<T>> + Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>
74+
}
7275

7376
validate {
7477
let payload = generate_payload::<T>();

pallets/passkey/src/lib.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use frame_support::{
2626
use frame_system::pallet_prelude::*;
2727
use pallet_transaction_payment::OnChargeTransaction;
2828
use sp_runtime::{
29-
traits::{Convert, DispatchInfoOf, Dispatchable, SignedExtension, Verify, Zero},
29+
traits::{Convert, Dispatchable, SignedExtension, Verify, Zero},
3030
transaction_validity::{TransactionValidity, TransactionValidityError},
3131
AccountId32, MultiSignature,
3232
};
@@ -149,6 +149,7 @@ pub mod module {
149149
#[pallet::validate_unsigned]
150150
impl<T: Config> ValidateUnsigned for Pallet<T>
151151
where
152+
BalanceOf<T>: Send + Sync + From<u64>,
152153
<T as frame_system::Config>::RuntimeCall:
153154
From<Call<T>> + Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
154155
{
@@ -196,6 +197,7 @@ pub mod module {
196197

197198
impl<T: Config> Pallet<T>
198199
where
200+
BalanceOf<T>: Send + Sync + From<u64>,
199201
<T as frame_system::Config>::RuntimeCall: From<Call<T>> + Dispatchable<Info = DispatchInfo>,
200202
{
201203
fn filter_valid_calls(call: &Call<T>) -> Result<PasskeyPayload<T>, TransactionValidityError> {
@@ -316,6 +318,7 @@ pub struct ChargeTransactionPayment<T: Config>(pub T::AccountId, pub Call<T>);
316318

317319
impl<T: Config> ChargeTransactionPayment<T>
318320
where
321+
BalanceOf<T>: Send + Sync + From<u64>,
319322
<T as frame_system::Config>::RuntimeCall:
320323
From<Call<T>> + Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
321324
{
@@ -326,7 +329,8 @@ where
326329
let runtime_call: <T as frame_system::Config>::RuntimeCall =
327330
<T as frame_system::Config>::RuntimeCall::from(self.1.clone());
328331
let who = self.0.clone();
329-
self.withdraw_token_fee(&who, &runtime_call, info, len, Zero::zero())?;
332+
pallet_transaction_payment::ChargeTransactionPayment::<T>::from(Zero::zero())
333+
.pre_dispatch(&who, &runtime_call, info, len)?;
330334
Ok(())
331335
}
332336

@@ -337,38 +341,12 @@ where
337341
let runtime_call: <T as frame_system::Config>::RuntimeCall =
338342
<T as frame_system::Config>::RuntimeCall::from(self.1.clone());
339343
let who = self.0.clone();
340-
let fee = self.withdraw_token_fee(&who, &runtime_call, info, len, Zero::zero())?;
341344

342-
let priority = pallet_transaction_payment::ChargeTransactionPayment::<T>::get_priority(
345+
pallet_transaction_payment::ChargeTransactionPayment::<T>::from(Zero::zero()).validate(
346+
&who,
347+
&runtime_call,
343348
info,
344349
len,
345-
Zero::zero(),
346-
fee,
347-
);
348-
349-
Ok(ValidTransaction { priority, ..Default::default() })
350-
}
351-
352-
/// Withdraws transaction fee paid with tokens.
353-
/// # Arguments
354-
/// * `who` - The account id of the payer
355-
/// * `call` - The call
356-
/// # Return
357-
/// * `Ok((fee, initial_payment))` if the fee is successfully withdrawn
358-
/// * `Err(InvalidTransaction::Payment)` if the fee cannot be withdrawn
359-
fn withdraw_token_fee(
360-
&self,
361-
who: &T::AccountId,
362-
call: &<T as frame_system::Config>::RuntimeCall,
363-
info: &DispatchInfoOf<<T as frame_system::Config>::RuntimeCall>,
364-
len: usize,
365-
tip: BalanceOf<T>,
366-
) -> Result<BalanceOf<T>, TransactionValidityError> {
367-
let fee = pallet_transaction_payment::Pallet::<T>::compute_fee(len as u32, info, tip);
368-
<OnChargeTransactionOf<T> as OnChargeTransaction<T>>::withdraw_fee(
369-
who, call, info, fee, tip,
370350
)
371-
.map(|_| (fee))
372-
.map_err(|_| -> TransactionValidityError { InvalidTransaction::Payment.into() })
373351
}
374352
}

0 commit comments

Comments
 (0)