throw new Error 'Returned values aren't valid, did it run Out of Gas?
Asked Answered
A

9

10

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();
Addams answered 12/4, 2022 at 21:17 Comment(1)
on metamask, make sure you are connected to correct networkLombok
V
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.
Vile answered 30/4, 2022 at 1:24 Comment(0)
G
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

Gaiser answered 15/7, 2022 at 18:32 Comment(1)
Thanks for this insight, its a small thing but one can get annoyed with this silly mistake! :)Gran
P
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.

Palpebrate answered 28/6, 2022 at 15:20 Comment(0)
N
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.

Nappe answered 22/9, 2022 at 17:38 Comment(0)
R
1

I also struggled with this error when I first used web3js with Ganache. I laboriously discovered that my smart contract was not deployed to the local Ganache blockchain and that resulted in this error. Now the question is why was the smart contract not successfully deployed on the Ganache blockchain? I found out that the then latest stable Solidity version 0.8.23 led to these errors. So I downgraded the version until it worked. And it only worked with pragma solidity 0.8.19; or below. Hence the tip for the community to try this solution.

Rosebud answered 10/9, 2023 at 10:56 Comment(0)
T
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)
Tani answered 7/6, 2022 at 23:7 Comment(0)
T
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.
Tore answered 11/3, 2023 at 19:51 Comment(0)
F
0

i had accidentally closed Ganache, meaning lost all the accounts with Ganache. Also, the smart contracts disappeared too.

Hence I to redeploy and it worked fine

Fabricate answered 16/9, 2023 at 13:21 Comment(0)
H
0

I was also getting same although everything was correct abi contract address, Solution was you need to make sure that you deployed contract on network using metamask put same testnetwork on browser or else it will throw error Eg I deployed contract on mumbai testnet so make sure that in browser same mumbai testnet is on current network.

Herc answered 5/3 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.