Skip to content

Commit

Permalink
Merge pull request #939 from galacticcouncil/fix/weird_tokens
Browse files Browse the repository at this point in the history
fix: erc20 edge cases error handling
  • Loading branch information
mrq1911 authored Nov 1, 2024
2 parents 8a26f60 + 000b69f commit 5d4121c
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 4 deletions.
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ serde_json = { workspace = true }
hex = { workspace = true }

[dev-dependencies]
pretty_assertions = { workspace = true }
pretty_assertions = { workspace = true }
pallet-relaychain-info = { workspace = true }
xcm-emulator = { workspace = true }
test-utils = { workspace = true }
Expand Down
18 changes: 18 additions & 0 deletions integration-tests/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,24 @@ fn account_should_receive_tokens() {
});
}

#[test]
fn erc20_transfer_returning_false_should_be_handled_as_error() {
TestNet::reset();
Hydra::execute_with(|| {
let token = deploy_contract("WeirdToken", deployer());

assert_noop!(
<Erc20Currency<Runtime> as MultiCurrency<AccountId>>::transfer(
token,
&AccountId::from(ALICE),
&AccountId::from(BOB),
100
),
Other("evm: erc20 transfer returned false")
);
});
}

#[test]
fn bound_erc20_should_have_issuance() {
TestNet::reset();
Expand Down
20 changes: 17 additions & 3 deletions runtime/hydradx/src/evm/erc20_currency.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::evm::executor::{CallResult, Executor};
use crate::evm::{EvmAccounts, EvmAddress};
use ethabi::ethereum_types::BigEndianHash;
use evm::ExitReason;
use evm::ExitReason::Succeed;
use evm::ExitSucceed::Returned;
use evm::{ExitReason, ExitSucceed};
use frame_support::{dispatch::DispatchResult, fail, pallet_prelude::*};
use hydradx_traits::evm::{CallContext, InspectEvmAccounts, ERC20, EVM};
use num_enum::{IntoPrimitive, TryFromPrimitive};
Expand Down Expand Up @@ -174,11 +174,25 @@ fn decode_integer(value: Vec<u8>) -> Option<U256> {
U256::checked_from(value.as_slice())
}

fn decode_bool(value: Vec<u8>) -> Option<bool> {
if value.len() != 32 {
return None;
}
let mut bytes = [0u8; 32];
U256::from(1).to_big_endian(&mut bytes);
Some(value == bytes)
}

fn handle_result(result: CallResult) -> DispatchResult {
let (exit_reason, value) = result;
match exit_reason {
ExitReason::Succeed(ExitSucceed::Returned) => Ok(()),
ExitReason::Succeed(ExitSucceed::Stopped) => Ok(()),
ExitReason::Succeed(_) => {
if Some(false) == decode_bool(value) {
Err(DispatchError::Other("evm: erc20 transfer returned false"))
} else {
Ok(())
}
}
_ => Err(DispatchError::Other(&*Box::leak(
format!("evm:0x{}", hex::encode(value)).into_boxed_str(),
))),
Expand Down
Loading

0 comments on commit 5d4121c

Please sign in to comment.