use parachains_common::AccountId;
use xcm::{prelude::*, DoubleEncoded};
pub fn xcm_transact_paid_execution(
call: DoubleEncoded<()>,
origin_kind: OriginKind,
fees: Asset,
beneficiary: AccountId,
) -> VersionedXcm<()> {
let weight_limit = WeightLimit::Unlimited;
VersionedXcm::from(Xcm(vec![
WithdrawAsset(fees.clone().into()),
BuyExecution { fees, weight_limit },
Transact { origin_kind, call, fallback_max_weight: None },
RefundSurplus,
DepositAsset {
assets: All.into(),
beneficiary: Location {
parents: 0,
interior: [AccountId32 { network: None, id: beneficiary.into() }].into(),
},
},
]))
}
pub fn xcm_transact_unpaid_execution(
call: DoubleEncoded<()>,
origin_kind: OriginKind,
) -> VersionedXcm<()> {
let weight_limit = WeightLimit::Unlimited;
let check_origin = None;
VersionedXcm::from(Xcm(vec![
UnpaidExecution { weight_limit, check_origin },
Transact { origin_kind, call, fallback_max_weight: None },
]))
}
pub fn non_fee_asset(assets: &Assets, fee_idx: usize) -> Option<(Location, u128)> {
let asset = assets.inner().into_iter().enumerate().find(|a| a.0 != fee_idx)?.1.clone();
let asset_amount = match asset.fun {
Fungible(amount) => amount,
_ => return None,
};
Some((asset.id.0, asset_amount))
}
pub fn get_amount_from_versioned_assets(assets: VersionedAssets) -> u128 {
let latest_assets: Assets = assets.try_into().unwrap();
let Fungible(amount) = latest_assets.inner()[0].fun else {
unreachable!("asset is non-fungible");
};
amount
}