@pythnetwork/solana-utils
Advanced tools
Comparing version
@@ -0,1 +1,2 @@ | ||
import { Logger } from "ts-log"; | ||
import { Wallet } from "@coral-xyz/anchor"; | ||
@@ -10,3 +11,6 @@ import { PublicKey, Signer, TransactionInstruction, VersionedTransaction } from "@solana/web3.js"; | ||
signers?: Signer[] | undefined; | ||
}[], searcherClient: SearcherClient, wallet: Wallet): Promise<string>; | ||
}[], searcherClients: SearcherClient | SearcherClient[], wallet: Wallet, options?: { | ||
maxRetryTimeMs?: number; | ||
delayBetweenCyclesMs?: number; | ||
}, logger?: Logger): Promise<string>; | ||
//# sourceMappingURL=jito.d.ts.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.sendTransactionsJito = exports.buildJitoTipInstruction = exports.getRandomTipAccount = exports.TIP_ACCOUNTS = void 0; | ||
const ts_log_1 = require("ts-log"); | ||
const web3_js_1 = require("@solana/web3.js"); | ||
@@ -34,3 +35,12 @@ const bs58_1 = __importDefault(require("bs58")); | ||
exports.buildJitoTipInstruction = buildJitoTipInstruction; | ||
async function sendTransactionsJito(transactions, searcherClient, wallet) { | ||
async function sendTransactionsJito(transactions, searcherClients, wallet, options = {}, logger = ts_log_1.dummyLogger) { | ||
const clients = Array.isArray(searcherClients) | ||
? searcherClients | ||
: [searcherClients]; | ||
if (clients.length === 0) { | ||
throw new Error("No searcher clients provided"); | ||
} | ||
const maxRetryTimeMs = options.maxRetryTimeMs || 60000; // Default to 60 seconds | ||
const delayBetweenCyclesMs = options.delayBetweenCyclesMs || 1000; // Default to 1 second | ||
const startTime = Date.now(); | ||
const signedTransactions = []; | ||
@@ -48,5 +58,39 @@ for (const transaction of transactions) { | ||
const bundle = new types_1.Bundle(signedTransactions, 2); | ||
await searcherClient.sendBundle(bundle); | ||
return firstTransactionSignature; | ||
let lastError = null; | ||
let totalAttempts = 0; | ||
while (Date.now() - startTime < maxRetryTimeMs) { | ||
// Try all clients in this cycle | ||
for (let i = 0; i < clients.length; i++) { | ||
const currentClient = clients[i]; | ||
totalAttempts++; | ||
try { | ||
await currentClient.sendBundle(bundle); | ||
logger.info({ clientIndex: i, totalAttempts }, `Successfully sent bundle to Jito client after ${totalAttempts} attempts`); | ||
return firstTransactionSignature; | ||
} | ||
catch (err) { | ||
lastError = err; | ||
logger.error({ clientIndex: i, totalAttempts, err: err.message }, `Attempt ${totalAttempts}: Error sending bundle to Jito client ${i}`); | ||
} | ||
// Check if we've run out of time | ||
if (Date.now() - startTime >= maxRetryTimeMs) { | ||
break; | ||
} | ||
} | ||
// If we've tried all clients and still have time, wait before next cycle | ||
const timeRemaining = maxRetryTimeMs - (Date.now() - startTime); | ||
if (timeRemaining > delayBetweenCyclesMs) { | ||
await new Promise((resolve) => setTimeout(resolve, delayBetweenCyclesMs)); | ||
} | ||
} | ||
const totalTimeMs = Date.now() - startTime; | ||
const errorMsg = `Failed to send transactions via JITO after ${totalAttempts} attempts over ${totalTimeMs}ms (max: ${maxRetryTimeMs}ms)`; | ||
logger.error({ | ||
totalAttempts, | ||
totalTimeMs, | ||
maxRetryTimeMs, | ||
lastError: lastError?.message, | ||
}, errorMsg); | ||
throw lastError || new Error(errorMsg); | ||
} | ||
exports.sendTransactionsJito = sendTransactionsJito; |
@@ -304,3 +304,3 @@ "use strict"; | ||
? tx.signatures?.[0] || new Uint8Array() | ||
: tx.signature ?? new Uint8Array()); | ||
: (tx.signature ?? new Uint8Array())); | ||
const confirmTransactionPromise = connection.confirmTransaction({ | ||
@@ -307,0 +307,0 @@ signature: txSignature, |
{ | ||
"name": "@pythnetwork/solana-utils", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"description": "Utility functions for Solana", | ||
@@ -19,11 +19,2 @@ "homepage": "https://pyth.network", | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"format": "prettier --write \"src/**/*.ts\"", | ||
"test:unit": "jest", | ||
"test:lint": "eslint src/", | ||
"prepublishOnly": "pnpm run build && pnpm test:unit && pnpm run test:lint", | ||
"preversion": "pnpm run test:lint", | ||
"version": "pnpm run format && git add -A src" | ||
}, | ||
"keywords": [ | ||
@@ -42,3 +33,3 @@ "pyth", | ||
"jest": "^29.4.0", | ||
"prettier": "^2.6.2", | ||
"prettier": "^3.5.3", | ||
"quicktype": "^23.0.76", | ||
@@ -52,5 +43,15 @@ "ts-jest": "^29.0.5", | ||
"bs58": "^5.0.0", | ||
"jito-ts": "^3.0.1" | ||
"jito-ts": "^3.0.1", | ||
"ts-log": "^2.2.7" | ||
}, | ||
"gitHead": "f5c4b3df87829b926d59a5058be74917eec05b90" | ||
} | ||
"scripts": { | ||
"build": "tsc", | ||
"test:unit": "jest", | ||
"test:lint": "eslint src/ --max-warnings 0", | ||
"test:format": "prettier --check \"src/**/*.ts\"", | ||
"fix:lint": "eslint src/ --fix --max-warnings 0", | ||
"fix:format": "prettier --write \"src/**/*.ts\"", | ||
"preversion": "pnpm run test:lint", | ||
"version": "pnpm run format && git add -A src" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35800
7.06%593
8.81%5
25%+ Added
+ Added