fetchBalance method

Future<Balance> fetchBalance(
  1. String address
)

Query the network to find a balance from an address @param {String} The address scalar type represents a cryptographic hash used in the ArchEthic network with an identification byte to specify from which algorithm the hash was generated. The Hash appears in a JSON response as Base16 formatted string. The parsed hash will be converted to a binary and any invalid hash with an invalid algorithm or invalid size will be rejected Returns Balance represents a ledger balance. It includes: UCO: uco balance & NFT: NFT balances

Implementation

Future<Balance> fetchBalance(String address) async {
  final Completer<Balance> _completer = Completer<Balance>();
  BalanceResponse? balanceResponse;
  Balance balance = Balance();

  final Map<String, String> requestHeaders = {
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };

  final String _body =
      '{"query": "query {balance(address: \\"$address\\") {uco, nft {address, amount}}}"}';
  logger.d('fetchBalance: requestHttp.body=' + _body);

  try {
    final http.Response responseHttp = await http.post(
        Uri.parse(endpoint! + '/api'),
        body: _body,
        headers: requestHeaders);
    logger.d('fetchBalance: responseHttp.body=' + responseHttp.body);

    if (responseHttp.statusCode == 200) {
      balanceResponse = balanceResponseFromJson(responseHttp.body);
      if (balanceResponse.data != null) {
        balance = balanceResponse.data!.balance!;
      }
    }
  } catch (e) {
    logger.d('fetchBalance: error=' + e.toString());
  }

  _completer.complete(balance);
  return _completer.future;
}