-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathlib.rs
More file actions
99 lines (84 loc) · 3.16 KB
/
lib.rs
File metadata and controls
99 lines (84 loc) · 3.16 KB
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
//! # XCM Support Module.
//!
//! ## Overview
//!
//! The XCM support module provides supporting traits, types and
//! implementations, to support cross-chain message(XCM) integration with ORML
//! modules.
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]
use frame_support::dispatch::{DispatchError, DispatchResult};
use sp_runtime::traits::{CheckedConversion, Convert};
use sp_std::{convert::TryFrom, marker::PhantomData, prelude::*};
use xcm::latest::prelude::*;
use xcm_executor::traits::{FilterAssetLocation, MatchesFungible};
use orml_traits::location::Reserve;
pub use currency_adapter::MultiCurrencyAdapter;
use frame_support::pallet_prelude::Get;
mod currency_adapter;
mod tests;
/// A `MatchesFungible` implementation. It matches concrete fungible assets
/// whose `id` could be converted into `CurrencyId`.
pub struct IsNativeConcrete<CurrencyId, CurrencyIdConvert>(PhantomData<(CurrencyId, CurrencyIdConvert)>);
impl<CurrencyId, CurrencyIdConvert, Amount> MatchesFungible<Amount> for IsNativeConcrete<CurrencyId, CurrencyIdConvert>
where
CurrencyIdConvert: Convert<MultiLocation, Option<CurrencyId>>,
Amount: TryFrom<u128>,
{
fn matches_fungible(a: &MultiAsset) -> Option<Amount> {
if let (Fungible(ref amount), Concrete(ref location)) = (&a.fun, &a.id) {
if CurrencyIdConvert::convert(location.clone()).is_some() {
return CheckedConversion::checked_from(*amount);
}
}
None
}
}
/// A `FilterAssetLocation` implementation. Filters multi native assets whose
/// reserve is same with `origin`.
pub struct MultiNativeAsset;
impl FilterAssetLocation for MultiNativeAsset {
fn filter_asset_location(asset: &MultiAsset, origin: &MultiLocation) -> bool {
if let Some(ref reserve) = asset.reserve() {
if reserve == origin {
return true;
}
}
false
}
}
/// Handlers unknown asset deposit and withdraw.
pub trait UnknownAsset {
/// Deposit unknown asset.
fn deposit(asset: &MultiAsset, to: &MultiLocation) -> DispatchResult;
/// Withdraw unknown asset.
fn withdraw(asset: &MultiAsset, from: &MultiLocation) -> DispatchResult;
}
const NO_UNKNOWN_ASSET_IMPL: &str = "NoUnknownAssetImpl";
impl UnknownAsset for () {
fn deposit(_asset: &MultiAsset, _to: &MultiLocation) -> DispatchResult {
Err(DispatchError::Other(NO_UNKNOWN_ASSET_IMPL))
}
fn withdraw(_asset: &MultiAsset, _from: &MultiLocation) -> DispatchResult {
Err(DispatchError::Other(NO_UNKNOWN_ASSET_IMPL))
}
}
/// Extracts the `AccountId32` from the passed `location` if the network matches.
pub struct RelaychainAccountId32Aliases<Network, AccountId>(PhantomData<(Network, AccountId)>);
impl<Network: Get<NetworkId>, AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone>
xcm_executor::traits::Convert<MultiLocation, AccountId> for RelaychainAccountId32Aliases<Network, AccountId>
{
fn convert(location: MultiLocation) -> Result<AccountId, MultiLocation> {
let id = match location {
MultiLocation {
parents: 1,
interior: X1(AccountId32 { id, network: NetworkId::Any }),
} => id,
_ => return Err(location),
};
Ok(id.into())
}
fn reverse(who: AccountId) -> Result<MultiLocation, AccountId> {
Ok(AccountId32 { id: who.into(), network: Network::get() }.into())
}
}