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
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use cumulus_primitives_core::ParaId;
pub use frame_support::{
	match_types, parameter_types,
	traits::{Everything, Get, Nothing},
	weights::Weight,
};

use orml_asset_registry::{AssetRegistryTrader, FixedRateAssetRegistryTrader};
use orml_traits::{parameter_type_with_key, FixedConversionRateProvider, GetByKey, MultiCurrency};
use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter};
use pallet_xcm::XcmPassthrough;
use polkadot_parachain_primitives::primitives::Sibling;

use sp_std::{marker::PhantomData, prelude::*};
use xcm::latest::{prelude::*, Weight as XcmWeight};
use xcm_builder::{
	Account32Hash, AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
	AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, FixedRateOfFungible,
	FixedWeightBounds, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
	SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
	SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit,
};
use xcm_executor::{traits::DropAssets, Assets};

pub fn general_key(key: &[u8]) -> Junction {
	let mut data = [0u8; 32];
	data[..key.len()].copy_from_slice(&key[..]);
	GeneralKey { length: key.len() as u8, data }
}

parameter_types! {
	pub KsmLocation: MultiLocation = MultiLocation::parent();
	pub const RelayNetwork: NetworkId = NetworkId::Polkadot;
	// pub RelayChainOrigin<T>: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
	// pub UniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(RelayNetwork::get()), Parachain(ParachainInfo::parachain_id().into()));
}

pub struct RelayChainOrigin<T>(PhantomData<T>);
impl<T> Get<T::RuntimeOrigin> for RelayChainOrigin<T>
where
	T: frame_system::Config,
	<T as frame_system::Config>::RuntimeOrigin: From<cumulus_pallet_xcm::Origin>,
{
	fn get() -> T::RuntimeOrigin {
		cumulus_pallet_xcm::Origin::Relay.into()
	}
}

pub struct UniversalLocation<T>(PhantomData<T>);
impl<T> Get<InteriorMultiLocation> for UniversalLocation<T>
where
	T: parachain_info::Config,
{
	fn get() -> InteriorMultiLocation {
		X2(
			GlobalConsensus(RelayNetwork::get()),
			Parachain(parachain_info::Pallet::<T>::parachain_id().into()),
		)
	}
}

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type RuntimeOriginOf<T> = <T as frame_system::Config>::AccountId;
pub type TokensIdOf<T> = <T as orml_tokens::Config>::CurrencyId;

/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
/// when determining ownership of accounts for asset transacting and when attempting to use XCM
/// `Transact` in order to determine the dispatch Origin.
pub type LocationToAccountId<T> = (
	// The parent (Relay-chain) origin converts to the default `AccountId`.
	ParentIsPreset<AccountIdOf<T>>,
	// Sibling parachain origins convert to AccountId via the `ParaId::into`.
	SiblingParachainConvertsVia<Sibling, AccountIdOf<T>>,
	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
	AccountId32Aliases<RelayNetwork, AccountIdOf<T>>,
	// Create hash of `AccountId32` used for proxy accounts
	Account32Hash<RelayNetwork, AccountIdOf<T>>,
);

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
/// biases the kind of local `Origin` it will become.
pub type XcmOriginToCallOrigin<T, RuntimeOrigin> = (
	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
	// foreign chains who want to have a local sovereign account on this chain which they control.
	SovereignSignedViaLocation<LocationToAccountId<T>, RuntimeOrigin>,
	// Native converter for Relay-chain (Parent) location; will converts to a `Relay` origin when
	// recognized.
	RelayChainAsNative<RelayChainOrigin<T>, RuntimeOrigin>,
	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
	// recognized.
	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
	// Native signed account converter; this just converts an `AccountId32` origin into a normal
	// `Origin::Signed` origin of the same 32-byte value.
	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
	XcmPassthrough<RuntimeOrigin>,
);

