referrerpolicy=no-referrer-when-downgrade

asset_test_utils/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
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}