Skip to content
Closed
Prev Previous commit
Next Next commit
wip
  • Loading branch information
girazoki committed Dec 2, 2021
commit fddb0916b95f7ec666b3834db914da7407ab54b9
35 changes: 34 additions & 1 deletion runtime/moonbase/tests/xcm_mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

pub mod parachain;
pub mod relay_chain;

pub mod statemine_like;
use cumulus_primitives_core::ParaId;
use polkadot_parachain::primitives::AccountIdConversion;
use sp_runtime::AccountId32;
Expand Down Expand Up @@ -55,6 +55,15 @@ decl_test_parachain! {
}
}

decl_test_parachain! {
pub struct Statemine {
Runtime = statemine_like::Runtime,
XcmpMessageHandler = statemine_like::MsgQueue,
DmpMessageHandler = statemine_like::MsgQueue,
new_ext = statemine_ext(4),
}
}

decl_test_relay_chain! {
pub struct Relay {
Runtime = relay_chain::Runtime,
Expand Down Expand Up @@ -96,6 +105,26 @@ pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
});
ext
}
pub fn statemine_ext(para_id: u32) -> sp_io::TestExternalities {
use statemine_like::{MsgQueue, Runtime, System};

let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();

pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(RELAYALICE.into(), INITIAL_BALANCE)],
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
MsgQueue::set_para_id(para_id.into());
});
ext
}

pub fn relay_ext() -> sp_io::TestExternalities {
use relay_chain::{Runtime, System};
Expand All @@ -116,8 +145,12 @@ pub fn relay_ext() -> sp_io::TestExternalities {
}

pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
pub type StatemineChainPalletXcm = pallet_xcm::Pallet<statemine_like::Runtime>;

pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
pub type Assets = pallet_assets::Pallet<parachain::Runtime>;
pub type StatemineAssets = pallet_assets::Pallet<statemine_like::Runtime>;

pub type Treasury = pallet_treasury::Pallet<parachain::Runtime>;
pub type AssetManager = pallet_asset_manager::Pallet<parachain::Runtime>;
pub type XTokens = orml_xtokens::Pallet<parachain::Runtime>;
Expand Down
45 changes: 45 additions & 0 deletions runtime/moonbase/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,51 @@ pub type XcmFeesToAccount_ = xcm_primitives::XcmFeesToAccount<
XcmFeesAccount,
>;

parameter_types! {
pub const Local: MultiLocation = Here.into();
}
/// Converter struct implementing `AssetIdConversion` converting a numeric asset ID (must be `TryFrom/TryInto<u128>`) into
/// a `GeneralIndex` junction, prefixed by some `MultiLocation` value. The `MultiLocation` value will typically be a
/// `PalletInstance` junction.
///
pub struct LocalAsPrefixedGeneralIndex<Prefix, AssetId, ConvertAssetId>(
sp_std::marker::PhantomData<(Prefix, AssetId, ConvertAssetId)>,
);
impl<
Prefix: Get<MultiLocation>,
AssetId: Clone,
ConvertAssetId: xcm_executor::traits::Convert<u128, AssetId>,
> xcm_executor::traits::Convert<MultiLocation, AssetId>
for LocalAsPrefixedGeneralIndex<Prefix, AssetId, ConvertAssetId>
{
fn convert_ref(id: impl sp_std::borrow::Borrow<MultiLocation>) -> Result<AssetId, ()> {
let prefix = Prefix::get();
let id = id.borrow();
if prefix.parent_count() != id.parent_count()
|| prefix
.interior()
.iter()
.enumerate()
.any(|(index, junction)| id.interior().at(index) != Some(junction))
{
return Err(());
}
match id.interior().at(prefix.interior().len()) {
Some(Junction::GeneralIndex(id)) => ConvertAssetId::convert_ref(id),
_ => Err(()),
}
}
fn reverse_ref(what: impl sp_std::borrow::Borrow<AssetId>) -> Result<MultiLocation, ()> {
let mut location = Prefix::get();
let id = ConvertAssetId::reverse_ref(what)?;
location
.push_interior(Junction::GeneralIndex(id))
.map_err(|_| ())?;
Ok(location)
}
}

pub type StatemintLike = LocalAsPrefixedGeneralIndex<Local, AssetId, JustTry>;
parameter_types! {
// We cannot skip the native trader for some specific tests, so we will have to work with
// a native trader that charges same number of units as weight
Expand Down
Loading