import type { Blockhash , Commitment , ConfirmOptions , Connection , RpcResponseAndContext , SendOptions , SimulatedTransactionResponse , Transaction , } from "@solana/web3.js" ; import type { Broadcaster } from "." ; import { DEFAULT_PROVIDER_OPTIONS , PendingTransaction , suppressConsoleErrorAsync , } from "." ; import { simulateTransactionWithCommitment } from "./utils/simulateTransactionWithCommitment" ; export interface BroadcastOptions extends ConfirmOptions { /**
     * Prints the transaction logs as emitted by @solana/web3.js. Defaults to true.
     */ printLogs ? : boolean ; } /**
 * Broadcasts transactions to a single connection.
 */ export class SingleConnectionBroadcaster implements Broadcaster { constructor ( readonly sendConnection , readonly opts = DEFAULT_PROVIDER_OPTIONS ) { } async getRecentBlockhash ( commitment = "processed" ) { const result = await this . sendConnection . getRecentBlockhash ( commitment ) ; return result . blockhash ; } param tx param confirm param opts returns async broadcast ( tx , { printLogs = true , ... opts } = this . opts ) { if ( tx . signatures . length === 0 ) { throw new Error ( "Transaction must be signed before broadcasting." ) ; } const rawTx = tx . serialize ( ) ; if ( printLogs ) { return new PendingTransaction ( this . sendConnection , await this . sendConnection . sendRawTransaction ( rawTx , opts ) ) ; } return await suppressConsoleErrorAsync ( async ( ) => { return new PendingTransaction ( this . sendConnection , await this . sendConnection . sendRawTransaction ( rawTx , opts ) ) ; } ) ; } param tx param commitment returns async simulate ( tx , { commitment = "processed" , verifySigners = true , } = { commitment : "processed" , verifySigners : true , } ) { if ( verifySigners && tx . signatures . length === 0 ) { throw new Error ( "Transaction must be signed before simulating." ) ; } return await simulateTransactionWithCommitment ( this . sendConnection , tx , commitment ) ; } } /**
 * Broadcasts transactions to multiple connections simultaneously.
 */ export class MultipleConnectionBroadcaster implements Broadcaster { constructor ( readonly connections , readonly opts = DEFAULT_PROVIDER_OPTIONS ) { } async getRecentBlockhash ( commitment = this . opts . commitment ?? "processed" ) { const result = await Promise . any ( this . connections . map ( ( conn ) => conn . getRecentBlockhash ( commitment ) ) ) ; return result . blockhash ; } private async _sendRawTransaction ( encoded , options ? ) { return await Promise . any ( this . connections . map ( async ( connection ) => { return new PendingTransaction ( connection , await connection . sendRawTransaction ( encoded , options ) ) ; } ) ) ; } param tx param confirm param opts returns async broadcast ( tx , { printLogs = true , ... opts } = this . opts ) { if ( tx . signatures . length === 0 ) { throw new Error ( "Transaction must be signed before broadcasting." ) ; } const rawTx = tx . serialize ( ) ; if ( printLogs ) { return await this . _sendRawTransaction ( rawTx , opts ) ; } return await suppressConsoleErrorAsync ( async ( ) => { return await this . _sendRawTransaction ( rawTx , opts ) ; } ) ; } param tx param commitment returns async simulate ( tx , { commitment = "processed" , verifySigners = true , } = { commitment : "processed" , verifySigners : true , } ) { if ( verifySigners && tx . signatures . length === 0 ) { throw new Error ( "Transaction must be signed before simulating." ) ; } return await Promise . any ( this . connections . map ( async ( connection ) => { return await simulateTransactionWithCommitment ( connection , tx , commitment ) ; } ) ) ; } }