match_types! {
	pub type ParentOrParentsExecutivePlurality: impl Contains<MultiLocation> = {
		MultiLocation { parents: 1, interior: Here } |
		MultiLocation { parents: 1, interior: X1(Plurality { id: BodyId::Executive, .. }) }
	};
}

pub type Barrier<T> = (
	TakeWeightCredit,
	AllowTopLevelPaidExecutionFrom<Everything>,
	AllowUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
	// ^^^ Parent and its exec plurality get free execution
	// Expected responses are OK.
	AllowKnownQueryResponses<pallet_xcm::Pallet<T>>,
	AllowSubscriptionsFrom<Everything>,
);
type AssetRegistryOf<T> = orml_asset_registry::Pallet<T>;

use sp_runtime::traits::Convert;

pub struct TokenToMultiLocation<T>(PhantomData<T>);
impl<T> Convert<T::AssetId, Option<MultiLocation>> for TokenToMultiLocation<T>
where
	T: parachain_info::Config,
	T: orml_asset_registry::Config,
	<T as orml_asset_registry::Config>::AssetId: From<u32>,
{
	fn convert(id: T::AssetId) -> Option<MultiLocation> {
		// allow relay asset
		if id == crate::tokens::RelayTokenId::get().into() {
			return Some(MultiLocation::parent())
		}
		// allow native asset
		if id == crate::tokens::MgxTokenId::get().into() {
			return Some(MultiLocation::new(
				1,
				X2(Parachain(parachain_info::Pallet::<T>::get().into()), general_key(&id.encode())),
			))
		}
		// allow assets in registry with location set
		AssetRegistryOf::<T>::multilocation(&id).unwrap_or(None)
	}
}

pub struct MultiLocationToToken<T>(PhantomData<T>);
impl<T> Convert<MultiLocation, Option<T::AssetId>> for MultiLocationToToken<T>
where
	T: parachain_info::Config,
	T: orml_asset_registry::Config,
	<T as orml_asset_registry::Config>::AssetId: From<u32>,
{
	fn convert(location: MultiLocation) -> Option<T::AssetId> {
		// allow relay asset
		if location == MultiLocation::parent() {
			return Some(crate::tokens::RelayTokenId::get().into())
		}

		match location {
			// allow native asset
			MultiLocation {
				parents: 1,
				interior: X2(Parachain(para_id), GeneralKey { length, data }),
			} if ParaId::from(para_id) == parachain_info::Pallet::<T>::get() => {
				let token_id = T::AssetId::decode(&mut &data[..(length as usize)]).ok();
				token_id.filter(|tid| *tid == crate::tokens::MGX_TOKEN_ID.into())
			},
			MultiLocation { parents: 0, interior: X1(GeneralKey { length, data }) } => {
				let token_id = T::AssetId::decode(&mut &data[..(length as usize)]).ok();
				token_id.filter(|tid| *tid == crate::tokens::MGX_TOKEN_ID.into())
			},

			// allow assets in registry with location set
			_ => AssetRegistryOf::<T>::location_to_asset_id(location.clone()),
		}
	}
}

pub struct MultiAssetToToken<T>(PhantomData<T>);
impl<T> Convert<MultiAsset, Option<T::AssetId>> for MultiAssetToToken<T>
where
	T: parachain_info::Config,
	T: orml_asset_registry::Config,
	<T as orml_asset_registry::Config>::AssetId: From<u32>,
{
	fn convert(asset: MultiAsset) -> Option<T::AssetId> {
		if let MultiAsset { id: Concrete(location), .. } = asset {
			MultiLocationToToken::<T>::convert(location)
		} else {
			None
		}
	}
}

