moonbeam_runtime_common/tests/
xcm.rs#[macro_export]
macro_rules! generate_common_xcm_tests {
($runtime: ident) => {
#[cfg(test)]
pub mod common_xcm_tests {
use crate::common::{ExtBuilder, ALICE};
use cumulus_primitives_core::ExecuteXcm;
use frame_support::assert_ok;
use frame_support::traits::fungible::Inspect;
use frame_support::traits::EnsureOrigin;
use moonbeam_core_primitives::{AccountId, Balance};
use parity_scale_codec::Encode;
use sp_weights::Weight;
use xcm::{
latest::{prelude::AccountKey20, Assets as XcmAssets, Xcm},
VersionedAssets, VersionedLocation, VersionedXcm,
};
use $runtime::{
xcm_config::SelfReserve, Balances, PolkadotXcm, Runtime, RuntimeEvent,
RuntimeOrigin, System,
};
pub(crate) fn last_events(n: usize) -> Vec<RuntimeEvent> {
System::events()
.into_iter()
.map(|e| e.event)
.rev()
.take(n)
.rev()
.collect()
}
#[test]
fn claim_assets_works() {
const INITIAL_BALANCE: Balance = 10_000_000_000_000_000_000;
const SEND_AMOUNT: Balance = 1_000_000_000_000_000_000;
let alice = AccountId::from(ALICE);
let balances = vec![(alice, INITIAL_BALANCE)];
ExtBuilder::default()
.with_balances(balances)
.build()
.execute_with(|| {
let assets = XcmAssets::from((SelfReserve::get(), SEND_AMOUNT));
let trapping_program =
Xcm::builder_unsafe().withdraw_asset(assets.clone()).build();
let origin_location =
<Runtime as pallet_xcm::Config>::ExecuteXcmOrigin::ensure_origin(
RuntimeOrigin::signed(alice),
)
.expect("qed");
let message = Box::new(VersionedXcm::V5(trapping_program));
let mut hash = message.using_encoded(sp_io::hashing::blake2_256);
let message = (*message).try_into().expect("qed");
let _ = <Runtime as pallet_xcm::Config>::XcmExecutor::prepare_and_execute(
origin_location,
message,
&mut hash,
Weight::MAX,
Weight::MAX,
);
assert_eq!(
Balances::total_balance(&alice),
INITIAL_BALANCE - SEND_AMOUNT
);
assert!(last_events(2).iter().any(|evt| matches!(
evt,
RuntimeEvent::PolkadotXcm(pallet_xcm::Event::AssetsTrapped { .. })
)));
assert_ok!(PolkadotXcm::claim_assets(
RuntimeOrigin::signed(alice),
Box::new(VersionedAssets::V5(assets)),
Box::new(VersionedLocation::V5(
AccountKey20 {
network: None,
key: alice.clone().into()
}
.into()
)),
));
assert_eq!(Balances::total_balance(&alice), INITIAL_BALANCE);
});
}
}
};
}