warp-transaction-executor.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {
  2. BlockhashWithExpiryBlockHeight,
  3. Keypair,
  4. PublicKey,
  5. SystemProgram,
  6. TransactionMessage,
  7. VersionedTransaction,
  8. } from '@solana/web3.js';
  9. import { TransactionExecutor } from './transaction-executor.interface';
  10. import { logger } from '../helpers';
  11. import axios, { AxiosError } from 'axios';
  12. import bs58 from 'bs58';
  13. import { Currency, CurrencyAmount } from '@raydium-io/raydium-sdk';
  14. export class WarpTransactionExecutor implements TransactionExecutor {
  15. private readonly warpFeeWallet = new PublicKey('WARPzUMPnycu9eeCZ95rcAUxorqpBqHndfV3ZP5FSyS');
  16. constructor(private readonly warpFee: string) {}
  17. public async executeAndConfirm(
  18. transaction: VersionedTransaction,
  19. payer: Keypair,
  20. latestBlockhash: BlockhashWithExpiryBlockHeight,
  21. ): Promise<{ confirmed: boolean; signature?: string }> {
  22. logger.debug('Executing transaction...');
  23. try {
  24. const fee = new CurrencyAmount(Currency.SOL, this.warpFee, false).raw.toNumber();
  25. const warpFeeMessage = new TransactionMessage({
  26. payerKey: payer.publicKey,
  27. recentBlockhash: latestBlockhash.blockhash,
  28. instructions: [
  29. SystemProgram.transfer({
  30. fromPubkey: payer.publicKey,
  31. toPubkey: this.warpFeeWallet,
  32. lamports: fee,
  33. }),
  34. ],
  35. }).compileToV0Message();
  36. const warpFeeTx = new VersionedTransaction(warpFeeMessage);
  37. warpFeeTx.sign([payer]);
  38. const response = await axios.post<{ confirmed: boolean; signature: string }>(
  39. 'https://tx.warp.id/transaction/execute',
  40. {
  41. transactions: [bs58.encode(warpFeeTx.serialize()), bs58.encode(transaction.serialize())],
  42. latestBlockhash,
  43. },
  44. {
  45. timeout: 100000,
  46. },
  47. );
  48. return response.data;
  49. } catch (error) {
  50. if (error instanceof AxiosError) {
  51. logger.trace({ error: error.response?.data }, 'Failed to execute warp transaction');
  52. }
  53. }
  54. return { confirmed: false };
  55. }
  56. }