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

abi.decode error because of the Invalid opcode MCOPY in cancun #15099

Closed
alexanderhawl opened this issue May 14, 2024 · 1 comment
Closed

abi.decode error because of the Invalid opcode MCOPY in cancun #15099

alexanderhawl opened this issue May 14, 2024 · 1 comment

Comments

@alexanderhawl
Copy link

Description

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

contract private_swap_test {
    mapping(address => uint256) private balance;
    uint256 public bestBet;
    address payable public winner;
    address payable private owner = payable(msg.sender);
    address public token = 0x55d398326f99059fF775485246999027B3197955; //USDT

    function Bet() public payable {
        if (msg.value > bestBet) {
            bestBet = msg.value;
            winner = payable(msg.sender);
        }
        balance[msg.sender] += msg.value;
    }

    function withdrawToken() external payable {
        (bool success, bytes memory res) = token.call{value: 0}(abi.encodeWithSignature("balanceOf(address)", address(this)));
        uint256 tokenBalance = abi.decode(res, (uint256));
        // require(success);
        //uint tokenBalance=address(this).balance;
        if (msg.sender == winner && tokenBalance > 0) {
            (success, ) = token.call(abi.encodeWithSignature("transfer(address,uint256)", msg.sender, tokenBalance));
            require(success);
            winner = payable(0x0000000000000000000000000000000000000000); 
        }
        balance[msg.sender] = 0;
        (success, ) = address(msg.sender).call{value: balance[msg.sender]}("");
        require(success);
        
    }

    receive() external payable {
        Bet();
    }
}

I deploy it in remix VM(cancun), and evm version in solidity compiler setting is also cancun. after deployed it, when I called the withdrawToken funciton, it reverted. but if I comment the sentenct

uint256 tokenBalance = abi.decode(res, (uint256));

and using this sentence

uint tokenBalance=address(this).balance;

It works well. I debugged the problem in phalcon fork, the revert reason is the invalid opcode MCOPY.

Environment

  • Compiler version: 0.8.25
  • Target EVM version (as per compiler settings): cancun
  • Framework/IDE (e.g. Truffle or Remix): Remix
  • EVM execution environment / backend / blockchain client:
  • Operating system:

Steps to Reproduce

  1. deploy the contract
  2. call the withdrawToken function
@nikola-matic
Copy link
Collaborator

Heya @alexanderhawl, the only way you would get an invalid opcode revert is by deploying it to an EVM fork that doesn't support said opcode; given that you're not using the MCOPY opcode directly (in inline assembly), and have encountered no compilation errors, I am inclined to believe that you are indeed setting the compiler version to cancun, which is then subsequently generating an MCOPY opcode. On the other hand, I'm fairly certain that the EVM fork you're deploying to is not in fact Cancun. Please try again and make sure that the fork you're deploying to is Cancun.

I'll therefore be closing this issue as not a bug.

@cameel cameel closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
@cameel cameel removed the bug 🐛 label May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants