1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam 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
// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
// We want to avoid including the rococo-runtime here.
18
// TODO: whenever a conclusion is taken from https://github.com/paritytech/substrate/issues/8158
19

            
20
use cumulus_primitives_core::{relay_chain::HrmpChannelId, ParaId};
21
use pallet_xcm_transactor::chain_indices::RelayChainIndices;
22
use parity_scale_codec::{Decode, Encode};
23
use sp_runtime::traits::{AccountIdLookup, StaticLookup};
24
use sp_runtime::AccountId32;
25
use sp_std::vec::Vec;
26

            
27
#[derive(Encode, Decode)]
28
pub enum RelayCall {
29
	#[codec(index = 16u8)]
30
	// the index should match the position of the module in `construct_runtime!`
31
	Utility(UtilityCall),
32

            
33
	#[codec(index = 6u8)]
34
	Stake(StakeCall),
35
	#[codec(index = 51u8)]
36
	// the index should match the position of the module in `construct_runtime!`
37
	Hrmp(HrmpCall),
38
}
39

            
40
#[derive(Encode, Decode)]
41
pub enum StakeCall {
42
	#[codec(index = 0u16)]
43
	// the index should match the position of the dispatchable in the target pallet
44
	Bond(
45
		#[codec(compact)] cumulus_primitives_core::relay_chain::Balance,
46
		pallet_staking::RewardDestination<AccountId32>,
47
	),
48
	#[codec(index = 1u16)]
49
	BondExtra(#[codec(compact)] cumulus_primitives_core::relay_chain::Balance),
50
	#[codec(index = 2u16)]
51
	Unbond(#[codec(compact)] cumulus_primitives_core::relay_chain::Balance),
52
	#[codec(index = 3u16)]
53
	WithdrawUnbonded(u32),
54
	#[codec(index = 4u16)]
55
	Validate(pallet_staking::ValidatorPrefs),
56
	#[codec(index = 5u16)]
57
	Nominate(Vec<<AccountIdLookup<AccountId32, ()> as StaticLookup>::Source>),
58
	#[codec(index = 6u16)]
59
	Chill,
60
	#[codec(index = 7u16)]
61
	SetPayee(pallet_staking::RewardDestination<AccountId32>),
62
	#[codec(index = 8u16)]
63
	SetController,
64
	#[codec(index = 19u16)]
65
	Rebond(#[codec(compact)] cumulus_primitives_core::relay_chain::Balance),
66
}
67

            
68
// Utility call encoding, needed for xcm transactor pallet
69
#[derive(Encode, Decode)]
70
pub enum UtilityCall {
71
	#[codec(index = 1u8)]
72
	AsDerivative(u16),
73
}
74

            
75
// HRMP call encoding, needed for xcm transactor pallet
76
#[derive(Encode, Decode)]
77
pub enum HrmpCall {
78
	#[codec(index = 0u8)]
79
	InitOpenChannel(ParaId, u32, u32),
80
	#[codec(index = 1u8)]
81
	AcceptOpenChannel(ParaId),
82
	#[codec(index = 2u8)]
83
	CloseChannel(HrmpChannelId),
84
	#[codec(index = 6u8)]
85
	CancelOpenRequest(HrmpChannelId, u32),
86
}
87

            
88
pub struct WestendEncoder;
89

            
90
impl xcm_primitives::UtilityEncodeCall for WestendEncoder {
91
1
	fn encode_call(self, call: xcm_primitives::UtilityAvailableCalls) -> Vec<u8> {
92
1
		match call {
93
1
			xcm_primitives::UtilityAvailableCalls::AsDerivative(a, b) => {
94
1
				let mut call = RelayCall::Utility(UtilityCall::AsDerivative(a.clone())).encode();
95
1
				// If we encode directly we inject the call length,
96
1
				// so we just append the inner call after encoding the outer
97
1
				call.append(&mut b.clone());
98
1
				call
99
1
			}
100
1
		}
101
1
	}
102
}
103

            
104
impl xcm_primitives::HrmpEncodeCall for WestendEncoder {
105
4
	fn hrmp_encode_call(
106
4
		call: xcm_primitives::HrmpAvailableCalls,
107
4
	) -> Result<Vec<u8>, xcm::latest::Error> {
108
4
		match call {
109
1
			xcm_primitives::HrmpAvailableCalls::InitOpenChannel(a, b, c) => Ok(RelayCall::Hrmp(
110
1
				HrmpCall::InitOpenChannel(a.clone(), b.clone(), c.clone()),
111
1
			)
112
1
			.encode()),
113
1
			xcm_primitives::HrmpAvailableCalls::AcceptOpenChannel(a) => {
114
1
				Ok(RelayCall::Hrmp(HrmpCall::AcceptOpenChannel(a.clone())).encode())
115
			}
116
1
			xcm_primitives::HrmpAvailableCalls::CloseChannel(a) => {
117
1
				Ok(RelayCall::Hrmp(HrmpCall::CloseChannel(a.clone())).encode())
118
			}
119
1
			xcm_primitives::HrmpAvailableCalls::CancelOpenRequest(a, b) => {
120
1
				Ok(RelayCall::Hrmp(HrmpCall::CancelOpenRequest(a.clone(), b.clone())).encode())
121
			}
122
		}
123
4
	}
124
}
125
impl xcm_primitives::StakeEncodeCall<()> for WestendEncoder {
126
11
	fn encode_call(_transactor: (), call: xcm_primitives::AvailableStakeCalls) -> Vec<u8> {
127
11
		match call {
128
1
			xcm_primitives::AvailableStakeCalls::Bond(b, c) => {
129
1
				RelayCall::Stake(StakeCall::Bond(b, c)).encode()
130
			}
131

            
132
1
			xcm_primitives::AvailableStakeCalls::BondExtra(a) => {
133
1
				RelayCall::Stake(StakeCall::BondExtra(a)).encode()
134
			}
135

            
136
1
			xcm_primitives::AvailableStakeCalls::Unbond(a) => {
137
1
				RelayCall::Stake(StakeCall::Unbond(a)).encode()
138
			}
139

            
140
1
			xcm_primitives::AvailableStakeCalls::WithdrawUnbonded(a) => {
141
1
				RelayCall::Stake(StakeCall::WithdrawUnbonded(a)).encode()
142
			}
143

            
144
1
			xcm_primitives::AvailableStakeCalls::Validate(a) => {
145
1
				RelayCall::Stake(StakeCall::Validate(a)).encode()
146
			}
147

            
148
			xcm_primitives::AvailableStakeCalls::Chill => {
149
2
				RelayCall::Stake(StakeCall::Chill).encode()
150
			}
151

            
152
1
			xcm_primitives::AvailableStakeCalls::SetPayee(a) => {
153
1
				RelayCall::Stake(StakeCall::SetPayee(a.into())).encode()
154
			}
155

            
156
			xcm_primitives::AvailableStakeCalls::SetController => {
157
1
				RelayCall::Stake(StakeCall::SetController).encode()
158
			}
159

            
160
1
			xcm_primitives::AvailableStakeCalls::Rebond(a) => {
161
1
				RelayCall::Stake(StakeCall::Rebond(a.into())).encode()
162
			}
163

            
164
1
			xcm_primitives::AvailableStakeCalls::Nominate(a) => {
165
1
				let nominated: Vec<<AccountIdLookup<AccountId32, ()> as StaticLookup>::Source> =
166
1
					a.iter().map(|add| (*add).clone().into()).collect();
167
1

            
168
1
				RelayCall::Stake(StakeCall::Nominate(nominated)).encode()
169
			}
170
		}
171
11
	}
172
}
173

            
174
/// Westend pallet and extrinsic indices
175
pub const WESTEND_RELAY_INDICES: RelayChainIndices = RelayChainIndices {
176
	staking: 6u8,
177
	utility: 16u8,
178
	hrmp: 51u8,
179
	bond: 0u8,
180
	bond_extra: 1u8,
181
	unbond: 2u8,
182
	withdraw_unbonded: 3u8,
183
	validate: 4u8,
184
	nominate: 5u8,
185
	chill: 6u8,
186
	set_payee: 7u8,
187
	set_controller: 8u8,
188
	rebond: 19u8,
189
	as_derivative: 1u8,
190
	init_open_channel: 0u8,
191
	accept_open_channel: 1u8,
192
	close_channel: 2u8,
193
	cancel_open_request: 6u8,
194
};
195

            
196
#[cfg(test)]
197
mod tests {
198
	use super::*;
199
	use crate::westend::WestendEncoder;
200
	use frame_support::traits::PalletInfo;
201
	use sp_runtime::Perbill;
202
	use xcm_primitives::{StakeEncodeCall, UtilityEncodeCall};
203

            
204
	#[test]
205
1
	fn test_as_derivative() {
206
1
		let mut expected_encoded: Vec<u8> = Vec::new();
207
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
208
1
			westend_runtime::Utility,
209
1
		>()
210
1
		.unwrap() as u8;
211
1
		expected_encoded.push(index);
212
1

            
213
1
		let mut expected = pallet_utility::Call::<westend_runtime::Runtime>::as_derivative {
214
1
			index: 1,
215
1
			call: westend_runtime::RuntimeCall::Staking(pallet_staking::Call::<
216
1
				westend_runtime::Runtime,
217
1
			>::chill {})
218
1
			.into(),
219
1
		}
220
1
		.encode();
221
1
		expected_encoded.append(&mut expected);
222
1

            
223
1
		let call_bytes = <WestendEncoder as StakeEncodeCall<()>>::encode_call(
224
1
			(),
225
1
			xcm_primitives::AvailableStakeCalls::Chill,
226
1
		);
227
1

            
228
1
		expected_encoded.append(&mut expected);
229
1

            
230
1
		assert_eq!(
231
1
			xcm_primitives::UtilityEncodeCall::encode_call(
232
1
				WestendEncoder,
233
1
				xcm_primitives::UtilityAvailableCalls::AsDerivative(1, call_bytes.clone())
234
1
			),
235
1
			expected_encoded.clone()
236
1
		);
237
1
		sp_io::TestExternalities::default().execute_with(|| {
238
1
			// Pallet-xcm-transactor default encoder returns same result
239
1
			// insert storage item as per migration to set the storage item
240
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
241
1
				WESTEND_RELAY_INDICES,
242
1
			);
243
1
			assert_eq!(
244
1
				<pallet_xcm_transactor::Pallet::<
245
1
					moonbase_runtime::Runtime> as UtilityEncodeCall
246
1
				>::encode_call(
247
1
					pallet_xcm_transactor::Pallet(
248
1
						sp_std::marker::PhantomData::<moonbase_runtime::Runtime>::default()
249
1
					),
250
1
					xcm_primitives::UtilityAvailableCalls::AsDerivative(1, call_bytes)
251
1
				),
252
1
				expected_encoded
253
1
			);
254
1
		});
255
1
	}
256

            
257
	#[test]
258
1
	fn test_stake_bond() {
259
1
		let mut expected_encoded: Vec<u8> = Vec::new();
260
1
		let controller: AccountId32 = [1u8; 32].into();
261
1

            
262
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
263
1
			westend_runtime::Staking,
264
1
		>()
265
1
		.unwrap() as u8;
266
1
		expected_encoded.push(index);
267
1

            
268
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::bond {
269
1
			value: 100u32.into(),
270
1
			payee: pallet_staking::RewardDestination::Account(controller.clone()),
271
1
		}
272
1
		.encode();
273
1
		expected_encoded.append(&mut expected);
274
1

            
275
1
		assert_eq!(
276
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
277
1
				(),
278
1
				xcm_primitives::AvailableStakeCalls::Bond(
279
1
					100u32.into(),
280
1
					pallet_staking::RewardDestination::Account(controller.clone()),
281
1
				)
282
1
			),
283
1
			expected_encoded.clone()
284
1
		);
285
1
		sp_io::TestExternalities::default().execute_with(|| {
286
1
			// Pallet-xcm-transactor default encoder returns same result
287
1
			// insert storage item as per migration to set the storage item
288
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
289
1
				WESTEND_RELAY_INDICES,
290
1
			);
291
1
			assert_eq!(
292
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
293
1
					moonbase_runtime::xcm_config::Transactors,
294
1
				>>::encode_call(
295
1
					moonbase_runtime::xcm_config::Transactors::Relay,
296
1
					xcm_primitives::AvailableStakeCalls::Bond(
297
1
						100u32.into(),
298
1
						pallet_staking::RewardDestination::Account(controller)
299
1
					)
300
1
				),
301
1
				expected_encoded
302
1
			);
303
1
		});
304
1
	}
305
	#[test]
306
1
	fn test_stake_bond_extra() {
307
1
		let mut expected_encoded: Vec<u8> = Vec::new();
308
1

            
309
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
310
1
			westend_runtime::Staking,
311
1
		>()
312
1
		.unwrap() as u8;
313
1
		expected_encoded.push(index);
314
1

            
315
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::bond_extra {
316
1
			max_additional: 100u32.into(),
317
1
		}
318
1
		.encode();
319
1
		expected_encoded.append(&mut expected);
320
1

            
321
1
		assert_eq!(
322
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
323
1
				(),
324
1
				xcm_primitives::AvailableStakeCalls::BondExtra(100u32.into(),)
325
1
			),
326
1
			expected_encoded.clone()
327
1
		);
328
1
		sp_io::TestExternalities::default().execute_with(|| {
329
1
			// Pallet-xcm-transactor default encoder returns same result
330
1
			// insert storage item as per migration to set the storage item
331
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
332
1
				WESTEND_RELAY_INDICES,
333
1
			);
334
1
			assert_eq!(
335
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
336
1
					moonbase_runtime::xcm_config::Transactors,
337
1
				>>::encode_call(
338
1
					moonbase_runtime::xcm_config::Transactors::Relay,
339
1
					xcm_primitives::AvailableStakeCalls::BondExtra(100u32.into(),)
340
1
				),
341
1
				expected_encoded
342
1
			);
343
1
		});
