sol-parser-sdk 0.3.1

A lightweight Rust library for real-time event streaming from Solana DEX trading programs. Supports PumpFun, PumpSwap, Bonk, and Raydium protocols with Yellowstone gRPC and ShredStream.
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Meteora Pools 日志解析器
//!
//! 解析 Meteora Pools 程序的日志事件

use super::utils::*;
use crate::core::events::*;
use solana_sdk::signature::Signature;

/// Meteora Pools 事件 discriminator 常量
pub mod discriminators {
    pub const SWAP_EVENT: [u8; 8] = [81, 108, 227, 190, 205, 208, 10, 196];
    pub const ADD_LIQUIDITY_EVENT: [u8; 8] = [31, 94, 125, 90, 227, 52, 61, 186];
    pub const REMOVE_LIQUIDITY_EVENT: [u8; 8] = [116, 244, 97, 232, 103, 31, 152, 58];
    pub const BOOTSTRAP_LIQUIDITY_EVENT: [u8; 8] = [121, 127, 38, 136, 92, 55, 14, 247];
    pub const POOL_CREATED_EVENT: [u8; 8] = [202, 44, 41, 88, 104, 220, 157, 82];
    pub const SET_POOL_FEES_EVENT: [u8; 8] = [245, 26, 198, 164, 88, 18, 75, 9];
}

/// 主要的 Meteora Pools 日志解析函数
pub fn parse_log(
    log: &str,
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    parse_structured_log(log, signature, slot, tx_index, block_time_us, grpc_recv_us)
}

/// 解析结构化日志(基于 discriminator)
fn parse_structured_log(
    log: &str,
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let program_data = extract_program_data(log)?;

    if program_data.len() < 8 {
        return None;
    }

    let discriminator: [u8; 8] = program_data[0..8].try_into().ok()?;
    let data = &program_data[8..];

    match discriminator {
        discriminators::SWAP_EVENT => {
            parse_swap_event(data, signature, slot, tx_index, block_time_us, grpc_recv_us)
        }
        discriminators::ADD_LIQUIDITY_EVENT => {
            parse_add_liquidity_event(data, signature, slot, tx_index, block_time_us, grpc_recv_us)
        }
        discriminators::REMOVE_LIQUIDITY_EVENT => parse_remove_liquidity_event(
            data,
            signature,
            slot,
            tx_index,
            block_time_us,
            grpc_recv_us,
        ),
        discriminators::BOOTSTRAP_LIQUIDITY_EVENT => parse_bootstrap_liquidity_event(
            data,
            signature,
            slot,
            tx_index,
            block_time_us,
            grpc_recv_us,
        ),
        discriminators::POOL_CREATED_EVENT => {
            parse_pool_created_event(data, signature, slot, tx_index, block_time_us, grpc_recv_us)
        }
        discriminators::SET_POOL_FEES_EVENT => {
            parse_set_pool_fees_event(data, signature, slot, tx_index, block_time_us, grpc_recv_us)
        }
        _ => None,
    }
}

// =============================================================================
// Public from_data parsers - Accept pre-decoded data, eliminate double decode
// =============================================================================

/// Parse Meteora AMM Swap event from pre-decoded data
#[inline(always)]
pub fn parse_swap_from_data(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    let mut offset = 0;

    let in_amount = read_u64_le(data, offset)?;
    offset += 8;

    let out_amount = read_u64_le(data, offset)?;
    offset += 8;

    let trade_fee = read_u64_le(data, offset)?;
    offset += 8;

    let admin_fee = read_u64_le(data, offset)?;
    offset += 8;

    let host_fee = read_u64_le(data, offset)?;

    Some(DexEvent::MeteoraPoolsSwap(MeteoraPoolsSwapEvent {
        metadata,
        in_amount,
        out_amount,
        trade_fee,
        admin_fee,
        host_fee,
    }))
}

/// Parse Meteora AMM AddLiquidity event from pre-decoded data
#[inline(always)]
pub fn parse_add_liquidity_from_data(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_mint_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_a_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_b_amount = read_u64_le(data, offset)?;

    Some(DexEvent::MeteoraPoolsAddLiquidity(MeteoraPoolsAddLiquidityEvent {
        metadata,
        lp_mint_amount,
        token_a_amount,
        token_b_amount,
    }))
}

/// Parse Meteora AMM RemoveLiquidity event from pre-decoded data
#[inline(always)]
pub fn parse_remove_liquidity_from_data(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_unmint_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_a_out_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_b_out_amount = read_u64_le(data, offset)?;

    Some(DexEvent::MeteoraPoolsRemoveLiquidity(MeteoraPoolsRemoveLiquidityEvent {
        metadata,
        lp_unmint_amount,
        token_a_out_amount,
        token_b_out_amount,
    }))
}

/// Parse Meteora AMM BootstrapLiquidity event from pre-decoded data
#[inline(always)]
pub fn parse_bootstrap_liquidity_from_data(
    data: &[u8],
    metadata: EventMetadata,
) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_mint_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_a_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_b_amount = read_u64_le(data, offset)?;
    offset += 8;

    let pool = read_pubkey(data, offset)?;

    Some(DexEvent::MeteoraPoolsBootstrapLiquidity(MeteoraPoolsBootstrapLiquidityEvent {
        metadata,
        lp_mint_amount,
        token_a_amount,
        token_b_amount,
        pool,
    }))
}

