xcm_primitives/
asset_id_conversions.rs

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
17use sp_runtime::traits::MaybeEquivalence;
18use sp_std::marker::PhantomData;
19use xcm::{v3::Location, IntoVersion};
20use 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
27pub struct AsAssetType<AssetId, AssetType, AssetIdInfoGetter>(
28	PhantomData<(AssetId, AssetType, AssetIdInfoGetter)>,
29);
30impl<AssetId, AssetType, AssetIdInfoGetter> MaybeEquivalence<Location, AssetId>
31	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
32where
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}
44impl<AssetId, AssetType, AssetIdInfoGetter> MaybeEquivalence<xcm::v5::Location, AssetId>
45	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
46where
47	AssetId: Clone,
48	AssetType: From<Location> + Into<Option<Location>> + Clone,
49	AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
50{
51	fn convert(id: &xcm::v5::Location) -> Option<AssetId> {
52		match xcm::VersionedLocation::V5(id.clone()).into_version(xcm::v3::VERSION) {
53			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	}
58
59	fn convert_back(what: &AssetId) -> Option<xcm::v5::Location> {
60		let v3_location: Location =
61			AssetIdInfoGetter::get_asset_type(what.clone()).and_then(Into::into)?;
62
63		// Convert v3 Location to v5 Location
64		let versioned = xcm::VersionedLocation::V3(v3_location);
65		match versioned.into_version(xcm::latest::VERSION) {
66			Ok(xcm::VersionedLocation::V5(loc)) => Some(loc),
67			_ => None,
68		}
69	}
70}
71impl<AssetId, AssetType, AssetIdInfoGetter> ConvertLocation<AssetId>
72	for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
73where
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
85pub 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
93/// This trait ensure we can convert AccountIds to CurrencyIds
94/// We will require Runtime to have this trait implemented
95pub trait AccountIdToCurrencyId<Account, CurrencyId> {
96	// Get assetId from account
97	fn account_to_currency_id(account: Account) -> Option<CurrencyId>;
98}