Random "Did it run out of gas" from Web3.js using Web3AuthProvider

Hello everybody and thanks for reading the following issue.

I have a DAPP, for UX purpose I want to know and display the amount of a specific ERC20 Token.

Everything is going fine, my code works, I can retrieve easily the amount of the specific asset of the Web3Auth connected wallet.

However, once in a while (i’d say 1 out of 20) i’ve go the following error (screenshot bellow) :

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 do not have the issues on mainnet yet (because I’m not on the mainnet :disguised_face:)

The other thing that trigger me is that I do not believe Web3Auth client is using the Infura RPC Url I give it (it shows in my Infura dashboard that no call has been done using my Infura key, so what’s the point of giving it in the first case :melting_face:)

The error seems to be triggered by Web3.js library used in the code bellow to create a tokenContract which I can call to retrieve said assets.

There is definitely something I can’t grasp my mind on, I’m new to Web3 development so do not restrain from asking obvious things.

One guess I have. I had to downgrade Web3.js from v4.0 to v1.8 to comply with Web3Auth expectation. Maybe there’s something going on with using a “not so recent” version of Web3.js ?

Please provide the following details too when asking for help in this category:

Please provide the Web3Auth initialization and login code snippet below:

My getBalance() function

public async getBalance(): Promise<string | null> {
    try {
      const address = await this.getAddress();
      if (!address) {
        return null;
      }
      // balance
      const balance = this.tokenContract.methods.balanceOf(address).call();
      return balance;
    } catch (error) {
      this.logger.error(error);
    }
    return null;
  }

My initTokenContract (initializing the tokenContract you can see above)

  private initTokenContract() {
    this.logger.info('[Web3Auth Client] Init TokenContract');
    if (!this.web3) {
      this.logger.warn('[Web3Auth Client] Web3 is not init');
      return;
    }
    this.tokenContract = new this.web3.eth.Contract(
      this.tokenConfig.abi,
      this.tokenConfig.contractAddress,
    );
  }

The initWeb3 allowing us to initiate the token contract

  private initWeb3() {
    this.logger.info('[Web3Auth Client] Init Web3');
    // any as in the documentation
    this.web3 = new Web3(this.web3Auth.provider as any);
  }

And last but not least : the Web3Auth init

this.web3Auth = new Web3Auth({
      clientId,
      web3AuthNetwork,
      chainConfig: {
        chainNamespace: CHAIN_NAMESPACES.EIP155,
        ...this.chainConfigs()[chainConfig],
      },
      ...
    });

My chain config look like this

return {
      // mainnet
      polygon: {
        chainId: '0x89',
        rpcTarget: `https://polygon-mainnet.infura.io/v3/${this.infuraApiKey}`,
      },
      // testnet
      mumbai: {
        chainId: '0x13881',
        rpcTarget: `https://polygon-mumbai.infura.io/v3/${this.infuraApiKey}`,
        displayName: 'Polygon Mumbai Testnet',
        blockExplorer: 'https://polygonscan.com/',
        ticker: 'MATIC',
        tickerName: 'Matic',
      },
    };
2 Likes

@JulienRousset Welcome Aboard!

Your issue has been forwarded to our team and we will get back to you with further updates.

@JulienRousset Can you check the version of Web3.js ? If you are on version 4 , downgrade to version 1.8.0 and it should work.

Hi there, as you can see in me previous post, I’ve already downgraded Web3.js to v1.8 :slightly_smiling_face:

  1. Check if you are using correct contract address and abi of contract.
  2. Check if you are connected to the same network. Make sure whichever network you deployed to, the created wallet is also using account from that network.
  3. The environment you have setup your project, the web3AuthNetwork should have the same corresponding environment in your code as well such as mainnet , aqua , cyan, sapphire_mainnet, testnet or sapphire_devnet
  4. Can you try using different RPC URL instead of the infura one just to check the behavior?

You can also try upgrading the SDK to the latest version V7 which supports the latest web3.js v4 version:

I’m gonna try this solution as soon as I can, thanks for the tips.

I’ll keep in touch

Using my own Infura url after a while the error stopped showing up. Sorry if someone have the same issue :grinning:

Thanks a lot @vjgee :slight_smile:

@JulienRousset Glad to hear that!