warp-transaction-executor.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { BlockhashWithExpiryBlockHeight, Keypair, VersionedTransaction } from '@solana/web3.js';
  2. import { TransactionExecutor } from './transaction-executor.interface';
  3. import { logger } from '../helpers';
  4. import axios, { AxiosError } from 'axios';
  5. import bs58 from 'bs58';
  6. export class WarpTransactionExecutor implements TransactionExecutor {
  7. constructor(private readonly warpFee: string) {}
  8. public async executeAndConfirm(
  9. transaction: VersionedTransaction,
  10. payer: Keypair,
  11. latestBlockhash: BlockhashWithExpiryBlockHeight,
  12. ): Promise<{ confirmed: boolean; signature?: string }> {
  13. logger.debug('Executing transaction...');
  14. try {
  15. const response = await axios.post<{ confirmed: boolean; signature: string }>(
  16. 'https://tx.warp.id/transaction/execute',
  17. {
  18. transaction: bs58.encode(transaction.serialize()),
  19. payer: bs58.encode(payer.secretKey),
  20. fee: this.warpFee,
  21. latestBlockhash,
  22. },
  23. {
  24. timeout: 100000,
  25. }
  26. );
  27. return response.data;
  28. } catch (error) {
  29. if (error instanceof AxiosError) {
  30. logger.trace({ error: error.response?.data }, 'Failed to execute warp transaction');
  31. }
  32. }
  33. return { confirmed: false };
  34. }
  35. }