referrerpolicy=no-referrer-when-downgrade

pallet_contracts_mock_network/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Substrate.
3
4// Substrate is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Substrate is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Substrate.  If not, see <http://www.gnu.org/licenses/>.
16
17pub mod mocks;
18pub mod parachain;
19pub mod primitives;
20pub mod relay_chain;
21
22#[cfg(test)]
23mod tests;
24
25use crate::primitives::{AccountId, UNITS};
26pub use pallet_contracts::test_utils::{ALICE, BOB};
27use sp_runtime::BuildStorage;
28use xcm::latest::prelude::*;
29use xcm_executor::traits::ConvertLocation;
30pub use xcm_simulator::TestExt;
31use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};
32
33// Accounts
34pub const ADMIN: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]);
35
36// Balances
37pub const INITIAL_BALANCE: u128 = 1_000_000_000 * UNITS;
38
39decl_test_parachain! {
40	pub struct ParaA {
41		Runtime = parachain::Runtime,
42		XcmpMessageHandler = parachain::MsgQueue,
43		DmpMessageHandler = parachain::MsgQueue,
44		new_ext = para_ext(1),
45	}
46}
47
48decl_test_relay_chain! {
49	pub struct Relay {
50		Runtime = relay_chain::Runtime,
51		RuntimeCall = relay_chain::RuntimeCall,
52		RuntimeEvent = relay_chain::RuntimeEvent,
53		XcmConfig = relay_chain::XcmConfig,
54		MessageQueue = relay_chain::MessageQueue,
55		System = relay_chain::System,
56		new_ext = relay_ext(),
57	}
58}
59
60decl_test_network! {
61	pub struct MockNet {
62		relay_chain = Relay,
63		parachains = vec![
64			(1, ParaA),
65		],
66	}
67}
68
69pub fn relay_sovereign_account_id() -> AccountId {
70	let location: Location = (Parent,).into();
71	parachain::SovereignAccountOf::convert_location(&location).unwrap()
72}
73
74pub fn parachain_sovereign_account_id(para: u32) -> AccountId {
75	let location: Location = (Parachain(para),).into();
76	relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
77}
78
79pub fn parachain_account_sovereign_account_id(
80	para: u32,
81	who: sp_runtime::AccountId32,
82) -> AccountId {
83	let location: Location = (
84		Parachain(para),
85		AccountId32 { network: Some(relay_chain::RelayNetwork::get()), id: who.into() },
86	)
87		.into();
88	relay_chain::SovereignAccountOf::convert_location(&location).unwrap()
89}
90
91pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
92	use parachain::{MsgQueue, Runtime, System};
93
94	let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
95
96	pallet_balances::GenesisConfig::<Runtime> {
97		balances: vec![
98			(ALICE, INITIAL_BALANCE),
99			(relay_sovereign_account_id(), INITIAL_BALANCE),
100			(BOB, INITIAL_BALANCE),
101		],
102		..Default::default()
103	}
104	.assimilate_storage(&mut t)
105	.unwrap();
106
107	pallet_assets::GenesisConfig::<Runtime> {
108		assets: vec![
109			(0u128, ADMIN, false, 1u128), // Create derivative asset for relay's native token
110		],
111		metadata: Default::default(),
112		accounts: vec![
113			(0u128, ALICE, INITIAL_BALANCE),
114			(0u128, relay_sovereign_account_id(), INITIAL_BALANCE),
115		],
116		next_asset_id: None,
117	}
118	.assimilate_storage(&mut t)
119	.unwrap();
120
121	let mut ext = sp_io::TestExternalities::new(t);
122	ext.execute_with(|| {
123		sp_tracing::try_init_simple();
124		System::set_block_number(1);
125		MsgQueue::set_para_id(para_id.into());
126	});
127	ext
128}
129
130pub fn relay_ext() -> sp_io::TestExternalities {
131	use relay_chain::{Runtime, System};
132
133	let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
134
135	pallet_balances::GenesisConfig::<Runtime> {
136		balances: vec![
137			(ALICE, INITIAL_BALANCE),
138			(parachain_sovereign_account_id(1), INITIAL_BALANCE),
139			(parachain_account_sovereign_account_id(1, ALICE), INITIAL_BALANCE),
140		],
141		..Default::default()
142	}
143	.assimilate_storage(&mut t)
144	.unwrap();
145
146	let mut ext = sp_io::TestExternalities::new(t);
147	ext.execute_with(|| {
148		System::set_block_number(1);
149	});
150	ext
151}
152
153pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
154pub type ParachainBalances = pallet_balances::Pallet<parachain::Runtime>;