y-pakorn / flutter_web3

Web3 Ethereum, Etherjs and Wallet Connect wrapper for Flutter Web.
Other
134 stars 47 forks source link

Get Balance from providers not working #43

Open princepspolycap opened 2 years ago

princepspolycap commented 2 years ago

I have been trying to get the balance from an account that is on the Gnosis Chain. I tried to get the balance on the Ethereum chain too and I haven't been able to. All the providers I have tried return null.

 WalletConnectProvider walletConnectProvider = WalletConnectProvider.fromRpc(
    {100: 'https://rpc.gnosischain.com/'},
    chainId: 100,
    network: 'xdai',
  );

  final rpcProvider = JsonRpcProvider('https://rpc.gnosischain.com/');

  Future<BigInt> getBalanceOf(String tokenAddress, String address) async {
    final contract = ContractERC20(tokenAddress, provider);
    return contract.balanceOf(address);
  }

  Future<BigInt> getNativeTokenBalanceOf(String address) async {
    try {
//      return await getBalanceOf(stakeTokenAddress, testAccountAddress);
//      return await provider!.getSigner().getBalance(ethTokenAddress);

    } catch (error) {
      _error = "${error.toString()} $rpcProvider";
      return BigInt.zero;
    }
  }

Can anyone let me know what I'm doing wrong?

patzu commented 2 years ago

Could you find a solution for this?

4xMafole commented 1 year ago

Did you add this to the below script in your web/index.html file? <script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script> I was facing a different issue of writing to the blockchain since I was missing the line. So, give it a try and hope it helps.