344
1
	}
345
	#[test]
346
1
	fn test_stake_unbond() {
347
1
		let mut expected_encoded: Vec<u8> = Vec::new();
348
1

            
349
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
350
1
			westend_runtime::Staking,
351
1
		>()
352
1
		.unwrap() as u8;
353
1
		expected_encoded.push(index);
354
1

            
355
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::unbond {
356
1
			value: 100u32.into(),
357
1
		}
358
1
		.encode();
359
1
		expected_encoded.append(&mut expected);
360
1

            
361
1
		assert_eq!(
362
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
363
1
				(),
364
1
				xcm_primitives::AvailableStakeCalls::Unbond(100u32.into(),)
365
1
			),
366
1
			expected_encoded.clone()
367
1
		);
368
1
		sp_io::TestExternalities::default().execute_with(|| {
369
1
			// Pallet-xcm-transactor default encoder returns same result
370
1
			// insert storage item as per migration to set the storage item
371
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
372
1
				WESTEND_RELAY_INDICES,
373
1
			);
374
1
			assert_eq!(
375
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
376
1
					moonbase_runtime::xcm_config::Transactors,
377
1
				>>::encode_call(
378
1
					moonbase_runtime::xcm_config::Transactors::Relay,
379
1
					xcm_primitives::AvailableStakeCalls::Unbond(100u32.into(),)
380
1
				),
381
1
				expected_encoded
382
1
			);
383
1
		});
