1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use core::fmt::Debug;
use codec::{Decode, Encode};
use frame_support::dispatch::{DispatchClass, DispatchInfo, PostDispatchInfo};
use pallet_transaction_payment as ptp;
use ptp::OnChargeTransaction;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
transaction_validity::{
TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction,
},
DispatchResult,
};
use crate::{BalanceOf, Config, CustomFeeDetailsOf, LiquidityInfo};
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct ChargeTransactionPayment<T: Config> {
#[codec(compact)]
tip: BalanceOf<T>,
}
impl<T: Config> From<u128> for ChargeTransactionPayment<T>
where
BalanceOf<T>: From<u128>,
{
fn from(value: u128) -> Self {
Self { tip: value.into() }
}
}
impl<T: Config> Debug for ChargeTransactionPayment<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("XorFeeChargeTransactionPayment")
.field(&self.tip)
.finish()
}
}
impl<T: Config> Default for ChargeTransactionPayment<T>
where
BalanceOf<T>: Default,
{
fn default() -> Self {
ChargeTransactionPayment {
tip: Default::default(),
}
}
}
type CallOf<T> = <T as frame_system::Config>::RuntimeCall;
impl<T: Config> ChargeTransactionPayment<T>
where
T: ptp::Config<OnChargeTransaction = crate::Pallet<T>>,
BalanceOf<T>: Into<u128>,
CallOf<T>: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
pub fn new() -> Self {
Default::default()
}
#[allow(clippy::type_complexity)] fn withdraw_fee(
&self,
who: &T::AccountId,
call: &CallOf<T>,
info: &DispatchInfoOf<CallOf<T>>,
len: usize,
) -> Result<
(
BalanceOf<T>,
LiquidityInfo<T>,
Option<CustomFeeDetailsOf<T>>,
),
TransactionValidityError,
> {
let tip = self.tip;
let (fee, fee_details) = crate::Pallet::<T>::compute_fee(len as u32, call, info, tip);
let liquidity_info = T::OnChargeTransaction::withdraw_fee(who, call, info, fee, tip)?;
Ok((fee, liquidity_info, fee_details))
}
}
impl<T: Config> SignedExtension for ChargeTransactionPayment<T>
where
BalanceOf<T>: Send + Sync + Into<u128>,
T: ptp::Config<OnChargeTransaction = crate::Pallet<T>>,
CallOf<T>: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
const IDENTIFIER: &'static str = "ChargeTransactionPayment";
type AccountId = T::AccountId;
type Call = CallOf<T>;
type AdditionalSigned = ();
type Pre = (
BalanceOf<T>,
Self::AccountId,
LiquidityInfo<T>,
Option<CustomFeeDetailsOf<T>>,
);
fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> {
Ok(())
}
fn validate(
&self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> TransactionValidity {
let (final_fee, _, _) = self.withdraw_fee(who, call, info, len)?;
let priority = match info.class {
DispatchClass::Normal => TransactionPriority::default(),
DispatchClass::Operational | DispatchClass::Mandatory => {
ptp::ChargeTransactionPayment::<T>::get_priority(info, len, self.tip, final_fee)
}
};
Ok(ValidTransaction {
priority,
..Default::default()
})
}
fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
let (_, liquidity_info, fee_kind) = self.withdraw_fee(who, call, info, len)?;
Ok((self.tip, who.clone(), liquidity_info, fee_kind))
}
fn post_dispatch(
maybe_pre: Option<Self::Pre>,
info: &DispatchInfoOf<Self::Call>,
post_info: &PostDispatchInfoOf<Self::Call>,
len: usize,
result: &DispatchResult,
) -> Result<(), TransactionValidityError> {
if let Some((tip, who, imbalance, custom_fee_details)) = maybe_pre {
let actual_fee = crate::Pallet::<T>::compute_actual_fee(
len as u32,
info,
post_info,
result,
tip,
custom_fee_details,
);
T::OnChargeTransaction::correct_and_deposit_fee(
&who, info, post_info, actual_fee, tip, imbalance,
)?;
let event: <T as ptp::Config>::RuntimeEvent = ptp::Event::<T>::TransactionFeePaid {
who,
actual_fee,
tip,
}
.into();
frame_system::Pallet::<T>::deposit_event(event);
}
Ok(())
}
}