pallet_evm_precompile_xcm_transactor/v1/
mod.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// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Precompile to xcm transactor runtime methods via the EVM
18
19use crate::functions::{CurrencyIdOf, GetDataLimit, TransactorOf, XcmTransactorWrapper};
20use fp_evm::PrecompileHandle;
21use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
22use pallet_evm::AddressMapping;
23use precompile_utils::prelude::*;
24use sp_core::{H160, U256};
25use sp_runtime::traits::Dispatchable;
26use sp_std::{convert::TryFrom, marker::PhantomData};
27use xcm::latest::Location;
28use xcm_primitives::AccountIdToCurrencyId;
29
30/// A precompile to wrap the functionality from xcm transactor
31pub struct XcmTransactorPrecompileV1<Runtime>(PhantomData<Runtime>);
32
33#[precompile_utils::precompile]
34impl<Runtime> XcmTransactorPrecompileV1<Runtime>
35where
36	Runtime: pallet_xcm_transactor::Config + pallet_evm::Config + frame_system::Config,
37	Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
38	Runtime::RuntimeCall: From<pallet_xcm_transactor::Call<Runtime>>,
39	TransactorOf<Runtime>: TryFrom<u8>,
40	Runtime::AccountId: Into<H160>,
41	Runtime: AccountIdToCurrencyId<Runtime::AccountId, CurrencyIdOf<Runtime>>,
42	<Runtime as pallet_evm::Config>::AddressMapping: AddressMapping<Runtime::AccountId>,
43{
44	#[precompile::public("indexToAccount(uint16)")]
45	#[precompile::public("index_to_account(uint16)")]
46	#[precompile::view]
47	fn index_to_account(handle: &mut impl PrecompileHandle, index: u16) -> EvmResult<Address> {
48		XcmTransactorWrapper::<Runtime>::account_index(handle, index)
49	}
50
51	#[precompile::public("transactInfo((uint8,bytes[]))")]
52	#[precompile::public("transact_info((uint8,bytes[]))")]
53	#[precompile::view]
54	fn transact_info(
55		handle: &mut impl PrecompileHandle,
56		location: Location,
57	) -> EvmResult<(u64, U256, u64)> {
58		XcmTransactorWrapper::<Runtime>::transact_info(handle, location)
59	}
60
61	#[precompile::public("transactInfoWithSigned((uint8,bytes[]))")]
62	#[precompile::public("transact_info_with_signed((uint8,bytes[]))")]
63	#[precompile::view]
64	fn transact_info_with_signed(
65		handle: &mut impl PrecompileHandle,
66		location: Location,
67	) -> EvmResult<(u64, u64, u64)> {
68		XcmTransactorWrapper::<Runtime>::transact_info_with_signed(handle, location)
69	}
70
71	#[precompile::public("feePerSecond((uint8,bytes[]))")]
72	#[precompile::public("fee_per_second((uint8,bytes[]))")]
73	#[precompile::view]
74	fn fee_per_second(handle: &mut impl PrecompileHandle, location: Location) -> EvmResult<U256> {
75		XcmTransactorWrapper::<Runtime>::fee_per_second(handle, location)
76	}
77
78	#[precompile::public(
79		"transactThroughDerivativeMultilocation(\
80		uint8,\
81		uint16,\
82		(uint8,bytes[]),\
83		uint64,\
84		bytes)"
85	)]
86	#[precompile::public(
87		"transact_through_derivative_multilocation(\
88		uint8,\
89		uint16,\
90		(uint8,bytes[]),\
91		uint64,\
92		bytes)"
93	)]
94	fn transact_through_derivative_multilocation(
95		handle: &mut impl PrecompileHandle,
96		transactor: u8,
97		index: u16,
98		fee_asset: Location,
99		weight: u64,
100		inner_call: BoundedBytes<GetDataLimit>,
101	) -> EvmResult {
102		XcmTransactorWrapper::<Runtime>::transact_through_derivative_multilocation(
103			handle, transactor, index, fee_asset, weight, inner_call,
104		)
105	}
106
107	#[precompile::public("transactThroughDerivative(uint8,uint16,address,uint64,bytes)")]
108	#[precompile::public("transact_through_derivative(uint8,uint16,address,uint64,bytes)")]
109	fn transact_through_derivative(
110		handle: &mut impl PrecompileHandle,
111		transactor: u8,
112		index: u16,
113		currency_id: Address,
114		weight: u64,
115		inner_call: BoundedBytes<GetDataLimit>,
116	) -> EvmResult {
117		XcmTransactorWrapper::<Runtime>::transact_through_derivative(
118			handle,
119			transactor,
120			index,
121			currency_id,
122			weight,
123			inner_call,
124		)
125	}
126
127	#[precompile::public(
128		"transactThroughSignedMultilocation(\
129		(uint8,bytes[]),\
130		(uint8,bytes[]),\
131		uint64,\
132		bytes)"
133	)]
134	#[precompile::public(
135		"transact_through_signed_multilocation(\
136		(uint8,bytes[]),\
137		(uint8,bytes[]),\
138		uint64,\
139		bytes)"
140	)]
141	fn transact_through_signed_multilocation(
142		handle: &mut impl PrecompileHandle,
143		dest: Location,
144		fee_asset: Location,
145		weight: u64,
146		call: BoundedBytes<GetDataLimit>,
147	) -> EvmResult {
148		XcmTransactorWrapper::<Runtime>::transact_through_signed_multilocation(
149			handle, dest, fee_asset, weight, call,
150		)
151	}
152
153	#[precompile::public("transactThroughSigned((uint8,bytes[]),address,uint64,bytes)")]
154	#[precompile::public("transact_through_signed((uint8,bytes[]),address,uint64,bytes)")]
155	fn transact_through_signed(
156		handle: &mut impl PrecompileHandle,
157		dest: Location,
158		fee_asset: Address,
159		weight: u64,
160		call: BoundedBytes<GetDataLimit>,
161	) -> EvmResult {
162		XcmTransactorWrapper::<Runtime>::transact_through_signed(
163			handle, dest, fee_asset, weight, call,
164		)
165	}
166
167	#[precompile::public("encodeUtilityAsDerivative(uint8,uint16,bytes)")]
168	#[precompile::public("encode_utility_as_derivative(uint8,uint16,bytes)")]
169	#[precompile::view]
170	fn encode_utility_as_derivative(
171		handle: &mut impl PrecompileHandle,
172		transactor: u8,
173		index: u16,
174		inner_call: BoundedBytes<GetDataLimit>,
175	) -> EvmResult<UnboundedBytes> {
176		XcmTransactorWrapper::<Runtime>::encode_utility_as_derivative(
177			handle, transactor, index, inner_call,
178		)
179	}
180}