Reputation: 61
I'm calling the methods in the deployed smart contracts in Ganache. Other methods are working fine, but the view functions returns an error.
Here's the view function in Solidity:
mapping (address => Count) private counts;
function getCounts (address user)
public
view
returns(uint a, uint b, uint total){
return(counts[user].a, counts[user].b, counts[user].total);
}
Here's how I call the method:
web3.eth.getAccounts(function(error, accounts){
if(error){
console.log(error);
}
var account = accounts[0];
contracts.SampleContract.deployed().then(function(instance){
credsInstance = instance;
return credsInstance.getCounts.call(account, {from: account});
}).then(function(creds){
console.log(creds[0]);
}).catch(function(error){
console.log(error.message);
});
});
Here's the ABI of getCounts
{
"inputs": [
{
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "getCounts",
"outputs": [
{
"internalType": "uint256",
"name": "academic",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "workExp",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "total",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
},
I'm getting this 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.
I also have the latest web3. How can I fix this?
Edit:
to initialize the contract I used Truffle's official documentation. Here's the code:
var contract = require('@truffle/contract');
$.getJSON('../build/contracts/SampleContract.json', function(data) {
// Get the necessary contract artifact file and instantiate it with @truffle/contract
var CredsArtifact = data;
contracts.SampleContract = TruffleContract(CredsArtifact);
// Set the provider for our contract
contracts.SampleContract.setProvider(web3Provider);
});
Upvotes: 2
Views: 1517
Reputation: 21557
I had the same problem, and I was using Hardhat .
after spending hours and not resolved, I switched Rinkeby test network, and everything goes well.
refer to: https://ethereum.stackexchange.com/a/129721/30431
Upvotes: 0