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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
//! # Restricted Xtokens Pallet
//!
//! ## Overview
//!
//! This pallet is a wrapper pallet over `orml-xtokens` which allows the runtime
//! to create arbitrary filters for transfers of x-chain-transfers.
//! The interface mimic 1-to-1 the interface of `orml-xtokens`.
//!
//! ## Interface
//!
//! ### Dispatchable functions
//!
//! - `transfer`: Transfer local assets with given `CurrencyId` and `Amount`.
//! - `transfer_multiasset`: Transfer `Asset` assets.
//! - `transfer_with_fee`: Transfer native currencies specifying the fee and
//!   amount as separate.
//! - `transfer_multiasset_with_fee`: Transfer `Asset` specifying the fee and
//!   amount as separate.
//! - `transfer_multicurrencies`: Transfer several currencies specifying the
//!   item to be used as fee.
//! - `transfer_multiassets`: Transfer several `Asset` specifying the item to be
//!   used as fee.

#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::from_over_into)]
#![allow(clippy::unused_unit)]
#![allow(clippy::large_enum_variant)]
#![allow(clippy::boxed_local)]
#![allow(clippy::too_many_arguments)]

use cfg_traits::PreConditions;
use frame_support::pallet_prelude::*;
use frame_system::{ensure_signed, pallet_prelude::*};
use orml_traits::XtokensWeightInfo;
pub use pallet::*;
use sp_std::{boxed::Box, vec::Vec};
use staging_xcm::{v4::prelude::*, VersionedAsset, VersionedAssets, VersionedLocation};

pub enum TransferEffects<AccountId, CurrencyId, Balance> {
	Transfer {
		sender: AccountId,
		destination: VersionedLocation,
		currency_id: CurrencyId,
		amount: Balance,
	},
	TransferMultiAsset {
		sender: AccountId,
		destination: VersionedLocation,
		asset: Asset,
	},
	TransferWithFee {
		sender: AccountId,
		destination: VersionedLocation,
		currency_id: CurrencyId,
		amount: Balance,
		fee: Balance,
	},
	TransferMultiAssetWithFee {
		sender: AccountId,
		destination: VersionedLocation,
		asset: Asset,
		fee_asset: Asset,
	},
	TransferMultiCurrencies {
		sender: AccountId,
		destination: VersionedLocation,
		currencies: Vec<(CurrencyId, Balance)>,
		fee: (CurrencyId, Balance),
	},
	TransferMultiAssets {
		sender: AccountId,
		destination: VersionedLocation,
		assets: Assets,
		fee_asset: Asset,
	},
}

#[frame_support::pallet]
pub mod pallet {
	use super::*;

	#[pallet::config]
	pub trait Config: frame_system::Config + orml_xtokens::Config {
		type PreTransfer: PreConditions<
			TransferEffects<Self::AccountId, Self::CurrencyId, Self::Balance>,
			Result = DispatchResult,
		>;
	}

	#[pallet::pallet]
	pub struct Pallet<T>(_);

	#[pallet::error]
	pub enum Error<T> {
		BadVersion,
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		/// Transfer native currencies.
		///
		/// `dest_weight_limit` is the weight for XCM execution on the dest
		/// chain, and it would be charged from the transferred assets. If set
		/// below requirements, the execution may fail and assets wouldn't be
		/// received.
		///
		/// It's a no-op if any error on local XCM execution or message sending.
		/// Note sending assets out per se doesn't guarantee they would be
		/// received. Receiving depends on if the XCM message could be delivered
		/// by the network, and if the receiving chain would handle
		/// messages correctly.
		#[pallet::call_index(0)]
		#[pallet::weight(orml_xtokens::XtokensWeight::< T >::weight_of_transfer(currency_id.clone(), * amount, dest) + T::DbWeight::get().reads(4))]
		pub fn transfer(
			origin: OriginFor<T>,
			currency_id: T::CurrencyId,
			amount: T::Balance,
			dest: Box<VersionedLocation>,
			dest_weight_limit: WeightLimit,
		) -> DispatchResult {
			let sender = ensure_signed(origin.clone())?;

			T::PreTransfer::check(TransferEffects::Transfer {
				sender,
				destination: (*dest.clone()),
				currency_id: currency_id.clone(),
				amount,
			})?;

			orml_xtokens::Pallet::<T>::transfer(
				origin,
				currency_id,
				amount,
				dest,
				dest_weight_limit,
			)
		}

