sendAndSubmitTransaction method
Sends and monitors the transaction on the Ethereum network via the provided RPC.
Throws an exception if there are validation errors, if the RPC request fails, or if the transaction receipt is not received within the specified timeout.
Implementation
Future<TransactionReceipt> sendAndSubmitTransaction(
EVMRPC rpc, {
Duration timeout = const Duration(minutes: 5),
Duration periodicTimeOut = const Duration(seconds: 10),
}) async {
_checkError();
Timer? timer;
try {
final rawHex =
BytesUtils.toHexString(signedSerializedTransaction(), prefix: "0x");
final result =
await rpc.request(RPCSendRawTransaction(transaction: rawHex));
final Completer<TransactionReceipt> completer =
Completer<TransactionReceipt>();
timer = Timer.periodic(periodicTimeOut, (t) async {
final receipt = await rpc
.request(RPCGetTransactionReceipt(transactionHash: result))
.catchError((e, s) {
return null;
});
if (receipt != null && !completer.isCompleted) {
completer.complete(receipt);
}
});
final receipt = await completer.future.timeout(timeout);
return receipt;
} finally {
timer?.cancel();
timer = null;
}
}