/// Parse Meteora AMM PoolCreated event from pre-decoded data
#[inline(always)]
pub fn parse_pool_created_from_data(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_mint = read_pubkey(data, offset)?;
    offset += 32;

    let token_a_mint = read_pubkey(data, offset)?;
    offset += 32;

    let token_b_mint = read_pubkey(data, offset)?;
    offset += 32;

    let pool_type = read_u8(data, offset)?;
    offset += 1;

    let pool = read_pubkey(data, offset)?;

    Some(DexEvent::MeteoraPoolsPoolCreated(MeteoraPoolsPoolCreatedEvent {
        metadata,
        lp_mint,
        token_a_mint,
        token_b_mint,
        pool_type,
        pool,
    }))
}

/// 解析 Swap 事件
fn parse_swap_event(
    data: &[u8],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    let in_amount = read_u64_le(data, offset)?;
    offset += 8;

    let out_amount = read_u64_le(data, offset)?;
    offset += 8;

    let trade_fee = read_u64_le(data, offset)?;
    offset += 8;

    let admin_fee = read_u64_le(data, offset)?;
    offset += 8;

    let host_fee = read_u64_le(data, offset)?;

    // 使用默认的程序 ID,实际应该从上下文获取
    let metadata = create_metadata_default(signature, slot, tx_index, block_time_us);

    Some(DexEvent::MeteoraPoolsSwap(MeteoraPoolsSwapEvent {
        metadata,
        in_amount,
        out_amount,
        trade_fee,
        admin_fee,
        host_fee,
    }))
}

/// 解析 Add Liquidity 事件
fn parse_add_liquidity_event(
    data: &[u8],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_mint_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_a_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_b_amount = read_u64_le(data, offset)?;

    let metadata = create_metadata_default(signature, slot, tx_index, block_time_us);

    Some(DexEvent::MeteoraPoolsAddLiquidity(MeteoraPoolsAddLiquidityEvent {
        metadata,
        lp_mint_amount,
        token_a_amount,
        token_b_amount,
    }))
}

/// 解析 Remove Liquidity 事件
fn parse_remove_liquidity_event(
    data: &[u8],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_unmint_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_a_out_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_b_out_amount = read_u64_le(data, offset)?;

    let metadata = create_metadata_default(signature, slot, tx_index, block_time_us);

    Some(DexEvent::MeteoraPoolsRemoveLiquidity(MeteoraPoolsRemoveLiquidityEvent {
        metadata,
        lp_unmint_amount,
        token_a_out_amount,
        token_b_out_amount,
    }))
}

/// 解析 Bootstrap Liquidity 事件
fn parse_bootstrap_liquidity_event(
    data: &[u8],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_mint_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_a_amount = read_u64_le(data, offset)?;
    offset += 8;

    let token_b_amount = read_u64_le(data, offset)?;
    offset += 8;

    let pool = read_pubkey(data, offset)?;

    let metadata =
        create_metadata_simple(signature, slot, tx_index, block_time_us, pool, grpc_recv_us);

    Some(DexEvent::MeteoraPoolsBootstrapLiquidity(MeteoraPoolsBootstrapLiquidityEvent {
        metadata,
        lp_mint_amount,
        token_a_amount,
        token_b_amount,
        pool,
    }))
}

/// 解析 Pool Created 事件
fn parse_pool_created_event(
    data: &[u8],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    let lp_mint = read_pubkey(data, offset)?;
    offset += 32;

    let token_a_mint = read_pubkey(data, offset)?;
    offset += 32;

    let token_b_mint = read_pubkey(data, offset)?;
    offset += 32;

    let pool_type = read_u8(data, offset)?;
    offset += 1;

    let pool = read_pubkey(data, offset)?;

    let metadata =
        create_metadata_simple(signature, slot, tx_index, block_time_us, pool, grpc_recv_us);

    Some(DexEvent::MeteoraPoolsPoolCreated(MeteoraPoolsPoolCreatedEvent {
        metadata,
        lp_mint,
        token_a_mint,
        token_b_mint,
        pool_type,
        pool,
    }))
}

/// 解析 Set Pool Fees 事件
fn parse_set_pool_fees_event(
    data: &[u8],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    let trade_fee_numerator = read_u64_le(data, offset)?;
    offset += 8;

    let trade_fee_denominator = read_u64_le(data, offset)?;
    offset += 8;

    let owner_trade_fee_numerator = read_u64_le(data, offset)?;
    offset += 8;

    let owner_trade_fee_denominator = read_u64_le(data, offset)?;
    offset += 8;

    let pool = read_pubkey(data, offset)?;

    let metadata =
        create_metadata_simple(signature, slot, tx_index, block_time_us, pool, grpc_recv_us);

    Some(DexEvent::MeteoraPoolsSetPoolFees(MeteoraPoolsSetPoolFeesEvent {
        metadata,
        trade_fee_numerator,
        trade_fee_denominator,
        owner_trade_fee_numerator,
        owner_trade_fee_denominator,
        pool,
    }))
}

/// 解析文本格式日志
fn parse_text_log(
    _log: &str,
    _signature: Signature,
    _slot: u64,
    tx_index: u64,
    _block_time_us: Option<i64>,
) -> Option<DexEvent> {
    // 目前暂不实现文本解析,主要依赖结构化解析
    None
}