ts: Allow control over tx confirmation commitment level

When talking to a load balanced endpoint it can be useful to read data
from rpc nodes at processed while waiting for confirmed commitment
on transactions.
This commit is contained in:
Christian Kamm 2023-02-03 13:51:18 +01:00
parent 38d408126e
commit 6c8204e9ef
5 changed files with 29 additions and 8 deletions

View File

@ -18,6 +18,7 @@ import {
SystemProgram,
TransactionInstruction,
TransactionSignature,
Commitment,
} from '@solana/web3.js';
import bs58 from 'bs58';
import { Bank, MintInfo, TokenIndex } from './accounts/bank';
@ -76,12 +77,14 @@ export type MangoClientOptions = {
idsSource?: IdsSource;
postSendTxCallback?: ({ txid }: { txid: string }) => void;
prioritizationFee?: number;
txConfirmationCommitment?: Commitment;
};
export class MangoClient {
private idsSource: IdsSource;
private postSendTxCallback?: ({ txid }) => void;
private prioritizationFee: number;
private txConfirmationCommitment: Commitment;
constructor(
public program: Program<MangoV4>,
@ -92,6 +95,10 @@ export class MangoClient {
this.idsSource = opts?.idsSource || 'get-program-accounts';
this.prioritizationFee = opts?.prioritizationFee || 0;
this.postSendTxCallback = opts?.postSendTxCallback;
this.txConfirmationCommitment =
opts?.txConfirmationCommitment ??
(program.provider as AnchorProvider).opts.commitment ??
'processed';
// TODO: evil side effect, but limited backtraces are a nightmare
Error.stackTraceLimit = 1000;
}
@ -109,6 +116,7 @@ export class MangoClient {
{
postSendTxCallback: this.postSendTxCallback,
prioritizationFee: this.prioritizationFee,
txConfirmationCommitment: this.txConfirmationCommitment,
...opts,
},
);

View File

@ -45,6 +45,8 @@ const NET_BORROWS_LIMIT_NATIVE = 1 * Math.pow(10, 7) * Math.pow(10, 6);
async function main() {
const options = AnchorProvider.defaultOptions();
options.commitment = 'processed';
options.preflightCommitment = 'finalized';
const connection = new Connection(process.env.CLUSTER_URL!, options);
const admin = Keypair.fromSecretKey(
@ -63,7 +65,8 @@ async function main() {
MANGO_V4_ID['mainnet-beta'],
{
idsSource: 'get-program-accounts',
prioritizationFee: 5,
prioritizationFee: 100,
txConfirmationCommitment: 'confirmed',
},
);

View File

@ -53,6 +53,8 @@ const TOKEN_SCENARIOS: [string, [string, number][], [string, number][]][] = [
async function main() {
const options = AnchorProvider.defaultOptions();
options.commitment = 'processed';
options.preflightCommitment = 'finalized';
const connection = new Connection(process.env.CLUSTER_URL!, options);
const admin = Keypair.fromSecretKey(
@ -70,7 +72,8 @@ async function main() {
MANGO_V4_ID['mainnet-beta'],
{
idsSource: 'get-program-accounts',
prioritizationFee: 5,
prioritizationFee: 100,
txConfirmationCommitment: 'confirmed',
},
);
console.log(`User ${userWallet.publicKey.toBase58()}`);

View File

@ -16,6 +16,8 @@ const MANGO_MAINNET_PAYER_KEYPAIR =
async function main() {
const options = AnchorProvider.defaultOptions();
options.commitment = 'processed';
options.preflightCommitment = 'finalized';
const connection = new Connection(CLUSTER_URL!, options);
const admin = Keypair.fromSecretKey(
@ -31,7 +33,8 @@ async function main() {
MANGO_V4_ID['mainnet-beta'],
{
idsSource: 'get-program-accounts',
prioritizationFee: 5,
prioritizationFee: 100,
txConfirmationCommitment: 'confirmed',
},
);
console.log(`User ${userWallet.publicKey.toBase58()}`);

View File

@ -17,7 +17,9 @@ export async function sendTransaction(
): Promise<string> {
const connection = provider.connection;
const latestBlockhash = await connection.getLatestBlockhash(
opts.preflightCommitment,
opts.preflightCommitment ??
provider.opts.preflightCommitment ??
'finalized',
);
const payer = (provider as AnchorProvider).wallet;
@ -50,7 +52,7 @@ export async function sendTransaction(
}
const signature = await connection.sendRawTransaction(vtx.serialize(), {
skipPreflight: true,
skipPreflight: true, // mergedOpts.skipPreflight,
});
// const signature = await connection.sendTransactionss(
@ -68,6 +70,7 @@ export async function sendTransaction(
}
}
const txConfirmationCommitment = opts.txConfirmationCommitment ?? 'processed';
let status: any;
if (
latestBlockhash.blockhash != null &&
@ -80,12 +83,13 @@ export async function sendTransaction(
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
},
'processed',
txConfirmationCommitment,
)
).value;
} else {
status = (await connection.confirmTransaction(signature, 'processed'))
.value;
status = (
await connection.confirmTransaction(signature, txConfirmationCommitment)
).value;
}
if (status.err) {