Reputation:
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();
Upvotes: 10
Views: 22541
Reputation: 1
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.
Upvotes: 0
Reputation: 1341
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
Upvotes: 0
Reputation: 691
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.
Upvotes: 1
Reputation: 1
This error is most likely to appear in two conditions:-
Upvotes: 0
Reputation: 11
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
.
Upvotes: 1
Reputation: 71
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
Upvotes: 2
Reputation: 103
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.
Upvotes: 1
Reputation: 21559
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)
Upvotes: 0
Reputation: 51
Can be many things, but there are two causes most common.
Upvotes: 4