@@ -7,12 +7,16 @@ use scale_info::TypeInfo;
7
7
use serde:: { Deserialize , Serialize } ;
8
8
use sp_io:: TestExternalities ;
9
9
use sp_runtime:: AccountId32 ;
10
+ use xcm_executor:: traits:: WeightTrader ;
11
+ use xcm_executor:: Assets ;
10
12
11
13
use xcm_simulator:: { decl_test_network, decl_test_parachain, decl_test_relay_chain} ;
12
14
13
15
pub mod para;
14
16
pub mod para_relative_view;
17
+ pub mod para_teleport;
15
18
pub mod relay;
19
+ pub mod teleport_currency_adapter;
16
20
17
21
pub const ALICE : AccountId32 = AccountId32 :: new ( [ 0u8 ; 32 ] ) ;
18
22
pub const BOB : AccountId32 = AccountId32 :: new ( [ 1u8 ; 32 ] ) ;
@@ -32,6 +36,8 @@ pub enum CurrencyId {
32
36
B1 ,
33
37
/// Parachain B B2 token
34
38
B2 ,
39
+ /// Parachain C token
40
+ C ,
35
41
/// Parachain D token
36
42
D ,
37
43
}
@@ -46,6 +52,7 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
46
52
CurrencyId :: B => Some ( ( Parent , Parachain ( 2 ) , GeneralKey ( "B" . into ( ) ) ) . into ( ) ) ,
47
53
CurrencyId :: B1 => Some ( ( Parent , Parachain ( 2 ) , GeneralKey ( "B1" . into ( ) ) ) . into ( ) ) ,
48
54
CurrencyId :: B2 => Some ( ( Parent , Parachain ( 2 ) , GeneralKey ( "B2" . into ( ) ) ) . into ( ) ) ,
55
+ CurrencyId :: C => Some ( ( Parent , Parachain ( 3 ) , GeneralKey ( "C" . into ( ) ) ) . into ( ) ) ,
49
56
CurrencyId :: D => Some ( ( Parent , Parachain ( 4 ) , GeneralKey ( "D" . into ( ) ) ) . into ( ) ) ,
50
57
}
51
58
}
@@ -57,6 +64,7 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
57
64
let b: Vec < u8 > = "B" . into ( ) ;
58
65
let b1: Vec < u8 > = "B1" . into ( ) ;
59
66
let b2: Vec < u8 > = "B2" . into ( ) ;
67
+ let c: Vec < u8 > = "C" . into ( ) ;
60
68
let d: Vec < u8 > = "D" . into ( ) ;
61
69
if l == MultiLocation :: parent ( ) {
62
70
return Some ( CurrencyId :: R ) ;
@@ -68,6 +76,7 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
68
76
X2 ( Parachain ( 2 ) , GeneralKey ( k) ) if k == b => Some ( CurrencyId :: B ) ,
69
77
X2 ( Parachain ( 2 ) , GeneralKey ( k) ) if k == b1 => Some ( CurrencyId :: B1 ) ,
70
78
X2 ( Parachain ( 2 ) , GeneralKey ( k) ) if k == b2 => Some ( CurrencyId :: B2 ) ,
79
+ X2 ( Parachain ( 3 ) , GeneralKey ( k) ) if k == c => Some ( CurrencyId :: C ) ,
71
80
X2 ( Parachain ( 4 ) , GeneralKey ( k) ) if k == d => Some ( CurrencyId :: D ) ,
72
81
_ => None ,
73
82
} ,
@@ -77,6 +86,7 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
77
86
X1 ( GeneralKey ( k) ) if k == a1 => Some ( CurrencyId :: A1 ) ,
78
87
X1 ( GeneralKey ( k) ) if k == b1 => Some ( CurrencyId :: B1 ) ,
79
88
X1 ( GeneralKey ( k) ) if k == b2 => Some ( CurrencyId :: B2 ) ,
89
+ X1 ( GeneralKey ( k) ) if k == c => Some ( CurrencyId :: C ) ,
80
90
X1 ( GeneralKey ( k) ) if k == d => Some ( CurrencyId :: D ) ,
81
91
_ => None ,
82
92
} ,
@@ -121,10 +131,10 @@ decl_test_parachain! {
121
131
122
132
decl_test_parachain ! {
123
133
pub struct ParaC {
124
- Runtime = para :: Runtime ,
125
- XcmpMessageHandler = para :: XcmpQueue ,
126
- DmpMessageHandler = para :: DmpQueue ,
127
- new_ext = para_ext ( 3 ) ,
134
+ Runtime = para_teleport :: Runtime ,
135
+ XcmpMessageHandler = para_teleport :: XcmpQueue ,
136
+ DmpMessageHandler = para_teleport :: DmpQueue ,
137
+ new_ext = para_teleport_ext ( 3 ) ,
128
138
}
129
139
}
130
140
@@ -166,6 +176,8 @@ pub type ParaXTokens = orml_xtokens::Pallet<para::Runtime>;
166
176
pub type ParaRelativeTokens = orml_tokens:: Pallet < para_relative_view:: Runtime > ;
167
177
pub type ParaRelativeXTokens = orml_xtokens:: Pallet < para_relative_view:: Runtime > ;
168
178
179
+ pub type ParaTeleportTokens = orml_tokens:: Pallet < para_teleport:: Runtime > ;
180
+
169
181
pub fn para_ext ( para_id : u32 ) -> TestExternalities {
170
182
use para:: { Runtime , System } ;
171
183
@@ -190,6 +202,30 @@ pub fn para_ext(para_id: u32) -> TestExternalities {
190
202
ext
191
203
}
192
204
205
+ pub fn para_teleport_ext ( para_id : u32 ) -> TestExternalities {
206
+ use para_teleport:: { Runtime , System } ;
207
+
208
+ let mut t = frame_system:: GenesisConfig :: default ( )
209
+ . build_storage :: < Runtime > ( )
210
+ . unwrap ( ) ;
211
+
212
+ let parachain_info_config = parachain_info:: GenesisConfig {
213
+ parachain_id : para_id. into ( ) ,
214
+ } ;
215
+ <parachain_info:: GenesisConfig as GenesisBuild < Runtime , _ > >:: assimilate_storage ( & parachain_info_config, & mut t)
216
+ . unwrap ( ) ;
217
+
218
+ orml_tokens:: GenesisConfig :: < Runtime > {
219
+ balances : vec ! [ ( ALICE , CurrencyId :: R , 1_000 ) ] ,
220
+ }
221
+ . assimilate_storage ( & mut t)
222
+ . unwrap ( ) ;
223
+
224
+ let mut ext = TestExternalities :: new ( t) ;
225
+ ext. execute_with ( || System :: set_block_number ( 1 ) ) ;
226
+ ext
227
+ }
228
+
193
229
pub fn relay_ext ( ) -> sp_io:: TestExternalities {
194
230
use relay:: { Runtime , System } ;
195
231
@@ -207,3 +243,46 @@ pub fn relay_ext() -> sp_io::TestExternalities {
207
243
ext. execute_with ( || System :: set_block_number ( 1 ) ) ;
208
244
ext
209
245
}
246
+
247
+ /// A trader who believes all tokens are created equal to "weight" of any chain,
248
+ /// which is not true, but good enough to mock the fee payment of XCM execution.
249
+ ///
250
+ /// This mock will always trade `n` amount of weight to `n` amount of tokens.
251
+ pub struct AllTokensAreCreatedEqualToWeight ( MultiLocation ) ;
252
+ impl WeightTrader for AllTokensAreCreatedEqualToWeight {
253
+ fn new ( ) -> Self {
254
+ Self ( MultiLocation :: parent ( ) )
255
+ }
256
+
257
+ fn buy_weight ( & mut self , weight : Weight , payment : Assets ) -> Result < Assets , XcmError > {
258
+ let asset_id = payment
259
+ . fungible
260
+ . iter ( )
261
+ . next ( )
262
+ . expect ( "Payment must be something; qed" )
263
+ . 0 ;
264
+ let required = MultiAsset {
265
+ id : asset_id. clone ( ) ,
266
+ fun : Fungible ( weight as u128 ) ,
267
+ } ;
268
+
269
+ if let MultiAsset {
270
+ fun : _,
271
+ id : Concrete ( ref id) ,
272
+ } = & required
273
+ {
274
+ self . 0 = id. clone ( ) ;
275
+ }
276
+
277
+ let unused = payment. checked_sub ( required) . map_err ( |_| XcmError :: TooExpensive ) ?;
278
+ Ok ( unused)
279
+ }
280
+
281
+ fn refund_weight ( & mut self , weight : Weight ) -> Option < MultiAsset > {
282
+ if weight. is_zero ( ) {
283
+ None
284
+ } else {
285
+ Some ( ( self . 0 . clone ( ) , weight as u128 ) . into ( ) )
286
+ }
287
+ }
288
+ }
0 commit comments