1
// Copyright 2019-2022 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
#[macro_export]
18
macro_rules! impl_evm_runner_precompile_or_eth_xcm {
19
	{} => {
20
		use fp_evm::{CallInfo, CallOrCreateInfo, Context, Transfer};
21
		use frame_support::dispatch::CallableCallFor;
22
		use pallet_evm::{Runner, RunnerError};
23
		use precompile_utils::{prelude::*, evm::handle::with_precompile_handle};
24
		use sp_core::U256;
25
		use sp_runtime::DispatchError;
26
		use sp_std::vec::Vec;
27
		use xcm_primitives::{EthereumXcmTransaction, EthereumXcmTransactionV2};
28

            
29
		pub struct EvmRunnerPrecompileOrEthXcm<CallDispatcher, Runtime>(
30
			core::marker::PhantomData<(CallDispatcher, Runtime)>,
31
		);
32

            
33
		impl<CallDispatcher, Runtime> Runner<Runtime>
34
			for EvmRunnerPrecompileOrEthXcm<CallDispatcher, Runtime>
35
		where
36
			CallDispatcher: xcm_executor::traits::CallDispatcher<RuntimeCall>,
37
			Runtime: pallet_evm::Config + pallet_ethereum_xcm::Config,
38
			Runtime::RuntimeOrigin: From<pallet_ethereum_xcm::RawOrigin>,
39
		{
40
			type Error = DispatchError;
41

            
42
24
			fn call(
43
24
				source: H160,
44
24
				target: H160,
45
24
				input: Vec<u8>,
46
24
				value: U256,
47
24
				gas_limit: u64,
48
24
				_max_fee_per_gas: Option<U256>,
49
24
				_max_priority_fee_per_gas: Option<U256>,
50
24
				_nonce: Option<U256>,
51
24
				access_list: Vec<(H160, Vec<H256>)>,
52
24
				_is_transactional: bool,
53
24
				_validate: bool,
54
24
				_weight_limit: Option<Weight>,
55
24
				_transaction_len: Option<u64>,
56
24
				_config: &fp_evm::Config,
57
24
			) -> Result<CallInfo, RunnerError<Self::Error>> {
58
				// The `with_precompile_handle` function will execute the closure (and return the
59
				// result in a Some) if and only if there is an available EVM context. Otherwise,
60
				// it will return None.
61
24
				if let Some((exit_reason, value)) = with_precompile_handle(|precompile_handle| {
62
					let transfer = if value.is_zero() {
63
						None
64
					} else {
65
						Some(Transfer {
66
							source,
67
							target,
68
							value,
69
						})
70
					};
71

            
72
					precompile_handle.call(
73
						target,
74
						transfer,
75
						input.clone(),
76
						Some(gas_limit),
77
						false,
78
						&Context {
79
							address: target,
80
							caller: source,
81
							apparent_value: value,
82
						},
83
					)
84
24
				}) {
85
16
					Ok(CallInfo {
86
16
						exit_reason,
87
16
						value,
88
16
						used_gas: fp_evm::UsedGas {
89
16
							standard: U256::default(),
90
16
							effective: U256::default(),
91
16
						},
92
16
						logs: Default::default(),
93
16
						weight_info: None,
94
16
					})
95
				} else {
96
8
					let xcm_transaction = EthereumXcmTransaction::V2(EthereumXcmTransactionV2 {
97
8
						gas_limit: gas_limit.into(),
98
8
						action: pallet_ethereum_xcm::TransactionAction::Call(target),
99
8
						value,
100
8
						input: input.try_into().map_err(|_| RunnerError {
101
							error: DispatchError::Exhausted,
102
							weight: Default::default(),
103
8
						})?,
104
8
						access_list: Some(access_list),
105
8
					});
106
8

            
107
8
					let mut execution_info: Option<CallOrCreateInfo> = None;
108
8
					pallet_ethereum::catch_exec_info(&mut execution_info, || {
109
						CallDispatcher::dispatch(
110
							RuntimeCall::EthereumXcm(pallet_ethereum_xcm::Call::transact { xcm_transaction }),
111
							RawOrigin::Signed(source.into()).into(),
112
						)
113
						.map_err(|DispatchErrorWithPostInfo { error, .. }| RunnerError {
114
							error,
115
							weight: Default::default(),
116
						})
117
8
					})?;
118

            
119
8
					if let Some(CallOrCreateInfo::Call(call_info))= execution_info {
120
8
						Ok(call_info)
121
					} else {
122
						// `execution_info` must have been filled in
123
						Err(RunnerError {
124
							error: DispatchError::Unavailable,
125
							weight: Default::default(),
126
						})
127
					}
128
				}
129
24
			}
130
24

            
131
24
			fn create(
132
				_source: H160,
133
				_init: Vec<u8>,
134
				_value: U256,
135
				_gas_limit: u64,
136
				_max_fee_per_gas: Option<U256>,
137
				_max_priority_fee_per_gas: Option<U256>,
138
				_nonce: Option<U256>,
139
				_access_list: Vec<(H160, Vec<H256>)>,
140
				_is_transactional: bool,
141
				_validate: bool,
142
				_weight_limit: Option<Weight>,
143
				_transaction_len: Option<u64>,
144
				_config: &fp_evm::Config,
145
			) -> Result<fp_evm::CreateInfo, RunnerError<Self::Error>> {
146
				unimplemented!()
147
			}
148

            
149
			fn create2(
150
				_source: H160,
151
				_init: Vec<u8>,
152
				_salt: H256,
153
				_value: U256,
154
				_gas_limit: u64,
155
				_max_fee_per_gas: Option<U256>,
156
				_max_priority_fee_per_gas: Option<U256>,
157
				_nonce: Option<U256>,
158
				_access_list: Vec<(H160, Vec<H256>)>,
159
				_is_transactional: bool,
160
				_validate: bool,
161
				_weight_limit: Option<Weight>,
162
				_transaction_len: Option<u64>,
163
				_config: &fp_evm::Config,
164
			) -> Result<fp_evm::CreateInfo, RunnerError<Self::Error>> {
165
				unimplemented!()
166
			}
167

            
168
5
			fn create_force_address(
169
5
				source: H160,
170
5
				init: Vec<u8>,
171
5
				value: U256,
172
5
				gas_limit: u64,
173
5
				max_fee_per_gas: Option<U256>,
174
5
				max_priority_fee_per_gas: Option<U256>,
175
5
				nonce: Option<U256>,
176
5
				access_list: Vec<(H160, Vec<H256>)>,
177
5
				is_transactional: bool,
178
5
				validate: bool,
179
5
				weight_limit: Option<Weight>,
180
5
				transaction_len: Option<u64>,
181
5
				config: &fp_evm::Config,
182
5
				force_address: H160,
183
5
			) -> Result<fp_evm::CreateInfo, RunnerError<Self::Error>> {
184
5
				let xcm_transaction = EthereumXcmTransaction::V2(EthereumXcmTransactionV2 {
185
5
					gas_limit: gas_limit.into(),
186
5
					action: pallet_ethereum_xcm::TransactionAction::Create,
187
5
					value,
188
5
					input: init.try_into().map_err(|_| RunnerError {
189
						error: DispatchError::Exhausted,
190
						weight: Default::default(),
191
5
					})?,
192
5
					access_list: Some(access_list),
193
5
				});
194
5

            
195
5
				let mut execution_info: Option<CallOrCreateInfo> = None;
196
5
				pallet_ethereum::catch_exec_info(&mut execution_info, || {
197
					CallDispatcher::dispatch(
198
						RuntimeCall::EthereumXcm(pallet_ethereum_xcm::Call::force_transact_as {
199
							transact_as: source,
200
							xcm_transaction,
201
							force_create_address: Some(force_address),
202
						}),
203
						RawOrigin::Root.into(),
204
					)
205
					.map_err(|DispatchErrorWithPostInfo { error, .. }| RunnerError {
206
						error,
207
						weight: Default::default(),
208
					})
209
5
				})?;
210

            
211
5
				if let Some(CallOrCreateInfo::Create(create_info))= execution_info {
212
5
					Ok(create_info)
213
				} else {
214
					// `execution_info` must have been filled in
215
					Err(RunnerError {
216
						error: DispatchError::Unavailable,
217
						weight: Default::default(),
218
					})
219
				}
220
5
			}
221
5

            
222
5
			fn validate(
223
				_source: H160,
224
				_target: Option<H160>,
225
				_input: Vec<u8>,
226
				_value: U256,
227
				_gas_limit: u64,
228
				_max_fee_per_gas: Option<U256>,
229
				_max_priority_fee_per_gas: Option<U256>,
230
				_nonce: Option<U256>,
231
				_access_list: Vec<(H160, Vec<H256>)>,
232
				_is_transactional: bool,
233
				_weight_limit: Option<Weight>,
234
				_transaction_len: Option<u64>,
235
				_evm_config: &fp_evm::Config,
236
			) -> Result<(), RunnerError<Self::Error>> {
237
				unimplemented!()
238
			}
239
		}
240

            
241
	}
242
}