assets_common/
local_and_foreign_assets.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
17use core::marker::PhantomData;
18use frame_support::traits::Get;
19use sp_runtime::{
20	traits::{Convert, MaybeEquivalence},
21	Either,
22	Either::{Left, Right},
23};
24use xcm::latest::Location;
25
26/// Information about reserve locations for Foreign Assets.
27#[derive(
28	Clone,
29	Debug,
30	Encode,
31	Decode,
32	DecodeWithMemTracking,
33	PartialEq,
34	Eq,
35	scale_info::TypeInfo,
36	MaxEncodedLen,
37	serde::Serialize,
38	serde::Deserialize,
39)]
40pub struct ForeignAssetReserveData {
41	/// A location acting as trusted reserve.
42	pub reserve: Location,
43	/// Whether asset is teleportable between local chain and `reserve`.
44	pub teleportable: bool,
45}
46
47impl From<(Location, bool)> for ForeignAssetReserveData {
48	fn from((reserve, teleportable): (Location, bool)) -> Self {
49		ForeignAssetReserveData { reserve, teleportable }
50	}
51}
52
53/// Converts a given [`Location`] to [`Either::Left`] when equal to `Target`, or
54/// [`Either::Right`] otherwise.
55///
56/// Suitable for use as a `Criterion` with [`frame_support::traits::tokens::fungible::UnionOf`].
57pub struct TargetFromLeft<Target, L = Location>(PhantomData<(Target, L)>);
58impl<Target: Get<L>, L: PartialEq + Eq> Convert<L, Either<(), L>> for TargetFromLeft<Target, L> {
59	fn convert(l: L) -> Either<(), L> {
60		Target::get().eq(&l).then(|| Left(())).map_or(Right(l), |n| n)
61	}
62}
63
64/// Converts a given [`Location`] to [`Either::Left`] based on the `Equivalence` criteria.
65/// Returns [`Either::Right`] if not equivalent.
66///
67/// Suitable for use as a `Criterion` with [`frame_support::traits::tokens::fungibles::UnionOf`].
68pub struct LocalFromLeft<Equivalence, AssetId, L = Location>(
69	PhantomData<(Equivalence, AssetId, L)>,
70);
71impl<Equivalence, AssetId, L> Convert<L, Either<AssetId, L>>
72	for LocalFromLeft<Equivalence, AssetId, L>
73where
74	Equivalence: MaybeEquivalence<L, AssetId>,
75{
76	fn convert(l: L) -> Either<AssetId, L> {
77		match Equivalence::convert(&l) {
78			Some(id) => Left(id),
79			None => Right(l),
80		}
81	}
82}