mirror of
https://github.com/fdundjer/solana-sniper-bot.git
synced 2025-11-12 07:42:39 +10:00
feat: complete rewrite
This commit is contained in:
37
transactions/default-transaction-executor.ts
Normal file
37
transactions/default-transaction-executor.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { BlockhashWithExpiryBlockHeight, Connection, Transaction, VersionedTransaction } from '@solana/web3.js';
|
||||
import { TransactionExecutor } from './transaction-executor.interface';
|
||||
import { logger } from '../helpers';
|
||||
|
||||
export class DefaultTransactionExecutor implements TransactionExecutor {
|
||||
constructor(private readonly connection: Connection) {}
|
||||
|
||||
public async executeAndConfirm(
|
||||
transaction: Transaction | VersionedTransaction,
|
||||
latestBlockhash: BlockhashWithExpiryBlockHeight,
|
||||
): Promise<{ confirmed: boolean; signature: string }> {
|
||||
logger.debug('Executing transaction...');
|
||||
const signature = await this.execute(transaction);
|
||||
|
||||
logger.debug({ signature }, 'Confirming transaction...');
|
||||
return this.confirm(signature, latestBlockhash);
|
||||
}
|
||||
|
||||
private async execute(transaction: Transaction | VersionedTransaction) {
|
||||
return this.connection.sendRawTransaction(transaction.serialize(), {
|
||||
preflightCommitment: this.connection.commitment,
|
||||
});
|
||||
}
|
||||
|
||||
private async confirm(signature: string, latestBlockhash: BlockhashWithExpiryBlockHeight) {
|
||||
const confirmation = await this.connection.confirmTransaction(
|
||||
{
|
||||
signature,
|
||||
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
|
||||
blockhash: latestBlockhash.blockhash,
|
||||
},
|
||||
this.connection.commitment,
|
||||
);
|
||||
|
||||
return { confirmed: !confirmation.value.err, signature };
|
||||
}
|
||||
}
|
||||
2
transactions/index.ts
Normal file
2
transactions/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './default-transaction-executor';
|
||||
export * from './transaction-executor.interface';
|
||||
8
transactions/transaction-executor.interface.ts
Normal file
8
transactions/transaction-executor.interface.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { BlockhashWithExpiryBlockHeight, Transaction, VersionedTransaction } from '@solana/web3.js';
|
||||
|
||||
export interface TransactionExecutor {
|
||||
executeAndConfirm(
|
||||
transaction: Transaction | VersionedTransaction,
|
||||
latestBlockhash: BlockhashWithExpiryBlockHeight,
|
||||
): Promise<{ confirmed: boolean; signature: string }>;
|
||||
}
|
||||
Reference in New Issue
Block a user