0

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);
    });
});

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:

Here's the ABI of getsCount:

{
  "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
},

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);
});
2
  • What's the ABI of getCounts?
    – Kof
    May 24 at 5:14
  • @Kof Hi! I edited my post and added the ABI of getCounts
    – fabricdev
    May 24 at 5:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.