pub type LocalAssetTransactor<Runtime> = MultiCurrencyAdapter<
	orml_tokens::Pallet<Runtime>,
	orml_unknown_tokens::Pallet<Runtime>,
	IsNativeConcrete<TokensIdOf<Runtime>, MultiLocationToToken<Runtime>>,
	AccountIdOf<Runtime>,
	LocationToAccountId<Runtime>,
	TokensIdOf<Runtime>,
	MultiAssetToToken<Runtime>,
	orml_xcm_support::DepositToAlternative<
		crate::config::TreasuryAccountIdOf<Runtime>,
		orml_tokens::Pallet<Runtime>,
		TokensIdOf<Runtime>,
		AccountIdOf<Runtime>,
		<Runtime as orml_tokens::Config>::Balance,
	>,
>;

pub struct ToTreasury<T>(PhantomData<T>);

impl<T> TakeRevenue for ToTreasury<T>
where
	T: orml_tokens::Config<
		AccountId = sp_runtime::AccountId32,
		CurrencyId = crate::TokenId,
		Balance = crate::Balance,
	>,
	T: pallet_treasury::Config,
	T: parachain_info::Config,
	T: orml_asset_registry::Config<AssetId = crate::TokenId>,
{
	fn take_revenue(revenue: MultiAsset) {
		if let MultiAsset { id: Concrete(location), fun: Fungible(amount) } = revenue {
			if let Some(currency_id) = MultiLocationToToken::<T>::convert(location) {
				// Ensure AcalaTreasuryAccount have ed requirement for native asset, but don't need
				// ed requirement for cross-chain asset because it's one of whitelist accounts.
				// Ignore the result.
				let _ = orml_tokens::Pallet::<T>::deposit(
					currency_id.into(),
					&crate::config::TreasuryAccountIdOf::<T>::get(),
					amount,
				);
			}
		}
	}
}

use crate::constants::fee::{ksm_per_second, mgx_per_second};

parameter_types! {
	// regular transfer is ~400M weight, xcm transfer weight is ~4*UnitWeightCost
	pub UnitWeightCost: XcmWeight = XcmWeight::from_parts(150_000_000, 0);
	pub const MaxInstructions: u32 = 100;

	pub KsmPerSecond: (AssetId, u128, u128) = (MultiLocation::parent().into(), ksm_per_second(), ksm_per_second());
	pub MgxPerSecond: (AssetId, u128, u128) = (
		MultiLocation::new(
			0,
			X1(general_key(&crate::tokens::MgxTokenId::get().encode())),
		).into(),
		mgx_per_second(),
		mgx_per_second(),
	);
	pub const MaxAssetsIntoHolding: u32 = 64;
}

pub struct FeePerSecondProvider<T>(PhantomData<T>);
impl<T> FixedConversionRateProvider for FeePerSecondProvider<T>
where
	T: orml_asset_registry::Config<CustomMetadata = mangata_types::assets::CustomMetadata>,
{
	fn get_fee_per_second(location: &MultiLocation) -> Option<u128> {
		if let Some(asset_id) = AssetRegistryOf::<T>::location_to_asset_id(location) {
			if let Some(xcm_meta) = AssetRegistryOf::<T>::metadata(asset_id.clone())
				.and_then(|metadata| metadata.additional.xcm)
			{
				let fee_per_second: u128 = xcm_meta.fee_per_second;
				log::debug!(
					target: "xcm::weight", "fee_per_second: asset: {:?}, fps:{:?}",
					asset_id, fee_per_second
				);
				return Some(fee_per_second)
			}
		}
		None
	}
}

pub type Trader<Runtime> = (
	FixedRateOfFungible<MgxPerSecond, ToTreasury<Runtime>>,
	AssetRegistryTrader<
		FixedRateAssetRegistryTrader<FeePerSecondProvider<Runtime>>,
		ToTreasury<Runtime>,
	>,
	FixedRateOfFungible<KsmPerSecond, ToTreasury<Runtime>>,
);

pub type Weigher<RuntimeCall> = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;

/// The means for routing XCM messages which are not for local execution into the right message
/// queues.
pub type XcmRouter<Runtime> = (
	// Two routers - use UMP to communicate with the relay chain:
	cumulus_primitives_utility::ParentAsUmp<
		cumulus_pallet_parachain_system::Pallet<Runtime>,
		pallet_xcm::Pallet<Runtime>,
		(),
	>,
	// ..and XCMP to communicate with the sibling chains.
	cumulus_pallet_xcmp_queue::Pallet<Runtime>,
);

