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 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
54pub 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
69pub 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
79pub 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
109pub 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
118pub 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
127pub 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
138pub 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
160pub 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
193pub 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}