All files / src/types trade.ts

100% Statements 3/3
50% Branches 1/2
100% Functions 1/1
100% Lines 2/2

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                                                                                                                                                                                                                                                                                      1x 6x    
import { ParsedTransactionWithMeta, TransactionResponse, VersionedTransactionResponse } from '@solana/web3.js';
 
/**
 * Union type for different Solana transaction formats
 * Supports both parsed and compiled transaction types
 */
export type SolanaTransaction =
  | ParsedTransactionWithMeta
  | VersionedTransactionResponse
  | (TransactionResponse & VersionedTransactionResponse);
 
/**
 * Configuration options for transaction parsing
 */
export interface ParseConfig {
  /**
   * If true, will try to parse unknown DEXes, results may be inaccurate
   * @default true
   */
  tryUnknowDEX?: boolean;
 
  /**
   * If set, will only parse transactions from these programIds
   * @default undefined
   */
  programIds?: string[];
 
  /**
   * If set, will ignore transactions from these programIds
   * @default undefined
   */
  ignoreProgramIds?: string[];
}
 
/**
 * Basic DEX protocol information
 */
export interface DexInfo {
  programId?: string; // DEX program ID on Solana
  amm?: string; // Automated Market Maker name
  route?: string; // Router or aggregator name
}
 
/**
 * Token information including balances and accounts
 */
export interface TokenInfo {
  mint: string; // Token mint address
  amount: number; // Token uiAmount
  amountRaw: string; // Raw token amount
  decimals: number; // Token decimals
  authority?: string; // Token authority (if applicable)
  destination?: string; // Destination token account
  destinationOwner?: string; // Owner of destination account
  destinationBalance?: TokenAmount; // Balance after transfer
  destinationPreBalance?: TokenAmount; // Balance before transfer
  source?: string; // Source token account
  sourceBalance?: TokenAmount; // Source balance after transfer
  sourcePreBalance?: TokenAmount; // Source balance before transfer
}
 
/**
 * Standard token amount format with both raw and UI amounts
 */
export interface TokenAmount {
  amount: string; // Raw token amount
  uiAmount: number | null; // Human-readable amount
  decimals: number; // Token decimals
}
 
/**
 * Transfer information for tracking token movements
 */
export interface TransferInfo {
  type: 'TRANSFER_IN' | 'TRANSFER_OUT'; // Transfer direction
  token: TokenInfo; // Token details
  from: string; // Source address
  to: string; // Destination address
  timestamp: number; // Unix timestamp
  signature: string; // Transaction signature
}
 
/**
 * Detailed transfer data including account information
 */
export interface TransferData {
  type: 'transfer' | 'transferChecked' | string; // Transfer instruction type
  programId: string; // Token program ID
  info: {
    authority?: string; // Transfer authority
    destination: string; // Destination account
    destinationOwner?: string; // Owner of destination account
    mint: string; // Token mint address
    source: string; // Source account
    tokenAmount: {
      amount: string; // Raw amount
      decimals: number; // Token decimals
      uiAmount: number; // Human-readable amount
    };
    sourceBalance?: TokenAmount; // Source balance after transfer
    sourcePreBalance?: TokenAmount; // Source balance before transfer
    destinationBalance?: TokenAmount; // Balance after transfer
    destinationPreBalance?: TokenAmount; // Balance before transfer
  };
  idx: string; // Instruction index
  timestamp: number; // Unix timestamp
  signature: string; // Transaction signature
  isFee?: boolean; // Whether it's a fee transfer
}
 
/**
 * Trade direction type
 */
export type TradeType = 'BUY' | 'SELL';
 
/**
 * Comprehensive trade information
 */
export interface TradeInfo {
  user: string; // Signer address (trader)
  type: TradeType; // Trade direction (BUY/SELL)
  inputToken: TokenInfo; // Token being sold
  outputToken: TokenInfo; // Token being bought
  fee?: TokenInfo; // Fee details (if any)
  programId?: string; // DEX program ID
  amm?: string; // AMM type (e.g., 'RaydiumV4', 'Meteora')
  route?: string; // Router or Bot (e.g., 'Jupiter','OKX','BananaGun')
  slot: number; // Block slot number
  timestamp: number; // Unix timestamp
  signature: string; // Transaction signature
  idx: string; // Instruction indexes
}
 
/**
 * Converts raw token amount to human-readable format
 * @param amount Raw amount in bigint or string format
 * @param decimals Token decimals (defaults to 9)
 * @returns Human-readable amount as number
 */
export const convertToUiAmount = (amount: bigint | string, decimals?: number) => {
  return Number(amount) / Math.pow(10, decimals || 9);
};