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

30.3% Statements 10/33
14.28% Branches 1/7
33.33% Functions 3/9
31.25% Lines 10/32

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 1171x   1x                   1x 1x   1x               1x 1x       1x   1x   1x                                                                                                                                                                        
import { DEX_PROGRAMS } from '../../constants';
import { TransactionAdapter } from '../../transaction-adapter';
import {
  ClassifiedInstruction,
  convertToUiAmount,
  PoolEvent,
  PumpswapCreatePoolEvent,
  PumpswapDepositEvent,
  PumpswapEvent,
  PumpswapWithdrawEvent,
  TransferData,
} from '../../types';
import { BaseLiquidityParser } from '../base-liquidity-parser';
import { PumpswapEventParser } from './parser-pumpswap-event';
 
export class PumpswapLiquidityParser extends BaseLiquidityParser {
  private eventParser: PumpswapEventParser;
 
  constructor(
    adapter: TransactionAdapter,
    transferActions: Record<string, TransferData[]>,
    classifiedInstructions: ClassifiedInstruction[]
  ) {
    super(adapter, transferActions, classifiedInstructions);
    this.eventParser = new PumpswapEventParser(adapter);
  }
 
  public processLiquidity(): PoolEvent[] {
    const events = this.eventParser
      .parseInstructions(this.classifiedInstructions)
      .filter((event) => ['CREATE', 'ADD', 'REMOVE'].includes(event.type));
 
    return events.length > 0 ? this.parseLiquidityEvents(events) : [];
  }
 
  private parseLiquidityEvents(events: PumpswapEvent[]): PoolEvent[] {
    Iif (!events.length) return [];
    return events
      .map((event) => {
        switch (event.type) {
          case 'CREATE':
            return this.parseCreateEvent(event);
          case 'ADD':
            return this.parseDepositEvent(event);
          case 'REMOVE':
            return this.parseWithdrawEvent(event);
          default:
            return null;
        }
      })
      .filter((it) => it != null);
  }
 
  private parseCreateEvent(data: PumpswapEvent): PoolEvent {
    const event = data.data as PumpswapCreatePoolEvent;
 
    return {
      ...this.adapter.getPoolEventBase('CREATE', DEX_PROGRAMS.PUMP_SWAP.id),
      idx: data.idx,
      poolId: event.pool,
      poolLpMint: event.lpMint,
      token0Mint: event.baseMint,
      token1Mint: event.quoteMint,
      token0Amount: convertToUiAmount(event.baseAmountIn, event.baseMintDecimals),
      token0AmountRaw: event.baseAmountIn.toString(),
      token1Amount: convertToUiAmount(event.quoteAmountIn, event.quoteMintDecimals),
      token1AmountRaw: event.quoteAmountIn.toString(),
      token0Decimals: event.baseMintDecimals,
      token1Decimals: event.quoteMintDecimals,
    };
  }
 
  private parseDepositEvent(data: PumpswapEvent): PoolEvent {
    const event = data.data as PumpswapDepositEvent;
    const token0Mint = this.adapter.splTokenMap.get(event.userBaseTokenAccount)!.mint;
    const token0Decimals = this.adapter.getTokenDecimals(token0Mint);
    const token1Mint = this.adapter.splTokenMap.get(event.userQuoteTokenAccount)!.mint;
    const token1Decimals = this.adapter.getTokenDecimals(token1Mint);
    return {
      ...this.adapter.getPoolEventBase('ADD', DEX_PROGRAMS.PUMP_SWAP.id),
      idx: data.idx,
      poolId: event.pool,
      poolLpMint: this.adapter.splTokenMap.get(event.userPoolTokenAccount)!.mint,
      token0Mint: token0Mint,
      token1Mint: token1Mint,
      token0Amount: convertToUiAmount(event.baseAmountIn, token0Decimals),
      token0AmountRaw: event.baseAmountIn.toString(),
      token1Amount: convertToUiAmount(event.quoteAmountIn, token1Decimals),
      token1AmountRaw: event.quoteAmountIn.toString(),
      token0Decimals: token0Decimals,
      token1Decimals: token1Decimals,
    };
  }
 
  private parseWithdrawEvent(data: PumpswapEvent): PoolEvent {
    const event = data.data as PumpswapWithdrawEvent;
    const token0Mint = this.adapter.splTokenMap.get(event.userBaseTokenAccount)!.mint;
    const token0Decimals = this.adapter.getTokenDecimals(token0Mint);
    const token1Mint = this.adapter.splTokenMap.get(event.userQuoteTokenAccount)!.mint;
    const token1Decimals = this.adapter.getTokenDecimals(token1Mint);
    return {
      ...this.adapter.getPoolEventBase('REMOVE', DEX_PROGRAMS.PUMP_SWAP.id),
      idx: data.idx,
      poolId: event.pool,
      poolLpMint: this.adapter.splTokenMap.get(event.userPoolTokenAccount)!.mint,
      token0Mint: token0Mint,
      token1Mint: token1Mint,
      token0Amount: convertToUiAmount(event.baseAmountOut, token0Decimals),
      token0AmountRaw: event.baseAmountOut.toString(),
      token1Amount: convertToUiAmount(event.quoteAmountOut, token1Decimals),
      token1AmountRaw: event.quoteAmountOut.toString(),
      token0Decimals: token0Decimals,
      token1Decimals: token1Decimals,
    };
  }
}