384
1
	}
385
	#[test]
386
1
	fn test_stake_withdraw_unbonded() {
387
1
		let mut expected_encoded: Vec<u8> = Vec::new();
388
1

            
389
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
390
1
			westend_runtime::Staking,
391
1
		>()
392
1
		.unwrap() as u8;
393
1
		expected_encoded.push(index);
394
1

            
395
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::withdraw_unbonded {
396
1
			num_slashing_spans: 100u32,
397
1
		}
398
1
		.encode();
399
1
		expected_encoded.append(&mut expected);
400
1

            
401
1
		assert_eq!(
402
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
403
1
				(),
404
1
				xcm_primitives::AvailableStakeCalls::WithdrawUnbonded(100u32,)
405
1
			),
406
1
			expected_encoded.clone()
407
1
		);
408
1
		sp_io::TestExternalities::default().execute_with(|| {
409
1
			// Pallet-xcm-transactor default encoder returns same result
410
1
			// insert storage item as per migration to set the storage item
411
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
412
1
				WESTEND_RELAY_INDICES,
413
1
			);
414
1
			assert_eq!(
415
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
416
1
					moonbase_runtime::xcm_config::Transactors,
417
1
				>>::encode_call(
418
1
					moonbase_runtime::xcm_config::Transactors::Relay,
419
1
					xcm_primitives::AvailableStakeCalls::WithdrawUnbonded(100u32,)
420
1
				),
421
1
				expected_encoded
422
1
			);
423
1
		});
