waitForSignatureStatus method
- TxSignature signature,
- TxStatus desiredStatus,
- [dynamic timeout = const Duration(seconds: 30)]
This is just a helper function that allows the caller
to wait for the transaction with signature signature
to
be in a desired desiredStatus
.
Optionally a timeout
can be specified and given that the state
did not change to or past desiredStatus
the method will
throw an error.
Note: the default timeout
is 30 seconds
Implementation
Future<void> waitForSignatureStatus(
TxSignature signature, TxStatus desiredStatus,
[timeout = const Duration(seconds: 30)]) async {
Completer completer = Completer<void>();
Stopwatch clock = Stopwatch();
void check() async {
if (clock.elapsed > timeout) {
completer.completeError(
'timed out waiting for the requested status $desiredStatus',
);
return;
}
final SignatureStatuses statuses =
await getSignatureStatuses([signature]);
final SignatureStatus status = statuses[0];
if (status != null) {
if (status.err != null) {
completer.completeError(status.err);
} else if (status.confirmationStatus.index >= desiredStatus.index) {
completer.complete();
} else {
await Future.delayed(Duration(seconds: 5), () => {});
check();
}
} else {
await Future.delayed(Duration(seconds: 5), () => {});
check();
}
}
clock.start();
check();
return completer.future;
}