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