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
//! Moonbase Runtime Xcm Tests
18

            
19
use crate::xcm_mock::*;
20
use parity_scale_codec::{Decode, Encode};
21
use sp_io::hashing::blake2_256;
22
use sp_runtime::traits::Convert;
23
use sp_weights::Weight;
24
use xcm::latest::prelude::{Asset, AssetId, Fungibility};
25
use xcm::{IntoVersion, VersionedLocation};
26

            
27
pub mod helpers;
28

            
29
mod automatic_versioning;
30
mod evm_accounts;
31
mod hrmp;
32
mod para_asset;
33
mod relay_asset;
34
mod statemint;
35
mod transact_derivative;
36
mod transact_signed;
37
mod transact_sovereign;
38

            
39
// Helper to derive accountIds
40
7
pub fn derivative_account_id(who: sp_runtime::AccountId32, index: u16) -> sp_runtime::AccountId32 {
41
7
	let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256);
42
7
	sp_runtime::AccountId32::decode(&mut &entropy[..]).expect("valid account id")
43
7
}
44

            
45
35
pub fn add_supported_asset(
46
35
	asset_type: parachain::AssetType,
47
35
	units_per_second: u128,
48
35
) -> Result<(), ()> {
49
35
	let parachain::AssetType::Xcm(location_v3) = asset_type;
50
35
	let VersionedLocation::V5(location_v5) = VersionedLocation::V3(location_v3)
51
35
		.into_version(xcm::latest::VERSION)
52
35
		.map_err(|_| ())?
53
	else {
54
		return Err(());
55
	};
56
	use frame_support::weights::WeightToFee as _;
57
35
	let native_amount_per_second: u128 =
58
35
		<parachain::Runtime as pallet_xcm_weight_trader::Config>::WeightToFee::weight_to_fee(
59
35
			&Weight::from_parts(
60
35
				frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND,
61
35
				0,
62
35
			),
63
35
		)
64
35
		.try_into()
65
35
		.map_err(|_| ())?;
66
35
	let precision_factor = 10u128.pow(pallet_xcm_weight_trader::RELATIVE_PRICE_DECIMALS);
67
35
	let relative_price: u128 = if units_per_second > 0u128 {
68
10
		native_amount_per_second
69
10
			.saturating_mul(precision_factor)
70
10
			.saturating_div(units_per_second)
71
	} else {
72
25
		0u128
73
	};
74
35
	pallet_xcm_weight_trader::SupportedAssets::<parachain::Runtime>::insert(
75
35
		location_v5,
76
35
		(true, relative_price),
77
35
	);
78
35
	Ok(())
79
35
}
80

            
81
25
pub fn currency_to_asset(currency_id: parachain::CurrencyId, amount: u128) -> Asset {
82
25
	Asset {
83
25
		id: AssetId(
84
25
			<parachain::Runtime as pallet_xcm_transactor::Config>::CurrencyIdToLocation::convert(
85
25
				currency_id,
86
25
			)
87
25
			.unwrap(),
88
25
		),
89
25
		fun: Fungibility::Fungible(amount),
90
25
	}
91
25
}