fetchBalance method

Future<Map<String, Balance>> fetchBalance(
  1. List<String> addresses, {
  2. String request = Transaction.kBalanceQueryAllFields,
})

Query the network to find a balance from a list of addresses

Implementation

Future<Map<String, Balance>> fetchBalance(
  List<String> addresses, {
  String request = Transaction.kBalanceQueryAllFields,
}) async {
  if (addresses.isEmpty) {
    return {};
  }

  final fragment = 'fragment fields on Balance { $request }';
  final body = StringBuffer()..write('query { ');
  for (final address in addresses) {
    body.write(
      ' _$address: balance(address:"$address") { ...fields }',
    );
  }
  body.write(' } $fragment');

  final result = await _client
      .withLogger(
        'fetchBalance',
      )
      .query(
        QueryOptions(
          document: gql(body.toString()),
          parserFn: (object) {
            final balances = object.mapValues(
              (value) {
                if (value != null) {
                  return Balance.fromJson(value as Map<String, dynamic>);
                }
              },
              keysToIgnore: _responseKeysToIgnore,
            );
            return removeAliasPrefix(balances) ?? {};
          },
        ),
      );
  manageLinkException(result);

  return result.parsedData ?? {};
}