pallet_xcm_benchmarks/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
20
21extern crate alloc;
22
23use alloc::vec::Vec;
24use codec::Encode;
25use frame_benchmarking::{account, BenchmarkError};
26use xcm::latest::prelude::*;
27use xcm_builder::EnsureDelivery;
28use xcm_executor::{traits::ConvertLocation, Config as XcmConfig};
29
30pub mod fungible;
31pub mod generic;
32
33#[cfg(test)]
34mod mock;
35
36pub trait Config: frame_system::Config {
38 type XcmConfig: XcmConfig;
43
44 type AccountIdConverter: ConvertLocation<Self::AccountId>;
46
47 type DeliveryHelper: EnsureDelivery;
49
50 fn valid_destination() -> Result<Location, BenchmarkError>;
53
54 fn worst_case_holding(depositable_count: u32) -> Assets;
58}
59
60const SEED: u32 = 0;
61
62pub type ExecutorOf<T> = xcm_executor::XcmExecutor<<T as Config>::XcmConfig>;
64pub type RuntimeCallOf<T> = <T as frame_system::Config>::RuntimeCall;
66pub type AssetTransactorOf<T> = <<T as Config>::XcmConfig as XcmConfig>::AssetTransactor;
68pub type XcmCallOf<T> = <<T as Config>::XcmConfig as XcmConfig>::RuntimeCall;
70
71pub fn generate_holding_assets(max_assets: u32) -> Assets {
72 let fungibles_amount: u128 = 100;
73 let holding_fungibles = max_assets / 2;
74 let holding_non_fungibles = max_assets - holding_fungibles - 1; (0..holding_fungibles)
77 .map(|i| {
78 Asset {
79 id: AssetId(GeneralIndex(i as u128).into()),
80 fun: Fungible(fungibles_amount * (i + 1) as u128), }
82 .into()
83 })
84 .chain(core::iter::once(Asset { id: AssetId(Here.into()), fun: Fungible(u128::MAX) }))
86 .chain((0..holding_non_fungibles).map(|i| Asset {
88 id: AssetId(GeneralIndex(i as u128).into()),
89 fun: NonFungible(asset_instance_from(i)),
90 }))
91 .collect::<Vec<_>>()
92 .into()
93}
94
95pub fn asset_instance_from(x: u32) -> AssetInstance {
96 let bytes = x.encode();
97 let mut instance = [0u8; 4];
98 instance.copy_from_slice(&bytes);
99 AssetInstance::Array4(instance)
100}
101
102pub fn new_executor<T: Config>(origin: Location) -> ExecutorOf<T> {
103 ExecutorOf::<T>::new(origin, [0; 32])
104}
105
106fn account_id_junction<T: frame_system::Config>(index: u32) -> Junction {
108 let account: T::AccountId = account("account", index, SEED);
109 let mut encoded = account.encode();
110 encoded.resize(32, 0u8);
111 let mut id = [0u8; 32];
112 id.copy_from_slice(&encoded);
113 Junction::AccountId32 { network: None, id }
114}
115
116pub fn account_and_location<T: Config>(index: u32) -> (T::AccountId, Location) {
117 let location: Location = account_id_junction::<T>(index).into();
118 let account = T::AccountIdConverter::convert_location(&location).unwrap();
119
120 (account, location)
121}