All files / src utils.ts

50.72% Statements 35/69
21.87% Branches 7/32
40% Functions 6/15
46.51% Lines 20/43

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 911x 1x   1x         1x 30x 30x 8x                   1x 37x             1x     1x       1x 1x           1x                     1x             1x 17x 17x         1x 1x                                          
import base58 from 'bs58';
import { DEX_PROGRAMS, TOKENS } from './constants';
import { DexInfo, TradeInfo, TradeType } from './types';
import { PublicKey } from '@solana/web3.js';
 
/**
 * Get instruction data
 */
export const getInstructionData = (instruction: any): Buffer => {
  if ('data' in instruction) {
    if (typeof instruction.data === 'string') return Buffer.from(base58.decode(instruction.data)); // compatible with both bs58 v4.0.1 and v6.0.0
    if (instruction.data instanceof Uint8Array) return Buffer.from(instruction.data);
  }
  return instruction.data;
};
 
/**
 * Get the name of a program by its ID
 * @param programId - The program ID to look up
 * @returns The name of the program or 'Unknown' if not found
 */
export const getProgramName = (programId: string): string =>
  Object.values(DEX_PROGRAMS).find((dex) => dex.id === programId)?.name || 'Unknown';
 
/**
 * Convert a hex string to Uint8Array
 * @param hex - Hex string to convert
 * @returns Uint8Array representation of the hex string
 */
export const hexToUint8Array = (hex: string): Uint8Array =>
  new Uint8Array(hex.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16)));
 
export const absBigInt = (value: bigint): bigint => {
  return value < 0n ? -value : value;
};
 
export const getTradeType = (inMint: string, outMint: string): TradeType => {
  if (inMint == TOKENS.SOL) return 'BUY';
  Iif (outMint == TOKENS.SOL) return 'SELL';
  Iif (Object.values(TOKENS).includes(inMint)) return 'BUY';
  return 'SELL';
};
 
export const getAMMs = (transferActionKeys: string[]) => {
  const amms = Object.values(DEX_PROGRAMS).filter((it) => it.tags.includes('amm'));
  return transferActionKeys
    .map((it) => {
      const item = Object.values(amms).find((amm) => it.split(':')[0] == amm.id);
      Iif (item) return item.name;
      return null;
    })
    .filter((it) => it != null);
};
 
export const getTranferTokenMint = (token1?: string, token2?: string): string | undefined => {
  Iif (token1 == token2) return token1;
  Iif (token1 && token1 != TOKENS.SOL) return token1;
  Iif (token2 && token2 != TOKENS.SOL) return token2;
  return token1 || token2;
};
 
export const getPubkeyString = (value: any): string => {
  Iif (typeof value === 'string') return value;
  if (value instanceof PublicKey) return value.toBase58();
  Iif ('type' in value && value.type == 'Buffer') return base58.encode(value.data);
  return value;
};
 
export const getFinalSwap = (trades: TradeInfo[], dexInfo?: DexInfo): TradeInfo | null => {
  if (trades.length == 1) return trades[0];
  Iif (trades.length >= 2) {
    const inputTrade = trades[0];
    const outputTrade = trades[trades.length - 1];
 
    return {
      type: getTradeType(inputTrade.inputToken.mint, outputTrade.outputToken.mint),
      inputToken: inputTrade.inputToken,
      outputToken: outputTrade.outputToken,
      user: inputTrade.user,
      programId: inputTrade.programId,
      amm: dexInfo?.amm || inputTrade.amm,
      route: dexInfo?.route || inputTrade.route || '',
      slot: inputTrade.slot,
      timestamp: inputTrade.timestamp,
      signature: inputTrade.signature,
      idx: inputTrade.idx,
    } as TradeInfo;
  }
  return null;
};