0

I'm using this code from Onboard.js:

import ethers from 'ethers'
import erc20 from './erc20'

function tokenBalance({ tokenAddress, minimumBalance, tokenName }) {
  let ethersProvider;
  let tokenContract;

  return async stateAndHelpers => {
    const {
      wallet: { provider },
      address,
      BigNumber
    } = stateAndHelpers;

    if (!tokenContract) {
      ethersProvider = new ethers.providers.Web3Provider(provider);
      tokenContract = new ethers.Contract(tokenAddress, erc20, ethersProvider);
    }

    const tokenDecimals = await tokenContract.decimals();
    const divideBy = new BigNumber(10).pow(tokenDecimals);
    const tokenBalanceResult = await tokenContract
      .balanceOf(address)
      .then(res => res.toString());
    const tokenBalance = new BigNumber(tokenBalanceResult).div(divideBy);

    if (tokenBalance.lt(minimumBalance)) {
      return {
        heading: `Get Some ${tokenName}`,
        description: `You need to have at least ${minimumBalance} ${tokenName} to interact with this Dapp. Send some more ${tokenName} to this address or switch to another address that has a higher ${tokenName} balance.`,
        eventCode: "tokenBalance",
        icon: `
            <svg 
                height="18" 
                viewBox="0 0 429 695" 
                width="18" xmlns="http://www.w3.org/2000/svg"
            >
                <g 
                    fill="currentColor" 
                    fill-rule="evenodd"
            >
                 <path d="m0 394 213 126.228516 214-126.228516-214 301z"/>
             <path d="m0 353.962264 213.5-353.962264 213.5 353.962264-213.5 126.037736z"/>
            </g>
           </svg>
        `
      };
    }
  };
}

const defaultWalletChecks = [
    { checkName: 'connect' },
    { checkName: 'network' },
    { checkName: 'balance', minimumBalance: '100000' }
]

const tokenBalanceCheck = tokenBalance({tokenAddress: '0x6b175474e89094c44da98b954eedeac495271d0f', tokenName: 'Dai', minimumBalance: 5})

const onboard = Onboard({
    //... other options
    walletCheck: [...defaultWalletChecks, tokenBalanceCheck]
})

And I'm getting this error: Error: call revert exception (method="balanceOf(address)", errorSignature=null, errorArgs=[null], reason=null, code=CALL_EXCEPTION, version=abi/5.1.0)

The only difference is that I'm using an erc20 abi format I got externally. I've also checked that my network and my addresses were correct. Not sure why I'm getting that error so any help would be appreciated!

emma
  • 359
  • 1
  • 3
  • 6
  • 1
    Call exception happens when the called address responds with unexpected format or doesn't respond at all (usually because there' no contract on this address). Can you check that you're performing the call on the same network as the `0x6b175...` contract exists? – Petr Hejda May 16 '21 at 09:31
  • @PetrHejda thank you! I tested again on the Kovan test network, and it seemed to work. It's not working on the mainnet. I'm using DAI's kovan contract address on the kovan network and DAI's mainnet contract address on the mainnet. So yes, the contract should exist on the respective networks. – emma May 17 '21 at 15:25

1 Answers1

2

This error (CALL_EXCEPTION) happens when your smart contract is not deployed yet.

Wait the transaction ends:

await contract.deployTransaction.wait()

and only after this you can do something with contract as get the balance of it.

Silvio Guedes
  • 1,134
  • 15
  • 16