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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

//! # A pallet to trade weight for XCM execution

#![allow(non_camel_case_types)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

pub mod weights;

pub use pallet::*;
pub use weights::WeightInfo;

use frame_support::pallet;
use frame_support::pallet_prelude::*;
use frame_support::traits::Contains;
use frame_support::weights::WeightToFee;
use frame_system::pallet_prelude::*;
use sp_runtime::traits::{Convert, Zero};
use sp_std::vec::Vec;
use xcm::v4::{Asset, AssetId as XcmAssetId, Error as XcmError, Fungibility, Location, XcmContext};
use xcm::{IntoVersion, VersionedAssetId};
use xcm_executor::traits::{TransactAsset, WeightTrader};
use xcm_runtime_apis::fees::Error as XcmPaymentApiError;

pub const RELATIVE_PRICE_DECIMALS: u32 = 18;

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

	/// Pallet for multi block migrations
	#[pallet::pallet]
	pub struct Pallet<T>(PhantomData<T>);

	/// Configuration trait of this pallet.
	#[pallet::config]
	pub trait Config: frame_system::Config {
		/// Convert `T::AccountId` to `Location`.
		type AccountIdToLocation: Convert<Self::AccountId, Location>;

		/// Origin that is allowed to register a supported asset
		type AddSupportedAssetOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// A filter to forbid some XCM Location to be supported for fees.
		/// if you don't use it, put "Everything".
		type AssetLocationFilter: Contains<Location>;

		/// How to withdraw and deposit an asset.
		type AssetTransactor: TransactAsset;

		/// The native balance type.
		type Balance: TryInto<u128>;

		/// Origin that is allowed to edit a supported asset units per seconds
		type EditSupportedAssetOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// XCM Location for native curreny
		type NativeLocation: Get<Location>;

		/// Origin that is allowed to pause a supported asset
		type PauseSupportedAssetOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// Origin that is allowed to remove a supported asset
		type RemoveSupportedAssetOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// The overarching event type.
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

		/// Origin that is allowed to unpause a supported asset
		type ResumeSupportedAssetOrigin: EnsureOrigin<Self::RuntimeOrigin>;

		/// Weight information for extrinsics in this pallet.
		type WeightInfo: WeightInfo;

		/// Convert a weight value into deductible native balance.
		type WeightToFee: WeightToFee<Balance = Self::Balance>;

		/// Account that will receive xcm fees
		type XcmFeesAccount: Get<Self::AccountId>;

		/// The benchmarks need a location that pass the filter AssetLocationFilter
		#[cfg(feature = "runtime-benchmarks")]
		type NotFilteredLocation: Get<Location>;
	}

	/// Stores all supported assets per XCM Location.
	/// The u128 is the asset price relative to native asset with 18 decimals
	/// The boolean specify if the support for this asset is active
	#[pallet::storage]
	#[pallet::getter(fn supported_assets)]
	pub type SupportedAssets<T: Config> = StorageMap<_, Blake2_128Concat, Location, (bool, u128)>;

	#[pallet::error]
	pub enum Error<T> {
		/// The given asset was already added
		AssetAlreadyAdded,
		/// The given asset was already paused
		AssetAlreadyPaused,
		/// The given asset was not found
		AssetNotFound,
		/// The given asset is not paused
		AssetNotPaused,
		/// XCM location filtered
		XcmLocationFiltered,
		/// The relative price cannot be zero
		PriceCannotBeZero,
	}

	#[pallet::event]
	#[pallet::generate_deposit(pub(crate) fn deposit_event)]
	pub enum Event<T: Config> {
		/// New supported asset is registered
		SupportedAssetAdded {
			location: Location,
			relative_price: u128,
		},
		/// Changed the amount of units we are charging per execution second for a given asset
		SupportedAssetEdited {
			location: Location,
			relative_price: u128,
		},
		/// Pause support for a given asset
		PauseAssetSupport { location: Location },
		/// Resume support for a given asset
		ResumeAssetSupport { location: Location },
		/// Supported asset type for fee payment removed
		SupportedAssetRemoved { location: Location },
	}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
		#[pallet::call_index(0)]
		#[pallet::weight(T::WeightInfo::add_asset())]
		pub fn add_asset(
			origin: OriginFor<T>,
			location: Location,
			relative_price: u128,
		) -> DispatchResult {
			T::AddSupportedAssetOrigin::ensure_origin(origin)?;

			ensure!(relative_price != 0, Error::<T>::PriceCannotBeZero);
			ensure!(
				!SupportedAssets::<T>::contains_key(&location),
				Error::<T>::AssetAlreadyAdded
			);
			ensure!(
				T::AssetLocationFilter::contains(&location),
				Error::<T>::XcmLocationFiltered
			);

			SupportedAssets::<T>::insert(&location, (true, relative_price));

			Self::deposit_event(Event::SupportedAssetAdded {
				location,
				relative_price,
			});

			Ok(())
		}

		#[pallet::call_index(1)]
		#[pallet::weight(T::WeightInfo::edit_asset())]
		pub fn edit_asset(
			origin: OriginFor<T>,
			location: Location,
			relative_price: u128,
		) -> DispatchResult {
			T::EditSupportedAssetOrigin::ensure_origin(origin)?;

			ensure!(relative_price != 0, Error::<T>::PriceCannotBeZero);

			let enabled = SupportedAssets::<T>::get(&location)
				.ok_or(Error::<T>::AssetNotFound)?
				.0;

			SupportedAssets::<T>::insert(&location, (enabled, relative_price));

			Self::deposit_event(Event::SupportedAssetEdited {
				location,
				relative_price,
			});

			Ok(())
		}

		#[pallet::call_index(2)]
		#[pallet::weight(T::WeightInfo::pause_asset_support())]
		pub fn pause_asset_support(origin: OriginFor<T>, location: Location) -> DispatchResult {
			T::PauseSupportedAssetOrigin::ensure_origin(origin)?;

			match SupportedAssets::<T>::get(&location) {
				Some((true, relative_price)) => {
					SupportedAssets::<T>::insert(&location, (false, relative_price));
					Self::deposit_event(Event::PauseAssetSupport { location });
					Ok(())
				}
				Some((false, _)) => Err(Error::<T>::AssetAlreadyPaused.into()),
				None => Err(Error::<T>::AssetNotFound.into()),
			}
		}

		#[pallet::call_index(3)]
		#[pallet::weight(T::WeightInfo::resume_asset_support())]
		pub fn resume_asset_support(origin: OriginFor<T>, location: Location) -> DispatchResult {
			T::ResumeSupportedAssetOrigin::ensure_origin(origin)?;

			match SupportedAssets::<T>::get(&location) {
				Some((false, relative_price)) => {
					SupportedAssets::<T>::insert(&location, (true, relative_price));
					Self::deposit_event(Event::ResumeAssetSupport { location });
					Ok(())
				}
				Some((true, _)) => Err(Error::<T>::AssetNotPaused.into()),
				None => Err(Error::<T>::AssetNotFound.into()),
			}
		}

		#[pallet::call_index(4)]
		#[pallet::weight(T::WeightInfo::remove_asset())]
		pub fn remove_asset(origin: OriginFor<T>, location: Location) -> DispatchResult {
			T::RemoveSupportedAssetOrigin::ensure_origin(origin)?;

			ensure!(
				SupportedAssets::<T>::contains_key(&location),
				Error::<T>::AssetNotFound
			);

			SupportedAssets::<T>::remove(&location);

			Self::deposit_event(Event::SupportedAssetRemoved { location });

			Ok(())
		}
	}

	impl<T: Config> Pallet<T> {
		pub fn get_asset_relative_price(location: &Location) -> Option<u128> {
			if let Some((true, ratio)) = SupportedAssets::<T>::get(location) {
				Some(ratio)
			} else {
				None
			}
		}
		pub fn query_acceptable_payment_assets(
			xcm_version: xcm::Version,
		) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
			if !matches!(xcm_version, 3 | 4) {
				return Err(XcmPaymentApiError::UnhandledXcmVersion);
			}

			let v4_assets = [VersionedAssetId::V4(XcmAssetId::from(
				T::NativeLocation::get(),
			))]
			.into_iter()
			.chain(
				SupportedAssets::<T>::iter().filter_map(|(asset_location, (enabled, _))| {
					enabled.then(|| VersionedAssetId::V4(XcmAssetId(asset_location)))
				}),
			)
			.collect::<Vec<_>>();

			if xcm_version == 3 {
				v4_assets
					.into_iter()
					.map(|v4_asset| v4_asset.into_version(3))
					.collect::<Result<_, _>>()
					.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)
			} else {
				Ok(v4_assets)
			}
		}
		pub fn query_weight_to_asset_fee(
			weight: Weight,
			asset: VersionedAssetId,
		) -> Result<u128, XcmPaymentApiError> {
			if let VersionedAssetId::V4(XcmAssetId(asset_location)) = asset
				.into_version(4)
				.map_err(|_| XcmPaymentApiError::VersionedConversionFailed)?
			{
				Trader::<T>::compute_amount_to_charge(&weight, &asset_location).map_err(|e| match e
				{
					XcmError::AssetNotFound => XcmPaymentApiError::AssetNotFound,
					_ => XcmPaymentApiError::WeightNotComputable,
				})
			} else {
				Err(XcmPaymentApiError::UnhandledXcmVersion)
			}
		}
		#[cfg(any(feature = "std", feature = "runtime-benchmarks"))]
		pub fn set_asset_price(asset_location: Location, relative_price: u128) {
			SupportedAssets::<T>::insert(&asset_location, (true, relative_price));
		}
	}
}

