From 6817f6451694cde093fee611ba267024528d42e5 Mon Sep 17 00:00:00 2001 From: rishavmehra Date: Tue, 28 Jan 2025 02:46:28 +0530 Subject: [PATCH] feat: Add configurable transaction priority support Signed-off-by: rishavmehra --- .../anchor/src/program/namespace/methods.ts | 45 ++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/ts/packages/anchor/src/program/namespace/methods.ts b/ts/packages/anchor/src/program/namespace/methods.ts index 2dec004506..64a14418dd 100644 --- a/ts/packages/anchor/src/program/namespace/methods.ts +++ b/ts/packages/anchor/src/program/namespace/methods.ts @@ -6,6 +6,7 @@ import { Transaction, TransactionInstruction, TransactionSignature, + ComputeBudgetProgram, } from "@solana/web3.js"; import { Idl, @@ -417,27 +418,61 @@ export class MethodsBuilder< } /** - * Send and confirm the configured transaction. + * Send and confirm the configured transaction with optional priority fees. * * See {@link rpcAndKeys} to both send the transaction and get the resolved * account public keys. * - * @param options confirmation options + * @param options confirmation options with priority configuration * @returns the transaction signature */ - public async rpc(options?: ConfirmOptions): Promise { + public async rpc( + options?: ConfirmOptions & { + priority?: "High" | "Medium" | "Low" | number; + priorityLevels?: { + High?: number; + Medium?: number; + Low?: number; + }; + } + ): Promise { if (this._resolveAccounts) { await this._accountsResolver.resolve(); } + const { priority, priorityLevels, ...confirmOptions } = options || {}; + let priorityIx: TransactionInstruction | null = null; + + if (priority !== undefined) { + const defaultLevels = { + High: 100000, + Medium: 50000, + Low: 10000 + }; + + const mergedLevels = { ...defaultLevels, ...priorityLevels }; + const microLamports = typeof priority === "string" + ? (mergedLevels[priority] || 0) + : priority; + + if (microLamports > 0) { + priorityIx = ComputeBudgetProgram.setComputeUnitPrice({ + microLamports + }); + } + } + // @ts-ignore return this._rpcFn(...this._args, { accounts: this._accounts, signers: this._signers, remainingAccounts: this._remainingAccounts, - preInstructions: this._preInstructions, + preInstructions: [ + ...(priorityIx ? [priorityIx] : []), + ...this._preInstructions + ], postInstructions: this._postInstructions, - options, + options: confirmOptions, }); }