Skip to content

Commit 0d6c35f

Browse files
authored
feat: propagate errors in send transactions (#1469)
* Go * Go * Propagate errors * lint * Bump version
1 parent c58b675 commit 0d6c35f

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/solana/sdk/js/solana_utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/solana-utils",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Utility functions for Solana",
55
"homepage": "https://pyth.network",
66
"main": "lib/index.js",

target_chains/solana/sdk/js/solana_utils/src/transaction.ts

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Connection,
66
PACKET_DATA_SIZE,
77
PublicKey,
8+
SignatureResult,
89
Signer,
910
Transaction,
1011
TransactionInstruction,
@@ -425,7 +426,7 @@ export async function sendTransactions(
425426
// In the following section, we wait and constantly check for the transaction to be confirmed
426427
// and resend the transaction if it is not confirmed within a certain time interval
427428
// thus handling tx retries on the client side rather than relying on the RPC
428-
let confirmedTx = null;
429+
let confirmedTx: SignatureResult | null = null;
429430
let retryCount = 0;
430431

431432
// Get the signature of the transaction with different logic for versioned transactions
@@ -435,57 +436,64 @@ export async function sendTransactions(
435436
: tx.signature ?? new Uint8Array()
436437
);
437438

438-
try {
439-
const confirmTransactionPromise = connection.confirmTransaction(
440-
{
441-
signature: txSignature,
442-
blockhash: blockhashResult.value.blockhash,
443-
lastValidBlockHeight: blockhashResult.value.lastValidBlockHeight,
444-
},
445-
"confirmed"
446-
);
439+
const confirmTransactionPromise = connection.confirmTransaction(
440+
{
441+
signature: txSignature,
442+
blockhash: blockhashResult.value.blockhash,
443+
lastValidBlockHeight: blockhashResult.value.lastValidBlockHeight,
444+
},
445+
"confirmed"
446+
);
447447

448-
confirmedTx = null;
449-
while (!confirmedTx) {
450-
confirmedTx = await Promise.race([
451-
confirmTransactionPromise,
452-
new Promise((resolve) =>
453-
setTimeout(() => {
454-
resolve(null);
455-
}, TX_RETRY_INTERVAL)
456-
),
457-
]);
458-
if (confirmedTx) {
459-
break;
460-
}
461-
if (maxRetries && maxRetries < retryCount) {
462-
break;
463-
}
464-
console.log(
465-
"Retrying transaction ",
466-
index,
467-
" of ",
468-
transactions.length - 1,
469-
" with signature: ",
470-
txSignature,
471-
" Retry count: ",
472-
retryCount
473-
);
474-
retryCount++;
475-
476-
await connection.sendRawTransaction(tx.serialize(), {
477-
// Skipping preflight i.e. tx simulation by RPC as we simulated the tx above
478-
// This allows Triton RPCs to send the transaction through multiple pathways for the fastest delivery
479-
skipPreflight: true,
480-
// Setting max retries to 0 as we are handling retries manually
481-
// Set this manually so that the default is skipped
482-
maxRetries: 0,
483-
preflightCommitment: "confirmed",
484-
minContextSlot: blockhashResult.context.slot,
485-
});
448+
confirmedTx = null;
449+
while (!confirmedTx) {
450+
confirmedTx = await Promise.race([
451+
new Promise<SignatureResult>((resolve) => {
452+
confirmTransactionPromise.then((result) => {
453+
resolve(result.value);
454+
});
455+
}),
456+
new Promise<null>((resolve) =>
457+
setTimeout(() => {
458+
resolve(null);
459+
}, TX_RETRY_INTERVAL)
460+
),
461+
]);
462+
if (confirmedTx) {
463+
break;
486464
}
487-
} catch (error) {
488-
console.error(error);
465+
if (maxRetries && maxRetries < retryCount) {
466+
break;
467+
}
468+
console.log(
469+
"Retrying transaction ",
470+
index,
471+
" of ",
472+
transactions.length - 1,
473+
" with signature: ",
474+
txSignature,
475+
" Retry count: ",
476+
retryCount
477+
);
478+
retryCount++;
479+
480+
await connection.sendRawTransaction(tx.serialize(), {
481+
// Skipping preflight i.e. tx simulation by RPC as we simulated the tx above
482+
// This allows Triton RPCs to send the transaction through multiple pathways for the fastest delivery
483+
skipPreflight: true,
484+
// Setting max retries to 0 as we are handling retries manually
485+
// Set this manually so that the default is skipped
486+
maxRetries: 0,
487+
preflightCommitment: "confirmed",
488+
minContextSlot: blockhashResult.context.slot,
489+
});
490+
}
491+
if (confirmedTx?.err) {
492+
throw new Error(
493+
`Transaction ${txSignature} has failed with error: ${JSON.stringify(
494+
confirmedTx.err
495+
)}`
496+
);
489497
}
490498

491499
if (!confirmedTx) {

0 commit comments

Comments
 (0)