From fa2191514747bf4276db60b58e3e685e9867b42d Mon Sep 17 00:00:00 2001 From: MrPai <1164934857@qq.com> Date: Fri, 17 Nov 2023 11:41:30 +0800 Subject: [PATCH] Fix: asset transfer bug in evm (#1953) * Fix: asset transfer bug in evm * fix ci --- precompiles/assets-erc20/src/lib.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/precompiles/assets-erc20/src/lib.rs b/precompiles/assets-erc20/src/lib.rs index 472b1edf2..a569466ae 100644 --- a/precompiles/assets-erc20/src/lib.rs +++ b/precompiles/assets-erc20/src/lib.rs @@ -15,7 +15,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(test, feature(assert_matches))] -use fp_evm::{PrecompileHandle, PrecompileOutput}; +use fp_evm::{PrecompileFailure, PrecompileHandle, PrecompileOutput}; use frame_support::traits::fungibles::approvals::Inspect as ApprovalInspect; use frame_support::traits::fungibles::metadata::Inspect as MetadataInspect; use frame_support::traits::fungibles::Inspect; @@ -26,8 +26,8 @@ use frame_support::{ }; use pallet_evm::{AddressMapping, PrecompileSet}; use precompile_utils::{ - keccak256, succeed, Address, Bytes, EvmData, EvmDataWriter, EvmResult, FunctionModifier, - LogExt, LogsBuilder, PrecompileHandleExt, RuntimeHelper, + keccak256, revert, succeed, Address, Bytes, EvmData, EvmDataWriter, EvmResult, + FunctionModifier, LogExt, LogsBuilder, PrecompileHandleExt, RuntimeHelper, }; use sp_runtime::traits::Bounded; @@ -340,12 +340,13 @@ where input.expect_arguments(2)?; let to: H160 = input.read::
()?.into(); - let amount = input.read::>()?; + let amount: U256 = input.read()?; // Build call with origin. { let origin = Runtime::AddressMapping::into_account_id(handle.context().caller); let to = Runtime::AddressMapping::into_account_id(to); + let amount = Self::u256_to_amount(amount)?; // Dispatch call (if enough gas). RuntimeHelper::::try_dispatch( @@ -382,13 +383,14 @@ where let from: H160 = input.read::
()?.into(); let to: H160 = input.read::
()?.into(); - let amount = input.read::>()?; + let amount: U256 = input.read()?; { 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 amount = Self::u256_to_amount(amount)?; // If caller is "from", it can spend as much as it wants from its own balance. if caller != from { @@ -500,7 +502,7 @@ where input.expect_arguments(2)?; let beneficiary: H160 = input.read::
()?.into(); - let amount = input.read::>()?; + let amount = Self::u256_to_amount(input.read::()?)?; let origin = Runtime::AddressMapping::into_account_id(handle.context().caller); let beneficiary = Runtime::AddressMapping::into_account_id(beneficiary); @@ -527,7 +529,7 @@ where input.expect_arguments(2)?; let who: H160 = input.read::
()?.into(); - let amount = input.read::>()?; + let amount = Self::u256_to_amount(input.read::()?)?; let origin = Runtime::AddressMapping::into_account_id(handle.context().caller); let who = Runtime::AddressMapping::into_account_id(who); @@ -545,4 +547,10 @@ where Ok(succeed(EvmDataWriter::new().write(true).build())) } + + fn u256_to_amount(value: U256) -> Result, PrecompileFailure> { + value + .try_into() + .map_err(|_| revert("Error processing amount")) + } }