424
1
	}
425
	#[test]
426
1
	fn test_stake_validate() {
427
1
		let mut expected_encoded: Vec<u8> = Vec::new();
428
1

            
429
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
430
1
			westend_runtime::Staking,
431
1
		>()
432
1
		.unwrap() as u8;
433
1
		expected_encoded.push(index);
434
1

            
435
1
		let validator_prefs = pallet_staking::ValidatorPrefs {
436
1
			commission: Perbill::from_percent(5),
437
1
			blocked: true,
438
1
		};
439
1

            
440
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::validate {
441
1
			prefs: validator_prefs.clone(),
442
1
		}
443
1
		.encode();
444
1
		expected_encoded.append(&mut expected);
445
1

            
446
1
		assert_eq!(
447
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
448
1
				(),
449
1
				xcm_primitives::AvailableStakeCalls::Validate(validator_prefs.clone())
450
1
			),
451
1
			expected_encoded.clone()
452
1
		);
453
1
		sp_io::TestExternalities::default().execute_with(|| {
454
1
			// Pallet-xcm-transactor default encoder returns same result
455
1
			// insert storage item as per migration to set the storage item
456
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
457
1
				WESTEND_RELAY_INDICES,
458
1
			);
459
1
			assert_eq!(
460
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
461
1
					moonbase_runtime::xcm_config::Transactors,
462
1
				>>::encode_call(
463
1
					moonbase_runtime::xcm_config::Transactors::Relay,
464
1
					xcm_primitives::AvailableStakeCalls::Validate(validator_prefs)
465
1
				),
466
1
				expected_encoded
467
1
			);
468
1
		});
