pallet_xcm_benchmarks/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
20
21extern crate alloc;
22
23use codec::Encode;
24use frame_benchmarking::{account, BenchmarkError};
25use xcm::latest::prelude::*;
26use xcm_builder::EnsureDelivery;
27use xcm_executor::{traits::ConvertLocation, AssetsInHolding, Config as XcmConfig};
28
29pub mod fungible;
30pub mod generic;
31
32#[cfg(test)]
33mod mock;
34
35pub trait Config: frame_system::Config {
37 type XcmConfig: XcmConfig;
42
43 type AccountIdConverter: ConvertLocation<Self::AccountId>;
45
46 type DeliveryHelper: EnsureDelivery;
48
49 fn valid_destination() -> Result<Location, BenchmarkError>;
52
53 fn worst_case_holding(depositable_count: u32) -> AssetsInHolding;
60}
61
62const SEED: u32 = 0;
63
64#[cfg(any(test, feature = "runtime-benchmarks"))]
67pub use xcm_executor::test_helpers::MockCredit;
68
69pub type ExecutorOf<T> = xcm_executor::XcmExecutor<<T as Config>::XcmConfig>;
71pub type RuntimeCallOf<T> = <T as frame_system::Config>::RuntimeCall;
73pub type AssetTransactorOf<T> = <<T as Config>::XcmConfig as XcmConfig>::AssetTransactor;
75pub type XcmCallOf<T> = <<T as Config>::XcmConfig as XcmConfig>::RuntimeCall;
77
78#[cfg(any(test, feature = "runtime-benchmarks"))]
79pub fn generate_holding_assets(max_assets: u32) -> AssetsInHolding {
80 use xcm_executor::AssetsInHolding;
81 let fungibles_amount: u128 = 100;
82 let holding_fungibles = max_assets / 2;
83 let holding_non_fungibles = max_assets - holding_fungibles - 1; let mut holding = AssetsInHolding::new();
86
87 for i in 0..holding_fungibles {
89 let asset_id = AssetId(GeneralIndex(i as u128).into());
90 let amount = fungibles_amount * (i + 1) as u128;
91 holding.fungible.insert(asset_id, alloc::boxed::Box::new(MockCredit(amount)));
92 }
93
94 holding
96 .fungible
97 .insert(AssetId(Here.into()), alloc::boxed::Box::new(MockCredit(u128::MAX)));
98
99 for i in 0..holding_non_fungibles {
101 let asset_id = AssetId(GeneralIndex(i as u128).into());
102 let instance = asset_instance_from(i);
103 holding.non_fungible.insert((asset_id, instance));
104 }
105
106 holding
107}
108
109#[cfg(any(test, feature = "runtime-benchmarks"))]
110pub fn asset_instance_from(x: u32) -> AssetInstance {
111 let bytes = x.encode();
112 let mut instance = [0u8; 4];
113 instance.copy_from_slice(&bytes);
114 AssetInstance::Array4(instance)
115}
116
117pub fn new_executor<T: Config>(origin: Location) -> ExecutorOf<T> {
118 ExecutorOf::<T>::new(origin, [0; 32])
119}
120
121fn account_id_junction<T: frame_system::Config>(index: u32) -> Junction {
123 let account: T::AccountId = account("account", index, SEED);
124 let mut encoded = account.encode();
125 encoded.resize(32, 0u8);
126 let mut id = [0u8; 32];
127 id.copy_from_slice(&encoded);
128 Junction::AccountId32 { network: None, id }
129}
130
131pub fn account_and_location<T: Config>(index: u32) -> (T::AccountId, Location) {
132 let location: Location = account_id_junction::<T>(index).into();
133 let account = T::AccountIdConverter::convert_location(&location).unwrap();
134
135 (account, location)
136}