#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use frame_benchmarking::{account, BenchmarkError};
use sp_std::prelude::*;
use xcm::latest::prelude::*;
use xcm_builder::EnsureDelivery;
use xcm_executor::{traits::ConvertLocation, Config as XcmConfig};
pub mod fungible;
pub mod generic;
#[cfg(test)]
mod mock;
pub trait Config: frame_system::Config {
type XcmConfig: XcmConfig;
type AccountIdConverter: ConvertLocation<Self::AccountId>;
type DeliveryHelper: EnsureDelivery;
fn valid_destination() -> Result<Location, BenchmarkError>;
fn worst_case_holding(depositable_count: u32) -> Assets;
}
const SEED: u32 = 0;
pub type ExecutorOf<T> = xcm_executor::XcmExecutor<<T as Config>::XcmConfig>;
pub type OverArchingCallOf<T> = <T as frame_system::Config>::RuntimeCall;
pub type AssetTransactorOf<T> = <<T as Config>::XcmConfig as XcmConfig>::AssetTransactor;
pub type XcmCallOf<T> = <<T as Config>::XcmConfig as XcmConfig>::RuntimeCall;
pub fn mock_worst_case_holding(depositable_count: u32, max_assets: u32) -> Assets {
let fungibles_amount: u128 = 100;
let holding_fungibles = max_assets / 2 - depositable_count;
let holding_non_fungibles = holding_fungibles;
(0..holding_fungibles)
.map(|i| {
Asset {
id: AssetId(GeneralIndex(i as u128).into()),
fun: Fungible(fungibles_amount * i as u128),
}
.into()
})
.chain(core::iter::once(Asset { id: AssetId(Here.into()), fun: Fungible(u128::MAX) }))
.chain((0..holding_non_fungibles).map(|i| Asset {
id: AssetId(GeneralIndex(i as u128).into()),
fun: NonFungible(asset_instance_from(i)),
}))
.collect::<Vec<_>>()
.into()
}
pub fn asset_instance_from(x: u32) -> AssetInstance {
let bytes = x.encode();
let mut instance = [0u8; 4];
instance.copy_from_slice(&bytes);
AssetInstance::Array4(instance)
}
pub fn new_executor<T: Config>(origin: Location) -> ExecutorOf<T> {
ExecutorOf::<T>::new(origin, [0; 32])
}
fn account_id_junction<T: frame_system::Config>(index: u32) -> Junction {
let account: T::AccountId = account("account", index, SEED);
let mut encoded = account.encode();
encoded.resize(32, 0u8);
let mut id = [0u8; 32];
id.copy_from_slice(&encoded);
Junction::AccountId32 { network: None, id }
}
pub fn account_and_location<T: Config>(index: u32) -> (T::AccountId, Location) {
let location: Location = account_id_junction::<T>(index).into();
let account = T::AccountIdConverter::convert_location(&location).unwrap();
(account, location)
}