1
// Copyright 2019-2022 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam 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
// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
use sp_runtime::traits::MaybeEquivalence;
18
use sp_std::marker::PhantomData;
19
use xcm::v3::Location;
20
use xcm_executor::traits::ConvertLocation;
21

            
22
/// Converter struct implementing `AssetIdConversion` converting a numeric asset ID
23
/// (must be `TryFrom/TryInto<u128>`) into a Location Value and vice versa through
24
/// an intermediate generic type AssetType.
25
/// The trait bounds enforce is that the AssetTypeGetter trait is also implemented for
26
/// AssetIdInfoGetter
27
pub struct AsAssetType<AssetId, AssetType, AssetIdInfoGetter>(
28
	PhantomData<(AssetId, AssetType, AssetIdInfoGetter)>,
29
);
30
impl<AssetId, AssetType, AssetIdInfoGetter> MaybeEquivalence<Location, AssetId>
31
	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
32
where
33
	AssetId: Clone,
34
	AssetType: From<Location> + Into<Option<Location>> + Clone,
35
	AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
36
{
37
	fn convert(id: &Location) -> Option<AssetId> {
38
		AssetIdInfoGetter::get_asset_id(id.clone().into())
39
	}
40
	fn convert_back(what: &AssetId) -> Option<Location> {
41
		AssetIdInfoGetter::get_asset_type(what.clone()).and_then(Into::into)
42
	}
43
}
44
impl<AssetId, AssetType, AssetIdInfoGetter> MaybeEquivalence<xcm::v4::Location, AssetId>
45
	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
46
where
47
	AssetId: Clone,
48
	AssetType: From<Location> + Into<Option<Location>> + Clone,
49
	AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
50
{
51
197
	fn convert(id: &xcm::v4::Location) -> Option<AssetId> {
52
197
		let v3_location =
53
197
			xcm_builder::WithLatestLocationConverter::<xcm::v3::Location>::convert(id)?;
54
197
		AssetIdInfoGetter::get_asset_id(v3_location.clone().into())
55
197
	}
56
94
	fn convert_back(what: &AssetId) -> Option<xcm::v4::Location> {
57
94
		let v3_location: Location =
58
94
			AssetIdInfoGetter::get_asset_type(what.clone()).and_then(Into::into)?;
59
94
		xcm_builder::WithLatestLocationConverter::convert_back(&v3_location)
60
94
	}
61
}
62
impl<AssetId, AssetType, AssetIdInfoGetter> ConvertLocation<AssetId>
63
	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
64
where
65
	AssetId: Clone,
66
	AssetType: From<Location> + Into<Option<Location>> + Clone,
67
	AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
68
{
69
	fn convert_location(id: &xcm::v4::Location) -> Option<AssetId> {
70
		let v3_location =
71
			xcm_builder::WithLatestLocationConverter::<xcm::v3::Location>::convert(id)?;
72
		AssetIdInfoGetter::get_asset_id(v3_location.clone().into())
73
	}
74
}
75

            
76
/// Defines the trait to obtain a generic AssetType from a generic AssetId and vice versa
77
pub trait AssetTypeGetter<AssetId, AssetType> {
78
	// Get asset type from assetId
79
	fn get_asset_type(asset_id: AssetId) -> Option<AssetType>;
80

            
81
	// Get assetId from assetType
82
	fn get_asset_id(asset_type: AssetType) -> Option<AssetId>;
83

            
84
	// Set assetId and assetType
85
	#[cfg(feature = "runtime-benchmarks")]
86
	fn set_asset_type_asset_id(asset_type: AssetType, asset_id: AssetId);
87
}
88

            
89
/// This trait ensure we can convert AccountIds to CurrencyIds
90
/// We will require Runtime to have this trait implemented
91
pub trait AccountIdToCurrencyId<Account, CurrencyId> {
92
	// Get assetId from account
93
	fn account_to_currency_id(account: Account) -> Option<CurrencyId>;
94
}