referrerpolicy=no-referrer-when-downgrade

emulated_integration_tests_common/
xcm_helpers.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16// Cumulus
17use parachains_common::AccountId;
18
19// Polkadot
20use sp_core::H256;
21use xcm::{prelude::*, DoubleEncoded};
22use xcm_emulator::Chain;
23
24use crate::impls::{bx, Encode};
25use frame_support::dispatch::{DispatchResultWithPostInfo, PostDispatchInfo};
26use sp_runtime::traits::{Dispatchable, Hash};
27use xcm::{VersionedLocation, VersionedXcm};
28
29/// Helper method to build a XCM with a `Transact` instruction and paying for its execution
30pub fn xcm_transact_paid_execution(
31	call: DoubleEncoded<()>,
32	origin_kind: OriginKind,
33	fees: Asset,
34	beneficiary: AccountId,
35) -> VersionedXcm<()> {
36	let weight_limit = WeightLimit::Unlimited;
37
38	VersionedXcm::from(Xcm(vec![
39		WithdrawAsset(fees.clone().into()),
40		BuyExecution { fees, weight_limit },
41		Transact { origin_kind, call, fallback_max_weight: None },
42		ExpectTransactStatus(MaybeErrorCode::Success),
43		RefundSurplus,
44		DepositAsset {
45			assets: All.into(),
46			beneficiary: Location {
47				parents: 0,
48				interior: [AccountId32 { network: None, id: beneficiary.into() }].into(),
49			},
50		},
51	]))
52}
53
54/// Helper method to build a XCM with a `Transact` instruction without paying for its execution
55pub fn xcm_transact_unpaid_execution(
56	call: DoubleEncoded<()>,
57	origin_kind: OriginKind,
58) -> VersionedXcm<()> {
59	let weight_limit = WeightLimit::Unlimited;
60	let check_origin = None;
61
62	VersionedXcm::from(Xcm(vec![
63		UnpaidExecution { weight_limit, check_origin },
64		Transact { origin_kind, call, fallback_max_weight: None },
65		ExpectTransactStatus(MaybeErrorCode::Success),
66	]))
67}
68
69/// Helper method to get the non-fee asset used in multiple assets transfer
70pub fn non_fee_asset(assets: &Assets, fee_idx: usize) -> Option<(Location, u128)> {
71	let asset = assets.inner().into_iter().enumerate().find(|a| a.0 != fee_idx)?.1.clone();
72	let asset_amount = match asset.fun {
73		Fungible(amount) => amount,
74		_ => return None,
75	};
76	Some((asset.id.0, asset_amount))
77}
78
79/// Helper method to get the fee asset used in multiple assets transfer
80pub fn fee_asset(assets: &Assets, fee_idx: usize) -> Option<(Location, u128)> {
81	let asset = assets.get(fee_idx)?;
82	let asset_amount = match asset.fun {
83		Fungible(amount) => amount,
84		_ => return None,
85	};
86	Some((asset.id.0.clone(), asset_amount))
87}
88
89pub fn get_amount_from_versioned_assets(assets: VersionedAssets) -> u128 {
90	let latest_assets: Assets = assets.try_into().unwrap();
91	let Fungible(amount) = latest_assets.inner()[0].fun else {
92		unreachable!("asset is non-fungible");
93	};
94	amount
95}
96
97fn to_mq_processed_id<C: Chain>(event: C::RuntimeEvent) -> Option<H256>
98where
99	<C as Chain>::Runtime: pallet_message_queue::Config,
100	C::RuntimeEvent: TryInto<pallet_message_queue::Event<<C as Chain>::Runtime>>,
101{
102	if let Ok(pallet_message_queue::Event::Processed { id, .. }) = event.try_into() {
103		Some(id)
104	} else {
105		None
106	}
107}
108
109/// Helper method to find all `Event::Processed` IDs from the chain's events.
110pub fn find_all_mq_processed_ids<C: Chain>() -> Vec<H256>
111where
112	<C as Chain>::Runtime: pallet_message_queue::Config,
113	C::RuntimeEvent: TryInto<pallet_message_queue::Event<<C as Chain>::Runtime>>,
114{
115	C::events().into_iter().filter_map(to_mq_processed_id::<C>).collect()
116}
117
118/// Helper method to find the ID of the first `Event::Processed` event in the chain's events.
119pub fn find_mq_processed_id<C: Chain>() -> Option<H256>
120where
121	<C as Chain>::Runtime: pallet_message_queue::Config,
122	C::RuntimeEvent: TryInto<pallet_message_queue::Event<<C as Chain>::Runtime>>,
123{
124	C::events().into_iter().find_map(to_mq_processed_id::<C>)
125}
126
127/// Helper method to find the message ID of the first `Event::Sent` event in the chain's events.
128pub fn find_xcm_sent_message_id<
129	C: Chain<RuntimeEvent = <<C as Chain>::Runtime as pallet_xcm::Config>::RuntimeEvent>,
130>() -> Option<XcmHash>
131where
132	C::Runtime: pallet_xcm::Config,
133	C::RuntimeEvent: TryInto<pallet_xcm::Event<C::Runtime>>,
134{
135	pallet_xcm::xcm_helpers::find_xcm_sent_message_id::<<C as Chain>::Runtime>(C::events())
136}
137
138/// Wraps a runtime call in a whitelist preimage call and dispatches it
139pub fn dispatch_whitelisted_call_with_preimage<T>(
140	call: T::RuntimeCall,
141	origin: T::RuntimeOrigin,
142) -> DispatchResultWithPostInfo
143where
144	T: Chain,
145	T::Runtime: pallet_whitelist::Config,
146	T::RuntimeCall: From<pallet_whitelist::Call<T::Runtime>>
147		+ Into<<T::Runtime as pallet_whitelist::Config>::RuntimeCall>
148		+ Dispatchable<RuntimeOrigin = T::RuntimeOrigin, PostInfo = PostDispatchInfo>,
149{
150	T::execute_with(|| {
151		let whitelist_call: T::RuntimeCall =
152			pallet_whitelist::Call::<T::Runtime>::dispatch_whitelisted_call_with_preimage {
153				call: Box::new(call.into()),
154			}
155			.into();
156		whitelist_call.dispatch(origin)
157	})
158}
159
160/// Builds a `pallet_xcm::send` call to authorize an upgrade at the provided location,
161/// wrapped in an unpaid XCM `Transact` with `OriginKind::Superuser`.
162pub fn build_xcm_send_authorize_upgrade_call<T, D>(
163	location: Location,
164	code_hash: &H256,
165	fallback_max_weight: Option<Weight>,
166) -> T::RuntimeCall
167where
168	T: Chain,
169	T::Runtime: pallet_xcm::Config,
170	T::RuntimeCall: Encode + From<pallet_xcm::Call<T::Runtime>>,
171	D: Chain,
172	D::Runtime: frame_system::Config<Hash = H256>,
173	D::RuntimeCall: Encode + From<frame_system::Call<D::Runtime>>,
174{
175	let transact_call: D::RuntimeCall =
176		frame_system::Call::authorize_upgrade { code_hash: *code_hash }.into();
177
178	let call: T::RuntimeCall = pallet_xcm::Call::send {
179		dest: bx!(VersionedLocation::from(location)),
180		message: bx!(VersionedXcm::from(Xcm(vec![
181			UnpaidExecution { weight_limit: Unlimited, check_origin: None },
182			Transact {
183				origin_kind: OriginKind::Superuser,
184				fallback_max_weight,
185				call: transact_call.encode().into(),
186			}
187		]))),
188	}
189	.into();
190	call
191}
192
193/// Encodes a runtime call and returns its H256 hash
194pub fn call_hash_of<T>(call: &T::RuntimeCall) -> H256
195where
196	T: Chain,
197	T::Runtime: frame_system::Config<Hash = H256>,
198	T::RuntimeCall: Encode,
199{
200	<T::Runtime as frame_system::Config>::Hashing::hash_of(&call)
201}