All files / src/parsers/meteora parser-meteora-liquidity-base.ts

12% Statements 3/25
0% Branches 0/12
0% Functions 0/3
13.04% Lines 3/23

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  1x 1x   1x                                                                                                                                    
import { PoolEvent, PoolEventType, TransferData } from '../../types';
import { BaseLiquidityParser } from '../base-liquidity-parser';
import { getInstructionData } from '../../utils';
 
export abstract class MeteoraLiquidityParserBase extends BaseLiquidityParser {
  abstract getPoolAction(data: Buffer): PoolEventType | { name: string; type: PoolEventType } | null;
 
  public processLiquidity(): PoolEvent[] {
    const events: PoolEvent[] = [];
 
    this.classifiedInstructions.forEach(({ instruction, programId, outerIndex, innerIndex }) => {
      const event = this.parseInstruction(instruction, programId, outerIndex, innerIndex);
      Iif (event) {
        events.push(event);
      }
    });
 
    return events;
  }
 
  protected parseInstruction(
    instruction: any,
    programId: string,
    outerIndex: number,
    innerIndex?: number
  ): PoolEvent | null {
    try {
      const data = getInstructionData(instruction);
      const action = this.getPoolAction(data);
      Iif (!action) return null;
 
      let transfers = this.getTransfersForInstruction(programId, outerIndex, innerIndex);
      Iif (transfers.length === 0) transfers = this.getTransfersForInstruction(programId, outerIndex, innerIndex ?? 0);
      const type = typeof action === 'string' ? action : action.type;
      switch (type) {
        case 'CREATE':
          return this.parseCreateLiquidityEvent?.(instruction, outerIndex, data, transfers) ?? null;
        case 'ADD':
          return this.parseAddLiquidityEvent(instruction, outerIndex, data, transfers);
        case 'REMOVE':
          return this.parseRemoveLiquidityEvent(instruction, outerIndex, data, transfers);
      }
      return null;
    } catch (error) {
      console.error('parseInstruction error:', error);
      return null;
    }
  }
 
  protected abstract parseAddLiquidityEvent(
    instruction: any,
    index: number,
    data: Buffer,
    transfers: TransferData[]
  ): PoolEvent;
 
  protected abstract parseRemoveLiquidityEvent(
    instruction: any,
    index: number,
    data: Buffer,
    transfers: TransferData[]
  ): PoolEvent;
 
  protected parseCreateLiquidityEvent?(
    instruction: any,
    index: number,
    data: Buffer,
    transfers: TransferData[]
  ): PoolEvent;
}