		/// Transfer `Asset`.
		///
		/// `dest_weight_limit` is the weight for XCM execution on the dest
		/// chain, and it would be charged from the transferred assets. If set
		/// below requirements, the execution may fail and assets wouldn't be
		/// received.
		///
		/// It's a no-op if any error on local XCM execution or message sending.
		/// Note sending assets out per se doesn't guarantee they would be
		/// received. Receiving depends on if the XCM message could be delivered
		/// by the network, and if the receiving chain would handle
		/// messages correctly.
		#[pallet::call_index(1)]
		#[pallet::weight(orml_xtokens::XtokensWeight::< T >::weight_of_transfer_multiasset(asset, dest) + T::DbWeight::get().reads(4))]
		pub fn transfer_multiasset(
			origin: OriginFor<T>,
			asset: Box<VersionedAsset>,
			dest: Box<VersionedLocation>,
			dest_weight_limit: WeightLimit,
		) -> DispatchResult {
			let sender = ensure_signed(origin.clone())?;
			let multi_asset: Asset = (*asset.clone())
				.try_into()
				.map_err(|()| Error::<T>::BadVersion)?;

			T::PreTransfer::check(TransferEffects::TransferMultiAsset {
				sender,
				destination: (*dest.clone()),
				asset: multi_asset,
			})?;

			orml_xtokens::Pallet::<T>::transfer_multiasset(origin, asset, dest, dest_weight_limit)
		}

		/// Transfer native currencies specifying the fee and amount as
		/// separate.
		///
		/// `dest_weight_limit` is the weight for XCM execution on the dest
		/// chain, and it would be charged from the transferred assets. If set
		/// below requirements, the execution may fail and assets wouldn't be
		/// received.
		///
		/// `fee` is the amount to be spent to pay for execution in destination
		/// chain. Both fee and amount will be subtracted form the callers
		/// balance.
		///
		/// If `fee` is not high enough to cover for the execution costs in the
		/// destination chain, then the assets will be trapped in the
		/// destination chain
		///
		/// It's a no-op if any error on local XCM execution or message sending.
		/// Note sending assets out per se doesn't guarantee they would be
		/// received. Receiving depends on if the XCM message could be delivered
		/// by the network, and if the receiving chain would handle
		/// messages correctly.
		#[pallet::call_index(2)]
		#[pallet::weight(orml_xtokens::XtokensWeight::< T >::weight_of_transfer(currency_id.clone(), * amount, dest) + T::DbWeight::get().reads(4))]
		pub fn transfer_with_fee(
			origin: OriginFor<T>,
			currency_id: T::CurrencyId,
			amount: T::Balance,
			fee: T::Balance,
			dest: Box<VersionedLocation>,
			dest_weight_limit: WeightLimit,
		) -> DispatchResult {
			let sender = ensure_signed(origin.clone())?;

			T::PreTransfer::check(TransferEffects::TransferWithFee {
				sender,
				destination: (*dest.clone()),
				currency_id: currency_id.clone(),
				amount,
				fee,
			})?;

			orml_xtokens::Pallet::<T>::transfer_with_fee(
				origin,
				currency_id,
				amount,
				fee,
				dest,
				dest_weight_limit,
			)
		}

		/// Transfer `Asset` specifying the fee and amount as separate.
		///
		/// `dest_weight_limit` is the weight for XCM execution on the dest
		/// chain, and it would be charged from the transferred assets. If set
		/// below requirements, the execution may fail and assets wouldn't be
		/// received.
		///
		/// `fee` is the multiasset to be spent to pay for execution in
		/// destination chain. Both fee and amount will be subtracted form the
		/// callers balance For now we only accept fee and asset having the same
		/// `Location` id.
		///
		/// If `fee` is not high enough to cover for the execution costs in the
		/// destination chain, then the assets will be trapped in the
		/// destination chain
		///
		/// It's a no-op if any error on local XCM execution or message sending.
		/// Note sending assets out per se doesn't guarantee they would be
		/// received. Receiving depends on if the XCM message could be delivered
		/// by the network, and if the receiving chain would handle
		/// messages correctly.
		#[pallet::call_index(3)]
		#[pallet::weight(orml_xtokens::XtokensWeight::< T >::weight_of_transfer_multiasset(asset, dest) + T::DbWeight::get().reads(4))]
		pub fn transfer_multiasset_with_fee(
			origin: OriginFor<T>,
			asset: Box<VersionedAsset>,
			fee: Box<VersionedAsset>,
			dest: Box<VersionedLocation>,
			dest_weight_limit: WeightLimit,
		) -> DispatchResult {
			let sender = ensure_signed(origin.clone())?;
			let multi_asset: Asset = (*asset.clone())
				.try_into()
				.map_err(|()| Error::<T>::BadVersion)?;
			let fee_asset: Asset = (*fee.clone())
				.try_into()
				.map_err(|()| Error::<T>::BadVersion)?;

			T::PreTransfer::check(TransferEffects::TransferMultiAssetWithFee {
				sender,
				destination: (*dest.clone()),
				asset: multi_asset,
				fee_asset,
			})?;

			orml_xtokens::Pallet::<T>::transfer_multiasset_with_fee(
				origin,
				asset,
				fee,
				dest,
				dest_weight_limit,
			)
		}