469
1
	}
470
	#[test]
471
1
	fn test_stake_nominate() {
472
1
		let mut expected_encoded: Vec<u8> = Vec::new();
473
1
		let relay_account: AccountId32 = [1u8; 32].into();
474
1

            
475
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
476
1
			westend_runtime::Staking,
477
1
		>()
478
1
		.unwrap() as u8;
479
1
		expected_encoded.push(index);
480
1

            
481
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::nominate {
482
1
			targets: vec![relay_account.clone().into()],
483
1
		}
484
1
		.encode();
485
1
		expected_encoded.append(&mut expected);
486
1

            
487
1
		assert_eq!(
488
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
489
1
				(),
490
1
				xcm_primitives::AvailableStakeCalls::Nominate(vec![relay_account.clone().into()])
491
1
			),
492
1
			expected_encoded.clone()
493
1
		);
494
1
		sp_io::TestExternalities::default().execute_with(|| {
495
1
			// Pallet-xcm-transactor default encoder returns same result
496
1
			// insert storage item as per migration to set the storage item
497
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
498
1
				WESTEND_RELAY_INDICES,
499
1
			);
500
1
			assert_eq!(
501
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
502
1
					moonbase_runtime::xcm_config::Transactors,
503
1
				>>::encode_call(
504
1
					moonbase_runtime::xcm_config::Transactors::Relay,
505
1
					xcm_primitives::AvailableStakeCalls::Nominate(vec![relay_account.into()])
506
1
				),
507
1
				expected_encoded
508
1
			);
509
1
		});
510
1
	}
511
	#[test]
512
1
	fn test_stake_chill() {
513
1
		let mut expected_encoded: Vec<u8> = Vec::new();
514
1

            
515
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
516
1
			westend_runtime::Staking,
517
1
		>()
518
1
		.unwrap() as u8;
519
1
		expected_encoded.push(index);
520
1

            
521
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::chill {}.encode();
522
1
		expected_encoded.append(&mut expected);
523
1

            
524
1
		assert_eq!(
525
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
526
1
				(),
527
1
				xcm_primitives::AvailableStakeCalls::Chill
528
1
			),
529
1
			expected_encoded.clone()
530
1
		);
531
1
		sp_io::TestExternalities::default().execute_with(|| {
532
1
			// Pallet-xcm-transactor default encoder returns same result
533
1
			// insert storage item as per migration to set the storage item
534
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
535
1
				WESTEND_RELAY_INDICES,
536
1
			);
537
1
			assert_eq!(
538
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
539
1
					moonbase_runtime::xcm_config::Transactors,
540
1
				>>::encode_call(
541
1
					moonbase_runtime::xcm_config::Transactors::Relay,
542
1
					xcm_primitives::AvailableStakeCalls::Chill
543
1
				),
544
1
				expected_encoded
545
1
			);
546
1
		});
547
1
	}
