All files / src/parsers/pumpfun util.ts

66.66% Statements 12/18
11.11% Branches 4/36
33.33% Functions 1/3
60% Lines 9/15

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 1521x 1x                 1x   1x                                                                         1x                                                                                                       1x                                   1x 1x 1x                                                            
import { DEX_PROGRAMS, TOKENS } from '../../constants';
import {
  DexInfo,
  PumpfunTradeEvent,
  PumpswapBuyEvent,
  PumpswapSellEvent,
  TradeInfo,
  TradeType,
  convertToUiAmount,
} from '../../types';
import { getTradeType } from '../../utils';
 
export const getPumpfunTradeInfo = (
  event: PumpfunTradeEvent,
  info: {
    slot: number;
    signature: string;
    timestamp: number;
    idx?: string;
    dexInfo?: DexInfo;
  }
): TradeInfo => {
  const tradeType: TradeType = event.isBuy ? 'BUY' : 'SELL';
  const isBuy = tradeType === 'BUY';
  return {
    type: tradeType,
    inputToken: {
      mint: isBuy ? TOKENS.SOL : event.mint,
      amount: isBuy ? convertToUiAmount(event.solAmount) : convertToUiAmount(event.tokenAmount, 6),
      amountRaw: isBuy ? event.solAmount.toString() : event.tokenAmount.toString(),
      decimals: isBuy ? 9 : 6,
    },
    outputToken: {
      mint: isBuy ? event.mint : TOKENS.SOL,
      amount: isBuy ? convertToUiAmount(event.tokenAmount, 6) : convertToUiAmount(event.solAmount),
      amountRaw: isBuy ? event.tokenAmount.toString() : event.solAmount.toString(),
      decimals: isBuy ? 6 : 9,
    },
    user: event.user,
    programId: DEX_PROGRAMS.PUMP_FUN.id,
    amm: info.dexInfo?.amm || DEX_PROGRAMS.PUMP_FUN.name,
    route: info.dexInfo?.route || '',
    slot: info.slot,
    timestamp: info.timestamp,
    signature: info.signature,
    idx: info.idx || '',
  };
};
 
export const getPumpswapBuyInfo = (
  event: PumpswapBuyEvent,
  inputToken: {
    mint: string;
    decimals: number;
  },
  outputToken: {
    mint: string;
    decimals: number;
  },
  info: {
    slot: number;
    signature: string;
    timestamp: number;
    idx?: string;
    dexInfo?: DexInfo;
  }
): TradeInfo => {
  const { mint: inputMint, decimals: inputDecimal } = inputToken;
  const { mint: outputMint, decimals: ouptDecimal } = outputToken;
 
  return {
    type: getTradeType(inputMint, outputMint),
    inputToken: {
      mint: inputMint,
      amount: convertToUiAmount(event.quoteAmountInWithLpFee, inputDecimal),
      amountRaw: event.quoteAmountInWithLpFee.toString(),
      decimals: inputDecimal,
    },
    outputToken: {
      mint: outputMint,
      amount: convertToUiAmount(event.baseAmountOut, ouptDecimal),
      amountRaw: event.baseAmountOut.toString(),
      decimals: ouptDecimal,
    },
    fee: {
      mint: inputMint,
      amount: convertToUiAmount(event.protocolFee, inputDecimal),
      amountRaw: event.protocolFee.toString(),
      decimals: inputDecimal,
    },
    user: event.user,
    programId: info.dexInfo?.programId || DEX_PROGRAMS.PUMP_SWAP.id,
    amm: DEX_PROGRAMS.PUMP_SWAP.name,
    route: info.dexInfo?.route || '',
    slot: info.slot,
    timestamp: info.timestamp,
    signature: info.signature,
    idx: info.idx || '',
  };
};
 
export const getPumpswapSellInfo = (
  event: PumpswapSellEvent,
  inputToken: {
    mint: string;
    decimals: number;
  },
  outputToken: {
    mint: string;
    decimals: number;
  },
  info: {
    slot: number;
    signature: string;
    timestamp: number;
    idx?: string;
    dexInfo?: DexInfo;
  }
): TradeInfo => {
  const { mint: inputMint, decimals: inputDecimal } = inputToken;
  const { mint: outputMint, decimals: ouptDecimal } = outputToken;
  return {
    type: getTradeType(inputMint, outputMint),
    inputToken: {
      mint: inputMint,
      amount: convertToUiAmount(event.baseAmountIn, inputDecimal),
      amountRaw: event.baseAmountIn.toString(),
      decimals: inputDecimal,
    },
    outputToken: {
      mint: outputMint,
      amount: convertToUiAmount(event.userQuoteAmountOut, ouptDecimal),
      amountRaw: event.userQuoteAmountOut.toString(),
      decimals: ouptDecimal,
    },
    fee: {
      mint: outputMint,
      amount: convertToUiAmount(event.protocolFee, ouptDecimal),
      amountRaw: event.protocolFee.toString(),
      decimals: ouptDecimal,
    },
    user: event.user,
    programId: info.dexInfo?.programId || DEX_PROGRAMS.PUMP_SWAP.id,
    amm: DEX_PROGRAMS.PUMP_SWAP.name,
    route: info.dexInfo?.route || '',
    slot: info.slot,
    timestamp: info.timestamp,
    signature: info.signature,
    idx: info.idx || '',
  };
};