-
Notifications
You must be signed in to change notification settings - Fork 93
/
lib.rs
642 lines (579 loc) · 19.1 KB
/
lib.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
mod parachain;
mod relay_chain;
use sp_runtime::BuildStorage;
use xcm::prelude::*;
use xcm_executor::traits::ConvertLocation;
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};
pub const ALICE: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]);
pub const INITIAL_BALANCE: u128 = 1_000_000_000;
decl_test_parachain! {
pub struct ParaA {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(1),
}
}
decl_test_parachain! {
pub struct ParaB {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(2),
}
}
decl_test_parachain! {
pub struct ParaC {
Runtime = parachain::Runtime,
XcmpMessageHandler = parachain::MsgQueue,
DmpMessageHandler = parachain::MsgQueue,
new_ext = para_ext(3),
}
}
decl_test_relay_chain! {
pub struct Relay {
Runtime = relay_chain::Runtime,
RuntimeCall = relay_chain::RuntimeCall,
RuntimeEvent = relay_chain::RuntimeEvent,
XcmConfig = relay_chain::XcmConfig,
MessageQueue = relay_chain::MessageQueue,
System = relay_chain::System,
new_ext = relay_ext(),
}
}
decl_test_network! {
pub struct MockNet {
relay_chain = Relay,
parachains = vec![
(1, ParaA),
(2, ParaB),
(3, ParaC),
],
}
}
pub fn parent_account_id() -> parachain::AccountId {
let location = (Parent,);
parachain::LocationToAccountId::convert_location(&location.into()).unwrap()
}
pub fn child_account_id(para: u32) -> relay_chain::AccountId {
let location = (Parachain(para),);
relay_chain::LocationToAccountId::convert_location(&location.into()).unwrap()
}
pub fn child_account_account_id(para: u32, who: sp_runtime::AccountId32) -> relay_chain::AccountId {
let location = (Parachain(para), AccountId32 { network: None, id: who.into() });
relay_chain::LocationToAccountId::convert_location(&location.into()).unwrap()
}
pub fn sibling_account_account_id(para: u32, who: sp_runtime::AccountId32) -> parachain::AccountId {
let location = (Parent, Parachain(para), AccountId32 { network: None, id: who.into() });
parachain::LocationToAccountId::convert_location(&location.into()).unwrap()
}
pub fn parent_account_account_id(who: sp_runtime::AccountId32) -> parachain::AccountId {
let location = (Parent, AccountId32 { network: None, id: who.into() });
parachain::LocationToAccountId::convert_location(&location.into()).unwrap()
}
pub fn para_ext(para_id: u32) -> sp_io::TestExternalities {
use parachain::{MsgQueue, Runtime, System};
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(ALICE, INITIAL_BALANCE), (parent_account_id(), INITIAL_BALANCE)],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
sp_tracing::try_init_simple();
System::set_block_number(1);
MsgQueue::set_para_id(para_id.into());
});
ext
}
pub fn relay_ext() -> sp_io::TestExternalities {
use relay_chain::{Runtime, RuntimeOrigin, System, Uniques};
let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![
(ALICE, INITIAL_BALANCE),
(child_account_id(1), INITIAL_BALANCE),
(child_account_id(2), INITIAL_BALANCE),
(child_account_id(3), INITIAL_BALANCE),
],
}
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
assert_eq!(Uniques::force_create(RuntimeOrigin::root(), 1, ALICE, true), Ok(()));
assert_eq!(Uniques::mint(RuntimeOrigin::signed(ALICE), 1, 42, child_account_id(1)), Ok(()));
});
ext
}
pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>;
pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>;
#[cfg(test)]
mod tests {
use super::*;
use crate::parachain::{Balances, System, XcmHelper};
use codec::Encode;
use frame_support::traits::fungible::Mutate;
use frame_support::{assert_ok, weights::Weight};
use sp_io::misc::print_num;
use thea_primitives::extras::ExtraData;
use xcm::latest::QueryResponseInfo;
use xcm_simulator::TestExt;
// Helper function for forming buy execution message
fn buy_execution<C>(fees: impl Into<MultiAsset>) -> Instruction<C> {
BuyExecution { fees: fees.into(), weight_limit: Unlimited }
}
#[test]
fn remote_account_ids_work() {
child_account_account_id(1, ALICE);
sibling_account_account_id(1, ALICE);
parent_account_account_id(ALICE);
}
#[test]
fn dmp() {
MockNet::reset();
let remark = parachain::RuntimeCall::System(
frame_system::Call::<parachain::Runtime>::remark_with_event { remark: vec![1, 2, 3] },
);
Relay::execute_with(|| {
assert_ok!(RelayChainPalletXcm::send_xcm(
Here,
Parachain(1),
Xcm(vec![Transact {
origin_kind: OriginKind::SovereignAccount,
require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024),
call: remark.encode().into(),
}]),
));
});
ParaA::execute_with(|| {
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
});
}
#[test]
fn ump() {
MockNet::reset();
let remark = relay_chain::RuntimeCall::System(
frame_system::Call::<relay_chain::Runtime>::remark_with_event { remark: vec![1, 2, 3] },
);
ParaA::execute_with(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Here,
Parent,
Xcm(vec![Transact {
origin_kind: OriginKind::SovereignAccount,
require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024),
call: remark.encode().into(),
}]),
));
});
Relay::execute_with(|| {
use relay_chain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
});
}
#[test]
fn xcmp() {
MockNet::reset();
let remark = parachain::RuntimeCall::System(
frame_system::Call::<parachain::Runtime>::remark_with_event { remark: vec![1, 2, 3] },
);
ParaA::execute_with(|| {
assert_ok!(ParachainPalletXcm::send_xcm(
Here,
(Parent, Parachain(2)),
Xcm(vec![Transact {
origin_kind: OriginKind::SovereignAccount,
require_weight_at_most: Weight::from_parts(INITIAL_BALANCE as u64, 1024 * 1024),
call: remark.encode().into(),
}]),
));
});
ParaB::execute_with(|| {
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
});
}
#[test]
fn reserve_transfer() {
MockNet::reset();
let withdraw_amount = 123;
Relay::execute_with(|| {
assert_ok!(RelayChainPalletXcm::reserve_transfer_assets(
relay_chain::RuntimeOrigin::signed(ALICE),
Box::new(Parachain(1).into()),
Box::new(AccountId32 { network: None, id: ALICE.into() }.into()),
Box::new((Here, withdraw_amount).into()),
0,
));
assert_eq!(
relay_chain::Balances::free_balance(&child_account_id(1)),
INITIAL_BALANCE + withdraw_amount
);
});
ParaA::execute_with(|| {
// free execution, full amount received
use parachain::{RuntimeEvent, System};
assert_eq!(
pallet_balances::Pallet::<parachain::Runtime>::free_balance(&ALICE),
INITIAL_BALANCE
);
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::XcmHelper(xcm_helper::Event::AssetDeposited(..))
)));
});
}
/// Scenario:
/// A parachain transfers an NFT resident on the relay chain to another parachain account.
///
/// Asserts that the parachain accounts are updated as expected.
#[test]
fn withdraw_and_deposit_nft() {
MockNet::reset();
Relay::execute_with(|| {
assert_eq!(relay_chain::Uniques::owner(1, 42), Some(child_account_id(1)));
});
ParaA::execute_with(|| {
let message = Xcm(vec![TransferAsset {
assets: (GeneralIndex(1), 42u32).into(),
beneficiary: Parachain(2).into(),
}]);
// Send withdraw and deposit
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message));
});
Relay::execute_with(|| {
assert_eq!(relay_chain::Uniques::owner(1, 42), Some(child_account_id(2)));
});
}
/// Scenario:
/// The relay-chain creates an asset class on a parachain and then Alice transfers her NFT into
/// that parachain's sovereign account, who then mints a trustless-backed-derivative locally.
///
/// Asserts that the parachain accounts are updated as expected.
#[test]
fn reserve_asset_class_create_and_reserve_transfer() {
MockNet::reset();
Relay::execute_with(|| {
assert_ok!(relay_chain::Uniques::force_create(
relay_chain::RuntimeOrigin::root(),
2,
ALICE,
false
));
assert_ok!(relay_chain::Uniques::mint(
relay_chain::RuntimeOrigin::signed(ALICE),
2,
69,
child_account_account_id(1, ALICE)
));
assert_eq!(
relay_chain::Uniques::owner(2, 69),
Some(child_account_account_id(1, ALICE))
);
let message = Xcm(vec![Transact {
origin_kind: OriginKind::Xcm,
require_weight_at_most: Weight::from_parts(1_000_000_000, 1024 * 1024),
call: parachain::RuntimeCall::from(
pallet_uniques::Call::<parachain::Runtime>::create {
collection: (Parent, 2u64).into(),
admin: parent_account_id(),
},
)
.encode()
.into(),
}]);
// Send creation.
assert_ok!(RelayChainPalletXcm::send_xcm(Here, Parachain(1), message));
});
ParaA::execute_with(|| {
// Then transfer
let message = Xcm(vec![
WithdrawAsset((GeneralIndex(2), 69u32).into()),
DepositReserveAsset {
assets: AllCounted(1).into(),
dest: Parachain(1).into(),
xcm: Xcm(vec![DepositAsset {
assets: AllCounted(1).into(),
beneficiary: (AccountId32 { id: ALICE.into(), network: None },).into(),
}]),
},
]);
let alice = AccountId32 { id: ALICE.into(), network: None };
assert_ok!(ParachainPalletXcm::send_xcm(alice, Parent, message));
});
ParaA::execute_with(|| {
assert_eq!(parachain::Balances::reserved_balance(&parent_account_id()), 1000);
assert_eq!(
parachain::ForeignUniques::collection_owner((Parent, 2u64).into()),
Some(parent_account_id())
);
});
}
/// Scenario:
/// A parachain transfers funds on the relay chain to another parachain account.
///
/// Asserts that the parachain accounts are updated as expected.
#[test]
fn withdraw_and_deposit() {
MockNet::reset();
let send_amount = 10;
ParaA::execute_with(|| {
let message = Xcm(vec![
WithdrawAsset((Here, send_amount).into()),
buy_execution((Here, send_amount)),
DepositAsset { assets: AllCounted(1).into(), beneficiary: Parachain(2).into() },
]);
// Send withdraw and deposit
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message.clone()));
});
Relay::execute_with(|| {
assert_eq!(
relay_chain::Balances::free_balance(child_account_id(1)),
INITIAL_BALANCE - send_amount
);
assert_eq!(
relay_chain::Balances::free_balance(child_account_id(2)),
INITIAL_BALANCE + send_amount
);
});
}
/// Scenario:
/// A parachain wants to be notified that a transfer worked correctly.
/// It sends a `QueryHolding` after the deposit to get notified on success.
///
/// Asserts that the balances are updated correctly and the expected XCM is sent.
#[test]
fn query_holding() {
MockNet::reset();
let send_amount = 10;
let query_id_set = 1234;
// Send a message which fully succeeds on the relay chain
ParaA::execute_with(|| {
let message = Xcm(vec![
WithdrawAsset((Here, send_amount).into()),
buy_execution((Here, send_amount)),
DepositAsset { assets: AllCounted(1).into(), beneficiary: Parachain(2).into() },
ReportHolding {
response_info: QueryResponseInfo {
destination: Parachain(1).into(),
query_id: query_id_set,
max_weight: Weight::from_parts(1_000_000_000, 1024 * 1024),
},
assets: All.into(),
},
]);
// Send withdraw and deposit with query holding
assert_ok!(ParachainPalletXcm::send_xcm(Here, Parent, message.clone(),));
});
// Check that transfer was executed
Relay::execute_with(|| {
// Withdraw executed
assert_eq!(
relay_chain::Balances::free_balance(child_account_id(1)),
INITIAL_BALANCE - send_amount
);
// Deposit executed
assert_eq!(
relay_chain::Balances::free_balance(child_account_id(2)),
INITIAL_BALANCE + send_amount
);
});
// Check that QueryResponse message was received
ParaA::execute_with(|| {
assert_eq!(
parachain::MsgQueue::received_dmp(),
vec![Xcm(vec![QueryResponse {
query_id: query_id_set,
response: Response::Assets(MultiAssets::new()),
max_weight: Weight::from_parts(1_000_000_000, 1024 * 1024),
querier: Some(Here.into()),
}])],
);
});
}
#[test]
fn trasnfer_pdex_token_to_non_native_chain() {
MockNet::reset();
ParaA::execute_with(|| {
let destination = MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(2),
Junction::AccountId32 { network: None, id: [1; 32] },
),
};
let destination = VersionedMultiLocation::V3(destination);
let deposit = thea_primitives::types::Withdraw {
id: Default::default(),
asset_id: polkadex_primitives::AssetId::Polkadex,
amount: 2000000000000,
destination: destination.encode(),
fee_asset_id: None,
fee_amount: None,
is_blocked: false,
extra: ExtraData::None,
};
let block_no = 1;
let multlocation =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(1)) };
let pdex_asset_id = AssetId::Concrete(multlocation);
assert_ok!(Balances::mint_into(&XcmHelper::get_pallet_account(), 100000000000000000));
XcmHelper::insert_parachain_asset(
pdex_asset_id,
polkadex_primitives::AssetId::Polkadex,
);
XcmHelper::insert_pending_withdrawal(block_no, deposit.clone());
XcmHelper::handle_new_pending_withdrawals(block_no);
});
ParaB::execute_with(|| {
use parachain::{RuntimeEvent, System};
let events = System::events();
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::XcmHelper(xcm_helper::Event::AssetDeposited(..))
)));
})
}
#[test]
fn test_on_initialize_with_non_native_asset_deposit_to_polkadex_parachain() {
MockNet::reset();
ParaA::execute_with(|| {
let location =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(1)) };
let asset_id_ml = AssetId::Concrete(location);
let amount = 1_000_000_000_000u128;
let destination = MultiLocation {
parents: 0,
interior: Junctions::X1(Junction::AccountId32 { network: None, id: [1; 32] }),
};
let destination: VersionedMultiLocation = destination.into();
// Register Asset Id
let asset_id = XcmHelper::generate_asset_id_for_parachain(asset_id_ml);
let deposit = thea_primitives::types::Withdraw {
id: Default::default(),
asset_id: polkadex_primitives::AssetId::Polkadex,
amount: 2000000000000,
destination: destination.encode(),
fee_asset_id: None,
fee_amount: None,
is_blocked: false,
extra: ExtraData::None,
};
assert_ok!(Balances::mint_into(&XcmHelper::get_pallet_account(), 100000000000000000));
XcmHelper::insert_parachain_asset(asset_id_ml, polkadex_primitives::AssetId::Polkadex);
let block_no = 1;
XcmHelper::insert_pending_withdrawal(block_no, deposit.clone());
XcmHelper::handle_new_pending_withdrawals(block_no);
});
}
#[test]
fn send_sibling_asset_to_non_reserve_sibling() {
MockNet::reset();
ParaC::execute_with(|| {
let multlocation = MultiLocation { parents: 0, interior: Junctions::Here };
let pdex_asset_id = AssetId::Concrete(multlocation);
assert_ok!(Balances::mint_into(
&XcmHelper::get_pallet_account(),
100_000_000_000_000_000
));
let para_a =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(1)) };
let para_b =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(2)) };
let para_c =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(3)) };
assert_ok!(Balances::mint_into(
&XcmHelper::sibling_account_converter(para_a).unwrap(),
100_000_000_000_000_000
));
assert_ok!(Balances::mint_into(
&XcmHelper::sibling_account_converter(para_b).unwrap(),
100_000_000_000_000_000
));
assert_ok!(Balances::mint_into(
&XcmHelper::sibling_account_converter(para_c).unwrap(),
100_000_000_000_000_000
));
XcmHelper::insert_parachain_asset(
pdex_asset_id,
polkadex_primitives::AssetId::Polkadex,
);
});
ParaA::execute_with(|| {
let destination = MultiLocation {
parents: 1,
interior: Junctions::X2(
Junction::Parachain(2),
Junction::AccountId32 { network: None, id: [1; 32] },
),
};
let destination = VersionedMultiLocation::V3(destination);
let multlocation =
MultiLocation { parents: 1, interior: Junctions::X1(Junction::Parachain(3)) };
let non_reserve_asset_id = AssetId::Concrete(multlocation);
let asset_id = XcmHelper::generate_asset_id_for_parachain(non_reserve_asset_id);
let deposit = thea_primitives::types::Withdraw {
id: Default::default(),
asset_id,
amount: 2000000000000,
destination: destination.encode(),
fee_asset_id: None,
fee_amount: None,
is_blocked: false,
extra: ExtraData::None,
};
let block_no = 1;
assert_ok!(Balances::mint_into(&XcmHelper::get_pallet_account(), 100000000000000000));
XcmHelper::insert_parachain_asset(non_reserve_asset_id, asset_id);
XcmHelper::insert_pending_withdrawal(block_no, deposit.clone());
XcmHelper::handle_new_pending_withdrawals(block_no);
});
ParaC::execute_with(|| {
let multlocation = MultiLocation { parents: 0, interior: Junctions::Here };
let pdex_asset_id = AssetId::Concrete(multlocation);
assert_ok!(Balances::mint_into(&XcmHelper::get_pallet_account(), 100000000000000000));
XcmHelper::insert_parachain_asset(
pdex_asset_id,
polkadex_primitives::AssetId::Polkadex,
);
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::XcmHelper(xcm_helper::Event::SiblingDeposit(..))
)));
});
ParaB::execute_with(|| {
use parachain::{RuntimeEvent, System};
let events = System::events();
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::XcmHelper(xcm_helper::Event::AssetDeposited(..))
)));
})
}
}