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 xcm::{prelude::*, DoubleEncoded};
21
22/// Helper method to build a XCM with a `Transact` instruction and paying for its execution
23pub fn xcm_transact_paid_execution(
24	call: DoubleEncoded<()>,
25	origin_kind: OriginKind,
26	fees: Asset,
27	beneficiary: AccountId,
28) -> VersionedXcm<()> {
29	let weight_limit = WeightLimit::Unlimited;
30
31	VersionedXcm::from(Xcm(vec![
32		WithdrawAsset(fees.clone().into()),
33		BuyExecution { fees, weight_limit },
34		Transact { origin_kind, call, fallback_max_weight: None },
35		RefundSurplus,
36		DepositAsset {
37			assets: All.into(),
38			beneficiary: Location {
39				parents: 0,
40				interior: [AccountId32 { network: None, id: beneficiary.into() }].into(),
41			},
42		},
43	]))
44}
45
46/// Helper method to build a XCM with a `Transact` instruction without paying for its execution
47pub fn xcm_transact_unpaid_execution(
48	call: DoubleEncoded<()>,
49	origin_kind: OriginKind,
50) -> VersionedXcm<()> {
51	let weight_limit = WeightLimit::Unlimited;
52	let check_origin = None;
53
54	VersionedXcm::from(Xcm(vec![
55		UnpaidExecution { weight_limit, check_origin },
56		Transact { origin_kind, call, fallback_max_weight: None },
57	]))
58}
59
60/// Helper method to get the non-fee asset used in multiple assets transfer
61pub fn non_fee_asset(assets: &Assets, fee_idx: usize) -> Option<(Location, u128)> {
62	let asset = assets.inner().into_iter().enumerate().find(|a| a.0 != fee_idx)?.1.clone();
63	let asset_amount = match asset.fun {
64		Fungible(amount) => amount,
65		_ => return None,
66	};
67	Some((asset.id.0, asset_amount))
68}
69
70pub fn get_amount_from_versioned_assets(assets: VersionedAssets) -> u128 {
71	let latest_assets: Assets = assets.try_into().unwrap();
72	let Fungible(amount) = latest_assets.inner()[0].fun else {
73		unreachable!("asset is non-fungible");
74	};
75	amount
76}