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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 1x 1x 1x 1x 1x 1x 1x | import { Buffer } from 'buffer';
import base58 from 'bs58';
import { DEX_PROGRAMS, DISCRIMINATORS } from '../constants';
import { convertToUiAmount, PumpfunCompleteEvent, PumpfunCreateEvent, PumpfunEvent, PumpfunTradeEvent } from '../types';
import { TransactionAdapter } from '../transaction-adapter';
import { getInstructionData } from '../utils';
export class PumpfunEventParser {
constructor(private readonly adapter: TransactionAdapter) {}
public processEvents(): PumpfunEvent[] {
return this.adapter.instructions.reduce((events: PumpfunEvent[], instruction: any, index: number) => {
Iif (this.adapter.getInstructionProgramId(instruction) === DEX_PROGRAMS.PUMP_FUN.id) {
events.push(...this.parseInnerInstructions(index));
}
return events;
}, []);
}
public parseInnerInstructions(instructionIndex: number): PumpfunEvent[] {
const innerInstructions = this.adapter.innerInstructions;
Iif (!innerInstructions) return [];
const txMeta = {
slot: this.adapter.slot,
timestamp: this.adapter.blockTime || 0,
signature: this.adapter.signature,
};
return innerInstructions
.filter((set) => set.index === instructionIndex)
.flatMap((set) =>
set.instructions
.flatMap((ix, idx: number) => {
const events: PumpfunEvent[] = [];
Iif (this.isPumpFunCreateEvent(ix)) {
const event = this.parseCreateEvent(ix);
Iif (event) {
events.push({
type: 'CREATE',
data: event,
...txMeta,
idx: `${instructionIndex}-${idx}`,
});
}
}
Iif (this.isPumpFunTradeEvent(ix)) {
const event = this.parseTradeEvent(ix);
Iif (event) {
events.push({
type: 'TRADE',
data: event,
...txMeta,
idx: `${instructionIndex}-${idx}`,
});
}
}
Iif (this.isPumpFunCompleteEvent(ix)) {
const event = this.parseCompleteEvent(ix);
Iif (event) {
events.push({
type: 'COMPLETE',
data: event,
...txMeta,
idx: `${instructionIndex}-${idx}`,
});
}
}
return events;
})
.filter((event) => event !== null)
);
}
private isPumpFunTradeEvent(instruction: any): boolean {
try {
Iif (this.adapter.getInstructionProgramId(instruction) != DEX_PROGRAMS.PUMP_FUN.id) return false;
const data = getInstructionData(instruction);
return Buffer.from(data.slice(0, 16)).equals(DISCRIMINATORS.PUMPFUN.TRADE_EVENT);
} catch {
return false;
}
}
private parseTradeEvent(instruction: any): PumpfunTradeEvent | null {
try {
const data = getInstructionData(instruction);
return this.decodeTradeEvent(data.slice(16));
} catch (error) {
console.error('Failed to parse PumpFun trade event:', error);
return null;
}
}
private decodeTradeEvent(data: Buffer): PumpfunTradeEvent {
const reader = new BinaryReader(data);
return {
mint: base58.encode(Buffer.from(reader.readFixedArray(32))),
solAmount: convertToUiAmount(reader.readU64()),
tokenAmount: convertToUiAmount(reader.readU64(), 6),
isBuy: reader.readU8() === 1,
user: base58.encode(reader.readFixedArray(32)),
timestamp: reader.readI64(),
virtualSolReserves: convertToUiAmount(reader.readU64()),
virtualTokenReserves: convertToUiAmount(reader.readU64(), 6),
};
}
private isPumpFunCreateEvent(instruction: any): boolean {
try {
const programId = this.adapter.getInstructionProgramId(instruction);
Iif (programId != DEX_PROGRAMS.PUMP_FUN.id) return false;
const data = getInstructionData(instruction);
return Buffer.from(data.slice(0, 16)).equals(DISCRIMINATORS.PUMPFUN.CREATE_EVENT);
} catch {
return false;
}
}
private parseCreateEvent(instruction: any): PumpfunCreateEvent | null {
try {
const data = getInstructionData(instruction);
return this.decodeCreateEvent(data.slice(16));
} catch (error) {
console.error('Failed to parse PumpFun create event:', error);
return null;
}
}
private decodeCreateEvent(data: Buffer): PumpfunCreateEvent {
const reader = new BinaryReader(data);
return {
name: reader.readString(),
symbol: reader.readString(),
uri: reader.readString(),
mint: base58.encode(Buffer.from(reader.readFixedArray(32))),
bondingCurve: base58.encode(reader.readFixedArray(32)),
user: base58.encode(reader.readFixedArray(32)),
};
}
private isPumpFunCompleteEvent(instruction: any): boolean {
try {
const programId = this.adapter.getInstructionProgramId(instruction);
Iif (programId != DEX_PROGRAMS.PUMP_FUN.id) return false;
const data = getInstructionData(instruction);
return Buffer.from(data.slice(0, 16)).equals(DISCRIMINATORS.PUMPFUN.COMPLETE_EVENT);
} catch {
return false;
}
}
private parseCompleteEvent(instruction: any): PumpfunCompleteEvent | null {
try {
const data = getInstructionData(instruction);
return this.decodeCompleteEvent(data.slice(16));
} catch (error) {
console.error('Failed to parse PumpFun complete event:', error);
return null;
}
}
private decodeCompleteEvent(data: Buffer): PumpfunCompleteEvent {
const reader = new BinaryReader(data);
return {
user: base58.encode(reader.readFixedArray(32)),
mint: base58.encode(Buffer.from(reader.readFixedArray(32))),
bondingCurve: base58.encode(reader.readFixedArray(32)),
timestamp: reader.readI64(),
};
}
}
export class BinaryReader {
private offset = 0;
constructor(private buffer: Buffer) {}
readFixedArray(length: number): Buffer {
this.checkBounds(length);
const array = this.buffer.slice(this.offset, this.offset + length);
this.offset += length;
return array;
}
readU8(): number {
this.checkBounds(1);
const value = this.buffer.readUInt8(this.offset);
this.offset += 1;
return value;
}
readU16(): number {
this.checkBounds(2);
const value = this.buffer.readUint16LE(this.offset);
this.offset += 2;
return value;
}
readU64(): bigint {
this.checkBounds(8);
const value = this.buffer.readBigUInt64LE(this.offset);
this.offset += 8;
return value;
}
readI64(): bigint {
this.checkBounds(8);
const value = this.buffer.readBigInt64LE(this.offset);
this.offset += 8;
return value;
}
readString(): string {
// Read 4-byte (32-bit) length instead of 1 byte
const length = this.buffer.readUInt32LE(this.offset);
this.offset += 4;
this.checkBounds(length);
const strBuffer = this.buffer.slice(this.offset, this.offset + length);
const content = strBuffer.toString('utf8');
this.offset += length;
return content;
}
readPubkey(): string {
return base58.encode(Buffer.from(this.readFixedArray(32)));
}
private checkBounds(length: number) {
Iif (this.offset + length > this.buffer.length) {
throw new Error(
`Buffer overflow: trying to read ${length} bytes at offset ${this.offset} in buffer of length ${this.buffer.length}`
);
}
}
getOffset(): number {
return this.offset;
}
}
|