All files / src/parsers/pumpfun parser-pumpswap.ts

70.83% Statements 17/24
50% Branches 1/2
83.33% Functions 5/6
69.56% Lines 16/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 71 72 73 74 75 76 77 78 79 80 81 82                    1x 1x 1x   1x                 1x 1x       1x   1x   1x                                                     1x 1x 1x 1x 1x   1x                         1x      
import { TransactionAdapter } from '../../transaction-adapter';
import {
  ClassifiedInstruction,
  DexInfo,
  PumpswapBuyEvent,
  PumpswapEvent,
  PumpswapSellEvent,
  TradeInfo,
  TransferData,
} from '../../types';
import { BaseParser } from '../base-parser';
import { PumpswapEventParser } from './parser-pumpswap-event';
import { getPumpswapBuyInfo, getPumpswapSellInfo } from './util';
 
export class PumpswapParser extends BaseParser {
  private eventParser: PumpswapEventParser;
 
  constructor(
    adapter: TransactionAdapter,
    dexInfo: DexInfo,
    transferActions: Record<string, TransferData[]>,
    classifiedInstructions: ClassifiedInstruction[]
  ) {
    super(adapter, dexInfo, transferActions, classifiedInstructions);
    this.eventParser = new PumpswapEventParser(adapter);
  }
 
  public processTrades(): TradeInfo[] {
    const events = this.eventParser
      .parseInstructions(this.classifiedInstructions)
      .filter((event) => ['BUY', 'SELL'].includes(event.type));
 
    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 = getPumpswapBuyInfo(
      event,
      { mint: inputMint, decimals: inputDecimal },
      { mint: outputMint, decimals: ouptDecimal },
      {
        slot: data.slot,
        signature: data.signature,
        timestamp: data.timestamp,
        idx: data.idx,
        dexInfo: this.dexInfo,
      }
    );
 
    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 = getPumpswapSellInfo(
      event,
      { mint: inputMint, decimals: inputDecimal },
      { mint: outputMint, decimals: ouptDecimal },
      {
        slot: data.slot,
        signature: data.signature,
        timestamp: data.timestamp,
        idx: data.idx,
        dexInfo: this.dexInfo,
      }
    );
 
    return this.utils.attachTokenTransferInfo(trade, this.transferActions);
  }
}