Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: asset transfer bug in evm #1953

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions precompiles/assets-erc20/src/lib.rs
Original file line number Diff line number Diff line change
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"))
}
}
Loading