Skip to content

Commit

Permalink
Merge pull request #284 from convergence-rfq/eng-1871-add-max-tries-a…
Browse files Browse the repository at this point in the history
…rg-to-cli

Eng 1871 add max retries arg to cli
  • Loading branch information
pindaroso authored Mar 21, 2024
2 parents 9e84a77 + 0957827 commit d8b4029
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/cli/src/cvg.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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;

export const createCvg = async (opts: Opts): Promise<Convergence> => {
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(
Expand All @@ -18,6 +20,7 @@ export const createCvg = async (opts: Opts): Promise<Convergence> => {
{
skipPreflight: opts.skipPreflight,
transactionPriority: txPriority,
maxRetries,
}
);
cvg.use(keypairIdentity(user));
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 : <number> mcLamports]',
'none'
);
cmd.option(
'--max-retries <number>',
'maximum numbers of retries for sending failed txs',
0
);
cmd.option('--keypair-file <string>', 'keypair file', DEFAULT_KEYPAIR_FILE);
cmd.option('--verbose <boolean>', 'verbose', false);
return cmd;
Expand Down Expand Up @@ -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;
};
3 changes: 3 additions & 0 deletions packages/js/src/Convergence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ export type ConvergenceOptions = {
cluster?: Cluster;
skipPreflight?: boolean;
transactionPriority?: TransactionPriority;
maxRetries?: number;
};

export class Convergence {
public readonly connection: Connection;
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());
}

Expand Down
8 changes: 8 additions & 0 deletions packages/js/src/plugins/rpcModule/RpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions packages/js/src/utils/TransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ export class TransactionBuilder<C extends object = object> {
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);
Expand Down

0 comments on commit d8b4029

Please sign in to comment.