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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Orca Whirlpool 日志解析器
//!
//! 解析 Orca Whirlpool 程序的日志事件

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

/// Orca Whirlpool 事件 discriminator 常量
pub mod discriminators {
    pub const TRADED_EVENT: [u8; 8] = [225, 202, 73, 175, 147, 43, 160, 150];
    pub const LIQUIDITY_INCREASED_EVENT: [u8; 8] = [30, 7, 144, 181, 102, 254, 155, 161];
    pub const LIQUIDITY_DECREASED_EVENT: [u8; 8] = [166, 1, 36, 71, 112, 202, 181, 171];
    pub const POOL_INITIALIZED_EVENT: [u8; 8] = [100, 118, 173, 87, 12, 198, 254, 229];
}

/// 主要的 Orca Whirlpool 日志解析函数
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::TRADED_EVENT => {
            parse_traded_event(data, signature, slot, tx_index, block_time_us, grpc_recv_us)
        }
        discriminators::LIQUIDITY_INCREASED_EVENT => parse_liquidity_increased_event(
            data,
            signature,
            slot,
            tx_index,
            block_time_us,
            grpc_recv_us,
        ),
        discriminators::LIQUIDITY_DECREASED_EVENT => parse_liquidity_decreased_event(
            data,
            signature,
            slot,
            tx_index,
            block_time_us,
            grpc_recv_us,
        ),
        discriminators::POOL_INITIALIZED_EVENT => parse_pool_initialized_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 Orca Whirlpool Traded (Swap) event from pre-decoded data
#[inline(always)]
pub fn parse_traded_from_data(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    let mut offset = 0;

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

    let a_to_b = read_bool(data, offset)?;
    offset += 1;

    let pre_sqrt_price = read_u128_le(data, offset)?;
    offset += 16;

    let post_sqrt_price = read_u128_le(data, offset)?;
    offset += 16;

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

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

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

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

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

    let protocol_fee = read_u64_le(data, offset)?;

    Some(DexEvent::OrcaWhirlpoolSwap(OrcaWhirlpoolSwapEvent {
        metadata,
        whirlpool,
        a_to_b,
        pre_sqrt_price,
        post_sqrt_price,
        input_amount,
        output_amount,
        input_transfer_fee,
        output_transfer_fee,
        lp_fee,
        protocol_fee,
    }))
}

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

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

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

    let tick_lower_index = read_i32_le(data, offset)?;
    offset += 4;

    let tick_upper_index = read_i32_le(data, offset)?;
    offset += 4;

    let liquidity = read_u128_le(data, offset)?;
    offset += 16;

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

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

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

    let token_b_transfer_fee = read_u64_le(data, offset)?;

    Some(DexEvent::OrcaWhirlpoolLiquidityIncreased(OrcaWhirlpoolLiquidityIncreasedEvent {
        metadata,
        whirlpool,
        position,
        tick_lower_index,
        tick_upper_index,
        liquidity,
        token_a_amount,
        token_b_amount,
        token_a_transfer_fee,
        token_b_transfer_fee,
    }))
}

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

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

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

    let tick_lower_index = read_i32_le(data, offset)?;
    offset += 4;

    let tick_upper_index = read_i32_le(data, offset)?;
    offset += 4;

    let liquidity = read_u128_le(data, offset)?;
    offset += 16;

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

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

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

    let token_b_transfer_fee = read_u64_le(data, offset)?;

    Some(DexEvent::OrcaWhirlpoolLiquidityDecreased(OrcaWhirlpoolLiquidityDecreasedEvent {
        metadata,
        whirlpool,
        position,
        tick_lower_index,
        tick_upper_index,
        liquidity,
        token_a_amount,
        token_b_amount,
        token_a_transfer_fee,
        token_b_transfer_fee,
    }))
}

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

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

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

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

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

    let tick_spacing = read_u16_le(data, offset)?;
    offset += 2;

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

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

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

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

    let initial_sqrt_price = read_u128_le(data, offset)?;

    Some(DexEvent::OrcaWhirlpoolPoolInitialized(OrcaWhirlpoolPoolInitializedEvent {
        metadata,
        whirlpool,
        whirlpools_config,
        token_mint_a,
        token_mint_b,
        tick_spacing,
        token_program_a,
        token_program_b,
        decimals_a,
        decimals_b,
        initial_sqrt_price,
    }))
}

