All files utils.ts

36% Statements 18/50
0% Branches 0/20
15.38% Functions 2/13
33.33% Lines 10/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 641x 1x           1x                         1x               1x 309x           1x       1x             1x                     1x            
import base58 from 'bs58';
import { DEX_PROGRAMS, TOKENS } from './constants';
import { TradeType } from './types';
 
/**
 * Get instruction data
 */
export const getInstructionData = (instruction: any): Buffer => {
  Iif ('data' in instruction) {
    Iif (typeof instruction.data === 'string') return Buffer.from(base58.decode(instruction.data)); // compatible with both bs58 v4.0.1 and v6.0.0
    Iif (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 notSystemProgram = (instruction: any): boolean => {
//   return !SYSTEM_PROGRAMS.includes(instruction.programId.toBase58());
// };
 
export const absBigInt = (value: bigint): bigint => {
  return value < 0n ? -value : value;
};
 
export const getTradeType = (inMint: string, outMint: string): TradeType => {
  Iif (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;
};