Skip to content

Commit

Permalink
Fix: asset transfer bug in evm (#1953)
Browse files Browse the repository at this point in the history
* Fix: asset transfer bug in evm

* fix ci
  • Loading branch information
mclyk committed Nov 17, 2023
1 parent 72345d6 commit fa21915
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions precompiles/assets-erc20/src/lib.rs
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -340,12 +340,13 @@ where
input.expect_arguments(2)?;

let to: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
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::<Runtime>::try_dispatch(
Expand Down Expand Up @@ -382,13 +383,14 @@ where

let from: H160 = input.read::<Address>()?.into();
let to: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
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 {
Expand Down Expand Up @@ -500,7 +502,7 @@ where
input.expect_arguments(2)?;

let beneficiary: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
let amount = Self::u256_to_amount(input.read::<U256>()?)?;

let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let beneficiary = Runtime::AddressMapping::into_account_id(beneficiary);
Expand All @@ -527,7 +529,7 @@ where
input.expect_arguments(2)?;

let who: H160 = input.read::<Address>()?.into();
let amount = input.read::<BalanceOf<Runtime, Instance>>()?;
let amount = Self::u256_to_amount(input.read::<U256>()?)?;

let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let who = Runtime::AddressMapping::into_account_id(who);
Expand All @@ -545,4 +547,10 @@ where

Ok(succeed(EvmDataWriter::new().write(true).build()))
}

fn u256_to_amount(value: U256) -> Result<BalanceOf<Runtime, Instance>, PrecompileFailure> {
value
.try_into()
.map_err(|_| revert("Error processing amount"))
}
}

0 comments on commit fa21915

Please sign in to comment.