xcm_primitives/
transactor_traits.rs

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
16use cumulus_primitives_core::{
17	relay_chain::{self, HrmpChannelId},
18	ParaId,
19};
20use sp_std::vec::Vec;
21use xcm::latest::{Error as XcmError, Location};
22
23// The utility calls that need to be implemented as part of
24// using a derivative account from a certain account
25#[derive(Debug, PartialEq, Eq)]
26pub enum UtilityAvailableCalls {
27	AsDerivative(u16, Vec<u8>),
28}
29
30// The hrmp calls that need to be implemented as part of
31// HRMP management process
32#[derive(Debug, PartialEq, Eq)]
33pub enum HrmpAvailableCalls {
34	InitOpenChannel(ParaId, u32, u32),
35	AcceptOpenChannel(ParaId),
36	CloseChannel(HrmpChannelId),
37	CancelOpenRequest(HrmpChannelId, u32),
38}
39
40// Trait that the ensures we can encode a call with utility functions.
41// With this trait we ensure that the user cannot control entirely the call
42// to be performed in the destination chain. It only can control the call inside
43// the as_derivative extrinsic, and thus, this call can only be dispatched from the
44// derivative account
45pub trait UtilityEncodeCall {
46	fn encode_call<Transactor: XcmTransact>(
47		transactor: Transactor,
48		call: UtilityAvailableCalls,
49	) -> Vec<u8>;
50}
51
52// Trait that the ensures we can encode a call with hrmp functions.
53// With this trait we ensure that the user cannot control entirely the call.
54// to be performed in the destination chain.
55pub trait HrmpEncodeCall {
56	fn hrmp_encode_call(call: HrmpAvailableCalls) -> Result<Vec<u8>, XcmError>;
57}
58
59impl HrmpEncodeCall for () {
60	fn hrmp_encode_call(_call: HrmpAvailableCalls) -> Result<Vec<u8>, XcmError> {
61		Err(XcmError::Unimplemented)
62	}
63}
64
65// Trait to ensure we can retrieve the destination if a given type
66// It must implement UtilityEncodeCall
67pub trait XcmTransact {
68	/// Encode call from the relay.
69	fn destination(self) -> Location;
70
71	fn utility_pallet_index(&self) -> u8;
72
73	fn staking_pallet_index(&self) -> u8;
74}
75
76pub enum AvailableStakeCalls {
77	Bond(
78		relay_chain::Balance,
79		pallet_staking::RewardDestination<relay_chain::AccountId>,
80	),
81	BondExtra(relay_chain::Balance),
82	Unbond(relay_chain::Balance),
83	WithdrawUnbonded(u32),
84	Validate(pallet_staking::ValidatorPrefs),
85	Nominate(Vec<relay_chain::AccountId>),
86	Chill,
87	SetPayee(pallet_staking::RewardDestination<relay_chain::AccountId>),
88	SetController,
89	Rebond(relay_chain::Balance),
90}
91
92pub trait StakeEncodeCall {
93	/// Encode call from the relay.
94	fn encode_call<Transactor: XcmTransact>(
95		transactor: Transactor,
96		call: AvailableStakeCalls,
97	) -> Vec<u8>;
98}