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;
}
}
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 tocontractAddress
, if that is the case, changedata: contractAddress
toto: contractAddress
.to
defaults to0x0
which is interpreted as a contract deployment, (i.e., data is some compiled code) butcontractAddress
contains bytes that are not valid opcodes, hence the error.