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
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]
use dex_general::{ExportDexGeneral, WeightInfo};
use frame_support::{
ensure,
traits::{Currency, Get, IsSubType, OnUnbalanced},
};
pub use pallet::*;
use pallet_transaction_payment::OnChargeTransaction;
use sp_runtime::{
traits::{DispatchInfoOf, PostDispatchInfoOf, Zero},
transaction_validity::{InvalidTransaction, TransactionValidityError},
};
use sp_std::{boxed::Box, vec::Vec};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
type CallOf<T> = <T as Config>::RuntimeCall;
type SubstrateDefaultPayment<T> =
pallet_transaction_payment::CurrencyAdapter<<T as Config>::Currency, <T as Config>::OnUnbalanced>;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{
dispatch::{DispatchInfo, GetDispatchInfo, PostDispatchInfo},
pallet_prelude::*,
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Dispatchable;
#[pallet::config]
pub trait Config: frame_system::Config<RuntimeCall = CallOf<Self>> + currency::Config<Balance = u128> {
type RuntimeCall: Parameter
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin, PostInfo = PostDispatchInfo, Info = DispatchInfo>
+ GetDispatchInfo
+ IsSubType<Call<Self>>;
type Dex: ExportDexGeneral<Self::AccountId, Self::CurrencyId>;
type Currency: Currency<Self::AccountId, Balance = u128>;
type OnUnbalanced: OnUnbalanced<<Self::Currency as Currency<Self::AccountId>>::NegativeImbalance>;
type DexWeightInfo: dex_general::WeightInfo;
}
#[pallet::error]
pub enum Error<T> {}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
((T::DexWeightInfo::swap_assets_for_exact_assets(_path.len() as u32)).saturating_add(dispatch_info.weight), dispatch_info.class,)
})]
#[frame_support::transactional]
pub fn with_fee_swap_path(
origin: OriginFor<T>,
_path: Vec<T::CurrencyId>,
_amount_in_max: <T as currency::Config>::Balance,
call: Box<CallOf<T>>,
) -> DispatchResultWithPostInfo {
let mut ret = call.dispatch(origin);
let modify_weight = |x: &mut PostDispatchInfo| {
x.actual_weight = x
.actual_weight
.map(|x| x.saturating_add(T::DexWeightInfo::swap_assets_for_exact_assets(_path.len() as u32)));
};
match ret {
Ok(ref mut info) => {
modify_weight(info);
}
Err(ref mut err) => {
modify_weight(&mut err.post_info);
}
};
ret
}
}
}
impl<T> OnChargeTransaction<T> for Pallet<T>
where
T: pallet::Config + pallet_transaction_payment::Config,
{
type LiquidityInfo = Option<<T::Currency as Currency<T::AccountId>>::NegativeImbalance>;
type Balance = <T as currency::Config>::Balance;
fn withdraw_fee(
who: &T::AccountId,
call: &CallOf<T>,
info: &DispatchInfoOf<CallOf<T>>,
fee: Self::Balance,
tip: Self::Balance,
) -> Result<Self::LiquidityInfo, TransactionValidityError> {
if fee.is_zero() {
return Ok(None);
}
match call.is_sub_type() {
Some(pallet::Call::with_fee_swap_path {
path, amount_in_max, ..
}) => {
ensure!(
path.last() == Some(&T::GetNativeCurrencyId::get()),
TransactionValidityError::Invalid(InvalidTransaction::Payment)
);
T::Dex::inner_swap_assets_for_exact_assets(who, fee, *amount_in_max, &path, &who)
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
}
_ => {}
}
<SubstrateDefaultPayment<T> as OnChargeTransaction<T>>::withdraw_fee(who, call, info, fee, tip)
}
fn correct_and_deposit_fee(
who: &T::AccountId,
dispatch_info: &DispatchInfoOf<CallOf<T>>,
post_info: &PostDispatchInfoOf<CallOf<T>>,
corrected_fee: Self::Balance,
tip: Self::Balance,
already_withdrawn: Self::LiquidityInfo,
) -> Result<(), TransactionValidityError> {
<SubstrateDefaultPayment<T> as OnChargeTransaction<T>>::correct_and_deposit_fee(
who,
dispatch_info,
post_info,
corrected_fee,
tip,
already_withdrawn,
)
}
}