getLastTransaction method

Future<Transaction> getLastTransaction(
  1. String address
)

Query the network to find the last transaction 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

Implementation

Future<Transaction> getLastTransaction(String address) async {
  final Completer<Transaction> _completer = Completer<Transaction>();
  TransactionLastResponse transactionLastResponse = TransactionLastResponse();
  Transaction lastTransaction = new Transaction(
      type: '', chainLength: 0, data: Transaction.initData(), address: '');
  final Map<String, String> requestHeaders = {
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };
  try {
    final String _body =
        '{"query": "query {lastTransaction(address: \\"$address\\") { ' +
            Transaction.getQLFields() +
            ' } }"}';
    logger.d('getTransactionIndex: requestHttp.body=' + _body);
    final http.Response responseHttp = await http.post(
        Uri.parse(endpoint! + '/api'),
        body: _body,
        headers: requestHeaders);
    logger.d('getTransactionIndex: responseHttp.body=' + responseHttp.body);
    if (responseHttp.statusCode == 200) {
      transactionLastResponse =
          transactionLastResponseFromJson(responseHttp.body);
      if (transactionLastResponse.data != null &&
          transactionLastResponse.data!.lastTransaction != null) {
        lastTransaction = transactionLastResponse.data!.lastTransaction!;
      }
    }
  } catch (e) {
    logger.d('getTransactionIndex: error=' + e.toString());
  }

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