/// 解析 Traded 事件
fn parse_traded_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 whirlpool = read_pubkey(data, offset)?;
    offset += 32;

    let a_to_b = read_bool(data, offset)?;
    offset += 1;

    let pre_sqrt_price = read_u128_le(data, offset)?;
    offset += 16;

    let post_sqrt_price = read_u128_le(data, offset)?;
    offset += 16;

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

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

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

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

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

    let protocol_fee = read_u64_le(data, offset)?;

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

    Some(DexEvent::OrcaWhirlpoolSwap(OrcaWhirlpoolSwapEvent {
        metadata,

        // IDL TradedEvent 事件字段
        whirlpool,
        a_to_b,
        pre_sqrt_price,
        post_sqrt_price,
        input_amount,
        output_amount,
        input_transfer_fee,
        output_transfer_fee,
        lp_fee,
        protocol_fee,
        // 暂时注释,以后会用,AI禁止改动
        // 指令参数字段
        // amount: input_amount,
        // other_amount_threshold: output_amount,
        // sqrt_price_limit: pre_sqrt_price,
        // amount_specified_is_input: true,

        // 指令账户字段
        // token_authority: solana_sdk::pubkey::Pubkey::default(),
        // token_owner_account_a: solana_sdk::pubkey::Pubkey::default(),
        // token_vault_a: solana_sdk::pubkey::Pubkey::default(),
        // token_owner_account_b: solana_sdk::pubkey::Pubkey::default(),
        // token_vault_b: solana_sdk::pubkey::Pubkey::default(),
        // tick_array_0: solana_sdk::pubkey::Pubkey::default(),
        // tick_array_1: solana_sdk::pubkey::Pubkey::default(),
        // tick_array_2: solana_sdk::pubkey::Pubkey::default(),
    }))
}

/// 解析 Liquidity Increased 事件
fn parse_liquidity_increased_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 whirlpool = read_pubkey(data, offset)?;
    offset += 32;

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

    let tick_lower_index = read_i32_le(data, offset)?;
    offset += 4;

    let tick_upper_index = read_i32_le(data, offset)?;
    offset += 4;

    let liquidity = read_u128_le(data, offset)?;
    offset += 16;

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

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

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

    let token_b_transfer_fee = read_u64_le(data, offset)?;

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

    Some(DexEvent::OrcaWhirlpoolLiquidityIncreased(OrcaWhirlpoolLiquidityIncreasedEvent {
        metadata,
        whirlpool,
        position,
        tick_lower_index,
        tick_upper_index,
        liquidity,
        token_a_amount,
        token_b_amount,
        token_a_transfer_fee,
        token_b_transfer_fee,
    }))
}

/// 解析 Liquidity Decreased 事件
fn parse_liquidity_decreased_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 whirlpool = read_pubkey(data, offset)?;
    offset += 32;

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

    let tick_lower_index = read_i32_le(data, offset)?;
    offset += 4;

    let tick_upper_index = read_i32_le(data, offset)?;
    offset += 4;

    let liquidity = read_u128_le(data, offset)?;
    offset += 16;

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

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

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

    let token_b_transfer_fee = read_u64_le(data, offset)?;

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

    Some(DexEvent::OrcaWhirlpoolLiquidityDecreased(OrcaWhirlpoolLiquidityDecreasedEvent {
        metadata,
        whirlpool,
        position,
        tick_lower_index,
        tick_upper_index,
        liquidity,
        token_a_amount,
        token_b_amount,
        token_a_transfer_fee,
        token_b_transfer_fee,
    }))
}

/// 解析 Pool Initialized 事件
fn parse_pool_initialized_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 whirlpool = read_pubkey(data, offset)?;
    offset += 32;

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

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

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

    let tick_spacing = read_u16_le(data, offset)?;
    offset += 2;

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

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

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

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

    let initial_sqrt_price = read_u128_le(data, offset)?;

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

    Some(DexEvent::OrcaWhirlpoolPoolInitialized(OrcaWhirlpoolPoolInitializedEvent {
        metadata,
        whirlpool,
        whirlpools_config,
        token_mint_a,
        token_mint_b,
        tick_spacing,
        token_program_a,
        token_program_b,
        decimals_a,
        decimals_b,
        initial_sqrt_price,
    }))
}

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