orml_xcm_support/
lib.rs

1//! # XCM Support Module.
2//!
3//! ## Overview
4//!
5//! The XCM support module provides supporting traits, types and
6//! implementations, to support cross-chain message(XCM) integration with ORML
7//! modules.
8
9#![cfg_attr(not(feature = "std"), no_std)]
10#![allow(clippy::unused_unit)]
11
12use frame_support::{dispatch::DispatchResult, traits::ContainsPair};
13use sp_runtime::{
14	traits::{CheckedConversion, Convert},
15	DispatchError,
16};
17use sp_std::marker::PhantomData;
18
19use xcm::v5::prelude::*;
20use xcm_executor::traits::MatchesFungible;
21
22use orml_traits::{location::Reserve, GetByKey};
23
24pub use currency_adapter::{DepositToAlternative, MultiCurrencyAdapter, OnDepositFail};
25
26mod currency_adapter;
27
28mod tests;
29
30/// A `MatchesFungible` implementation. It matches concrete fungible assets
31/// whose `id` could be converted into `CurrencyId`.
32pub struct IsNativeConcrete<CurrencyId, CurrencyIdConvert>(PhantomData<(CurrencyId, CurrencyIdConvert)>);
33impl<CurrencyId, CurrencyIdConvert, Amount> MatchesFungible<Amount> for IsNativeConcrete<CurrencyId, CurrencyIdConvert>
34where
35	CurrencyIdConvert: Convert<Location, Option<CurrencyId>>,
36	Amount: TryFrom<u128>,
37{
38	fn matches_fungible(a: &Asset) -> Option<Amount> {
39		if let (Fungible(ref amount), AssetId(location)) = (&a.fun, &a.id) {
40			if CurrencyIdConvert::convert(location.clone()).is_some() {
41				return CheckedConversion::checked_from(*amount);
42			}
43		}
44		None
45	}
46}
47
48/// A `ContainsPair` implementation. Filters multi native assets whose
49/// reserve is same with `origin`.
50pub struct MultiNativeAsset<ReserveProvider>(PhantomData<ReserveProvider>);
51impl<ReserveProvider> ContainsPair<Asset, Location> for MultiNativeAsset<ReserveProvider>
52where
53	ReserveProvider: Reserve,
54{
55	fn contains(asset: &Asset, origin: &Location) -> bool {
56		if let Some(ref reserve) = ReserveProvider::reserve(asset) {
57			if reserve == origin {
58				return true;
59			}
60		}
61		false
62	}
63}
64
65/// Handlers unknown asset deposit and withdraw.
66pub trait UnknownAsset {
67	/// Deposit unknown asset.
68	fn deposit(asset: &Asset, to: &Location) -> DispatchResult;
69
70	/// Withdraw unknown asset.
71	fn withdraw(asset: &Asset, from: &Location) -> DispatchResult;
72}
73
74const NO_UNKNOWN_ASSET_IMPL: &str = "NoUnknownAssetImpl";
75
76impl UnknownAsset for () {
77	fn deposit(_asset: &Asset, _to: &Location) -> DispatchResult {
78		Err(DispatchError::Other(NO_UNKNOWN_ASSET_IMPL))
79	}
80	fn withdraw(_asset: &Asset, _from: &Location) -> DispatchResult {
81		Err(DispatchError::Other(NO_UNKNOWN_ASSET_IMPL))
82	}
83}
84
85// Default implementation for xTokens::MinXcmFee
86pub struct DisabledParachainFee;
87impl GetByKey<Location, Option<u128>> for DisabledParachainFee {
88	fn get(_key: &Location) -> Option<u128> {
89		None
90	}
91}