feat: add warp tx executor

This commit is contained in:
Filip Dunder
2024-04-15 22:58:36 +02:00
parent 0c5786ca1c
commit 25bb1a696c
9 changed files with 122 additions and 36 deletions

View File

@ -1,4 +1,10 @@
import { BlockhashWithExpiryBlockHeight, Connection, Transaction, VersionedTransaction } from '@solana/web3.js';
import {
BlockhashWithExpiryBlockHeight,
Connection,
Keypair,
Transaction,
VersionedTransaction,
} from '@solana/web3.js';
import { TransactionExecutor } from './transaction-executor.interface';
import { logger } from '../helpers';
@ -6,9 +12,10 @@ export class DefaultTransactionExecutor implements TransactionExecutor {
constructor(private readonly connection: Connection) {}
public async executeAndConfirm(
transaction: Transaction | VersionedTransaction,
transaction: VersionedTransaction,
payer: Keypair,
latestBlockhash: BlockhashWithExpiryBlockHeight,
): Promise<{ confirmed: boolean; signature: string }> {
): Promise<{ confirmed: boolean; signature?: string }> {
logger.debug('Executing transaction...');
const signature = await this.execute(transaction);

View File

@ -1,8 +1,9 @@
import { BlockhashWithExpiryBlockHeight, Transaction, VersionedTransaction } from '@solana/web3.js';
import { BlockhashWithExpiryBlockHeight, Keypair, MessageV0, Signer, VersionedTransaction } from '@solana/web3.js';
export interface TransactionExecutor {
executeAndConfirm(
transaction: Transaction | VersionedTransaction,
latestBlockhash: BlockhashWithExpiryBlockHeight,
): Promise<{ confirmed: boolean; signature: string }>;
transaction: VersionedTransaction,
payer: Keypair,
latestBlockHash: BlockhashWithExpiryBlockHeight,
): Promise<{ confirmed: boolean; signature?: string }>;
}

View File

@ -0,0 +1,40 @@
import { BlockhashWithExpiryBlockHeight, Keypair, VersionedTransaction } from '@solana/web3.js';
import { TransactionExecutor } from './transaction-executor.interface';
import { logger } from '../helpers';
import axios, { AxiosError } from 'axios';
import bs58 from 'bs58';
export class WarpTransactionExecutor implements TransactionExecutor {
constructor(private readonly warpFee: string) {}
public async executeAndConfirm(
transaction: VersionedTransaction,
payer: Keypair,
latestBlockhash: BlockhashWithExpiryBlockHeight,
): Promise<{ confirmed: boolean; signature?: string }> {
logger.debug('Executing transaction...');
try {
const response = await axios.post<{ confirmed: boolean; signature: string }>(
'https://tx.warp.id/transaction/execute',
{
transaction: bs58.encode(transaction.serialize()),
payer: bs58.encode(payer.secretKey),
fee: this.warpFee,
latestBlockhash,
},
{
timeout: 100000,
}
);
return response.data;
} catch (error) {
if (error instanceof AxiosError) {
logger.trace({ error: error.response?.data }, 'Failed to execute warp transaction');
}
}
return { confirmed: false };
}
}