hashgraph / hedera-smart-contracts

Contains Hedera Smart Contract Service supporting files
Apache License 2.0
38 stars 53 forks source link

Hedera Token Service return `EvmError: InvalidEFOpcode` while running forking tests with foundry #863

Open sqrlfirst opened 2 months ago

sqrlfirst commented 2 months ago

Description

I'm trying to run I'm trying to run mainnet fork test in foundry, however I'm getting following error while I try to execute vm.deal cheat code to mint erc20:

Ran 1 test for test/foundry/testforissue.t.sol:DealCheatCodeIssue
[FAIL. Reason: setup failed: EvmError: Revert] setUp() (gas: 0)
Traces:
  [1040430288] DealCheatCodeIssue::setUp()
    ├─ [0] VM::addr(<pk>) [staticcall]
    │   └─ ← [Return] user_one: [0x10717341b635a148A81A6e6e8c0EDe50E59490B2]
    ├─ [0] VM::label(user_one: [0x10717341b635a148A81A6e6e8c0EDe50E59490B2], "user_one")
    │   └─ ← [Return] 
    ├─ [0] VM::deal(user_one: [0x10717341b635a148A81A6e6e8c0EDe50E59490B2], 100000000000 [1e11])
    │   └─ ← [Return] 
    ├─ [1040398513] 0x000000000000000000000000000000000006f89a::balanceOf(user_one: [0x10717341b635a148A81A6e6e8c0EDe50E59490B2]) [staticcall]
    │   ├─ [0] 0x0000000000000000000000000000000000000167::redirectForToken() [delegatecall]
    │   │   └─ ← [InvalidEFOpcode] EvmError: InvalidEFOpcode
    │   └─ ← [Revert] EvmError: Revert
    └─ ← [Revert] EvmError: Revert

Steps to reproduce

  1. Run the test with following command:
    forge test --fork-url "https://mainnet.hashio.io/api" --match-contract "DealCheatCodeIssue"  -vvvv 

Example of test:

// SPDX-License-Identifier: unlicensed
pragma solidity ^0.8.17;

import {Test, Vm, console} from "forge-std/Test.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract DealCheatCodeIssue is Test {
    address USDC_mainnet = 0x000000000000000000000000000000000006f89a;
    address user1;

    function setUp() public {
        user1 = makeAddr("user_one");

        deal(user1, 100 * 10e8);
        deal(USDC_mainnet, user1, 1000 * 10e8);
    }

    function test_UserBalance() public {
        uint256 userBalance = IERC20(USDC_mainnet).balanceOf(user1);
        assertEq(userBalance, 1000 * 10e8);
    }
}

Additional context

No response

Hedera network

mainnet

Version

v0.1.0

Operating system

macOS

acuarica commented 1 month ago

Hi @sqrlfirst, thanks for reporting this. We are aware of this situation. This happens when trying to use network forking with some of the Hedera native services, _e.g., Hedera Token Service. In this specific case, the issue arises because USDC at 0x000000000000000000000000000000000006f89a is a Hedera Native Token. Hedera Native Tokens implement HIP-719, which in turns calls the HTS at 0x0000000000000000000000000000000000000167. However, the eth_getCode for 0x0000000000000000000000000000000000000167 returns 0xfe, hence the EvmError: InvalidEFOpcode.

We are working on a solution, which in a nutshell involves emulating the HTS at a Solidity level. We estimate to have some results in the next weeks.