@@ -7,12 +7,16 @@ use scale_info::TypeInfo;
77use serde:: { Deserialize , Serialize } ;
88use sp_io:: TestExternalities ;
99use sp_runtime:: AccountId32 ;
10+ use xcm_executor:: traits:: WeightTrader ;
11+ use xcm_executor:: Assets ;
1012
1113use xcm_simulator:: { decl_test_network, decl_test_parachain, decl_test_relay_chain} ;
1214
1315pub mod para;
1416pub mod para_relative_view;
17+ pub mod para_teleport;
1518pub mod relay;
19+ pub mod teleport_currency_adapter;
1620
1721pub const ALICE : AccountId32 = AccountId32 :: new ( [ 0u8 ; 32 ] ) ;
1822pub const BOB : AccountId32 = AccountId32 :: new ( [ 1u8 ; 32 ] ) ;
@@ -32,6 +36,8 @@ pub enum CurrencyId {
3236 B1 ,
3337 /// Parachain B B2 token
3438 B2 ,
39+ /// Parachain C token
40+ C ,
3541 /// Parachain D token
3642 D ,
3743}
@@ -46,6 +52,7 @@ impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
4652 CurrencyId :: B => Some ( ( Parent , Parachain ( 2 ) , GeneralKey ( "B" . into ( ) ) ) . into ( ) ) ,
4753 CurrencyId :: B1 => Some ( ( Parent , Parachain ( 2 ) , GeneralKey ( "B1" . into ( ) ) ) . into ( ) ) ,
4854 CurrencyId :: B2 => Some ( ( Parent , Parachain ( 2 ) , GeneralKey ( "B2" . into ( ) ) ) . into ( ) ) ,
55+ CurrencyId :: C => Some ( ( Parent , Parachain ( 3 ) , GeneralKey ( "C" . into ( ) ) ) . into ( ) ) ,
4956 CurrencyId :: D => Some ( ( Parent , Parachain ( 4 ) , GeneralKey ( "D" . into ( ) ) ) . into ( ) ) ,
5057 }
5158 }
@@ -57,6 +64,7 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
5764 let b: Vec < u8 > = "B" . into ( ) ;
5865 let b1: Vec < u8 > = "B1" . into ( ) ;
5966 let b2: Vec < u8 > = "B2" . into ( ) ;
67+ let c: Vec < u8 > = "C" . into ( ) ;
6068 let d: Vec < u8 > = "D" . into ( ) ;
6169 if l == MultiLocation :: parent ( ) {
6270 return Some ( CurrencyId :: R ) ;
@@ -68,6 +76,7 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
6876 X2 ( Parachain ( 2 ) , GeneralKey ( k) ) if k == b => Some ( CurrencyId :: B ) ,
6977 X2 ( Parachain ( 2 ) , GeneralKey ( k) ) if k == b1 => Some ( CurrencyId :: B1 ) ,
7078 X2 ( Parachain ( 2 ) , GeneralKey ( k) ) if k == b2 => Some ( CurrencyId :: B2 ) ,
79+ X2 ( Parachain ( 3 ) , GeneralKey ( k) ) if k == c => Some ( CurrencyId :: C ) ,
7180 X2 ( Parachain ( 4 ) , GeneralKey ( k) ) if k == d => Some ( CurrencyId :: D ) ,
7281 _ => None ,
7382 } ,
@@ -77,6 +86,7 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
7786 X1 ( GeneralKey ( k) ) if k == a1 => Some ( CurrencyId :: A1 ) ,
7887 X1 ( GeneralKey ( k) ) if k == b1 => Some ( CurrencyId :: B1 ) ,
7988 X1 ( GeneralKey ( k) ) if k == b2 => Some ( CurrencyId :: B2 ) ,
89+ X1 ( GeneralKey ( k) ) if k == c => Some ( CurrencyId :: C ) ,
8090 X1 ( GeneralKey ( k) ) if k == d => Some ( CurrencyId :: D ) ,
8191 _ => None ,
8292 } ,
@@ -121,10 +131,10 @@ decl_test_parachain! {
121131
122132decl_test_parachain ! {
123133 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 ) ,
128138 }
129139}
130140
@@ -166,6 +176,8 @@ pub type ParaXTokens = orml_xtokens::Pallet<para::Runtime>;
166176pub type ParaRelativeTokens = orml_tokens:: Pallet < para_relative_view:: Runtime > ;
167177pub type ParaRelativeXTokens = orml_xtokens:: Pallet < para_relative_view:: Runtime > ;
168178
179+ pub type ParaTeleportTokens = orml_tokens:: Pallet < para_teleport:: Runtime > ;
180+
169181pub fn para_ext ( para_id : u32 ) -> TestExternalities {
170182 use para:: { Runtime , System } ;
171183
@@ -190,6 +202,30 @@ pub fn para_ext(para_id: u32) -> TestExternalities {
190202 ext
191203}
192204
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+
193229pub fn relay_ext ( ) -> sp_io:: TestExternalities {
194230 use relay:: { Runtime , System } ;
195231
@@ -207,3 +243,46 @@ pub fn relay_ext() -> sp_io::TestExternalities {
207243 ext. execute_with ( || System :: set_block_number ( 1 ) ) ;
208244 ext
209245}
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