emulated_integration_tests_common/
xcm_helpers.rs1use parachains_common::AccountId;
18
19use 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
29pub 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 RefundSurplus,
43 DepositAsset {
44 assets: All.into(),
45 beneficiary: Location {
46 parents: 0,
47 interior: [AccountId32 { network: None, id: beneficiary.into() }].into(),
48 },
49 },
50 ]))
51}
52
53pub fn xcm_transact_unpaid_execution(
55 call: DoubleEncoded<()>,
56 origin_kind: OriginKind,
57) -> VersionedXcm<()> {
58 let weight_limit = WeightLimit::Unlimited;
59 let check_origin = None;
60
61 VersionedXcm::from(Xcm(vec![
62 UnpaidExecution { weight_limit, check_origin },
63 Transact { origin_kind, call, fallback_max_weight: None },
64 ]))
65}
66
67pub fn non_fee_asset(assets: &Assets, fee_idx: usize) -> Option<(Location, u128)> {
69 let asset = assets.inner().into_iter().enumerate().find(|a| a.0 != fee_idx)?.1.clone();
70 let asset_amount = match asset.fun {
71 Fungible(amount) => amount,
72 _ => return None,
73 };
74 Some((asset.id.0, asset_amount))
75}
76
77pub fn fee_asset(assets: &Assets, fee_idx: usize) -> Option<(Location, u128)> {
79 let asset = assets.get(fee_idx)?;
80 let asset_amount = match asset.fun {
81 Fungible(amount) => amount,
82 _ => return None,
83 };
84 Some((asset.id.0.clone(), asset_amount))
85}
86
87pub fn get_amount_from_versioned_assets(assets: VersionedAssets) -> u128 {
88 let latest_assets: Assets = assets.try_into().unwrap();
89 let Fungible(amount) = latest_assets.inner()[0].fun else {
90 unreachable!("asset is non-fungible");
91 };
92 amount
93}
94
95pub fn find_mq_processed_id<C: Chain>() -> Option<H256>
97where
98 <C as Chain>::Runtime: pallet_message_queue::Config,
99 C::RuntimeEvent: TryInto<pallet_message_queue::Event<<C as Chain>::Runtime>>,
100{
101 C::events().into_iter().find_map(|event| {
102 if let Ok(pallet_message_queue::Event::Processed { id, .. }) = event.try_into() {
103 Some(id)
104 } else {
105 None
106 }
107 })
108}
109
110pub fn find_xcm_sent_message_id<
112 C: Chain<RuntimeEvent = <<C as Chain>::Runtime as pallet_xcm::Config>::RuntimeEvent>,
113>() -> Option<XcmHash>
114where
115 C::Runtime: pallet_xcm::Config,
116 C::RuntimeEvent: TryInto<pallet_xcm::Event<C::Runtime>>,
117{
118 pallet_xcm::xcm_helpers::find_xcm_sent_message_id::<<C as Chain>::Runtime>(C::events())
119}
120
121pub fn dispatch_whitelisted_call_with_preimage<T>(
123 call: T::RuntimeCall,
124 origin: T::RuntimeOrigin,
125) -> DispatchResultWithPostInfo
126where
127 T: Chain,
128 T::Runtime: pallet_whitelist::Config,
129 T::RuntimeCall: From<pallet_whitelist::Call<T::Runtime>>
130 + Into<<T::Runtime as pallet_whitelist::Config>::RuntimeCall>
131 + Dispatchable<RuntimeOrigin = T::RuntimeOrigin, PostInfo = PostDispatchInfo>,
132{
133 T::execute_with(|| {
134 let whitelist_call: T::RuntimeCall =
135 pallet_whitelist::Call::<T::Runtime>::dispatch_whitelisted_call_with_preimage {
136 call: Box::new(call.into()),
137 }
138 .into();
139 whitelist_call.dispatch(origin)
140 })
141}
142
143pub fn build_xcm_send_authorize_upgrade_call<T, D>(
146 location: Location,
147 code_hash: &H256,
148 fallback_max_weight: Option<Weight>,
149) -> T::RuntimeCall
150where
151 T: Chain,
152 T::Runtime: pallet_xcm::Config,
153 T::RuntimeCall: Encode + From<pallet_xcm::Call<T::Runtime>>,
154 D: Chain,
155 D::Runtime: frame_system::Config<Hash = H256>,
156 D::RuntimeCall: Encode + From<frame_system::Call<D::Runtime>>,
157{
158 let transact_call: D::RuntimeCall =
159 frame_system::Call::authorize_upgrade { code_hash: *code_hash }.into();
160
161 let call: T::RuntimeCall = pallet_xcm::Call::send {
162 dest: bx!(VersionedLocation::from(location)),
163 message: bx!(VersionedXcm::from(Xcm(vec![
164 UnpaidExecution { weight_limit: Unlimited, check_origin: None },
165 Transact {
166 origin_kind: OriginKind::Superuser,
167 fallback_max_weight,
168 call: transact_call.encode().into(),
169 }
170 ]))),
171 }
172 .into();
173 call
174}
175
176pub fn call_hash_of<T>(call: &T::RuntimeCall) -> H256
178where
179 T: Chain,
180 T::Runtime: frame_system::Config<Hash = H256>,
181 T::RuntimeCall: Encode,
182{
183 <T::Runtime as frame_system::Config>::Hashing::hash_of(&call)
184}