Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add configurable transaction priority support #3517

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions ts/packages/anchor/src/program/namespace/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Transaction,
TransactionInstruction,
TransactionSignature,
ComputeBudgetProgram,
} from "@solana/web3.js";
import {
Idl,
Expand Down Expand Up @@ -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<TransactionSignature> {
public async rpc(
options?: ConfirmOptions & {
priority?: "High" | "Medium" | "Low" | number;
priorityLevels?: {
High?: number;
Medium?: number;
Low?: number;
};
}
): Promise<TransactionSignature> {
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,
});
}

Expand Down