All files / src/parsers parser-meteora.ts

7.4% Statements 4/54
0% Branches 0/23
0% Functions 0/16
8% Lines 4/50

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 1051x   1x   1x   1x                                                                                                                                                                                                    
import { DEX_PROGRAMS, DISCRIMINATORS } from '../constants';
import { DexInfo, TradeInfo, TransferData } from '../types';
import { getInstructionData, getProgramName } from '../utils';
import { TransactionAdapter } from '../transaction-adapter';
import { TransactionUtils } from '../transaction-utils';
 
export class MeteoraParser {
  private readonly utils: TransactionUtils;
 
  constructor(
    private readonly adapter: TransactionAdapter,
    private readonly dexInfo: DexInfo,
    private readonly transferActions: Record<string, TransferData[]>
  ) {
    this.utils = new TransactionUtils(adapter);
  }
 
  public processTrades(): TradeInfo[] {
    const trades: TradeInfo[] = [];
    Object.entries(this.transferActions).forEach((it) => {
      trades.push(...this.parseTransferAction(it));
    });
    return trades;
  }
 
  public parseTransferAction(transfer: [string, TransferData[]]): TradeInfo[] {
    const trades: TradeInfo[] = [];
    const [programId, idxs] = transfer[0].split(':');
    const [outerIndex, innerIndex] = idxs.split('-');
 
    Iif (transfer[1].length >= 2 && [DEX_PROGRAMS.METEORA.id, DEX_PROGRAMS.METEORA_POOLS.id].includes(programId)) {
      const instruction = innerIndex
        ? this.adapter.getInnerInstruction(Number(outerIndex), Number(innerIndex))
        : this.adapter.instructions[Number(outerIndex)];
 
      Iif (this.notLiquidityEvent(instruction)) {
        const trade = this.utils.processSwapData(transfer[1], {
          ...this.dexInfo,
          amm: this.dexInfo.amm || getProgramName(programId),
        });
        Iif (trade) {
          trades.push(trade);
        }
      }
    }
    return trades;
  }
 
  public processInstructionTrades(instruction: any, outerIndex: number, innerIndex?: number): TradeInfo[] {
    try {
      const accounts = this.adapter.getInstructionAccounts(instruction);
      const curIdx = innerIndex === undefined ? outerIndex.toString() : `${outerIndex}-${innerIndex}`;
      const transfers = this.processMeteoraSwaps(outerIndex).filter(
        (it) => accounts.includes(it.info.destination) && it.idx >= curIdx
      );
      Iif (transfers.length > 0) {
        const trade = this.utils.processSwapData(transfers, this.dexInfo);
        Iif (trade) return [trade];
      }
      return [];
    } catch (error) {
      console.error('Error processing Meteora trades:', error);
      return [];
    }
  }
 
  public isTradeInstruction(instruction: any): boolean {
    const programId = this.adapter.getInstructionProgramId(instruction);
    return (
      [DEX_PROGRAMS.METEORA.id, DEX_PROGRAMS.METEORA_POOLS.id].includes(programId) &&
      this.notLiquidityEvent(instruction)
    );
  }
 
  private notLiquidityEvent(instruction: any): boolean {
    const data = getInstructionData(instruction);
    Iif (!data) return true;
 
    const instructionType = data.slice(0, 8);
    const isDLMMLiquidity = Object.values(DISCRIMINATORS.METEORA_DLMM)
      .flatMap((it) => Object.values(it))
      .some((it) => instructionType.equals(it));
    const isPoolsLiquidity = Object.values(DISCRIMINATORS.METEORA_POOLS).some((it) => instructionType.equals(it));
 
    return !isDLMMLiquidity && !isPoolsLiquidity;
  }
 
  private processMeteoraSwaps(instructionIndex: number): TransferData[] {
    const innerInstructions = this.adapter.innerInstructions;
    Iif (!innerInstructions) return [];
 
    return innerInstructions
      .filter((set) => set.index === instructionIndex)
      .flatMap((set) =>
        set.instructions
          .map((instruction, index) =>
            this.notLiquidityEvent(instruction)
              ? this.utils.parseInstructionAction(instruction, `${instructionIndex}-${index}`)
              : null
          )
          .filter((transfer): transfer is TransferData => transfer !== null)
      );
  }
}