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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// 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 interact with pallet_balances instances using the ERC20 interface standard.

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

use account::SYSTEM_ACCOUNT_SIZE;
use fp_evm::PrecompileHandle;
use frame_support::{
	dispatch::{GetDispatchInfo, PostDispatchInfo},
	sp_runtime::traits::{Bounded, CheckedSub, Dispatchable, StaticLookup},
	storage::types::{StorageDoubleMap, StorageMap, ValueQuery},
	traits::StorageInstance,
	Blake2_128Concat,
};
use pallet_balances::pallet::{
	Instance1, Instance10, Instance11, Instance12, Instance13, Instance14, Instance15, Instance16,
	Instance2, Instance3, Instance4, Instance5, Instance6, Instance7, Instance8, Instance9,
};
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;
use sp_core::{H160, H256, U256};
use sp_std::{
	convert::{TryFrom, TryInto},
	marker::PhantomData,
};

mod eip2612;
use eip2612::Eip2612;

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

/// Solidity selector of the Transfer log, which is the Keccak of the Log signature.
pub const SELECTOR_LOG_TRANSFER: [u8; 32] = keccak256!("Transfer(address,address,uint256)");

/// Solidity selector of the Approval log, which is the Keccak of the Log signature.
pub const SELECTOR_LOG_APPROVAL: [u8; 32] = keccak256!("Approval(address,address,uint256)");

/// Solidity selector of the Deposit log, which is the Keccak of the Log signature.
pub const SELECTOR_LOG_DEPOSIT: [u8; 32] = keccak256!("Deposit(address,uint256)");

/// Solidity selector of the Withdraw log, which is the Keccak of the Log signature.
pub const SELECTOR_LOG_WITHDRAWAL: [u8; 32] = keccak256!("Withdrawal(address,uint256)");

/// Associates pallet Instance to a prefix used for the Approves storage.
/// This trait is implemented for () and the 16 substrate Instance.
pub trait InstanceToPrefix {
	/// Prefix used for the Approves storage.
	type ApprovesPrefix: StorageInstance;

	/// Prefix used for the Approves storage.
	type NoncesPrefix: StorageInstance;
}

// We use a macro to implement the trait for () and the 16 substrate Instance.
macro_rules! impl_prefix {
	($instance:ident, $name:literal) => {
		// Using `paste!` we generate a dedicated module to avoid collisions
		// between each instance `Approves` struct.
		paste::paste! {
			mod [<_impl_prefix_ $instance:snake>] {
				use super::*;

				pub struct Approves;

				impl StorageInstance for Approves {
					const STORAGE_PREFIX: &'static str = "Approves";

					fn pallet_prefix() -> &'static str {
						$name
					}
				}

				pub struct Nonces;

				impl StorageInstance for Nonces {
					const STORAGE_PREFIX: &'static str = "Nonces";

					fn pallet_prefix() -> &'static str {
						$name
					}
				}

				impl InstanceToPrefix for $instance {
					type ApprovesPrefix = Approves;
					type NoncesPrefix = Nonces;
				}
			}
		}
	};
}

// Since the macro expect a `ident` to be used with `paste!` we cannot provide `()` directly.
type Instance0 = ();

impl_prefix!(Instance0, "Erc20Instance0Balances");
impl_prefix!(Instance1, "Erc20Instance1Balances");
impl_prefix!(Instance2, "Erc20Instance2Balances");
impl_prefix!(Instance3, "Erc20Instance3Balances");
impl_prefix!(Instance4, "Erc20Instance4Balances");
impl_prefix!(Instance5, "Erc20Instance5Balances");
impl_prefix!(Instance6, "Erc20Instance6Balances");
impl_prefix!(Instance7, "Erc20Instance7Balances");
impl_prefix!(Instance8, "Erc20Instance8Balances");
impl_prefix!(Instance9, "Erc20Instance9Balances");
impl_prefix!(Instance10, "Erc20Instance10Balances");
impl_prefix!(Instance11, "Erc20Instance11Balances");
impl_prefix!(Instance12, "Erc20Instance12Balances");
impl_prefix!(Instance13, "Erc20Instance13Balances");
impl_prefix!(Instance14, "Erc20Instance14Balances");
impl_prefix!(Instance15, "Erc20Instance15Balances");
impl_prefix!(Instance16, "Erc20Instance16Balances");