548

            
549
	#[test]
550
1
	fn test_set_payee() {
551
1
		let mut expected_encoded: Vec<u8> = Vec::new();
552
1
		let controller: AccountId32 = [1u8; 32].into();
553
1

            
554
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
555
1
			westend_runtime::Staking,
556
1
		>()
557
1
		.unwrap() as u8;
558
1
		expected_encoded.push(index);
559
1

            
560
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::set_payee {
561
1
			payee: pallet_staking::RewardDestination::Account(controller.clone()),
562
1
		}
563
1
		.encode();
564
1
		expected_encoded.append(&mut expected);
565
1

            
566
1
		assert_eq!(
567
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
568
1
				(),
569
1
				xcm_primitives::AvailableStakeCalls::SetPayee(
570
1
					pallet_staking::RewardDestination::Account(controller.clone())
571
1
				)
572
1
			),
573
1
			expected_encoded.clone()
574
1
		);
575
1
		sp_io::TestExternalities::default().execute_with(|| {
576
1
			// Pallet-xcm-transactor default encoder returns same result
577
1
			// insert storage item as per migration to set the storage item
578
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
579
1
				WESTEND_RELAY_INDICES,
580
1
			);
581
1
			assert_eq!(
582
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
583
1
					moonbase_runtime::xcm_config::Transactors,
584
1
				>>::encode_call(
585
1
					moonbase_runtime::xcm_config::Transactors::Relay,
586
1
					xcm_primitives::AvailableStakeCalls::SetPayee(
587
1
						pallet_staking::RewardDestination::Account(controller)
588
1
					)
589
1
				),
590
1
				expected_encoded
591
1
			);
592
1
		});
593
1
	}
594

            
595
	#[test]
596
1
	fn test_set_controller() {
597
1
		let mut expected_encoded: Vec<u8> = Vec::new();
598
1

            
599
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
600
1
			westend_runtime::Staking,
601
1
		>()
602
1
		.unwrap() as u8;
603
1
		expected_encoded.push(index);
604
1

            
605
1
		let mut expected =
606
1
			pallet_staking::Call::<westend_runtime::Runtime>::set_controller {}.encode();
607
1
		expected_encoded.append(&mut expected);
608
1

            
609
1
		assert_eq!(
610
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
611
1
				(),
612
1
				xcm_primitives::AvailableStakeCalls::SetController
613
1
			),
614
1
			expected_encoded.clone()
615
1
		);
616
1
		sp_io::TestExternalities::default().execute_with(|| {
617
1
			// Pallet-xcm-transactor default encoder returns same result
618
1
			// insert storage item as per migration to set the storage item
619
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
620
1
				WESTEND_RELAY_INDICES,
621
1
			);
622
1
			assert_eq!(
623
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
624
1
					moonbase_runtime::xcm_config::Transactors,
625
1
				>>::encode_call(
626
1
					moonbase_runtime::xcm_config::Transactors::Relay,
627
1
					xcm_primitives::AvailableStakeCalls::SetController
628
1
				),
629
1
				expected_encoded
630
1
			);
631
1
		});
632
1
	}
633
	#[test]
634
1
	fn test_rebond() {
635
1
		let mut expected_encoded: Vec<u8> = Vec::new();
636
1

            
637
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
638
1
			westend_runtime::Staking,
639
1
		>()
640
1
		.unwrap() as u8;
641
1
		expected_encoded.push(index);
642
1

            
643
1
		let mut expected = pallet_staking::Call::<westend_runtime::Runtime>::rebond {
644
1
			value: 100u32.into(),
645
1
		}
646
1
		.encode();
647
1
		expected_encoded.append(&mut expected);
648
1

            
649
1
		assert_eq!(
650
1
			<WestendEncoder as StakeEncodeCall<()>>::encode_call(
651
1
				(),
652
1
				xcm_primitives::AvailableStakeCalls::Rebond(100u32.into())
653
1
			),
654
1
			expected_encoded.clone()
655
1
		);
656
1
		sp_io::TestExternalities::default().execute_with(|| {
657
1
			// Pallet-xcm-transactor default encoder returns same result
658
1
			// insert storage item as per migration to set the storage item
659
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
660
1
				WESTEND_RELAY_INDICES,
661
1
			);
662
1
			assert_eq!(
663
1
				<pallet_xcm_transactor::Pallet::<moonbase_runtime::Runtime> as StakeEncodeCall<
664
1
					moonbase_runtime::xcm_config::Transactors,
665
1
				>>::encode_call(
666
1
					moonbase_runtime::xcm_config::Transactors::Relay,
667
1
					xcm_primitives::AvailableStakeCalls::Rebond(100u32.into())
668
1
				),
669
1
				expected_encoded
670
1
			);
671
1
		});
