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
//! Pallet that allow to transact erc20 tokens trought xcm directly.
18

            
19
#![cfg_attr(not(feature = "std"), no_std)]
20

            
21
#[cfg(test)]
22
mod mock;
23
#[cfg(test)]
24
mod tests;
25

            
26
mod erc20_matcher;
27
mod erc20_trap;
28
mod errors;
29
mod xcm_holding_ext;
30

            
31
use frame_support::pallet;
32

            
33
pub use erc20_trap::AssetTrapWrapper;
34
pub use pallet::*;
35
pub use xcm_holding_ext::XcmExecutorWrapper;
36

            
37
60
#[pallet]
38
pub mod pallet {
39

            
40
	use crate::erc20_matcher::*;
41
	use crate::errors::*;
42
	use crate::xcm_holding_ext::*;
43
	use ethereum_types::BigEndianHash;
44
	use fp_evm::{ExitReason, ExitSucceed};
45
	use frame_support::pallet_prelude::*;
46
	use pallet_evm::{GasWeightMapping, Runner};
47
	use sp_core::{H160, H256, U256};
48
	use sp_std::vec::Vec;
49
	use xcm::latest::{
50
		Asset, AssetId, Error as XcmError, Junction, Location, Result as XcmResult, XcmContext,
51
	};
52
	use xcm_executor::traits::ConvertLocation;
53
	use xcm_executor::traits::{Error as MatchError, MatchesFungibles};
54
	use xcm_executor::AssetsInHolding;
55

            
56
	const ERC20_TRANSFER_CALL_DATA_SIZE: usize = 4 + 32 + 32; // selector + from + amount
57
	const ERC20_TRANSFER_SELECTOR: [u8; 4] = [0xa9, 0x05, 0x9c, 0xbb];
58

            
59
64
	#[pallet::pallet]
60
	pub struct Pallet<T>(PhantomData<T>);
61

            
62
	#[pallet::config]
63
	pub trait Config: frame_system::Config + pallet_evm::Config {
64
		type AccountIdConverter: ConvertLocation<H160>;
65
		type Erc20MultilocationPrefix: Get<Location>;
66
		type Erc20TransferGasLimit: Get<u64>;
67
		type EvmRunner: Runner<Self>;
68
	}
69

            
70
	impl<T: Config> Pallet<T> {
71
140
		pub fn is_erc20_asset(asset: &Asset) -> bool {
72
140
			Erc20Matcher::<T::Erc20MultilocationPrefix>::is_erc20_asset(asset)
73
140
		}
74
3
		pub fn gas_limit_of_erc20_transfer(asset_id: &AssetId) -> u64 {
75
3
			let location = &asset_id.0;
76
			if let Some(Junction::GeneralKey {
77
				length: _,
78
3
				ref data,
79
3
			}) = location.interior().into_iter().next_back()
80
			{
81
				// As GeneralKey definition might change in future versions of XCM, this is meant
82
				// to throw a compile error as a warning that data type has changed.
83
				// If that happens, a new check is needed to ensure that data has at least 18
84
				// bytes (size of b"gas_limit:" + u64)
85
3
				let data: &[u8; 32] = &data;
86
3
				if let Ok(content) = core::str::from_utf8(&data[0..10]) {
87
2
					if content == "gas_limit:" {
88
1
						let mut bytes: [u8; 8] = Default::default();
89
1
						bytes.copy_from_slice(&data[10..18]);
90
1
						return u64::from_le_bytes(bytes);
91
1
					}
92
1
				}
93
			}
94
2
			T::Erc20TransferGasLimit::get()
95
3
		}
96
		pub fn weight_of_erc20_transfer(asset_id: &AssetId) -> Weight {
97
			T::GasWeightMapping::gas_to_weight(Self::gas_limit_of_erc20_transfer(asset_id), true)
98
		}
99
		fn erc20_transfer(
100
			erc20_contract_address: H160,
101
			from: H160,
102
			to: H160,
103
			amount: U256,
104
			gas_limit: u64,
105
		) -> Result<(), Erc20TransferError> {
106
			let mut input = Vec::with_capacity(ERC20_TRANSFER_CALL_DATA_SIZE);
107
			// ERC20.transfer method hash
108
			input.extend_from_slice(&ERC20_TRANSFER_SELECTOR);
109
			// append receiver address
110
			input.extend_from_slice(H256::from(to).as_bytes());
111
			// append amount to be transferred
112
			input.extend_from_slice(H256::from_uint(&amount).as_bytes());
113

            
114
			let weight_limit: Weight = T::GasWeightMapping::gas_to_weight(gas_limit, true);
115

            
116
			let exec_info = T::EvmRunner::call(
117
				from,
118
				erc20_contract_address,
119
				input,
120
				U256::default(),
121
				gas_limit,
122
				None,
123
				None,
124
				None,
125
				Default::default(),
126
				false,
127
				false,
128
				Some(weight_limit),
129
				Some(0),
130
				&<T as pallet_evm::Config>::config(),
131
			)
132
			.map_err(|_| Erc20TransferError::EvmCallFail)?;
133

            
134
			ensure!(
135
				matches!(
136
					exec_info.exit_reason,
137
					ExitReason::Succeed(ExitSucceed::Returned | ExitSucceed::Stopped)
138
				),
139
				Erc20TransferError::ContractTransferFail
140
			);
141

            
142
			// return value is true.
143
			let bytes: [u8; 32] = U256::from(1).to_big_endian();
144

            
145
			// Check return value to make sure not calling on empty contracts.
146
			ensure!(
147
				!exec_info.value.is_empty() && exec_info.value == bytes,
148
				Erc20TransferError::ContractReturnInvalidValue
149
			);
150

            
151
			Ok(())
152
		}
153
	}
154

            
155
	impl<T: Config> xcm_executor::traits::TransactAsset for Pallet<T> {
156
		// For optimization reasons, the asset we want to deposit has not really been withdrawn,
157
		// we have just traced from which account it should have been withdrawn.
158
		// So we will retrieve these information and make the transfer from the origin account.
159
		fn deposit_asset(what: &Asset, who: &Location, _context: Option<&XcmContext>) -> XcmResult {
160
			let (contract_address, amount) =
161
				Erc20Matcher::<T::Erc20MultilocationPrefix>::matches_fungibles(what)?;
162

            
163
			let beneficiary = T::AccountIdConverter::convert_location(who)
164
				.ok_or(MatchError::AccountIdConversionFailed)?;
165

            
166
			let gas_limit = Self::gas_limit_of_erc20_transfer(&what.id);
167

            
168
			// Get the global context to recover accounts origins.
169
			XcmHoldingErc20sOrigins::with(|erc20s_origins| {
170
				match erc20s_origins.drain(contract_address, amount) {
171
					// We perform the evm transfers in a storage transaction to ensure that if one
172
					// of them fails all the changes of the previous evm calls are rolled back.
173
					Ok(tokens_to_transfer) => frame_support::storage::with_storage_layer(|| {
174
						tokens_to_transfer
175
							.into_iter()
176
							.try_for_each(|(from, subamount)| {
177
								Self::erc20_transfer(
178
									contract_address,
179
									from,
180
									beneficiary,
181
									subamount,
182
									gas_limit,
183
								)
184
							})
185
					})
186
					.map_err(Into::into),
187
					Err(DrainError::AssetNotFound) => Err(XcmError::AssetNotFound),
188
					Err(DrainError::NotEnoughFounds) => Err(XcmError::FailedToTransactAsset(
189
						"not enough founds in xcm holding",
190
					)),
191
					Err(DrainError::SplitError) => Err(XcmError::FailedToTransactAsset(
192
						"SplitError: each withdrawal of erc20 tokens must be deposited at once",
193
					)),
194
				}
195
			})
196
			.ok_or(XcmError::FailedToTransactAsset(
197
				"missing erc20 executor context",
198
			))?
199
		}
200

            
201
		fn internal_transfer_asset(
202
			asset: &Asset,
203
			from: &Location,
204
			to: &Location,
205
			_context: &XcmContext,
206
		) -> Result<AssetsInHolding, XcmError> {
207
			let (contract_address, amount) =
208
				Erc20Matcher::<T::Erc20MultilocationPrefix>::matches_fungibles(asset)?;
209

            
210
			let from = T::AccountIdConverter::convert_location(from)
211
				.ok_or(MatchError::AccountIdConversionFailed)?;
212

            
213
			let to = T::AccountIdConverter::convert_location(to)
214
				.ok_or(MatchError::AccountIdConversionFailed)?;
215

            
216
			let gas_limit = Self::gas_limit_of_erc20_transfer(&asset.id);
217

            
218
			// We perform the evm transfers in a storage transaction to ensure that if it fail
219
			// any contract storage changes are rolled back.
220
			frame_support::storage::with_storage_layer(|| {
221
				Self::erc20_transfer(contract_address, from, to, amount, gas_limit)
222
			})?;
223

            
224
			Ok(asset.clone().into())
225
		}
226

            
227
		// Since we don't control the erc20 contract that manages the asset we want to withdraw,
228
		// we can't really withdraw this asset, we can only transfer it to another account.
229
		// It would be possible to transfer the asset to a dedicated account that would reflect
230
		// the content of the xcm holding, but this would imply to perform two evm calls instead of
231
		// one (1 to withdraw the asset and a second one to deposit it).
232
		// In order to perform only one evm call, we just trace the origin of the asset,
233
		// and then the transfer will only really be performed in the deposit instruction.
234
		fn withdraw_asset(
235
			what: &Asset,
236
			who: &Location,
237
			_context: Option<&XcmContext>,
238
		) -> Result<AssetsInHolding, XcmError> {
239
			let (contract_address, amount) =
240
				Erc20Matcher::<T::Erc20MultilocationPrefix>::matches_fungibles(what)?;
241
			let who = T::AccountIdConverter::convert_location(who)
242
				.ok_or(MatchError::AccountIdConversionFailed)?;
243

            
244
			XcmHoldingErc20sOrigins::with(|erc20s_origins| {
245
				erc20s_origins.insert(contract_address, who, amount)
246
			})
247
			.ok_or(XcmError::FailedToTransactAsset(
248
				"missing erc20 executor context",
249
			))?;
250

            
251
			Ok(what.clone().into())
252
		}
253
	}
254
}