Skip to content

Commit

Permalink
Replace transaction send-and-confirm with default implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Aug 8, 2024
1 parent 8870bfd commit 949da54
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions ping-thing-client.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { safeRace } from "@solana/promises";
import {
createTransactionMessage,
pipe,
Expand All @@ -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";
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 949da54

Please sign in to comment.