waitForTransactionWithResult method

Future waitForTransactionWithResult(
  1. String txnHash, {
  2. int? timeoutSecs,
  3. bool? checkSuccess,
})

Implementation

Future<dynamic> waitForTransactionWithResult(
  String txnHash,
  { int? timeoutSecs, bool? checkSuccess }
) async {
  timeoutSecs = timeoutSecs ?? 20;
  checkSuccess = checkSuccess ?? false;

  var isPending = true;
  var count = 0;
  dynamic lastTxn;
  while (isPending) {
    if (count >= timeoutSecs) {
      break;
    }
    try {
      lastTxn = await getTransactionByHash(txnHash);
      isPending = lastTxn["type"] == "pending_transaction";
      if (!isPending) {
        break;
      }
    } catch (e) {
      final isDioError = e is DioError;
      int statusCode = 0;
      if (isDioError) {
        statusCode = e.response?.statusCode ?? 0;
      }
      if (isDioError && statusCode != 404 && statusCode >= 400 && statusCode < 500) {
        rethrow;
      }
    }
    await Future.delayed(const Duration(seconds: 1));
    count += 1;
  }

  if (lastTxn == null) {
    throw Exception("Waiting for transaction $txnHash failed");
  }

  if (isPending) {
    throw Exception(
      "Waiting for transaction $txnHash timed out after $timeoutSecs seconds"
    );
  }
  if (!checkSuccess) {
    return lastTxn;
  }
  if (!(lastTxn["success"])) {
    throw Exception(
      "Transaction $txnHash committed to the blockchain but execution failed"
    );
  }
  return lastTxn;
}