672
1
	}
673

            
674
	#[test]
675
1
	fn test_hrmp_init() {
676
1
		let mut expected_encoded: Vec<u8> = Vec::new();
677
1

            
678
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
679
1
			westend_runtime::Hrmp,
680
1
		>()
681
1
		.unwrap() as u8;
682
1
		expected_encoded.push(index);
683
1

            
684
1
		let mut expected = polkadot_runtime_parachains::hrmp::Call::<
685
1
			westend_runtime::Runtime
686
1
		>::hrmp_init_open_channel {
687
1
			recipient: 1000u32.into(),
688
1
			proposed_max_capacity: 100u32,
689
1
			proposed_max_message_size: 100u32,
690
1
		}
691
1
		.encode();
692
1
		expected_encoded.append(&mut expected);
693
1

            
694
1
		assert_eq!(
695
1
			<WestendEncoder as xcm_primitives::HrmpEncodeCall>::hrmp_encode_call(
696
1
				xcm_primitives::HrmpAvailableCalls::InitOpenChannel(
697
1
					1000u32.into(),
698
1
					100u32.into(),
699
1
					100u32.into()
700
1
				)
701
1
			),
702
1
			Ok(expected_encoded.clone())
703
1
		);
704
1
		sp_io::TestExternalities::default().execute_with(|| {
705
1
			// Pallet-xcm-transactor default encoder returns same result
706
1
			// insert storage item as per migration to set the storage item
707
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
708
1
				WESTEND_RELAY_INDICES,
709
1
			);
710
1
			assert_eq!(
711
1
				<pallet_xcm_transactor::Pallet::<
712
1
					moonbase_runtime::Runtime> as xcm_primitives::HrmpEncodeCall
713
1
				>::hrmp_encode_call(
714
1
					xcm_primitives::HrmpAvailableCalls::InitOpenChannel(
715
1
						1000u32.into(),
716
1
						100u32.into(),
717
1
						100u32.into()
718
1
					)
719
1
				),
720
1
				Ok(expected_encoded)
721
1
			);
722
1
		});
723
1
	}
724

            
725
	#[test]
726
1
	fn test_hrmp_accept() {
727
1
		let mut expected_encoded: Vec<u8> = Vec::new();
728
1

            
729
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
730
1
			westend_runtime::Hrmp,
731
1
		>()
732
1
		.unwrap() as u8;
733
1
		expected_encoded.push(index);
734
1

            
735
1
		let mut expected = polkadot_runtime_parachains::hrmp::Call::<
736
1
			westend_runtime::Runtime
737
1
		>::hrmp_accept_open_channel {
738
1
			sender: 1000u32.into()
739
1
		}
740
1
		.encode();
741
1
		expected_encoded.append(&mut expected);
742
1

            
743
1
		assert_eq!(
744
1
			<WestendEncoder as xcm_primitives::HrmpEncodeCall>::hrmp_encode_call(
745
1
				xcm_primitives::HrmpAvailableCalls::AcceptOpenChannel(1000u32.into(),)
746
1
			),
747
1
			Ok(expected_encoded.clone())
748
1
		);
749
1
		sp_io::TestExternalities::default().execute_with(|| {
750
1
			// Pallet-xcm-transactor default encoder returns same result
751
1
			// insert storage item as per migration to set the storage item
752
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
753
1
				WESTEND_RELAY_INDICES,
754
1
			);
755
1
			assert_eq!(
756
1
				<pallet_xcm_transactor::Pallet::<
757
1
					moonbase_runtime::Runtime> as xcm_primitives::HrmpEncodeCall
758
1
				>::hrmp_encode_call(
759
1
					xcm_primitives::HrmpAvailableCalls::AcceptOpenChannel(1000u32.into(),)
760
1
				),
761
1
				Ok(expected_encoded)
762
1
			);
763
1
		});
764
1
	}
765

            
766
	#[test]