		/// Transfer several currencies specifying the item to be used as fee
		///
		/// `dest_weight_limit` is the weight for XCM execution on the dest
		/// chain, and it would be charged from the transferred assets. If set
		/// below requirements, the execution may fail and assets wouldn't be
		/// received.
		///
		/// `fee_item` is index of the currencies tuple that we want to use for
		/// payment
		///
		/// It's a no-op if any error on local XCM execution or message sending.
		/// Note sending assets out per se doesn't guarantee they would be
		/// received. Receiving depends on if the XCM message could be delivered
		/// by the network, and if the receiving chain would handle
		/// messages correctly.
		#[pallet::call_index(4)]
		#[pallet::weight(orml_xtokens::XtokensWeight::< T >::weight_of_transfer_multicurrencies(currencies, fee_item, dest) + T::DbWeight::get().reads(4))]
		pub fn transfer_multicurrencies(
			origin: OriginFor<T>,
			currencies: Vec<(T::CurrencyId, T::Balance)>,
			fee_item: u32,
			dest: Box<VersionedLocation>,
			dest_weight_limit: WeightLimit,
		) -> DispatchResult {
			let sender = ensure_signed(origin.clone())?;
			let fee = currencies
				.get(fee_item as usize)
				.ok_or(orml_xtokens::Error::<T>::AssetIndexNonExistent)?;

			T::PreTransfer::check(TransferEffects::TransferMultiCurrencies {
				sender,
				destination: (*dest.clone()),
				currencies: currencies.clone(),
				fee: fee.clone(),
			})?;

			orml_xtokens::Pallet::<T>::transfer_multicurrencies(
				origin,
				currencies,
				fee_item,
				dest,
				dest_weight_limit,
			)
		}

		/// Transfer several `Asset` specifying the item to be used as fee
		///
		/// `dest_weight_limit` is the weight for XCM execution on the dest
		/// chain, and it would be charged from the transferred assets. If set
		/// below requirements, the execution may fail and assets wouldn't be
		/// received.
		///
		/// `fee_item` is index of the Assets that we want to use for
		/// payment
		///
		/// It's a no-op if any error on local XCM execution or message sending.
		/// Note sending assets out per se doesn't guarantee they would be
		/// received. Receiving depends on if the XCM message could be delivered
		/// by the network, and if the receiving chain would handle
		/// messages correctly.
		#[pallet::call_index(5)]
		#[pallet::weight(orml_xtokens::XtokensWeight::< T >::weight_of_transfer_multiassets(assets, fee_item, dest) + T::DbWeight::get().reads(4))]
		pub fn transfer_multiassets(
			origin: OriginFor<T>,
			assets: Box<VersionedAssets>,
			fee_item: u32,
			dest: Box<VersionedLocation>,
			dest_weight_limit: WeightLimit,
		) -> DispatchResult {
			let sender = ensure_signed(origin.clone())?;
			let multi_assets: Assets = (*assets.clone())
				.try_into()
				.map_err(|()| Error::<T>::BadVersion)?;
			let fee_asset: &Asset = multi_assets
				.get(fee_item as usize)
				.ok_or(orml_xtokens::Error::<T>::AssetIndexNonExistent)?;

			T::PreTransfer::check(TransferEffects::TransferMultiAssets {
				sender,
				destination: (*dest.clone()),
				assets: multi_assets.clone(),
				fee_asset: fee_asset.clone(),
			})?;

			orml_xtokens::Pallet::<T>::transfer_multiassets(
				origin,
				assets,
				fee_item,
				dest,
				dest_weight_limit,
			)
		}
	}
}