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

49.15% Statements 29/59
16.66% Branches 3/18
72.22% Functions 13/18
56.25% Lines 27/48

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 1521x   1x   1x   1x   1x 1x       1x 4x 4x   1x       1x 4x               4x 4x   1x 12x 12x         16x 16x           16x 16x   1x           1x 1x 1x 1x 1x                                                                                                                                                                                      
import { DEX_PROGRAMS, DISCRIMINATORS } from '../constants';
import { TransactionAdapter } from '../transaction-adapter';
import { TransactionUtils } from '../transaction-utils';
import { PoolEvent, PoolEventType } from '../types';
import { ParseEventConfig, RaydiumLiquidityParserBase } from './parser-raydium-liquidity-base';
 
export class RaydiumLiquidityParser {
    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 Raydium 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, outerIndex: number, innerIndex?: number): PoolEvent | null {
        const programId = this.adapter.getInstructionProgramId(instruction);
        const parsers = {
            [DEX_PROGRAMS.RAYDIUM_V4.id]: RaydiumV4PoolParser,
            [DEX_PROGRAMS.RAYDIUM_CL.id]: RaydiumCLPoolParser,
            [DEX_PROGRAMS.RAYDIUM_CPMM.id]: RaydiumCPMMPoolParser,
        };
 
        const ParserClass = parsers[programId];
        if (!ParserClass) return null;
 
        return new ParserClass(this.adapter, this.utils).parseRaydiumInstruction(instruction, outerIndex, innerIndex);
    }
}
 
class RaydiumV4PoolParser extends RaydiumLiquidityParserBase {
    public getPoolAction(data: Buffer): PoolEventType | null {
        const instructionType = data.slice(0, 1);
        Iif (instructionType.equals(DISCRIMINATORS.RAYDIUM.CREATE)) return 'CREATE';
        Iif (instructionType.equals(DISCRIMINATORS.RAYDIUM.ADD_LIQUIDITY)) return 'ADD';
        Iif (instructionType.equals(DISCRIMINATORS.RAYDIUM.REMOVE_LIQUIDITY)) return 'REMOVE';
        return null;
    }
 
    protected getEventConfig(type: PoolEventType): ParseEventConfig {
        const configs = {
            CREATE: { eventType: 'CREATE' as const, poolIdIndex: 4, lpMintIndex: 7 },
            ADD: { eventType: 'ADD' as const, poolIdIndex: 1, lpMintIndex: 5 },
            REMOVE: { eventType: 'REMOVE' as const, poolIdIndex: 1, lpMintIndex: 5 }
        };
        return configs[type];
    }
}
 
class RaydiumCLPoolParser extends RaydiumLiquidityParserBase {
    public getPoolAction(data: Buffer): { name: string; type: PoolEventType } | null {
        const instructionType = data.slice(0, 8);
 
        for (const [name, discriminator] of Object.entries(DISCRIMINATORS.RAYDIUM_CL.CREATE)) {
            Iif (instructionType.equals(discriminator)) return { name, type: 'CREATE' };
        }
 
        for (const [name, discriminator] of Object.entries(DISCRIMINATORS.RAYDIUM_CL.ADD_LIQUIDITY)) {
            Iif (instructionType.equals(discriminator)) return { name, type: 'ADD' };
        }
 
        for (const [name, discriminator] of Object.entries(DISCRIMINATORS.RAYDIUM_CL.REMOVE_LIQUIDITY)) {
            Iif (instructionType.equals(discriminator)) return { name, type: 'REMOVE' };
        }
 
        return null;
    }
 
    protected getEventConfig(
        type: PoolEventType,
        instructionType: { name: string; type: PoolEventType }
    ): ParseEventConfig {
        const configs = {
            CREATE: {
                eventType: 'CREATE' as const,
                poolIdIndex: ['openPosition', 'openPositionV2'].includes(instructionType.name) ? 5 : 4,
                lpMintIndex: ['openPosition', 'openPositionV2'].includes(instructionType.name) ? 5 : 4
            },
            ADD: {
                eventType: 'ADD' as const,
                poolIdIndex: 2,
                lpMintIndex: 2,
                tokenAmountOffsets: { token0: 32, token1: 24, lp: 8 }
            },
            REMOVE: {
                eventType: 'REMOVE' as const,
                poolIdIndex: 3,
                lpMintIndex: 3,
                tokenAmountOffsets: { token0: 32, token1: 24, lp: 8 }
            }
        };
        return configs[type];
    }
}
 
class RaydiumCPMMPoolParser extends RaydiumLiquidityParserBase {
    public getPoolAction(data: Buffer): PoolEventType | null {
        const instructionType = data.slice(0, 8);
        Iif (instructionType.equals(DISCRIMINATORS.RAYDIUM_CPMM.CREATE)) return 'CREATE';
        Iif (instructionType.equals(DISCRIMINATORS.RAYDIUM_CPMM.ADD_LIQUIDITY)) return 'ADD';
        Iif (instructionType.equals(DISCRIMINATORS.RAYDIUM_CPMM.REMOVE_LIQUIDITY)) return 'REMOVE';
        return null;
    }
 
    protected getEventConfig(type: PoolEventType): ParseEventConfig {
        const configs = {
            CREATE: {
                eventType: 'CREATE' as const,
                poolIdIndex: 3,
                lpMintIndex: 6,
                tokenAmountOffsets: { token0: 8, token1: 16, lp: 0 }
            },
            ADD: {
                eventType: 'ADD' as const,
                poolIdIndex: 2,
                lpMintIndex: 12,
                tokenAmountOffsets: { token0: 16, token1: 24, lp: 8 }
            },
            REMOVE: {
                eventType: 'REMOVE' as const,
                poolIdIndex: 2,
                lpMintIndex: 12,
                tokenAmountOffsets: { token0: 16, token1: 24, lp: 8 }
            }
        };
        return configs[type];
    }
}