solana-dex-parser
Version:
Solana Dex Transaction Parser
108 lines • 4.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrcaLiquidityParser = void 0;
const constants_1 = require("../../constants");
const types_1 = require("../../types");
const utils_1 = require("../../utils");
const base_liquidity_parser_1 = require("../base-liquidity-parser");
class OrcaLiquidityParser extends base_liquidity_parser_1.BaseLiquidityParser {
processLiquidity() {
const events = [];
this.classifiedInstructions
.filter(({ programId }) => programId === constants_1.DEX_PROGRAMS.ORCA.id)
.forEach(({ instruction, programId, outerIndex, innerIndex }) => {
const event = this.parseInstruction(instruction, programId, outerIndex, innerIndex);
if (event) {
events.push(event);
}
});
return events;
}
parseInstruction(instruction, programId, outerIndex, innerIndex) {
try {
const data = (0, utils_1.getInstructionData)(instruction);
const action = this.getPoolAction(data);
if (!action)
return null;
const transfers = this.getTransfersForInstruction(programId, outerIndex, innerIndex);
switch (action) {
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);
throw error;
}
}
getPoolAction(data) {
const instructionType = data.slice(0, 8);
if (instructionType.equals(constants_1.DISCRIMINATORS.ORCA.ADD_LIQUIDITY) ||
instructionType.equals(constants_1.DISCRIMINATORS.ORCA.ADD_LIQUIDITY2)) {
return 'ADD';
}
else if (instructionType.equals(constants_1.DISCRIMINATORS.ORCA.REMOVE_LIQUIDITY)) {
return 'REMOVE';
}
return null;
}
parseAddLiquidityEvent(instruction, index, data, transfers) {
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,
token0AmountRaw: token0?.info.tokenAmount.amount,
token1Amount: token1?.info.tokenAmount.uiAmount,
token1AmountRaw: token1?.info.tokenAmount.amount,
token0Decimals: token0Decimals,
token1Decimals: token1Decimals,
lpAmount: (0, types_1.convertToUiAmount)(data.readBigUInt64LE(8), this.adapter.getTokenDecimals(accounts[1])),
lpAmountRaw: data.readBigUInt64LE(8).toString(),
};
}
parseRemoveLiquidityEvent(instruction, index, data, transfers) {
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,
token0AmountRaw: token0?.info.tokenAmount.amount,
token1Amount: token1?.info.tokenAmount.uiAmount,
token1AmountRaw: token1?.info.tokenAmount.amount,
token0Decimals: token0Decimals,
token1Decimals: token1Decimals,
lpAmount: (0, types_1.convertToUiAmount)(data.readBigUInt64LE(8), this.adapter.getTokenDecimals(accounts[1])),
lpAmountRaw: data.readBigUInt64LE(8).toString(),
};
}
}
exports.OrcaLiquidityParser = OrcaLiquidityParser;
//# sourceMappingURL=parser-orca-liquidity.js.map