Search code examples
reactjsblockchainethereumsolidityweb3js

What is error Error: Returned error: execution reverted when calling blockchain data and how to fix it?


I am working with React and getting some data from the blockchain in the App.js using useEffect and storing it with useState but when promises are to be resolved it catches this error:

error Error: Returned error: execution reverted
    at Object.ErrorResponse (errors.js:28)
    at index.js:303
    at XMLHttpRequest.request.onreadystatechange (index.js:98)

This is the code in App.js

const data = mainnet.FairLaunch.pools.map((pool) => {
  const loadingStakingData = async () => {
    const stakedValue = await getStakeValue(pool);
    console.log('Loop staking data', stakedValue); // logging correctly
    return stakedValue;
  };
  return loadingStakingData();
});

Promise.all(data)
  .then((values) => {
    console.log('values', values); // error breaks app before console.log is triggered
    setStakingData(values);
  })
  .catch((error) => console.error('error', error));

This is the code for the getStakeValue() which queries the blockchain for the data:

export async function getStakeValue(param = {}) {
  const vault = getWeb3VaultContract(param.address); 

  const totalSupply = parseInt(await vault.methods.totalSupply().call());
  const totalToken = parseInt(await vault.methods.totalToken().call());
  //getFairLaunch gets the address according to the environment prod or dev
  const balance = await vault.methods.balanceOf(getFairLaunch()).call();

  let stakeValue = (balance * 100 * totalToken) / totalSupply;
  console.log('stakeValue: ' , typeof stakeValue); //number
  console.log('stakeValue: ' , parseInt(stakeValue)); // log correctly

  return stakeValue;
}

And this is the ABI with the functions:

export default [
  {
    "inputs": [],
    "name": "totalToken",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  }, {
    "inputs": [],
    "name": "totalSupply",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },  {
    "inputs": [
      {
        "internalType": "address",
        "name": "account",
        "type": "address"
      }
    ],
    "name": "balanceOf",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
];

Any idea what this error is and how to fix it?


Solution

  • having the same issue..

    after changing the contract addresses and abi's.

    approving fails ..