767
1
	fn test_hrmp_close() {
768
1
		let mut expected_encoded: Vec<u8> = Vec::new();
769
1

            
770
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
771
1
			westend_runtime::Hrmp,
772
1
		>()
773
1
		.unwrap() as u8;
774
1
		expected_encoded.push(index);
775
1

            
776
1
		let mut expected = polkadot_runtime_parachains::hrmp::Call::<
777
1
			westend_runtime::Runtime
778
1
		>::hrmp_close_channel {
779
1
			channel_id: HrmpChannelId {
780
1
				sender: 1000u32.into(),
781
1
				recipient: 1001u32.into()
782
1
			}
783
1
		}
784
1
		.encode();
785
1
		expected_encoded.append(&mut expected);
786
1

            
787
1
		assert_eq!(
788
1
			<WestendEncoder as xcm_primitives::HrmpEncodeCall>::hrmp_encode_call(
789
1
				xcm_primitives::HrmpAvailableCalls::CloseChannel(HrmpChannelId {
790
1
					sender: 1000u32.into(),
791
1
					recipient: 1001u32.into()
792
1
				})
793
1
			),
794
1
			Ok(expected_encoded.clone())
795
1
		);
796
1
		sp_io::TestExternalities::default().execute_with(|| {
797
1
			// Pallet-xcm-transactor default encoder returns same result
798
1
			// insert storage item as per migration to set the storage item
799
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
800
1
				WESTEND_RELAY_INDICES,
801
1
			);
802
1
			assert_eq!(
803
1
				<pallet_xcm_transactor::Pallet::<
804
1
					moonbase_runtime::Runtime> as xcm_primitives::HrmpEncodeCall
805
1
				>::hrmp_encode_call(
806
1
					xcm_primitives::HrmpAvailableCalls::CloseChannel(HrmpChannelId {
807
1
						sender: 1000u32.into(),
808
1
						recipient: 1001u32.into()
809
1
					})
810
1
				),
811
1
				Ok(expected_encoded)
812
1
			);
813
1
		});
814
1
	}
815

            
816
	#[test]
817
1
	fn test_hrmp_cancel() {
818
1
		let mut expected_encoded: Vec<u8> = Vec::new();
819
1

            
820
1
		let index = <westend_runtime::Runtime as frame_system::Config>::PalletInfo::index::<
821
1
			westend_runtime::Hrmp,
822
1
		>()
823
1
		.unwrap() as u8;
824
1
		expected_encoded.push(index);
825
1

            
826
1
		let channel_id = HrmpChannelId {
827
1
			sender: 1u32.into(),
828
1
			recipient: 1u32.into(),
829
1
		};
830
1
		let open_requests: u32 = 1;
831
1

            
832
1
		let mut expected = polkadot_runtime_parachains::hrmp::Call::<
833
1
			westend_runtime::Runtime
834
1
		>::hrmp_cancel_open_request {
835
1
			channel_id: channel_id.clone(),
836
1
			open_requests
837
1
		}
838
1
		.encode();
839
1
		expected_encoded.append(&mut expected);
840
1

            
841
1
		assert_eq!(
842
1
			<WestendEncoder as xcm_primitives::HrmpEncodeCall>::hrmp_encode_call(
843
1
				xcm_primitives::HrmpAvailableCalls::CancelOpenRequest(
844
1
					channel_id.clone(),
845
1
					open_requests
846
1
				)
847
1
			),
848
1
			Ok(expected_encoded.clone())
849
1
		);
850
1
		sp_io::TestExternalities::default().execute_with(|| {
851
1
			// Pallet-xcm-transactor default encoder returns same result
852
1
			// insert storage item as per migration to set the storage item
853
1
			pallet_xcm_transactor::RelayIndices::<moonbase_runtime::Runtime>::put(
854
1
				WESTEND_RELAY_INDICES,
855
1
			);
856
1
			assert_eq!(
857
1
				<pallet_xcm_transactor::Pallet::<
858
1
					moonbase_runtime::Runtime> as xcm_primitives::HrmpEncodeCall
859
1
				>::hrmp_encode_call(
860
1
					xcm_primitives::HrmpAvailableCalls::CancelOpenRequest(
861
1
						channel_id.clone(),
862
1
						open_requests
863
1
					)
864
1
				),
865
1
				Ok(expected_encoded)
866
1
			);
867
1
		});
868
1
	}
869
}