Skip to content

Commit

Permalink
fix: adding sign and send to js
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Sep 27, 2024
1 parent c8b3cf3 commit 88a8361
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions js/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = '';
Expand Down Expand Up @@ -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<Keypair>,
feePayer: Keypair,
txInstructions: Array<TransactionInstruction>,
skipPreflight: boolean = false,
preflightCommitment: Commitment = "confirmed",
confirmCommitment: Commitment = "confirmed"
): Promise<string> => {
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;
};

0 comments on commit 88a8361

Please sign in to comment.