1
// Copyright 2019-2025 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, IntoVersion};
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::v5::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
231
	fn convert(id: &xcm::v5::Location) -> Option<AssetId> {
52
231
		match xcm::VersionedLocation::V5(id.clone()).into_version(xcm::v3::VERSION) {
53
231
			Ok(xcm::VersionedLocation::V3(loc)) => AssetIdInfoGetter::get_asset_id(loc.into()),
54
			// Any other version or conversion error returns an error
55
			_ => None,
56
		}
57
231
	}
58

            
59
61
	fn convert_back(what: &AssetId) -> Option<xcm::v5::Location> {
60
61
		let v3_location: Location =
61
61
			AssetIdInfoGetter::get_asset_type(what.clone()).and_then(Into::into)?;
62

            
63
		// Convert v3 Location to v5 Location
64
61
		let versioned = xcm::VersionedLocation::V3(v3_location);
65
61
		match versioned.into_version(xcm::latest::VERSION) {
66
61
			Ok(xcm::VersionedLocation::V5(loc)) => Some(loc),
67
			_ => None,
68
		}
69
61
	}
70
}
71
impl<AssetId, AssetType, AssetIdInfoGetter> ConvertLocation<AssetId>
72
	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
73
where
74
	AssetId: Clone,
75
	AssetType: From<Location> + Into<Option<Location>> + Clone,
76
	AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
77
{
78
	fn convert_location(id: &xcm::v5::Location) -> Option<AssetId> {
79
		// Use the same conversion logic from MaybeEquivalence implementation
80
		Self::convert(id)
81
	}
82
}
83

            
84
/// Defines the trait to obtain a generic AssetType from a generic AssetId and vice versa
85
pub trait AssetTypeGetter<AssetId, AssetType> {
86
	// Get asset type from assetId
87
	fn get_asset_type(asset_id: AssetId) -> Option<AssetType>;
88

            
89
	// Get assetId from assetType
90
	fn get_asset_id(asset_type: AssetType) -> Option<AssetId>;
91

            
92
	// Set assetId and assetType
93
	#[cfg(feature = "runtime-benchmarks")]
94
	fn set_asset_type_asset_id(asset_type: AssetType, asset_id: AssetId);
95
}
96

            
97
/// This trait ensure we can convert AccountIds to CurrencyIds
98
/// We will require Runtime to have this trait implemented
99
pub trait AccountIdToCurrencyId<Account, CurrencyId> {
100
	// Get assetId from account
101
	fn account_to_currency_id(account: Account) -> Option<CurrencyId>;
102
}