All files / src/parsers parser-pumpfun.ts

14.81% Statements 4/27
0% Branches 0/23
0% Functions 0/9
17.39% Lines 4/23

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 711x   1x   1x   1x                                                                                                                                
import { DEX_PROGRAMS, TOKENS } from '../constants';
import { DexInfo, PumpfunEvent, PumpfunTradeEvent, TradeInfo, TradeType, TransferData } from '../types';
import { PumpfunEventParser } from './parser-pumpfun-event';
import { TransactionAdapter } from '../transaction-adapter';
import { TransactionUtils } from '../transaction-utils';
 
export class PumpfunParser {
  private eventParser: PumpfunEventParser;
  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 PumpfunEventParser(this.adapter);
  }
 
  public processTrades(): TradeInfo[] {
    const events = this.eventParser.processEvents().filter((it) => it.type == 'TRADE');
    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): PumpfunEvent[] {
    return this.eventParser.parseInnerInstructions(instructionIndex).filter((it) => it.type == 'TRADE');
  }
 
  private processSwapData(events: PumpfunEvent[]): TradeInfo[] {
    Iif (!events.length) return [];
    return events.map((event) => this.createTradeInfo(event));
  }
 
  private createTradeInfo(data: PumpfunEvent): TradeInfo {
    const event = data.data as PumpfunTradeEvent;
    const tradeType: TradeType = event.isBuy ? 'BUY' : 'SELL';
    const isBuy = tradeType === 'BUY';
 
    const trade: TradeInfo = {
      type: tradeType,
      inputToken: {
        mint: isBuy ? TOKENS.SOL : event.mint,
        amount: isBuy ? event.solAmount : event.tokenAmount,
        decimals: isBuy ? 9 : 6,
      },
      outputToken: {
        mint: isBuy ? event.mint : TOKENS.SOL,
        amount: isBuy ? event.tokenAmount : event.solAmount,
        decimals: isBuy ? 6 : 9,
      },
      user: event.user,
      programId: this.dexInfo.programId || DEX_PROGRAMS.PUMP_FUN.id,
      amm: DEX_PROGRAMS.PUMP_FUN.name,
      route: this.dexInfo.route || '',
      slot: data.slot,
      timestamp: data.timestamp,
      signature: data.signature,
      idx: data.idx,
    };
 
    return this.utils.attachTokenTransferInfo(trade, this.transferActions);
  }
}