-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathmod.rs
197 lines (167 loc) · 4.8 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#![cfg(test)]
use super::*;
use crate as orml_xtokens;
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_io::TestExternalities;
use sp_runtime::AccountId32;
use cumulus_primitives_core::ParaId;
use polkadot_parachain::primitives::{AccountIdConversion, Sibling};
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain};
pub mod para;
pub mod relay;
pub mod tests;
pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);
pub const BOB: AccountId32 = AccountId32::new([2u8; 32]);
pub const DEFAULT: AccountId32 = AccountId32::new([0u8; 32]);
pub const INITIAL_BALANCE: u128 = 1_000;
pub fn para_a_account() -> AccountId32 {
ParaId::from(1).into_account()
}
pub fn para_b_account() -> AccountId32 {
ParaId::from(2).into_account()
}
pub fn sibling_a_account() -> AccountId32 {
use sp_runtime::traits::AccountIdConversion;
Sibling::from(1).into_account()
}
pub fn sibling_b_account() -> AccountId32 {
use sp_runtime::traits::AccountIdConversion;
Sibling::from(2).into_account()
}
pub fn sibling_c_account() -> AccountId32 {
use sp_runtime::traits::AccountIdConversion;
Sibling::from(3).into_account()
}
#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum CurrencyId {
/// Relay chain token.
R,
/// Parachain A token.
A,
/// Parachain B token.
B,
}
pub struct CurrencyIdConvert;
impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
match id {
CurrencyId::R => Some(Parent.into()),
CurrencyId::A => Some((Parent, Parachain(1), GeneralKey("A".into())).into()),
CurrencyId::B => Some((Parent, Parachain(2), GeneralKey("B".into())).into()),
}
}
}
impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(l: MultiLocation) -> Option<CurrencyId> {
let a: Vec<u8> = "A".into();
let b: Vec<u8> = "B".into();
if l == MultiLocation::parent() {
return Some(CurrencyId::R);
}
match l {
MultiLocation { parents, interior } if parents == 1 => match interior {
X2(Parachain(1), GeneralKey(k)) if k == a => Some(CurrencyId::A),
X2(Parachain(2), GeneralKey(k)) if k == b => Some(CurrencyId::B),
_ => None,
},
_ => None,
}
}
}
impl Convert<MultiAsset, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(a: MultiAsset) -> Option<CurrencyId> {
if let MultiAsset {
fun: Fungible(_),
id: Concrete(id),
} = a
{
Self::convert(id)
} else {
Option::None
}
}
}
pub type Balance = u128;
pub type Amount = i128;
decl_test_parachain! {
pub struct ParaA {
Runtime = para::Runtime,
XcmpMessageHandler = para::XcmpQueue,
DmpMessageHandler = para::DmpQueue,
new_ext = para_ext(1),
}
}
decl_test_parachain! {
pub struct ParaB {
Runtime = para::Runtime,
XcmpMessageHandler = para::XcmpQueue,
DmpMessageHandler = para::DmpQueue,
new_ext = para_ext(2),
}
}
decl_test_parachain! {
pub struct ParaC {
Runtime = para::Runtime,
XcmpMessageHandler = para::XcmpQueue,
DmpMessageHandler = para::DmpQueue,
new_ext = para_ext(3),
}
}
decl_test_relay_chain! {
pub struct Relay {
Runtime = relay::Runtime,
XcmConfig = relay::XcmConfig,
new_ext = relay_ext(),
}
}
decl_test_network! {
pub struct TestNet {
relay_chain = Relay,
parachains = vec![
(1, ParaA),
(2, ParaB),
(3, ParaC),
],
}
}
pub type RelayBalances = pallet_balances::Pallet<relay::Runtime>;
pub type ParaBalances = pallet_balances::Pallet<para::Runtime>;
pub type ParaTokens = orml_tokens::Pallet<para::Runtime>;
pub type ParaXTokens = orml_xtokens::Pallet<para::Runtime>;
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay::Runtime>;
pub type ParachainPalletXcm = pallet_xcm::Pallet<para::Runtime>;
pub fn para_ext(para_id: u32) -> TestExternalities {
use para::{Runtime, System};
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();
let parachain_info_config = parachain_info::GenesisConfig {
parachain_id: para_id.into(),
};
<parachain_info::GenesisConfig as GenesisBuild<Runtime, _>>::assimilate_storage(¶chain_info_config, &mut t)
.unwrap();
orml_tokens::GenesisConfig::<Runtime> {
balances: vec![(ALICE, CurrencyId::R, INITIAL_BALANCE)],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext = TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
pub fn relay_ext() -> sp_io::TestExternalities {
use relay::{Runtime, System};
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(ALICE, INITIAL_BALANCE)],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}