Skip to content

Commit

Permalink
Confirmation logic redo (still waiting for the Solana devs to fix this)
Browse files Browse the repository at this point in the history
  • Loading branch information
martincik committed Oct 21, 2022
1 parent 7ab5c3b commit 9f3f790
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/libs/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PublicKey,
TransactionInstruction,
Transaction,
ConfirmOptions,
} from '@solana/web3.js';

export const sendTx = async (
Expand All @@ -13,16 +14,46 @@ export const sendTx = async (
sendTransaction: (
tx: Transaction,
connection: Connection,
options?: SendTransactionOptions,
sendOptions?: SendTransactionOptions
) => Promise<string>,
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;
};

0 comments on commit 9f3f790

Please sign in to comment.