1
// Copyright 2025 Moonbeam foundation
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
use crate::AvailableStakeCalls;
18
use crate::StakeEncodeCall;
19
use pallet_xcm_transactor::chain_indices::AssetHubIndices;
20
use parity_scale_codec::{Decode, Encode};
21
use sp_runtime::traits::{AccountIdLookup, StaticLookup};
22
use sp_runtime::AccountId32;
23
use sp_std::vec::Vec;
24

            
25
/// Test AssetHub indices - using indices similar to real AssetHub
26
pub const TEST_ASSETHUB_INDICES: AssetHubIndices = AssetHubIndices {
27
	// Pallet indices
28
	utility: 40,
29
	proxy: 42,
30
	staking: 80,
31
	nomination_pools: 81,
32
	delegated_staking: 84,
33
	assets: 50,
34
	nfts: 52,
35

            
36
	// Utility call indices
37
	as_derivative: 1,
38
	batch: 0,
39
	batch_all: 2,
40

            
41
	// Proxy call indices
42
	proxy_call: 0,
43
	add_proxy: 1,
44
	remove_proxy: 2,
45

            
46
	// Staking call indices
47
	bond: 0,
48
	bond_extra: 1,
49
	unbond: 2,
50
	withdraw_unbonded: 3,
51
	validate: 4,
52
	nominate: 5,
53
	chill: 6,
54
	set_payee: 7,
55
	set_controller: 8,
56
	rebond: 19,
57
};
58

            
59
#[derive(Encode, Decode)]
60
pub enum AssetHubCall {
61
	#[codec(index = 80u8)]
62
	Stake(StakeCall),
63
}
64

            
65
#[derive(Encode, Decode)]
66
pub enum StakeCall {
67
	#[codec(index = 0u16)]
68
	Bond(
69
		#[codec(compact)] u128,
70
		pallet_staking::RewardDestination<AccountId32>,
71
	),
72
	#[codec(index = 1u16)]
73
	BondExtra(#[codec(compact)] u128),
74
	#[codec(index = 2u16)]
75
	Unbond(#[codec(compact)] u128),
76
	#[codec(index = 3u16)]
77
	WithdrawUnbonded(u32),
78
	#[codec(index = 4u16)]
79
	Validate(pallet_staking::ValidatorPrefs),
80
	#[codec(index = 5u16)]
81
	Nominate(Vec<<AccountIdLookup<AccountId32, ()> as StaticLookup>::Source>),
82
	#[codec(index = 6u16)]
83
	Chill,
84
	#[codec(index = 7u16)]
85
	SetPayee(pallet_staking::RewardDestination<AccountId32>),
86
	#[codec(index = 8u16)]
87
	SetController,
88
	#[codec(index = 19u16)]
89
	Rebond(#[codec(compact)] u128),
90
}
91

            
92
pub struct TestEncoder;
93

            
94
impl StakeEncodeCall<()> for TestEncoder {
95
10
	fn encode_call(_transactor: (), call: AvailableStakeCalls) -> Vec<u8> {
96
10
		match call {
97
1
			AvailableStakeCalls::Bond(bonded_amount, reward_destination) => {
98
1
				AssetHubCall::Stake(StakeCall::Bond(bonded_amount, reward_destination)).encode()
99
			}
100
1
			AvailableStakeCalls::BondExtra(bonded_amount) => {
101
1
				AssetHubCall::Stake(StakeCall::BondExtra(bonded_amount)).encode()
102
			}
103
1
			AvailableStakeCalls::Unbond(bonded_amount) => {
104
1
				AssetHubCall::Stake(StakeCall::Unbond(bonded_amount)).encode()
105
			}
106
1
			AvailableStakeCalls::WithdrawUnbonded(num_slashing_spans) => {
107
1
				AssetHubCall::Stake(StakeCall::WithdrawUnbonded(num_slashing_spans)).encode()
108
			}
109
1
			AvailableStakeCalls::Validate(validator_prefs) => {
110
1
				AssetHubCall::Stake(StakeCall::Validate(validator_prefs)).encode()
111
			}
112
1
			AvailableStakeCalls::Nominate(targets) => {
113
1
				let nominated: Vec<<AccountIdLookup<AccountId32, ()> as StaticLookup>::Source> =
114
2
					targets.iter().map(|add| (*add).clone().into()).collect();
115
1
				AssetHubCall::Stake(StakeCall::Nominate(nominated)).encode()
116
			}
117
1
			AvailableStakeCalls::Chill => AssetHubCall::Stake(StakeCall::Chill).encode(),
118
1
			AvailableStakeCalls::SetPayee(reward_destination) => {
119
1
				AssetHubCall::Stake(StakeCall::SetPayee(reward_destination.into())).encode()
120
			}
121
			AvailableStakeCalls::SetController => {
122
1
				AssetHubCall::Stake(StakeCall::SetController).encode()
123
			}
124
1
			AvailableStakeCalls::Rebond(bonded_amount) => {
125
1
				AssetHubCall::Stake(StakeCall::Rebond(bonded_amount)).encode()
126
			}
127
		}
128
10
	}
129
}