moonbase_runtime/
asset_config.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
17//! Asset configuration for Moonbase.
18//!
19
20use super::{AccountId, AssetId, Runtime, FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX};
21
22use moonkit_xcm_primitives::AccountIdAssetIdConversion;
23
24use sp_core::H160;
25
26use sp_std::{
27	convert::{From, Into},
28	prelude::*,
29};
30
31// Instruct how to go from an H160 to an AssetID
32// We just take the lowest 128 bits
33impl AccountIdAssetIdConversion<AccountId, AssetId> for Runtime {
34	/// The way to convert an account to assetId is by ensuring that the prefix is 0XFFFFFFFF
35	/// and by taking the lowest 128 bits as the assetId
36	fn account_to_asset_id(account: AccountId) -> Option<(Vec<u8>, AssetId)> {
37		let h160_account: H160 = account.into();
38		let mut data = [0u8; 16];
39		let (prefix_part, id_part) = h160_account.as_fixed_bytes().split_at(4);
40		if prefix_part == FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX {
41			data.copy_from_slice(id_part);
42			let asset_id: AssetId = u128::from_be_bytes(data).into();
43			Some((prefix_part.to_vec(), asset_id))
44		} else {
45			None
46		}
47	}
48
49	// The opposite conversion
50	fn asset_id_to_account(prefix: &[u8], asset_id: AssetId) -> AccountId {
51		let mut data = [0u8; 20];
52		data[0..4].copy_from_slice(prefix);
53		data[4..20].copy_from_slice(&asset_id.to_be_bytes());
54		AccountId::from(data)
55	}
56}