From 949da54c3553620d50d1aa1b7fa4ebed76427902 Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Thu, 8 Aug 2024 18:00:15 +0000 Subject: [PATCH] Replace transaction send-and-confirm with default implementation --- ping-thing-client.mjs | 52 ++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/ping-thing-client.mjs b/ping-thing-client.mjs index 27b5bf0..af8d6ef 100644 --- a/ping-thing-client.mjs +++ b/ping-thing-client.mjs @@ -1,3 +1,4 @@ +import { safeRace } from "@solana/promises"; import { createTransactionMessage, pipe, @@ -6,19 +7,18 @@ import { createKeyPairFromBytes, createSignerFromKeyPair, appendTransactionMessageInstructions, - sendTransactionWithoutConfirmingFactory, signTransactionMessageWithSigners, SOLANA_ERROR__TRANSACTION_ERROR__BLOCKHASH_NOT_FOUND, isSolanaError, getSignatureFromTransaction, + sendAndConfirmTransactionFactory, // Address, } from "@solana/web3.js"; import dotenv from "dotenv"; import bs58 from "bs58"; import { getSetComputeUnitLimitInstruction } from "@solana-program/compute-budget"; import { getTransferSolInstruction } from "@solana-program/system"; -import { createRecentSignatureConfirmationPromiseFactory } from "@solana/transaction-confirmation"; -import { sleep } from "./utils/misc.mjs"; +import { sleep, timeout } from "./utils/misc.mjs"; import { getLatestBlockhash } from "./utils/blockhash.mjs"; import { rpc, rpcSubscriptions } from "./utils/rpc.mjs"; import { getNextSlot } from "./utils/slot.mjs"; @@ -54,16 +54,11 @@ const TX_RETRY_INTERVAL = 2000; setMaxListeners(100); -const mSendTransaction = sendTransactionWithoutConfirmingFactory({ +const mSendAndConfirmTransaction = sendAndConfirmTransactionFactory({ rpc, + rpcSubscriptions, }); -const getRecentSignatureConfirmationPromise = - createRecentSignatureConfirmationPromiseFactory({ - rpc, - rpcSubscriptions, - }); - async function pingThing() { USER_KEYPAIR = await createKeyPairFromBytes( bs58.decode(process.env.WALLET_PRIVATE_KEYPAIR) @@ -119,39 +114,30 @@ async function pingThing() { console.log(`Sending ${signature}`); - const abortController = new AbortController(); - while (true) { try { [slotSent] = await Promise.all([ getNextSlot(), - mSendTransaction(transactionSignedWithFeePayer, { - commitment: "confirmed", - maxRetries: 0n, - skipPreflight: true, - }), - ]); - - await Promise.race([ - getRecentSignatureConfirmationPromise({ - signature, - commitment: "confirmed", - abortSignal: abortController.signal, - }), - sleep(TX_RETRY_INTERVAL * txSendAttempts).then(() => { - throw new Error("Tx Send Timeout"); - }), + safeRace([ + mSendAndConfirmTransaction(transactionSignedWithFeePayer, { + commitment: "confirmed", + }), + timeout(TX_RETRY_INTERVAL * txSendAttempts), + ]), ]); console.log(`Confirmed tx ${signature}`); break; } catch (e) { - console.log( - `Tx not confirmed after ${ - TX_RETRY_INTERVAL * txSendAttempts++ - }ms, resending` - ); + if (e.message === "Timeout") { + console.log( + `Tx not confirmed after ${TX_RETRY_INTERVAL * txSendAttempts++ + }ms, resending` + ); + } else { + throw e; + } } } } catch (e) {