All files / src/parsers parser-pumpswap.ts

13.51% Statements 5/37
0% Branches 0/15
0% Functions 0/10
15.15% Lines 5/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1211x 1x                 1x   1x   1x                                                                                                                                                                                                                    
import { DEX_PROGRAMS } from '../constants';
import {
  convertToUiAmount,
  DexInfo,
  PumpswapBuyEvent,
  PumpswapEvent,
  PumpswapSellEvent,
  TradeInfo,
  TransferData,
} from '../types';
import { PumpswapEventParser } from './parser-pumpswap-event';
import { TransactionAdapter } from '../transaction-adapter';
import { TransactionUtils } from '../transaction-utils';
 
export class PumpswapParser {
  private eventParser: PumpswapEventParser;
  private readonly utils: TransactionUtils;
 
  constructor(
    private readonly adapter: TransactionAdapter,
    private readonly dexInfo: DexInfo,
    private readonly transferActions: Record<string, TransferData[]>
  ) {
    this.utils = new TransactionUtils(adapter);
    this.eventParser = new PumpswapEventParser(this.adapter);
  }
 
  public processTrades(): TradeInfo[] {
    const events = this.eventParser.processEvents().filter((it) => ['BUY', 'SELL'].includes(it.type));
    return events.length > 0 ? this.processSwapData(events) : [];
  }
 
  public parseTransferAction(transfer: [string, TransferData[]]): TradeInfo[] {
    const [, idxs] = transfer[0].split(':');
    const [outerIndex] = idxs.split('-');
    const events = this.parseInnerInstructions(Number(outerIndex));
    return events.length > 0 ? this.processSwapData(events) : [];
  }
 
  private parseInnerInstructions(instructionIndex: number): PumpswapEvent[] {
    return this.eventParser.processInnerInstruction(instructionIndex).filter((it) => ['BUY', 'SELL'].includes(it.type));
  }
 
  private processSwapData(events: PumpswapEvent[]): TradeInfo[] {
    Iif (!events.length) return [];
    return events.map((event) => (event.type == 'BUY' ? this.createBuyInfo(event) : this.createSellInfo(event)));
  }
 
  private createBuyInfo(data: PumpswapEvent): TradeInfo {
    const event = data.data as PumpswapBuyEvent;
    const inputMint = this.adapter.splTokenMap.get(event.userQuoteTokenAccount)!.mint;
    const inputDecimal = this.adapter.getTokenDecimals(inputMint);
    const outputMint = this.adapter.splTokenMap.get(event.userBaseTokenAccount)!.mint;
    const ouptDecimal = this.adapter.getTokenDecimals(outputMint);
    const trade: TradeInfo = {
      type: 'BUY',
      inputToken: {
        mint: inputMint,
        amount: convertToUiAmount(event.quoteAmountInWithLpFee, inputDecimal),
        decimals: inputDecimal,
      },
      outputToken: {
        mint: outputMint,
        amount: convertToUiAmount(event.baseAmountOut, ouptDecimal),
        decimals: ouptDecimal,
      },
      fee: {
        mint: inputMint,
        amount: convertToUiAmount(event.protocolFee, inputDecimal),
        decimals: inputDecimal,
      },
      user: event.user,
      programId: this.dexInfo.programId || DEX_PROGRAMS.PUMP_SWAP.id,
      amm: DEX_PROGRAMS.PUMP_SWAP.name,
      route: this.dexInfo.route || '',
      slot: data.slot,
      timestamp: data.timestamp,
      signature: data.signature,
      idx: data.idx,
    };
 
    return this.utils.attachTokenTransferInfo(trade, this.transferActions);
  }
 
  private createSellInfo(data: PumpswapEvent): TradeInfo {
    const event = data.data as PumpswapSellEvent;
    const inputMint = this.adapter.splTokenMap.get(event.userBaseTokenAccount)!.mint;
    const inputDecimal = this.adapter.getTokenDecimals(inputMint);
    const outputMint = this.adapter.splTokenMap.get(event.userQuoteTokenAccount)!.mint;
    const ouptDecimal = this.adapter.getTokenDecimals(outputMint);
    const trade: TradeInfo = {
      type: 'SELL',
      inputToken: {
        mint: inputMint,
        amount: convertToUiAmount(event.baseAmountIn, inputDecimal),
        decimals: inputDecimal,
      },
      outputToken: {
        mint: outputMint,
        amount: convertToUiAmount(event.userQuoteAmountOut, ouptDecimal),
        decimals: ouptDecimal,
      },
      fee: {
        mint: outputMint,
        amount: convertToUiAmount(event.protocolFee, ouptDecimal),
        decimals: ouptDecimal,
      },
      user: event.user,
      programId: this.dexInfo.programId || DEX_PROGRAMS.PUMP_SWAP.id,
      amm: DEX_PROGRAMS.PUMP_SWAP.name,
      route: this.dexInfo.route || '',
      slot: data.slot,
      timestamp: data.timestamp,
      signature: data.signature,
      idx: data.idx,
    };
 
    return this.utils.attachTokenTransferInfo(trade, this.transferActions);
  }
}