feat: propagate errors in send transactions (#1469)

* Go

* Go

* Propagate errors

* lint

* Bump version
This commit is contained in:
guibescos 2024-04-19 16:57:00 +01:00 committed by GitHub
parent c58b675a63
commit 0d6c35fce8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 52 deletions

2
package-lock.json generated
View File

@ -59689,7 +59689,7 @@
},
"target_chains/solana/sdk/js/solana_utils": {
"name": "@pythnetwork/solana-utils",
"version": "0.4.0",
"version": "0.4.1",
"license": "Apache-2.0",
"dependencies": {
"@coral-xyz/anchor": "^0.29.0",

View File

@ -1,6 +1,6 @@
{
"name": "@pythnetwork/solana-utils",
"version": "0.4.0",
"version": "0.4.1",
"description": "Utility functions for Solana",
"homepage": "https://pyth.network",
"main": "lib/index.js",

View File

@ -5,6 +5,7 @@ import {
Connection,
PACKET_DATA_SIZE,
PublicKey,
SignatureResult,
Signer,
Transaction,
TransactionInstruction,
@ -425,7 +426,7 @@ export async function sendTransactions(
// In the following section, we wait and constantly check for the transaction to be confirmed
// and resend the transaction if it is not confirmed within a certain time interval
// thus handling tx retries on the client side rather than relying on the RPC
let confirmedTx = null;
let confirmedTx: SignatureResult | null = null;
let retryCount = 0;
// Get the signature of the transaction with different logic for versioned transactions
@ -435,57 +436,64 @@ export async function sendTransactions(
: tx.signature ?? new Uint8Array()
);
try {
const confirmTransactionPromise = connection.confirmTransaction(
{
signature: txSignature,
blockhash: blockhashResult.value.blockhash,
lastValidBlockHeight: blockhashResult.value.lastValidBlockHeight,
},
"confirmed"
);
const confirmTransactionPromise = connection.confirmTransaction(
{
signature: txSignature,
blockhash: blockhashResult.value.blockhash,
lastValidBlockHeight: blockhashResult.value.lastValidBlockHeight,
},
"confirmed"
);
confirmedTx = null;
while (!confirmedTx) {
confirmedTx = await Promise.race([
confirmTransactionPromise,
new Promise((resolve) =>
setTimeout(() => {
resolve(null);
}, TX_RETRY_INTERVAL)
),
]);
if (confirmedTx) {
break;
}
if (maxRetries && maxRetries < retryCount) {
break;
}
console.log(
"Retrying transaction ",
index,
" of ",
transactions.length - 1,
" with signature: ",
txSignature,
" Retry count: ",
retryCount
);
retryCount++;
await connection.sendRawTransaction(tx.serialize(), {
// Skipping preflight i.e. tx simulation by RPC as we simulated the tx above
// This allows Triton RPCs to send the transaction through multiple pathways for the fastest delivery
skipPreflight: true,
// Setting max retries to 0 as we are handling retries manually
// Set this manually so that the default is skipped
maxRetries: 0,
preflightCommitment: "confirmed",
minContextSlot: blockhashResult.context.slot,
});
confirmedTx = null;
while (!confirmedTx) {
confirmedTx = await Promise.race([
new Promise<SignatureResult>((resolve) => {
confirmTransactionPromise.then((result) => {
resolve(result.value);
});
}),
new Promise<null>((resolve) =>
setTimeout(() => {
resolve(null);
}, TX_RETRY_INTERVAL)
),
]);
if (confirmedTx) {
break;
}
} catch (error) {
console.error(error);
if (maxRetries && maxRetries < retryCount) {
break;
}
console.log(
"Retrying transaction ",
index,
" of ",
transactions.length - 1,
" with signature: ",
txSignature,
" Retry count: ",
retryCount
);
retryCount++;
await connection.sendRawTransaction(tx.serialize(), {
// Skipping preflight i.e. tx simulation by RPC as we simulated the tx above
// This allows Triton RPCs to send the transaction through multiple pathways for the fastest delivery
skipPreflight: true,
// Setting max retries to 0 as we are handling retries manually
// Set this manually so that the default is skipped
maxRetries: 0,
preflightCommitment: "confirmed",
minContextSlot: blockhashResult.context.slot,
});
}
if (confirmedTx?.err) {
throw new Error(
`Transaction ${txSignature} has failed with error: ${JSON.stringify(
confirmedTx.err
)}`
);
}
if (!confirmedTx) {