Expand usage of postTxConfirmationCallback (#842)

* ts: include slot when fetching LatestBlockHash

* ts: add txSignatureBlockHash to postTxConfirmationCallback

* v0.21.9
This commit is contained in:
Lou-Kamades 2024-01-09 14:38:57 -06:00 committed by GitHub
parent bc8bdaed89
commit 0971557047
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 19 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@blockworks-foundation/mango-v4",
"version": "0.21.8",
"version": "0.21.9",
"description": "Typescript Client for mango-v4 program.",
"repository": "https://github.com/blockworks-foundation/mango-v4",
"author": {

View File

@ -94,6 +94,7 @@ import {
toNativeSellPerBuyTokenPrice,
} from './utils';
import {
LatestBlockhash,
MangoSignature,
MangoSignatureStatus,
SendTransactionOpts,
@ -116,7 +117,13 @@ export type IdsSource = 'api' | 'static' | 'get-program-accounts';
export type MangoClientOptions = {
idsSource?: IdsSource;
postSendTxCallback?: ({ txid }: { txid: string }) => void;
postTxConfirmationCallback?: ({ txid }: { txid: string }) => void;
postTxConfirmationCallback?: ({
txid,
txSignatureBlockHash,
}: {
txid: string;
txSignatureBlockHash: LatestBlockhash;
}) => void;
prioritizationFee?: number;
estimateFee?: boolean;
txConfirmationCommitment?: Commitment;

View File

@ -32,17 +32,26 @@ export interface MangoSignature {
signature: TransactionSignature;
}
export interface LatestBlockhash {
slot: number;
blockhash: string;
lastValidBlockHeight: number;
}
export type SendTransactionOpts = Partial<{
preflightCommitment: Commitment;
latestBlockhash: Readonly<{
blockhash: string;
lastValidBlockHeight: number;
}>;
latestBlockhash: Readonly<LatestBlockhash>;
prioritizationFee: number;
estimateFee: boolean;
additionalSigners: Keypair[];
postSendTxCallback: ({ txid }: { txid: string }) => void;
postTxConfirmationCallback: ({ txid }: { txid: string }) => void;
postTxConfirmationCallback: ({
txid,
txSignatureBlockHash,
}: {
txid: string;
txSignatureBlockHash: LatestBlockhash;
}) => void;
txConfirmationCommitment: Commitment;
confirmInBackground: boolean;
alts: AddressLookupTableAccount[];
@ -70,13 +79,7 @@ export async function sendTransaction(
opts: SendTransactionOpts = {},
): Promise<MangoSignatureStatus | MangoSignature> {
const connection = provider.connection;
const latestBlockhash =
opts.latestBlockhash ??
(await connection.getLatestBlockhash(
opts.preflightCommitment ??
provider.opts.preflightCommitment ??
'finalized',
));
const latestBlockhash = await fetchLatestBlockHash(provider, opts);
const payer = (provider as AnchorProvider).wallet;
@ -174,10 +177,7 @@ export async function sendTransaction(
const confirmTransaction = async (
connection: Connection,
opts: Partial<SendTransactionOpts> = {},
latestBlockhash: Readonly<{
blockhash: string;
lastValidBlockHeight: number;
}>,
latestBlockhash: Readonly<LatestBlockhash>,
signature: string,
): Promise<MangoSignatureStatus> => {
const txConfirmationCommitment = opts.txConfirmationCommitment ?? 'processed';
@ -210,7 +210,10 @@ const confirmTransaction = async (
}
if (opts.postTxConfirmationCallback) {
try {
opts.postTxConfirmationCallback({ txid: signature });
opts.postTxConfirmationCallback({
txid: signature,
txSignatureBlockHash: latestBlockhash,
});
} catch (e) {
console.warn(`postTxConfirmationCallback error ${e}`);
}
@ -218,6 +221,26 @@ const confirmTransaction = async (
return { signature, slot: status.context.slot, ...signatureResult };
};
export async function fetchLatestBlockHash(
provider: AnchorProvider,
opts: SendTransactionOpts = {},
): Promise<LatestBlockhash> {
if (opts.latestBlockhash) {
return opts.latestBlockhash;
}
const commitment =
opts.preflightCommitment ??
provider.opts.preflightCommitment ??
'finalized';
const blockhashRequest =
await provider.connection.getLatestBlockhashAndContext(commitment);
return {
slot: blockhashRequest.context.slot,
lastValidBlockHeight: blockhashRequest.value.lastValidBlockHeight,
blockhash: blockhashRequest.value.blockhash,
};
}
export const createComputeBudgetIx = (
microLamports: number,
): TransactionInstruction => {