diff --git a/js/src/utils.ts b/js/src/utils.ts index 20834e8..c61f3bd 100644 --- a/js/src/utils.ts +++ b/js/src/utils.ts @@ -1,5 +1,13 @@ import BN from 'bn.js'; +import { + Keypair, + Connection, + TransactionInstruction, + Transaction, + Commitment, +} from "@solana/web3.js"; + export const generateRandomSeed = () => { // Generate a random seed let seed = ''; @@ -82,3 +90,39 @@ export class Numberu32 extends BN { ); } } + +/** + * Sign and send transaction instructions + * @param connection The solana connection object to the RPC node + * @param signers The signers of the transaction + * @param feePayer The fee payer of the transaction + * @param txInstructions The instructions to + * @param skipPreflight Whether to skip the preflight check or not + * @param preflightCommitment The commitment to use for the preflight + * @param confirmCommitment The commitment to use for the transaction confirmation + * @returns The signature of the transaction as a string + */ +export const signAndSendInstructions = async ( + connection: Connection, + signers: Array, + feePayer: Keypair, + txInstructions: Array, + skipPreflight: boolean = false, + preflightCommitment: Commitment = "confirmed", + confirmCommitment: Commitment = "confirmed" +): Promise => { + const tx = new Transaction(); + + tx.feePayer = feePayer.publicKey; + + signers.push(feePayer); + tx.add(...txInstructions); + + const signature = await connection.sendTransaction(tx, signers, { + skipPreflight, + preflightCommitment, + }); + + await connection.confirmTransaction(signature, confirmCommitment); + return signature; +};