staging_xcm_builder/matches_token.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17//! Various implementations for the `MatchesFungible` trait.
18
19use core::marker::PhantomData;
20use frame_support::traits::Get;
21use xcm::latest::{
22 Asset, AssetId, AssetInstance,
23 Fungibility::{Fungible, NonFungible},
24 Location,
25};
26use xcm_executor::traits::{MatchesFungible, MatchesNonFungible};
27
28/// Converts a `Asset` into balance `B` if its id is equal to that
29/// given by `T`'s `Get`.
30///
31/// # Example
32///
33/// ```
34/// use xcm::latest::{Location, Parent};
35/// use staging_xcm_builder::IsConcrete;
36/// use xcm_executor::traits::MatchesFungible;
37///
38/// frame_support::parameter_types! {
39/// pub TargetLocation: Location = Parent.into();
40/// }
41///
42/// # fn main() {
43/// let asset = (Parent, 999).into();
44/// // match `asset` if it is a concrete asset in `TargetLocation`.
45/// assert_eq!(<IsConcrete<TargetLocation> as MatchesFungible<u128>>::matches_fungible(&asset), Some(999));
46/// # }
47/// ```
48pub struct IsConcrete<T>(PhantomData<T>);
49impl<T: Get<Location>, B: TryFrom<u128>> MatchesFungible<B> for IsConcrete<T> {
50 fn matches_fungible(a: &Asset) -> Option<B> {
51 match (&a.id, &a.fun) {
52 (AssetId(ref id), Fungible(ref amount)) if id == &T::get() => (*amount).try_into().ok(),
53 _ => None,
54 }
55 }
56}
57impl<T: Get<Location>, I: TryFrom<AssetInstance>> MatchesNonFungible<I> for IsConcrete<T> {
58 fn matches_nonfungible(a: &Asset) -> Option<I> {
59 match (&a.id, &a.fun) {
60 (AssetId(id), NonFungible(instance)) if id == &T::get() => (*instance).try_into().ok(),
61 _ => None,
62 }
63 }
64}