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

12.5% Statements 4/32
0% Branches 0/7
0% Functions 0/9
13.33% Lines 4/30

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 1001x 1x               1x     1x                                                                                                                                                                              
import { DEX_PROGRAMS } from '../constants';
import {
  convertToUiAmount,
  PoolEvent,
  PumpswapCreatePoolEvent,
  PumpswapDepositEvent,
  PumpswapEvent,
  PumpswapWithdrawEvent,
} from '../types';
import { PumpswapEventParser } from './parser-pumpswap-event';
import { TransactionAdapter } from '../transaction-adapter';
 
export class PumpswapLiquidityParser {
  private eventParser: PumpswapEventParser;
 
  constructor(private readonly adapter: TransactionAdapter) {
    this.eventParser = new PumpswapEventParser(this.adapter);
  }
 
  public processLiquidity(): PoolEvent[] {
    const events = this.eventParser.processEvents().filter((it) => ['CREATE', 'ADD', 'REMOVE'].includes(it.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),
      token1Amount: convertToUiAmount(event.quoteAmountIn, event.quoteMintDecimals),
      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),
      token1Amount: convertToUiAmount(event.quoteAmountIn,token1Decimals),
      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),
      token1Amount: convertToUiAmount(event.quoteAmountOut,token1Decimals),
      token0Decimals: token0Decimals,
      token1Decimals: token1Decimals,
    };
  }
}