fetchBalance method Null safety
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
@param {String} The endpoint
Returns BalanceResponse
represents a ledger balance. It includes: UCO: uco balance & NFT: NFT balances
Implementation
Future<BalanceResponse> fetchBalance(String address, String endpoint) async {
final Completer<BalanceResponse> _completer = Completer<BalanceResponse>();
BalanceResponse? balanceResponse;
final Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
final String _body =
'{"query": "query {balance(address: \\"$address\\") {uco, nft {address, amount}}}"}';
print(_body);
try {
final http.Response responseHttp = await http.post(
Uri.parse(endpoint + '/api'),
body: _body,
headers: requestHeaders);
print(responseHttp.body);
if (responseHttp.statusCode == 200) {
balanceResponse = balanceResponseFromJson(responseHttp.body);
}
} catch (e) {
print('error: ' + e.toString());
}
_completer.complete(balanceResponse!);
return _completer.future;
}