/// Alias for the Balance type for the provided Runtime and Instance.
pub type BalanceOf<Runtime, Instance = ()> =
	<Runtime as pallet_balances::Config<Instance>>::Balance;

/// Storage type used to store approvals, since `pallet_balances` doesn't
/// handle this behavior.
/// (Owner => Allowed => Amount)
pub type ApprovesStorage<Runtime, Instance> = StorageDoubleMap<
	<Instance as InstanceToPrefix>::ApprovesPrefix,
	Blake2_128Concat,
	<Runtime as frame_system::Config>::AccountId,
	Blake2_128Concat,
	<Runtime as frame_system::Config>::AccountId,
	BalanceOf<Runtime, Instance>,
>;

/// Storage type used to store EIP2612 nonces.
pub type NoncesStorage<Instance> = StorageMap<
	<Instance as InstanceToPrefix>::NoncesPrefix,
	// Owner
	Blake2_128Concat,
	H160,
	// Nonce
	U256,
	ValueQuery,
>;

/// Metadata of an ERC20 token.
pub trait Erc20Metadata {
	/// Returns the name of the token.
	fn name() -> &'static str;

	/// Returns the symbol of the token.
	fn symbol() -> &'static str;

	/// Returns the decimals places of the token.
	fn decimals() -> u8;

	/// Must return `true` only if it represents the main native currency of
	/// the network. It must be the currency used in `pallet_evm`.
	fn is_native_currency() -> bool;
}

/// Precompile exposing a pallet_balance as an ERC20.
/// Multiple precompiles can support instances of pallet_balance.
/// The precompile uses an additional storage to store approvals.
pub struct Erc20BalancesPrecompile<Runtime, Metadata: Erc20Metadata, Instance: 'static = ()>(
	PhantomData<(Runtime, Metadata, Instance)>,
);

