How do I retrieve the balance of a Goerli testnet wallet using code?

35 views Asked by At

I want to use this code to fetch the wallet balance, but the balance is not being retrieved. What could be the problem?

  Future<String> getCAMTBalance(String publickey) async {
    final httpClient = Client();
    final ethClient = Web3Client(goerliEndPoint, httpClient);
    final camtAbi = await rootBundle.loadString("assets/CAMT.json");
    const camtTokenAddress = "ㅡㅡ";

    final DeployedContract camtContract = DeployedContract(
        ContractAbi.fromJson(camtAbi, "CAMT"),
        EthereumAddress.fromHex(camtTokenAddress));
    final balanceFunction = camtContract.function('balanceOf');
    final address = EthereumAddress.fromHex(publickey);
    final balance = await ethClient.call(
        contract: camtContract, function: balanceFunction, params: [address]);
    print("$balance");
    final balanceValue = balance.first;
    final camtBalance = balanceValue / BigInt.from(10).pow(3);
    return camtBalance.toString();
  }

  Future<void> fetchBalance() async {
    if (widget.userData['wallet'] != null) {
      final camtBalanceResult =
          await getCAMTBalance(widget.userData['wallet']['address']);
      if (mounted) {
        setState(() {
          walletCAMT = camtBalanceResult;
        });
      }
    } else {
      print('wallet key is missing in userData');
    }
  }

I have a specific wallet with approximately 20 billion worth of tokens, but the app is only displaying about 9 billion tokens. Other wallets are shown as having 0 tokens regardless of their actual amount, so I suspect the app is assigning values to a variable instead of fetching the token balance from the wallets. I've been searching but cannot find any code that directly assigns these values.

When a withdrawal is made within the app, it seems to be processed correctly, but only the balance within the app changes and no actual withdrawal occurs.

0

There are 0 answers