0

I'm trying to create a new transaction and deploy it to the blockchain, but I'm obtaining this warning when I go to inspect the transaction result on etherscan: Warning! Error encountered during contract execution [invalid opcode: opcode 0x4b not defined].

The contract seems to be correct because when I test it on remix.ethereum.org, transactions are correctly done; so my error seems to be when calling the function window.ethereum.request() from my React code and passing the parameters.

My application has been developed using Metamask, React and the library Ethers.js, and according to what I've seen on Metamask docs I'm doing correctly the function call.

As Metamask docs say (https://eth.wiki/json-rpc/API#eth_sendtransaction) this method only requires from and data parameters, when creating a new contract. If to param is specified, a transfer is done between 2 accounts (but what I want is to send Eth to a new contract instance, so to param is not required if data field is specified)

const hash = await ethereum.request({
   method: 'eth_sendTransaction',
   params: [{
      from: currentAccount, //User's metamask account
      value: parsedAmount._hex, //value converted to HEX wei
      gas: '0x15F90', //random gas fee
      data: contractAddress, //address obtained when compiling & deploying the contract to blockchain
   }]
});

My error happens when I execute the previous function call, and I go to etherscan to check the transaction result and I get an error like this: Warning! Error encountered during contract execution [invalid opcode: opcode 0x4b not defined]. Does someone know how to solve this error?

(I attach the contract code if someone needs it)

Thanks in advance :)

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

contract Transactions {
    uint256 transactionCount;

    event Transfer(address from, address to, uint amount, string message, uint256 timestamp, string keyword);

    struct TransferStruct {
        address sender;
        address receiver;
        uint amount;
        string message;
        uint256 timestamp;
        string keyword;
    }

    TransferStruct[] transactions;

    function addToBlockchain(address payable receiver, uint amount, string memory message, string memory keyword) public {
        transactionCount++;
        transactions.push(TransferStruct(msg.sender, receiver, amount, message, block.timestamp, keyword));

        emit Transfer(msg.sender, receiver, amount, message, block.timestamp, keyword);
    }

    function getAllTransactions() public view returns (TransferStruct[] memory) {
        return transactions;
    }
    
    function getTransactionCount() public view returns (uint256) {
        return transactionCount;
    }
}
2
  • the data field is for calldata or deployement bytecode, anyway it doesn't seem to be what you think it is. I suspect you want to send the transaction to contractAddress, if that is the case, change data: contractAddress to to: contractAddress.
    – hroussille
    May 3, 2022 at 10:53
  • Otherwise, your to defaults to 0x0 which is interpreted as a contract deployment, (i.e., data is some compiled code) but contractAddress contains bytes that are not valid opcodes, hence the error.
    – hroussille
    May 3, 2022 at 10:54

1 Answer 1

0

Problem seems to be solved by adding a constructor function on the contract:

constructor() {

}
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 3, 2022 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.