pub struct Trader<T: crate::Config>(Weight, Option<Asset>, core::marker::PhantomData<T>);

impl<T: crate::Config> Trader<T> {
	fn compute_amount_to_charge(
		weight: &Weight,
		asset_location: &Location,
	) -> Result<u128, XcmError> {
		if *asset_location == <T as crate::Config>::NativeLocation::get() {
			<T as crate::Config>::WeightToFee::weight_to_fee(&weight)
				.try_into()
				.map_err(|_| XcmError::Overflow)
		} else if let Some(relative_price) = Pallet::<T>::get_asset_relative_price(asset_location) {
			if relative_price == 0u128 {
				Ok(0u128)
			} else {
				let native_amount: u128 = <T as crate::Config>::WeightToFee::weight_to_fee(&weight)
					.try_into()
					.map_err(|_| XcmError::Overflow)?;
				Ok(native_amount
					.checked_mul(10u128.pow(RELATIVE_PRICE_DECIMALS))
					.ok_or(XcmError::Overflow)?
					.checked_div(relative_price)
					.ok_or(XcmError::Overflow)?)
			}
		} else {
			Err(XcmError::AssetNotFound)
		}
	}
}

impl<T: crate::Config> WeightTrader for Trader<T> {
	fn new() -> Self {
		Self(Weight::zero(), None, PhantomData)
	}
	fn buy_weight(
		&mut self,
		weight: Weight,
		payment: xcm_executor::AssetsInHolding,
		context: &XcmContext,
	) -> Result<xcm_executor::AssetsInHolding, XcmError> {
		log::trace!(
			target: "xcm::weight",
			"UsingComponents::buy_weight weight: {:?}, payment: {:?}, context: {:?}",
			weight,
			payment,
			context
		);

		// Can only call one time
		if self.1.is_some() {
			return Err(XcmError::NotWithdrawable);
		}

		// Consistency check for tests only, we should never panic in release mode
		debug_assert_eq!(self.0, Weight::zero());

		// We support only one fee asset per buy, so we take the first one.
		let first_asset = payment
			.clone()
			.fungible_assets_iter()
			.next()
			.ok_or(XcmError::AssetNotFound)?;

		match (first_asset.id, first_asset.fun) {
			(XcmAssetId(location), Fungibility::Fungible(_)) => {
				let amount: u128 = Self::compute_amount_to_charge(&weight, &location)?;

				// We don't need to proceed if the amount is 0
				// For cases (specially tests) where the asset is very cheap with respect
				// to the weight needed
				if amount.is_zero() {
					return Ok(payment);
				}

				let required = Asset {
					fun: Fungibility::Fungible(amount),
					id: XcmAssetId(location),
				};
				let unused = payment
					.checked_sub(required.clone())
					.map_err(|_| XcmError::TooExpensive)?;

				self.0 = weight;
				self.1 = Some(required);

				Ok(unused)
			}
			_ => Err(XcmError::AssetNotFound),
		}
	}

	fn refund_weight(&mut self, actual_weight: Weight, context: &XcmContext) -> Option<Asset> {
		log::trace!(
			target: "xcm-weight-trader",
			"refund_weight weight: {:?}, context: {:?}, available weight: {:?}, asset: {:?}",
			actual_weight,
			context,
			self.0,
			self.1
		);
		if let Some(Asset {
			fun: Fungibility::Fungible(initial_amount),
			id: XcmAssetId(location),
		}) = self.1.take()
		{
			if actual_weight == self.0 {
				self.1 = Some(Asset {
					fun: Fungibility::Fungible(initial_amount),
					id: XcmAssetId(location),
				});
				None
			} else {
				let weight = actual_weight.min(self.0);
				let amount: u128 =
					Self::compute_amount_to_charge(&weight, &location).unwrap_or(u128::MAX);
				let final_amount = amount.min(initial_amount);
				let amount_to_refund = initial_amount.saturating_sub(final_amount);
				self.0 -= weight;
				self.1 = Some(Asset {
					fun: Fungibility::Fungible(final_amount),
					id: XcmAssetId(location.clone()),
				});
				log::trace!(
					target: "xcm-weight-trader",
					"refund_weight amount to refund: {:?}",
					amount_to_refund
				);
				Some(Asset {
					fun: Fungibility::Fungible(amount_to_refund),
					id: XcmAssetId(location),
				})
			}
		} else {
			None
		}
	}
}

impl<T: crate::Config> Drop for Trader<T> {
	fn drop(&mut self) {
		log::trace!(
			target: "xcm-weight-trader",
			"Dropping `Trader` instance: (weight: {:?}, asset: {:?})",
			&self.0,
			&self.1
		);
		if let Some(asset) = self.1.take() {
			let res = T::AssetTransactor::deposit_asset(
				&asset,
				&T::AccountIdToLocation::convert(T::XcmFeesAccount::get()),
				None,
			);
			debug_assert!(res.is_ok());
		}
	}
}