/// `DropAssets` implementation support asset amount lower thant ED handled by `TakeRevenue`.
///
/// parameters type:
/// - `NC`: native currency_id type.
/// - `NB`: the ExistentialDeposit amount of native currency_id.
/// - `GK`: the ExistentialDeposit amount of tokens.
pub struct MangataDropAssets<X, T, C, GK>(PhantomData<(X, T, C, GK)>);
impl<X, T, C, GK> DropAssets for MangataDropAssets<X, T, C, GK>
where
	X: DropAssets,
	T: TakeRevenue,
	C: Convert<MultiLocation, Option<crate::TokenId>>,
	GK: GetByKey<crate::TokenId, crate::Balance>,
{
	fn drop_assets(
		origin: &MultiLocation,
		assets: Assets,
		context: &XcmContext,
	) -> sp_weights::Weight {
		let multi_assets: Vec<MultiAsset> = assets.into();
		let mut asset_traps: Vec<MultiAsset> = Default::default();
		for asset in multi_assets {
			if let MultiAsset { id: Concrete(location), fun: Fungible(amount) } = asset.clone() {
				let currency_id = C::convert(location);
				// burn asset(do nothing here) if convert result is None
				if let Some(currency_id) = currency_id {
					let ed = GK::get(&currency_id);
					if amount < ed {
						T::take_revenue(asset);
					} else {
						asset_traps.push(asset);
					}
				}
			}
		}
		if !asset_traps.is_empty() {
			X::drop_assets(origin, asset_traps.into(), context);
		}
		XcmWeight::from_parts(0, 0)
	}
}

pub type DropAssetsHandler<T> = MangataDropAssets<
	pallet_xcm::Pallet<T>,
	ToTreasury<T>,
	MultiLocationToToken<T>,
	crate::config::ExistentialDepositsOf<T>,
>;

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
/// biases the kind of local `Origin` it will become.
pub type XcmOriginToTransactDispatchOrigin<Runtime, RuntimeOrigin> = (
	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
	// foreign chains who want to have a local sovereign account on this chain which they control.
	SovereignSignedViaLocation<LocationToAccountId<Runtime>, RuntimeOrigin>,
	// Native converter for Relay-chain (Parent) location; will converts to a `Relay` origin when
	// recognized.
	RelayChainAsNative<RelayChainOrigin<Runtime>, RuntimeOrigin>,
	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
	// recognized.
	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
	// Native signed account converter; this just converts an `AccountId32` origin into a normal
	// `Origin::Signed` origin of the same 32-byte value.
	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
	XcmPassthrough<RuntimeOrigin>,
);

parameter_type_with_key! {
	pub ParachainMinFee: |_location: MultiLocation| -> Option<u128> {
		None
	};
}

pub struct AccountIdToMultiLocation;
impl Convert<crate::AccountId, MultiLocation> for AccountIdToMultiLocation {
	fn convert(account: crate::AccountId) -> MultiLocation {
		X1(AccountId32 { network: None, id: account.into() }).into()
	}
}

parameter_types! {
	pub const BaseXcmWeight: XcmWeight = XcmWeight::from_parts(100_000_000, 0); // TODO: recheck this
	pub const MaxAssetsForTransfer:usize = 2;
}

pub struct SelfLocation<T>(PhantomData<T>);
impl<T> Get<MultiLocation> for SelfLocation<T>
where
	T: parachain_info::Config,
{
	fn get() -> MultiLocation {
		MultiLocation::new(1, X1(Parachain(parachain_info::Pallet::<T>::get().into())))
	}
}

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation<RuntimeOrigin> =
	SignedToAccountId32<RuntimeOrigin, crate::AccountId, RelayNetwork>;