All files / src/parsers parser-orca-liquidity.ts

7.57% Statements 5/66
0% Branches 0/30
0% Functions 0/21
7.81% Lines 5/64

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 1611x   1x 1x 1x   1x                                                                                                                                                                                                                                                                                                                    
import { DEX_PROGRAMS, DISCRIMINATORS } from '../constants';
import { TransactionAdapter } from '../transaction-adapter';
import { TransactionUtils } from '../transaction-utils';
import { convertToUiAmount, PoolEvent, PoolEventType, TransferData } from '../types';
import { getInstructionData } from '../utils';
 
export class OrcaLiquidityParser {
  private readonly utils: TransactionUtils;
  constructor(private readonly adapter: TransactionAdapter) {
    this.utils = new TransactionUtils(adapter);
  }
 
  public processLiquidity(): PoolEvent[] {
    const events = this.adapter.instructions
      .map((instr: any, idx: number) => this.processInstruction(instr, idx))
      .filter((event: any): event is PoolEvent => event !== null);
 
    return events.length > 0 ? events : this.processInnerInstructions();
  }
 
  private processInnerInstructions(): PoolEvent[] {
    try {
      return this.adapter.instructions.flatMap((_: any, idx: number) => this.processInnerInstruction(idx));
    } catch (error) {
      console.error('Error processing Meteora inner instructions:', error);
      return [];
    }
  }
 
  private processInnerInstruction(outerIndex: number): PoolEvent[] {
    return (this.adapter.innerInstructions || [])
      .filter((set) => set.index === outerIndex)
      .flatMap((set) =>
        set.instructions
          .map((instr, innerIdx) => this.processInstruction(instr, outerIndex, innerIdx))
          .filter((event): event is PoolEvent => event !== null)
      );
  }
 
  private processInstruction(instruction: any, index: number, innerIndex?: number): PoolEvent | null {
    const programId = this.adapter.getInstructionProgramId(instruction);
    const parser = this.getParser(programId);
    return parser ? parser.parseInstruction(instruction, index, innerIndex) : null;
  }
 
  private getParser(programId: string): OrcaPoolParser | null {
    switch (programId) {
      case DEX_PROGRAMS.ORCA.id:
        return new OrcaPoolParser(this.adapter, this.utils);
      default:
        return null;
    }
  }
}
 
class OrcaPoolParser {
  constructor(
    private readonly adapter: TransactionAdapter,
    private readonly utils: TransactionUtils
  ) { }
 
  public getPoolAction(data: any): PoolEventType | null {
    const instructionType = data.slice(0, 8);
    if (
      instructionType.equals(DISCRIMINATORS.ORCA.ADD_LIQUIDITY) ||
      instructionType.equals(DISCRIMINATORS.ORCA.ADD_LIQUIDITY2)
    ) {
      return 'ADD';
    } else Iif (instructionType.equals(DISCRIMINATORS.ORCA.REMOVE_LIQUIDITY)) {
      return 'REMOVE';
    }
    return null;
  }
 
  public parseInstruction(instruction: any, index: number, innerIndex?: number): PoolEvent | null {
    try {
      const data = getInstructionData(instruction);
      const action = this.getPoolAction(data);
      Iif (!action) return null;
 
      const transfers = this.parseTransfers(instruction, index, innerIndex);
      switch (action) {
        case 'ADD':
          return this.parseAddLiquidityEvent(instruction, index, data, transfers);
        case 'REMOVE':
          return this.parseRemoveLiquidityEvent(instruction, index, data, transfers);
      }
      return null;
    } catch (error) {
      console.error('parseInstruction error:', error);
      return null;
    }
  }
 
  protected getInstructionId(index: number, innerIndex?: number): string {
    return innerIndex === undefined ? index.toString() : `${index}-${innerIndex}`;
  }
 
  protected parseTransfers(instruction: any, index: number, innerIndex?: number): TransferData[] {
    const curIdx = this.getInstructionId(index, innerIndex);
    const accounts = this.adapter.getInstructionAccounts(instruction);
    return this.utils
      .processTransferInstructions(index)
      .filter((transfer) => accounts.includes(transfer.info.destination) && transfer.idx >= curIdx);
  }
 
  private parseAddLiquidityEvent(instruction: any, index: number, data: any, transfers: TransferData[]): PoolEvent {
    const [token0, token1] = this.utils.getLPTransfers(transfers);
    const token0Mint = token0?.info.mint;
    const token1Mint = token1?.info.mint;
    const programId = this.adapter.getInstructionProgramId(instruction);
    const accounts = this.adapter.getInstructionAccounts(instruction);
    const [token0Decimals, token1Decimals] = [this.adapter.getTokenDecimals(token0Mint), this.adapter.getTokenDecimals(token1Mint)];
 
    return {
      ...this.adapter.getPoolEventBase('ADD', programId),
      idx: index.toString(),
      poolId: accounts[0],
      poolLpMint: accounts[0],
      token0Mint: token0Mint,
      token1Mint: token1Mint,
      token0Amount:
        token0?.info.tokenAmount.uiAmount ||
        convertToUiAmount(data.readBigUInt64LE(32), token0Decimals),
      token1Amount:
        token1?.info.tokenAmount.uiAmount ||
        convertToUiAmount(data.readBigUInt64LE(24), token1Decimals),
      token0Decimals: token0Decimals,
      token1Decimals: token1Decimals,
      lpAmount: convertToUiAmount(data.readBigUInt64LE(8), this.adapter.getTokenDecimals(accounts[1])) || 0,
    };
  }
 
  private parseRemoveLiquidityEvent(instruction: any, index: number, data: any, transfers: TransferData[]): PoolEvent {
    const [token0, token1] = this.utils.getLPTransfers(transfers);
    const token0Mint = token0?.info.mint;
    const token1Mint = token1?.info.mint;
    const programId = this.adapter.getInstructionProgramId(instruction);
    const accounts = this.adapter.getInstructionAccounts(instruction);
    const [token0Decimals, token1Decimals] = [this.adapter.getTokenDecimals(token0Mint), this.adapter.getTokenDecimals(token1Mint)];
 
    return {
      ...this.adapter.getPoolEventBase('REMOVE', programId),
      idx: index.toString(),
      poolId: accounts[0],
      poolLpMint: accounts[0],
      token0Mint: token0Mint,
      token1Mint: token1Mint,
      token0Amount:
        token0?.info.tokenAmount.uiAmount ||
        convertToUiAmount(data.readBigUInt64LE(32), token0Decimals),
      token1Amount:
        token1?.info.tokenAmount.uiAmount ||
        convertToUiAmount(data.readBigUInt64LE(24), token1Decimals),
      token0Decimals: token0Decimals,
      token1Decimals: token1Decimals,
      lpAmount: convertToUiAmount(data.readBigUInt64LE(8), this.adapter.getTokenDecimals(accounts[1])),
    };
  }
}