Skip to content

Commit a81e908

Browse files
committed
Adapt XcmTransactor to AssetHub migration (#3544)
* Adapt XcmTransactor to AssetHub migration * add migration * add migration * Re-organize transactors encoding * Revert "Re-organize transactors encoding" This reverts commit eeb2709. * improvements * Revert "add migration" This reverts commit 88a7e3b. * Revert "add migration" This reverts commit 36c2727. * fix encoding * remove moonbeam-relay-encoder * fix ts tests * fix tests * remove unused import * cleanup * fix tests
1 parent 3b6b95a commit a81e908

File tree

38 files changed

+443
-3041
lines changed

38 files changed

+443
-3041
lines changed

Cargo.lock

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ members = [
4141
"runtime/moonbase",
4242
"runtime/moonbeam",
4343
"runtime/moonriver",
44-
"runtime/relay-encoder",
4544
"runtime/summarize-precompile-checks",
4645
]
4746
resolver = "2"
@@ -70,7 +69,6 @@ moonbeam-rpc-primitives-txpool = { path = "primitives/rpc/txpool", default-featu
7069
storage-proof-primitives = { path = "primitives/storage-proof", default-features = false }
7170

7271
moonbeam-evm-tracer = { path = "runtime/evm_tracer", default-features = false }
73-
moonbeam-relay-encoder = { path = "runtime/relay-encoder", default-features = false }
7472
moonbeam-runtime-common = { path = "runtime/common", default-features = false }
7573

7674
pallet-crowdloan-rewards = { path = "pallets/crowdloan-rewards", default-features = false }

pallets/xcm-transactor/src/encode.rs

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,78 +26,73 @@ use sp_runtime::traits::{AccountIdLookup, StaticLookup};
2626
use sp_std::prelude::*;
2727
use xcm_primitives::{
2828
AvailableStakeCalls, HrmpAvailableCalls, HrmpEncodeCall, StakeEncodeCall,
29-
UtilityAvailableCalls, UtilityEncodeCall,
29+
UtilityAvailableCalls, UtilityEncodeCall, XcmTransact,
3030
};
3131

3232
pub use crate::pallet::*;
3333

3434
pub use crate::weights::WeightInfo;
3535

3636
impl<T: Config> UtilityEncodeCall for Pallet<T> {
37-
fn encode_call(self, call: UtilityAvailableCalls) -> Vec<u8> {
37+
fn encode_call<Transactor: XcmTransact>(
38+
transactor: Transactor,
39+
call: UtilityAvailableCalls,
40+
) -> Vec<u8> {
41+
let mut encoded_call: Vec<u8> = Vec::new();
42+
// pallet index
43+
encoded_call.push(transactor.utility_pallet_index());
44+
3845
match call {
3946
UtilityAvailableCalls::AsDerivative(a, b) => {
40-
let mut encoded_call: Vec<u8> = Vec::new();
41-
// pallet index
42-
encoded_call.push(RelayIndices::<T>::get().utility);
4347
// call index
4448
encoded_call.push(RelayIndices::<T>::get().as_derivative);
4549
// encoded argument
4650
encoded_call.append(&mut a.encode());
4751
encoded_call.append(&mut b.clone());
48-
encoded_call
4952
}
5053
}
54+
55+
encoded_call
5156
}
5257
}
5358

5459
impl<T: Config> HrmpEncodeCall for Pallet<T> {
5560
fn hrmp_encode_call(call: HrmpAvailableCalls) -> Result<Vec<u8>, xcm::latest::Error> {
61+
let mut encoded_call: Vec<u8> = Vec::new();
62+
// pallet index
63+
encoded_call.push(RelayIndices::<T>::get().hrmp);
64+
5665
match call {
5766
HrmpAvailableCalls::InitOpenChannel(a, b, c) => {
58-
let mut encoded_call: Vec<u8> = Vec::new();
59-
// pallet index
60-
encoded_call.push(RelayIndices::<T>::get().hrmp);
6167
// call index
6268
encoded_call.push(RelayIndices::<T>::get().init_open_channel);
6369
// encoded arguments
6470
encoded_call.append(&mut a.encode());
6571
encoded_call.append(&mut b.encode());
6672
encoded_call.append(&mut c.encode());
67-
Ok(encoded_call)
6873
}
6974
HrmpAvailableCalls::AcceptOpenChannel(a) => {
70-
let mut encoded_call: Vec<u8> = Vec::new();
71-
// pallet index
72-
encoded_call.push(RelayIndices::<T>::get().hrmp);
7375
// call index
7476
encoded_call.push(RelayIndices::<T>::get().accept_open_channel);
7577
// encoded argument
7678
encoded_call.append(&mut a.encode());
77-
Ok(encoded_call)
7879
}
7980
HrmpAvailableCalls::CloseChannel(a) => {
80-
let mut encoded_call: Vec<u8> = Vec::new();
81-
// pallet index
82-
encoded_call.push(RelayIndices::<T>::get().hrmp);
8381
// call index
8482
encoded_call.push(RelayIndices::<T>::get().close_channel);
8583
// encoded argument
8684
encoded_call.append(&mut a.encode());
87-
Ok(encoded_call)
8885
}
8986
HrmpAvailableCalls::CancelOpenRequest(a, b) => {
90-
let mut encoded_call: Vec<u8> = Vec::new();
91-
// pallet index
92-
encoded_call.push(RelayIndices::<T>::get().hrmp);
9387
// call index
9488
encoded_call.push(RelayIndices::<T>::get().cancel_open_request);
9589
// encoded argument
9690
encoded_call.append(&mut a.encode());
9791
encoded_call.append(&mut b.encode());
98-
Ok(encoded_call)
9992
}
10093
}
94+
95+
Ok(encoded_call)
10196
}
10297
}
10398

@@ -111,116 +106,76 @@ fn encode_compact_arg<T: parity_scale_codec::HasCompact>(input: T) -> Vec<u8> {
111106
}
112107

113108
impl<T: Config> StakeEncodeCall for Pallet<T> {
114-
fn encode_call(call: AvailableStakeCalls) -> Vec<u8> {
109+
fn encode_call<Transactor: XcmTransact>(
110+
transactor: Transactor,
111+
call: AvailableStakeCalls,
112+
) -> Vec<u8> {
113+
let mut encoded_call: Vec<u8> = Vec::new();
114+
// pallet index
115+
encoded_call.push(transactor.staking_pallet_index());
116+
115117
match call {
116118
AvailableStakeCalls::Bond(b, c) => {
117-
let mut encoded_call: Vec<u8> = Vec::new();
118-
// pallet index
119-
encoded_call.push(RelayIndices::<T>::get().staking);
120119
// call index
121120
encoded_call.push(RelayIndices::<T>::get().bond);
122121
// encoded arguments
123122
encoded_call.append(&mut encode_compact_arg(b));
124123
encoded_call.append(&mut c.encode());
125-
encoded_call
126124
}
127-
128125
AvailableStakeCalls::BondExtra(a) => {
129-
let mut encoded_call: Vec<u8> = Vec::new();
130-
// pallet index
131-
encoded_call.push(RelayIndices::<T>::get().staking);
132126
// call index
133127
encoded_call.push(RelayIndices::<T>::get().bond_extra);
134128
// encoded argument
135129
encoded_call.append(&mut encode_compact_arg(a));
136-
encoded_call
137130
}
138-
139131
AvailableStakeCalls::Unbond(a) => {
140-
let mut encoded_call: Vec<u8> = Vec::new();
141-
// pallet index
142-
encoded_call.push(RelayIndices::<T>::get().staking);
143132
// call index
144133
encoded_call.push(RelayIndices::<T>::get().unbond);
145134
// encoded argument
146135
encoded_call.append(&mut encode_compact_arg(a));
147-
encoded_call
148136
}
149-
150137
AvailableStakeCalls::WithdrawUnbonded(a) => {
151-
let mut encoded_call: Vec<u8> = Vec::new();
152-
// pallet index
153-
encoded_call.push(RelayIndices::<T>::get().staking);
154138
// call index
155139
encoded_call.push(RelayIndices::<T>::get().withdraw_unbonded);
156140
// encoded argument
157141
encoded_call.append(&mut a.encode());
158-
encoded_call
159142
}
160-
161143
AvailableStakeCalls::Validate(a) => {
162-
let mut encoded_call: Vec<u8> = Vec::new();
163-
// pallet index
164-
encoded_call.push(RelayIndices::<T>::get().staking);
165144
// call index
166145
encoded_call.push(RelayIndices::<T>::get().validate);
167146
// encoded argument
168147
encoded_call.append(&mut a.encode());
169-
encoded_call
170148
}
171-
172149
AvailableStakeCalls::Chill => {
173-
let mut encoded_call: Vec<u8> = Vec::new();
174-
// pallet index
175-
encoded_call.push(RelayIndices::<T>::get().staking);
176150
// call index
177151
encoded_call.push(RelayIndices::<T>::get().chill);
178-
encoded_call
179152
}
180-
181153
AvailableStakeCalls::SetPayee(a) => {
182-
let mut encoded_call: Vec<u8> = Vec::new();
183-
// pallet index
184-
encoded_call.push(RelayIndices::<T>::get().staking);
185154
// call index
186155
encoded_call.push(RelayIndices::<T>::get().set_payee);
187156
// encoded argument
188157
encoded_call.append(&mut a.encode());
189-
encoded_call
190158
}
191-
192159
AvailableStakeCalls::SetController => {
193-
let mut encoded_call: Vec<u8> = Vec::new();
194-
// pallet index
195-
encoded_call.push(RelayIndices::<T>::get().staking);
196160
// call index
197161
encoded_call.push(RelayIndices::<T>::get().set_controller);
198-
encoded_call
199162
}
200-
201163
AvailableStakeCalls::Rebond(a) => {
202-
let mut encoded_call: Vec<u8> = Vec::new();
203-
// pallet index
204-
encoded_call.push(RelayIndices::<T>::get().staking);
205164
// call index
206165
encoded_call.push(RelayIndices::<T>::get().rebond);
207166
// encoded argument
208167
encoded_call.append(&mut encode_compact_arg(a));
209-
encoded_call
210168
}
211-
212169
AvailableStakeCalls::Nominate(a) => {
213-
let mut encoded_call: Vec<u8> = Vec::new();
214-
// pallet index
215-
encoded_call.push(RelayIndices::<T>::get().staking);
216170
// call index
217171
encoded_call.push(RelayIndices::<T>::get().nominate);
218172
let nominated: Vec<
219173
<AccountIdLookup<sp_runtime::AccountId32, ()> as StaticLookup>::Source,
220174
> = a.iter().map(|add| (*add).clone().into()).collect();
221175
encoded_call.append(&mut nominated.encode());
222-
encoded_call
223176
}
224177
}
178+
179+
encoded_call
225180
}
226181
}

pallets/xcm-transactor/src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ pub use crate::weights::WeightInfo;
8787

8888
type CurrencyIdOf<T> = <T as Config>::CurrencyId;
8989

90+
/// TODO: Temporary (Will be removed when we have a proper way of getting pallet indices)
91+
/// Same index on both Polkadot and Kusama Asset Hub
92+
/// Kusama: https://github.com/polkadot-fellows/runtimes/blob/release-v2.0.0/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs#L1596
93+
/// Polkadot: https://github.com/polkadot-fellows/runtimes/blob/release-v2.0.0/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs#L1400
94+
pub const ASSET_HUB_UTILITY_PALLET_INDEX: u8 = 40;
95+
96+
/// TODO: Temporary (Will be removed when we have a proper way of getting pallet indices)
97+
/// Same index on both Polkadot and Kusama Asset Hub
98+
/// Kusama: https://github.com/polkadot-fellows/runtimes/blob/release-v2.0.0/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs#L1628
99+
/// Polkadot: https://github.com/polkadot-fellows/runtimes/blob/release-v2.0.0/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs#L1434
100+
pub const ASSET_HUB_STAKING_PALLET_INDEX: u8 = 89;
101+
90102
#[pallet]
91103
pub mod pallet {
92104

@@ -563,9 +575,10 @@ pub mod pallet {
563575

564576
// Encode call bytes
565577
// We make sure the inner call is wrapped on a as_derivative dispatchable
566-
let call_bytes: Vec<u8> = dest
567-
.clone()
568-
.encode_call(UtilityAvailableCalls::AsDerivative(index, inner_call));
578+
let call_bytes: Vec<u8> = <Self as UtilityEncodeCall>::encode_call(
579+
dest.clone(),
580+
UtilityAvailableCalls::AsDerivative(index, inner_call),
581+
);
569582

570583
// Grab the destination
571584
let dest = dest.destination();

pallets/xcm-transactor/src/mock.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use xcm::latest::{
3737
XcmHash,
3838
};
3939
use xcm::{IntoVersion, VersionedXcm, WrapVersion};
40-
use xcm_primitives::{UtilityAvailableCalls, UtilityEncodeCall, XcmTransact};
40+
use xcm_primitives::XcmTransact;
4141

4242
use sp_std::cell::RefCell;
4343
use xcm_executor::{
@@ -306,20 +306,13 @@ impl XcmTransact for Transactors {
306306
Transactors::Relay => Location::parent(),
307307
}
308308
}
309-
}
310309

311-
impl UtilityEncodeCall for Transactors {
312-
fn encode_call(self, call: UtilityAvailableCalls) -> Vec<u8> {
313-
match self {
314-
Transactors::Relay => match call {
315-
UtilityAvailableCalls::AsDerivative(a, b) => {
316-
let mut call =
317-
RelayCall::Utility(UtilityCall::AsDerivative(a.clone())).encode();
318-
call.append(&mut b.clone());
319-
call
320-
}
321-
},
322-
}
310+
fn utility_pallet_index(&self) -> u8 {
311+
RelayIndices::<Test>::get().utility
312+
}
313+
314+
fn staking_pallet_index(&self) -> u8 {
315+
RelayIndices::<Test>::get().staking
323316
}
324317
}
325318

0 commit comments

Comments
 (0)