#[precompile_utils::precompile]
impl<Runtime, Metadata, Instance> Erc20BalancesPrecompile<Runtime, Metadata, Instance>
where
	Runtime: pallet_balances::Config<Instance> + pallet_evm::Config,
	Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
	Runtime::RuntimeCall: From<pallet_balances::Call<Runtime, Instance>>,
	<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
	BalanceOf<Runtime, Instance>: TryFrom<U256> + Into<U256>,
	Metadata: Erc20Metadata,
	Instance: InstanceToPrefix + 'static,
{
	#[precompile::public("totalSupply()")]
	#[precompile::view]
	fn total_supply(handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
		// TotalIssuance: Balance(16)
		handle.record_db_read::<Runtime>(16)?;

		Ok(pallet_balances::Pallet::<Runtime, Instance>::total_issuance().into())
	}

	#[precompile::public("balanceOf(address)")]
	#[precompile::view]
	fn balance_of(handle: &mut impl PrecompileHandle, owner: Address) -> EvmResult<U256> {
		// frame_system::Account:
		// Blake2128(16) + AccountId(20) + AccountInfo ((4 * 4) + AccountData(16 * 4))
		handle.record_db_read::<Runtime>(116)?;

		let owner: H160 = owner.into();
		let owner: Runtime::AccountId = Runtime::AddressMapping::into_account_id(owner);

		Ok(pallet_balances::Pallet::<Runtime, Instance>::usable_balance(&owner).into())
	}

	#[precompile::public("allowance(address,address)")]
	#[precompile::view]
	fn allowance(
		handle: &mut impl PrecompileHandle,
		owner: Address,
		spender: Address,
	) -> EvmResult<U256> {
		// frame_system::ApprovesStorage:
		// (2 * (Blake2128(16) + AccountId(20)) + Balanceof(16)
		handle.record_db_read::<Runtime>(88)?;

		let owner: H160 = owner.into();
		let spender: H160 = spender.into();

		let owner: Runtime::AccountId = Runtime::AddressMapping::into_account_id(owner);
		let spender: Runtime::AccountId = Runtime::AddressMapping::into_account_id(spender);

		Ok(ApprovesStorage::<Runtime, Instance>::get(owner, spender)
			.unwrap_or_default()
			.into())
	}

	#[precompile::public("approve(address,uint256)")]
	fn approve(
		handle: &mut impl PrecompileHandle,
		spender: Address,
		value: U256,
	) -> EvmResult<bool> {
		handle.record_cost(RuntimeHelper::<Runtime>::db_write_gas_cost())?;
		handle.record_log_costs_manual(3, 32)?;

		let spender: H160 = spender.into();

		// Write into storage.
		{
			let caller: Runtime::AccountId =
				Runtime::AddressMapping::into_account_id(handle.context().caller);
			let spender: Runtime::AccountId = Runtime::AddressMapping::into_account_id(spender);
			// Amount saturate if too high.
			let value = Self::u256_to_amount(value).unwrap_or_else(|_| Bounded::max_value());

			ApprovesStorage::<Runtime, Instance>::insert(caller, spender, value);
		}

		log3(
			handle.context().address,
			SELECTOR_LOG_APPROVAL,
			handle.context().caller,
			spender,
			solidity::encode_event_data(value),
		)
		.record(handle)?;

		// Build output.
		Ok(true)
	}

	#[precompile::public("transfer(address,uint256)")]
	fn transfer(handle: &mut impl PrecompileHandle, to: Address, value: U256) -> EvmResult<bool> {
		handle.record_log_costs_manual(3, 32)?;

		let to: H160 = to.into();

		// Build call with origin.
		{
			let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
			let to = Runtime::AddressMapping::into_account_id(to);
			let value = Self::u256_to_amount(value).in_field("value")?;

			// Dispatch call (if enough gas).
			RuntimeHelper::<Runtime>::try_dispatch(
				handle,
				Some(origin).into(),
				pallet_balances::Call::<Runtime, Instance>::transfer_allow_death {
					dest: Runtime::Lookup::unlookup(to),
					value: value,
				},
				SYSTEM_ACCOUNT_SIZE,
			)?;
		}

		log3(
			handle.context().address,
			SELECTOR_LOG_TRANSFER,
			handle.context().caller,
			to,
			solidity::encode_event_data(value),
		)
		.record(handle)?;

		Ok(true)
	}

	#[precompile::public("transferFrom(address,address,uint256)")]
	fn transfer_from(
		handle: &mut impl PrecompileHandle,
		from: Address,
		to: Address,
		value: U256,
	) -> EvmResult<bool> {
		// frame_system::ApprovesStorage:
		// (2 * (Blake2128(16) + AccountId(20)) + Balanceof(16)
		handle.record_db_read::<Runtime>(88)?;
		handle.record_cost(RuntimeHelper::<Runtime>::db_write_gas_cost())?;
		handle.record_log_costs_manual(3, 32)?;

		let from: H160 = from.into();
		let to: H160 = to.into();

		{
			let caller: Runtime::AccountId =
				Runtime::AddressMapping::into_account_id(handle.context().caller);
			let from: Runtime::AccountId = Runtime::AddressMapping::into_account_id(from);
			let to: Runtime::AccountId = Runtime::AddressMapping::into_account_id(to);
			let value = Self::u256_to_amount(value).in_field("value")?;

			// If caller is "from", it can spend as much as it wants.
			if caller != from {
				ApprovesStorage::<Runtime, Instance>::mutate(from.clone(), caller, |entry| {
					// Get current allowed value, exit if None.
					let allowed = entry.ok_or(revert("spender not allowed"))?;

					// Remove "value" from allowed, exit if underflow.
					let allowed = allowed
						.checked_sub(&value)
						.ok_or_else(|| revert("trying to spend more than allowed"))?;

					// Update allowed value.
					*entry = Some(allowed);

					EvmResult::Ok(())
				})?;
			}

			// Build call with origin. Here origin is the "from"/owner field.
			// Dispatch call (if enough gas).
			RuntimeHelper::<Runtime>::try_dispatch(
				handle,
				Some(from).into(),
				pallet_balances::Call::<Runtime, Instance>::transfer_allow_death {
					dest: Runtime::Lookup::unlookup(to),
					value: value,
				},
				SYSTEM_ACCOUNT_SIZE,
			)?;
		}

		log3(
			handle.context().address,
			SELECTOR_LOG_TRANSFER,
			from,
			to,
			solidity::encode_event_data(value),
		)
		.record(handle)?;

		Ok(true)
	}

	#[precompile::public("name()")]
	#[precompile::view]
	fn name(_handle: &mut impl PrecompileHandle) -> EvmResult<UnboundedBytes> {
		Ok(Metadata::name().into())
	}

	#[precompile::public("symbol()")]
	#[precompile::view]
	fn symbol(_handle: &mut impl PrecompileHandle) -> EvmResult<UnboundedBytes> {
		Ok(Metadata::symbol().into())
	}

	#[precompile::public("decimals()")]
	#[precompile::view]
	fn decimals(_handle: &mut impl PrecompileHandle) -> EvmResult<u8> {
		Ok(Metadata::decimals())
	}

	#[precompile::public("deposit()")]
	#[precompile::fallback]
	#[precompile::payable]
	fn deposit(handle: &mut impl PrecompileHandle) -> EvmResult {
		// Deposit only makes sense for the native currency.
		if !Metadata::is_native_currency() {
			return Err(RevertReason::UnknownSelector.into());
		}

		let caller: Runtime::AccountId =
			Runtime::AddressMapping::into_account_id(handle.context().caller);
		let precompile = Runtime::AddressMapping::into_account_id(handle.context().address);
		let amount = Self::u256_to_amount(handle.context().apparent_value)?;

		if amount.into() == U256::from(0u32) {
			return Err(revert("deposited amount must be non-zero"));
		}

		handle.record_log_costs_manual(2, 32)?;

		// Send back funds received by the precompile.
		RuntimeHelper::<Runtime>::try_dispatch(
			handle,
			Some(precompile).into(),
			pallet_balances::Call::<Runtime, Instance>::transfer_allow_death {
				dest: Runtime::Lookup::unlookup(caller),
				value: amount,
			},
			SYSTEM_ACCOUNT_SIZE,
		)?;

		log2(
			handle.context().address,
			SELECTOR_LOG_DEPOSIT,
			handle.context().caller,
			solidity::encode_event_data(handle.context().apparent_value),
		)
		.record(handle)?;

		Ok(())
	}

	#[precompile::public("withdraw(uint256)")]
	fn withdraw(handle: &mut impl PrecompileHandle, value: U256) -> EvmResult {
		// Withdraw only makes sense for the native currency.
		if !Metadata::is_native_currency() {
			return Err(RevertReason::UnknownSelector.into());
		}

		handle.record_log_costs_manual(2, 32)?;

		let account_amount: U256 = {
			let owner: Runtime::AccountId =
				Runtime::AddressMapping::into_account_id(handle.context().caller);
			pallet_balances::Pallet::<Runtime, Instance>::usable_balance(&owner).into()
		};

		if value > account_amount {
			return Err(revert("Trying to withdraw more than owned"));
		}

		log2(
			handle.context().address,
			SELECTOR_LOG_WITHDRAWAL,
			handle.context().caller,
			solidity::encode_event_data(value),
		)
		.record(handle)?;

		Ok(())
	}

	#[precompile::public("permit(address,address,uint256,uint256,uint8,bytes32,bytes32)")]
	fn eip2612_permit(
		handle: &mut impl PrecompileHandle,
		owner: Address,
		spender: Address,
		value: U256,
		deadline: U256,
		v: u8,
		r: H256,
		s: H256,
	) -> EvmResult {
		<Eip2612<Runtime, Metadata, Instance>>::permit(
			handle, owner, spender, value, deadline, v, r, s,
		)
	}

	#[precompile::public("nonces(address)")]
	#[precompile::view]
	fn eip2612_nonces(handle: &mut impl PrecompileHandle, owner: Address) -> EvmResult<U256> {
		<Eip2612<Runtime, Metadata, Instance>>::nonces(handle, owner)
	}

	#[precompile::public("DOMAIN_SEPARATOR()")]
	#[precompile::view]
	fn eip2612_domain_separator(handle: &mut impl PrecompileHandle) -> EvmResult<H256> {
		<Eip2612<Runtime, Metadata, Instance>>::domain_separator(handle)
	}

	fn u256_to_amount(value: U256) -> MayRevert<BalanceOf<Runtime, Instance>> {
		value
			.try_into()
			.map_err(|_| RevertReason::value_is_too_large("balance type").into())
	}
}