1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

//! Precompile to xcm transactor runtime methods via the EVM

use fp_evm::PrecompileHandle;
use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};

use crate::functions::{CurrencyIdOf, GetDataLimit, TransactorOf, XcmTransactorWrapper};
use precompile_utils::prelude::*;
use sp_core::{H160, U256};
use sp_runtime::traits::Dispatchable;
use sp_std::{convert::TryFrom, marker::PhantomData};
use xcm::latest::Location;
use xcm_primitives::AccountIdToCurrencyId;

/// A precompile to wrap the functionality from xcm transactor
pub struct XcmTransactorPrecompileV1<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> XcmTransactorPrecompileV1<Runtime>
where
	Runtime: pallet_xcm_transactor::Config + pallet_evm::Config + frame_system::Config,
	Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
	Runtime::RuntimeCall: From<pallet_xcm_transactor::Call<Runtime>>,
	<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
	TransactorOf<Runtime>: TryFrom<u8>,
	Runtime::AccountId: Into<H160>,
	Runtime: AccountIdToCurrencyId<Runtime::AccountId, CurrencyIdOf<Runtime>>,
{
	#[precompile::public("indexToAccount(uint16)")]
	#[precompile::public("index_to_account(uint16)")]
	#[precompile::view]
	fn index_to_account(handle: &mut impl PrecompileHandle, index: u16) -> EvmResult<Address> {
		XcmTransactorWrapper::<Runtime>::account_index(handle, index)
	}

	#[precompile::public("transactInfo((uint8,bytes[]))")]
	#[precompile::public("transact_info((uint8,bytes[]))")]
	#[precompile::view]
	fn transact_info(
		handle: &mut impl PrecompileHandle,
		location: Location,
	) -> EvmResult<(u64, U256, u64)> {
		XcmTransactorWrapper::<Runtime>::transact_info(handle, location)
	}

	#[precompile::public("transactInfoWithSigned((uint8,bytes[]))")]
	#[precompile::public("transact_info_with_signed((uint8,bytes[]))")]
	#[precompile::view]
	fn transact_info_with_signed(
		handle: &mut impl PrecompileHandle,
		location: Location,
	) -> EvmResult<(u64, u64, u64)> {
		XcmTransactorWrapper::<Runtime>::transact_info_with_signed(handle, location)
	}

	#[precompile::public("feePerSecond((uint8,bytes[]))")]
	#[precompile::public("fee_per_second((uint8,bytes[]))")]
	#[precompile::view]
	fn fee_per_second(handle: &mut impl PrecompileHandle, location: Location) -> EvmResult<U256> {
		XcmTransactorWrapper::<Runtime>::fee_per_second(handle, location)
	}

	#[precompile::public(
		"transactThroughDerivativeMultilocation(\
		uint8,\
		uint16,\
		(uint8,bytes[]),\
		uint64,\
		bytes)"
	)]
	#[precompile::public(
		"transact_through_derivative_multilocation(\
		uint8,\
		uint16,\
		(uint8,bytes[]),\
		uint64,\
		bytes)"
	)]
	fn transact_through_derivative_multilocation(
		handle: &mut impl PrecompileHandle,
		transactor: u8,
		index: u16,
		fee_asset: Location,
		weight: u64,
		inner_call: BoundedBytes<GetDataLimit>,
	) -> EvmResult {
		XcmTransactorWrapper::<Runtime>::transact_through_derivative_multilocation(
			handle, transactor, index, fee_asset, weight, inner_call,
		)
	}

	#[precompile::public("transactThroughDerivative(uint8,uint16,address,uint64,bytes)")]
	#[precompile::public("transact_through_derivative(uint8,uint16,address,uint64,bytes)")]
	fn transact_through_derivative(
		handle: &mut impl PrecompileHandle,
		transactor: u8,
		index: u16,
		currency_id: Address,
		weight: u64,
		inner_call: BoundedBytes<GetDataLimit>,
	) -> EvmResult {
		XcmTransactorWrapper::<Runtime>::transact_through_derivative(
			handle,
			transactor,
			index,
			currency_id,
			weight,
			inner_call,
		)
	}

	#[precompile::public(
		"transactThroughSignedMultilocation(\
		(uint8,bytes[]),\
		(uint8,bytes[]),\
		uint64,\
		bytes)"
	)]
	#[precompile::public(
		"transact_through_signed_multilocation(\
		(uint8,bytes[]),\
		(uint8,bytes[]),\
		uint64,\
		bytes)"
	)]
	fn transact_through_signed_multilocation(
		handle: &mut impl PrecompileHandle,
		dest: Location,
		fee_asset: Location,
		weight: u64,
		call: BoundedBytes<GetDataLimit>,
	) -> EvmResult {
		XcmTransactorWrapper::<Runtime>::transact_through_signed_multilocation(
			handle, dest, fee_asset, weight, call,
		)
	}

	#[precompile::public("transactThroughSigned((uint8,bytes[]),address,uint64,bytes)")]
	#[precompile::public("transact_through_signed((uint8,bytes[]),address,uint64,bytes)")]
	fn transact_through_signed(
		handle: &mut impl PrecompileHandle,
		dest: Location,
		fee_asset: Address,
		weight: u64,
		call: BoundedBytes<GetDataLimit>,
	) -> EvmResult {
		XcmTransactorWrapper::<Runtime>::transact_through_signed(
			handle, dest, fee_asset, weight, call,
		)
	}

	#[precompile::public("encodeUtilityAsDerivative(uint8,uint16,bytes)")]
	#[precompile::public("encode_utility_as_derivative(uint8,uint16,bytes)")]
	#[precompile::view]
	fn encode_utility_as_derivative(
		handle: &mut impl PrecompileHandle,
		transactor: u8,
		index: u16,
		inner_call: BoundedBytes<GetDataLimit>,
	) -> EvmResult<UnboundedBytes> {
		XcmTransactorWrapper::<Runtime>::encode_utility_as_derivative(
			handle, transactor, index, inner_call,
		)
	}
}