From 88a8361ad9357aa9ee165dd2d644b913edf0a4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wei=C3=9Fer=20Hase?= Date: Fri, 27 Sep 2024 15:11:11 -0300 Subject: [PATCH] fix: adding sign and send to js --- js/src/utils.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) 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; +};