8

I am getting this error:

Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.

const Web3 = require("web3");
const MyContract = require("./build/contracts/MyContract.json");

const init = async () => {
  const web3 = new Web3("http://127.0.0.1:9545");

  const id = await web3.eth.net.getId();
  const deployedNetwork = MyContract.networks[id];
  const contract = new web3.eth.Contract(
    MyContract.abi,
    deployedNetwork.address
  );

  const addresses = await web3.eth.getAccounts();
   await contract.methods.setData(10).send({
      from: addresses[0]   
  });

  const data = await contract.methods.getData().call();
  console.log(data)
};

init();
TylerH
  • 20,799
  • 66
  • 75
  • 101
Rwitesh Bera
  • 389
  • 2
  • 3
  • 9

6 Answers6

4

Can be many things, but there are two causes most common.

  1. Very if you are using abi and a contract address correct.
  2. Very if you are selected the correct network, for exempo, if your contract is on Rinkeby you can selected Rinkeby or if your contract is on Mainnet select the Mainnet.
2

I made a mistake... I was using 'account address', instead of 'contract address'!

The code works, once the correct 'contract address' was used.

In remix ide, Copy the contact address from here enter image description here

1

The reason could be the network. If your abi belongs to a contract on mainnet then your provider must be mainnet and if it's blongs to one of the testnet then you must use the corresponding testnet provider.

This is how I fixed my issue.

Bharat Singh
  • 93
  • 1
  • 12
1

I also encountered this error. To fix it, I used truffle develop --log, then opened a new terminal window and connected to the current session by running truffle develop.

Pandapip1
  • 730
  • 5
  • 15
Tú Lê
  • 11
  • 2
0

You are calling a "send" function, which need a "from" address which has enough balance.

so , you should make sure : ( I copied your code )


  const addresses = await web3.eth.getAccounts();
   await contract.methods.setData(10).send({
      // MAKE SURE this account has enough balance
      from: addresses[0]     
  });

also , you called "getData()" method via "call()", if this operation was executed in your Hardhat environment, I suggest you switch to a real Test Network, e.g. Rinkeby

  const data = await contract.methods.getData().call();
  console.log(data)
Siwei
  • 19,858
  • 7
  • 75
  • 95
0

This error is most likely to appear in two conditions:-

  1. Check if you are using correct address and abi of contract.
  2. Check if you are connected to the same network.