From 9f3f7907f8abc5d5e67837304d9b2ac10ec527ae Mon Sep 17 00:00:00 2001 From: Ladislav Martincik Date: Fri, 21 Oct 2022 10:38:02 +0200 Subject: [PATCH] Confirmation logic redo (still waiting for the Solana devs to fix this) https://github.com/solana-labs/solana/pull/28290 --- src/libs/transactions.ts | 43 ++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/libs/transactions.ts b/src/libs/transactions.ts index 032a9e7..a9e109b 100644 --- a/src/libs/transactions.ts +++ b/src/libs/transactions.ts @@ -4,6 +4,7 @@ import { PublicKey, TransactionInstruction, Transaction, + ConfirmOptions, } from '@solana/web3.js'; export const sendTx = async ( @@ -13,16 +14,46 @@ export const sendTx = async ( sendTransaction: ( tx: Transaction, connection: Connection, - options?: SendTransactionOptions, + sendOptions?: SendTransactionOptions ) => Promise, - options?: SendTransactionOptions, + sendOptions?: SendTransactionOptions, + options?: ConfirmOptions ) => { const latestBlockHash = await connection.getLatestBlockhash(); - const tx = new Transaction().add(...instructions); - tx.recentBlockhash = latestBlockHash.blockhash; - tx.feePayer = feePayer; + const transaction = new Transaction({ ...latestBlockHash }).add( + ...instructions + ); + transaction.feePayer = feePayer; + + const signature = await sendTransaction(transaction, connection, sendOptions); + + // TODO: https://github.com/solana-labs/solana/pull/28290 + const status = + transaction.recentBlockhash != null && + transaction.lastValidBlockHeight != null + ? ( + await connection.confirmTransaction( + { + signature: signature, + ...latestBlockHash, + }, + options && options.commitment + ) + ).value + : ( + await connection.confirmTransaction( + signature, + options && options.commitment + ) + ).value; + + if (status.err) { + throw new Error( + `Transaction ${signature} failed (${JSON.stringify(status)})` + ); + } - const signature = await sendTransaction(tx, connection, options); console.log('Signature: ', signature); + return signature; };