From 09578278c3908d816a4fcca274b54160e2babf40 Mon Sep 17 00:00:00 2001 From: Nagaprasadvr Date: Thu, 21 Mar 2024 18:50:15 +0530 Subject: [PATCH] add max-retries arg to cli and logic to sdk --- packages/cli/src/cvg.ts | 5 ++++- packages/cli/src/helpers.ts | 13 +++++++++++++ packages/js/src/Convergence.ts | 3 +++ packages/js/src/plugins/rpcModule/RpcClient.ts | 8 ++++++++ packages/js/src/utils/TransactionBuilder.ts | 8 ++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/cvg.ts b/packages/cli/src/cvg.ts index e570dfd85..91e4d1a6e 100644 --- a/packages/cli/src/cvg.ts +++ b/packages/cli/src/cvg.ts @@ -1,7 +1,7 @@ import { readFileSync } from 'fs'; import { Connection, Keypair } from '@solana/web3.js'; import { Convergence, keypairIdentity } from '@convergence-rfq/sdk'; -import { resolveTxPriorityArg } from './helpers'; +import { resolveMaxRetriesArg, resolveTxPriorityArg } from './helpers'; export type Opts = any; @@ -9,6 +9,8 @@ export const createCvg = async (opts: Opts): Promise => { const buffer = JSON.parse(readFileSync(opts.keypairFile, 'utf8')); const user = Keypair.fromSecretKey(new Uint8Array(buffer)); const txPriorityString: string = opts.txPriorityFee; + const maxRetriesString = opts.maxRetries; + const maxRetries = resolveMaxRetriesArg(maxRetriesString); const txPriority = resolveTxPriorityArg(txPriorityString); const cvg = new Convergence( @@ -18,6 +20,7 @@ export const createCvg = async (opts: Opts): Promise => { { skipPreflight: opts.skipPreflight, transactionPriority: txPriority, + maxRetries, } ); cvg.use(keypairIdentity(user)); diff --git a/packages/cli/src/helpers.ts b/packages/cli/src/helpers.ts index 38477be03..c0da41811 100644 --- a/packages/cli/src/helpers.ts +++ b/packages/cli/src/helpers.ts @@ -58,6 +58,11 @@ export const addDefaultArgs = (cmd: any) => { 'transaction priority fee can be [none : 0 mcLamports , normal : 1 mcLamports, high : 10 mcLamports, turbo : 100 mcLamports, custom : mcLamports]', 'none' ); + cmd.option( + '--max-retries ', + 'maximum numbers of retries for sending failed txs', + 0 + ); cmd.option('--keypair-file ', 'keypair file', DEFAULT_KEYPAIR_FILE); cmd.option('--verbose ', 'verbose', false); return cmd; @@ -175,3 +180,11 @@ export const resolveTxPriorityArg = ( } } }; + +export const resolveMaxRetriesArg = (maxRetries: string): number => { + const maxRetriesInNumber = Number(maxRetries); + if (isNaN(maxRetriesInNumber) || maxRetriesInNumber < 0) { + return 0; + } + return maxRetriesInNumber; +}; diff --git a/packages/js/src/Convergence.ts b/packages/js/src/Convergence.ts index 3108eff55..d34dc12a4 100644 --- a/packages/js/src/Convergence.ts +++ b/packages/js/src/Convergence.ts @@ -11,6 +11,7 @@ export type ConvergenceOptions = { cluster?: Cluster; skipPreflight?: boolean; transactionPriority?: TransactionPriority; + maxRetries?: number; }; export class Convergence { @@ -18,12 +19,14 @@ export class Convergence { public readonly cluster: Cluster; public readonly skipPreflight: boolean; public readonly transactionPriority: TransactionPriority; + public readonly maxRetries: number; constructor(connection: Connection, options: ConvergenceOptions = {}) { this.connection = connection; this.cluster = options.cluster ?? resolveClusterFromConnection(connection); this.skipPreflight = options.skipPreflight ?? false; this.transactionPriority = options.transactionPriority ?? 'none'; + this.maxRetries = options.maxRetries ?? 0; this.use(corePlugins()); } diff --git a/packages/js/src/plugins/rpcModule/RpcClient.ts b/packages/js/src/plugins/rpcModule/RpcClient.ts index 8303bd5e8..54064adfc 100644 --- a/packages/js/src/plugins/rpcModule/RpcClient.ts +++ b/packages/js/src/plugins/rpcModule/RpcClient.ts @@ -232,6 +232,14 @@ export class RpcClient { } const rawTransaction = transaction.serialize(); + + const { maxRetries } = this.convergence; + if (maxRetries > 0) { + confirmOptions = { + ...confirmOptions, + maxRetries, + }; + } const signature = await this.sendRawTransaction( rawTransaction, confirmOptions, diff --git a/packages/js/src/utils/TransactionBuilder.ts b/packages/js/src/utils/TransactionBuilder.ts index 3fa577864..cda667efa 100644 --- a/packages/js/src/utils/TransactionBuilder.ts +++ b/packages/js/src/utils/TransactionBuilder.ts @@ -267,6 +267,14 @@ export class TransactionBuilder { convergence: Convergence, confirmOptions?: ConfirmOptions ): Promise<{ response: SendAndConfirmTransactionResponse } & C> { + const { maxRetries } = convergence; + + if (maxRetries > 0) { + confirmOptions = { + ...confirmOptions, + maxRetries, + }; + } const response = await convergence .rpc() .sendAndConfirmTransaction(this, [], confirmOptions);