asset_test_utils/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3
4// Cumulus 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// Cumulus 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 Cumulus.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Module contains predefined test-case scenarios for `Runtime` with various assets.
18
19pub mod test_cases;
20pub mod test_cases_over_bridge;
21pub mod xcm_helpers;
22
23use frame_support::traits::ProcessMessageError;
24pub use parachains_runtimes_test_utils::*;
25use std::fmt::Debug;
26
27use xcm::latest::prelude::*;
28use xcm_builder::{CreateMatcher, MatchXcm};
29
30/// Given a message, a sender, and a destination, it returns the delivery fees
31fn get_fungible_delivery_fees<S: SendXcm>(destination: Location, message: Xcm<()>) -> u128 {
32	let Ok((_, delivery_fees)) = validate_send::<S>(destination, message) else {
33		unreachable!("message can be sent; qed")
34	};
35	if let Some(delivery_fee) = delivery_fees.inner().first() {
36		let Fungible(delivery_fee_amount) = delivery_fee.fun else {
37			unreachable!("asset is fungible; qed");
38		};
39		delivery_fee_amount
40	} else {
41		0
42	}
43}
44
45/// Helper function to verify `xcm` contains all relevant instructions expected on destination
46/// chain as part of a reserve-asset-transfer.
47pub(crate) fn assert_matches_reserve_asset_deposited_instructions<RuntimeCall: Debug>(
48	xcm: &mut Xcm<RuntimeCall>,
49	expected_reserve_assets_deposited: &Assets,
50	expected_beneficiary: &Location,
51) {
52	let _ = xcm
53		.0
54		.matcher()
55		.skip_inst_while(|inst| !matches!(inst, ReserveAssetDeposited(..)))
56		.expect("no instruction ReserveAssetDeposited?")
57		.match_next_inst(|instr| match instr {
58			ReserveAssetDeposited(reserve_assets) => {
59				assert_eq!(reserve_assets, expected_reserve_assets_deposited);
60				Ok(())
61			},
62			_ => Err(ProcessMessageError::BadFormat),
63		})
64		.expect("expected instruction ReserveAssetDeposited")
65		.match_next_inst(|instr| match instr {
66			ClearOrigin => Ok(()),
67			_ => Err(ProcessMessageError::BadFormat),
68		})
69		.expect("expected instruction ClearOrigin")
70		.match_next_inst(|instr| match instr {
71			BuyExecution { .. } => Ok(()),
72			_ => Err(ProcessMessageError::BadFormat),
73		})
74		.expect("expected instruction BuyExecution")
75		.match_next_inst(|instr| match instr {
76			DepositAsset { assets: _, beneficiary } if beneficiary == expected_beneficiary =>
77				Ok(()),
78			_ => Err(ProcessMessageError::BadFormat),
79		})
80		.expect("expected instruction DepositAsset");
81}