xcm_simulator_example/
lib.rs
1#[allow(unexpected_cfgs)]
19mod parachain;
20
21#[allow(unexpected_cfgs)]
23mod relay_chain;
24
25#[cfg(test)]
26mod tests;
27
28use sp_runtime::BuildStorage;
29use sp_tracing;
30use xcm::prelude::*;
31use xcm_executor::traits::ConvertLocation;
32use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};
33
34pub const ALICE: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([1u8; 32]);
35pub const INITIAL_BALANCE: u128 = 1_000_000_000;
36
37decl_test_parachain! {
38 pub struct ParaA {
39 Runtime = parachain::Runtime,
40 XcmpMessageHandler = parachain::MsgQueue,
41 DmpMessageHandler = parachain::MsgQueue,
42 new_ext = para_ext(1),
43 }
44}
45
46decl_test_parachain! {
47 pub struct ParaB {
48 Runtime = parachain::Runtime,
49 XcmpMessageHandler = parachain::MsgQueue,
50 DmpMessageHandler = parachain::MsgQueue,
51 new_ext = para_ext(2),
52 }
53}
54
55decl_test_relay_chain! {
56 pub struct Relay {
57 Runtime = relay_chain::Runtime,
58 RuntimeCall = relay_chain::RuntimeCall,
59 RuntimeEvent = relay_chain::RuntimeEvent,
60 XcmConfig = relay_chain::XcmConfig,
61 MessageQueue = relay_chain::MessageQueue,
62 System = relay_chain::System,
63 new_ext = relay_ext(),
64 }
65}
66
67decl_test_network! {
68 pub struct MockNet {
69 relay_chain = Relay,
70 parachains = vec![
71 (1, ParaA),
72 (2, ParaB),
73 ],
74 }
75}
76
77pub fn parent_account_id() -> parachain::AccountId {
78 let location = (Parent,);
79 parachain::location_converter::LocationConverter::convert_location(&location.into()).unwrap()
80}
81
82pub fn child_account_id(para: u32) -> relay_chain::AccountId {
83 let location = (Parachain(para),);
84 relay_chain::location_converter::LocationConverter::convert_location(&location.into()).unwrap()
85}
86
87pub fn child_account_account_id(para: u32, who: sp_runtime::AccountId32) -> relay_chain::AccountId {
88 let location = (Parachain(para), AccountId32 { network: None, id: who.into() });
89 relay_chain::location_converter::LocationConverter::convert_location(&location.into()).unwrap()
90}
91
92pub fn sibling_account_account_id(para: u32, who: sp_runtime::AccountId32) -> parachain::AccountId {
93 let location = (Parent, Parachain(para), AccountId32 { network: None, id: who.into() });
94 parachain::location_converter::LocationConverter::convert_location(&location.into()).unwrap()
95}
96
97pub fn parent_account_account_id(who: sp_runtime::AccountId32) -> parachain::AccountId {
98 let location = (Parent, AccountId32 { network: None, id: who.into() });
99 parachain::location_converter::LocationConverter::convert_location(&location.into()).unwrap()
100}
101
102pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
103 use parachain::{MsgQueue, Runtime, System};
104
105 let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
106
107 pallet_balances::GenesisConfig::<Runtime> {
108 balances: vec![(ALICE, INITIAL_BALANCE), (parent_account_id(), INITIAL_BALANCE)],
109 ..Default::default()
110 }
111 .assimilate_storage(&mut t)
112 .unwrap();
113
114 let mut ext = sp_io::TestExternalities::new(t);
115 ext.execute_with(|| {
116 sp_tracing::try_init_simple();
117 System::set_block_number(1);
118 MsgQueue::set_para_id(para_id.into());
119 });
120 ext
121}
122
123pub fn relay_ext() -> sp_io::TestExternalities {
124 use relay_chain::{Runtime, RuntimeOrigin, System, Uniques};
125
126 let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
127
128 pallet_balances::GenesisConfig::<Runtime> {
129 balances: vec![
130 (ALICE, INITIAL_BALANCE),
131 (child_account_id(1), INITIAL_BALANCE),
132 (child_account_id(2), INITIAL_BALANCE),
133 ],
134 ..Default::default()
135 }
136 .assimilate_storage(&mut t)
137 .unwrap();
138
139 let mut ext = sp_io::TestExternalities::new(t);
140 ext.execute_with(|| {
141 System::set_block_number(1);
142 assert_eq!(Uniques::force_create(RuntimeOrigin::root(), 1, ALICE, true), Ok(()));
143 assert_eq!(Uniques::mint(RuntimeOrigin::signed(ALICE), 1, 42, child_account_id(1)), Ok(()));
144 });
145 ext
146}
147
148pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
149pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;