javascriptsolidity

VM Exception while processing transaction: invalid opcode | Using string


Here there is a really simple contract:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.25;

contract TestContract {
  string public name;
  uint public number;
  
  constructor() {
    number = 1;
    name = "Normal name";
  }
}

That's my test file:

import ganache from "ganache";
import { Web3 } from "web3";

import { abi, evm } from "../compile.js";

const web3 = new Web3(ganache.provider());

let accounts;
let testContract;

describe("TestContract Test", () => {
  it("should read the value of names state variable", async () => {
    accounts = await web3.eth.getAccounts();
    testContract = await new web3.eth.Contract(abi)
      .deploy({ data: evm.bytecode.object })
      .send({ from: accounts[0], gas: "10000000"});  

    const name = await testContract.methods.name().call();

    console.log(name);
    
  });
});

That's my damn output:

1) TestContract Test
       should read the value of names state variable:
     CallError: VM Exception while processing transaction: invalid opcode
      at Blockchain.simulateTransaction (node_modules\ganache\dist\node\1.js:2:100650)

When I try to call the number() method, it works normally. It's only when I try to retrieve a string variable or array of string that the error pop up. It looks like a very simple task, but I don't really know where I'm wronging.

My dependecies:

"dependencies": {
    "ganache": "^7.9.2",
    "mocha": "^10.4.0",
    "solc": "^0.8.25",
    "web3": "^4.1"
  }

Solution

  • Downgrade to Solidity 0.8.19.

    8.20 upwards caused compatibility issues.

    Also, try to migrate to Ethers and the Hardhat suite. It's more well maintained.