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
530
531
532
533
//! Meteora DAMM V2 指令解析器
//!
//! 使用 match discriminator 模式解析 Meteora DAMM V2 指令

use super::program_ids;
use super::utils::*;
use crate::core::events::*;
use solana_sdk::{pubkey::Pubkey, signature::Signature};

/// Meteora DAMM V2 discriminator 常量
pub mod discriminators {
    pub const SWAP_LOG: [u8; 8] = [27, 60, 21, 213, 138, 170, 187, 147];
    pub const SWAP2_LOG: [u8; 8] = [189, 66, 51, 168, 38, 80, 117, 153];
    pub const CREATE_POSITION_LOG: [u8; 8] = [156, 15, 119, 198, 29, 181, 221, 55];
    pub const CLOSE_POSITION_LOG: [u8; 8] = [20, 145, 144, 68, 143, 142, 214, 178];
    pub const ADD_LIQUIDITY_LOG: [u8; 8] = [175, 242, 8, 157, 30, 247, 185, 169];
    pub const REMOVE_LIQUIDITY_LOG: [u8; 8] = [87, 46, 88, 98, 175, 96, 34, 91];
}

/// Meteora DAMM 程序 ID
pub const PROGRAM_ID_PUBKEY: Pubkey = program_ids::METEORA_DAMM_V2_PROGRAM_ID;

/// 主要的 Meteora DAMM V2 指令解析函数
#[allow(unused_variables)]
pub fn parse_instruction(
    instruction_data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    grpc_recv_us: i64,
) -> Option<DexEvent> {
    if instruction_data.len() < 8 {
        return None;
    }

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

    if instruction_data.len() < 16 {
        return None;
    }

    let cpi_discriminator: [u8; 8] = instruction_data[8..16].try_into().ok()?;
    let cpi_data = &instruction_data[16..];

    match cpi_discriminator {
        discriminators::SWAP_LOG => {
            return parse_swap_log_instruction(
                cpi_data,
                accounts,
                signature,
                slot,
                tx_index,
                block_time_us,
                grpc_recv_us,
            )
        }
        discriminators::SWAP2_LOG => {
            return parse_swap2_log_instruction(
                cpi_data,
                accounts,
                signature,
                slot,
                tx_index,
                block_time_us,
                grpc_recv_us,
            )
        }
        discriminators::CREATE_POSITION_LOG => {
            return parse_create_position_log_instruction(
                cpi_data,
                accounts,
                signature,
                slot,
                tx_index,
                block_time_us,
                grpc_recv_us,
            );
        }
        discriminators::CLOSE_POSITION_LOG => {
            return parse_close_position_log_instruction(
                data,
                accounts,
                signature,
                slot,
                tx_index,
                block_time_us,
                grpc_recv_us,
            );
        }
        discriminators::ADD_LIQUIDITY_LOG => {
            return parse_add_liquidity_log_instruction(
                cpi_data,
                accounts,
                signature,
                slot,
                tx_index,
                block_time_us,
                grpc_recv_us,
            );
        }
        discriminators::REMOVE_LIQUIDITY_LOG => {
            return parse_remove_liquidity_log_instruction(
                cpi_data,
                accounts,
                signature,
                slot,
                tx_index,
                block_time_us,
                grpc_recv_us,
            );
        }
        _ => None,
    }
}

/// 解析 Swap 指令
#[allow(unused_variables)]
fn parse_swap_log_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    rpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    // pool (Pubkey - 32 bytes)
    let pool = read_pubkey(data, offset)?;
    offset += 32;

    // tradeDirection (u8 - 1 byte)
    let trade_direction = read_u8(data, offset)?;
    offset += 1;

    // hasReferral (bool - 1 byte)
    let has_referral = read_bool(data, offset)?;
    offset += 1;

    // params.amountIn (u64 - 8 bytes)
    let amount_in = read_u64_le(data, offset)?;
    offset += 8;

    // params.minimumAmountOut (u64 - 8 bytes)
    let min_amount_out = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.outputAmount (u64 - 8 bytes)
    let output_amount = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.nextSqrtPrice (u128 - 16 bytes)
    let next_sqrt_price = read_u128_le(data, offset)?;
    offset += 16;

    // swapResult.lpFee (u64 - 8 bytes)
    let lp_fee = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.protocolFee (u64 - 8 bytes)
    let protocol_fee = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.partnerFee (u64 - 8 bytes)
    let partner_fee = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.referralFee (u64 - 8 bytes)
    let referral_fee = read_u64_le(data, offset)?;
    offset += 8;

    // actualAmountIn (u64 - 8 bytes)
    let actual_amount_in = read_u64_le(data, offset)?;
    offset += 8;

    // currentTimestamp (u64 - 8 bytes)
    let current_timestamp = read_u64_le(data, offset)?;

    let metadata =
        create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);

    Some(DexEvent::MeteoraDammV2Swap(MeteoraDammV2SwapEvent {
        metadata,
        pool,
        trade_direction,
        has_referral,
        amount_in,
        minimum_amount_out: min_amount_out,
        output_amount,
        next_sqrt_price,
        lp_fee,
        protocol_fee,
        partner_fee,
        referral_fee,
        actual_amount_in,
        current_timestamp,
        ..Default::default()
    }))
}

/// 解析 Swap2 指令 (EvtSwap2 格式)
#[allow(unused_variables)]
fn parse_swap2_log_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    rpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    // pool (Pubkey - 32 bytes)
    let pool = read_pubkey(data, offset)?;
    offset += 32;

    // trade_direction (u8 - 1 byte)
    let trade_direction = read_u8(data, offset)?;
    offset += 1;

    // collect_fee_mode (u8 - 1 byte)
    let _collect_fee_mode = read_u8(data, offset)?;
    offset += 1;

    // has_referral (bool - 1 byte)
    let has_referral = read_bool(data, offset)?;
    offset += 1;

    // SwapParameters2
    // params.amount_0 (u64 - 8 bytes)
    let amount_0 = read_u64_le(data, offset)?;
    offset += 8;

    // params.amount_1 (u64 - 8 bytes)
    let amount_1 = read_u64_le(data, offset)?;
    offset += 8;

    // params.swap_mode (u8 - 1 byte)
    let swap_mode = read_u8(data, offset)?;
    offset += 1;

    // SwapResult2
    // swapResult.included_fee_input_amount (u64 - 8 bytes)
    let included_fee_input_amount = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.excluded_fee_input_amount (u64 - 8 bytes)
    let _excluded_fee_input_amount = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.amount_left (u64 - 8 bytes)
    let _amount_left = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.output_amount (u64 - 8 bytes)
    let output_amount = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.next_sqrt_price (u128 - 16 bytes)
    let next_sqrt_price = read_u128_le(data, offset)?;
    offset += 16;

    // swapResult.trading_fee (u64 - 8 bytes)
    let lp_fee = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.protocol_fee (u64 - 8 bytes)
    let protocol_fee = read_u64_le(data, offset)?;
    offset += 8;

    // swapResult.referral_fee (u64 - 8 bytes)
    let referral_fee = read_u64_le(data, offset)?;
    offset += 8;

    // quote_reserve_amount (u64 - 8 bytes)
    let _quote_reserve_amount = read_u64_le(data, offset)?;
    offset += 8;

    // migration_threshold (u64 - 8 bytes)
    let _migration_threshold = read_u64_le(data, offset)?;
    offset += 8;

    // currentTimestamp (u64 - 8 bytes)
    let current_timestamp = read_u64_le(data, offset)?;

    let metadata =
        create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);

    // 根据 swap_mode 和 trade_direction 确定实际的 amount_in
    // swap_mode: 0 = ExactIn, 1 = ExactOut
    let (amount_in, minimum_amount_out) = if swap_mode == 0 {
        // ExactIn: amount_0 is amount_in, amount_1 is minimum_amount_out
        (amount_0, amount_1)
    } else {
        // ExactOut: amount_1 is maximum_amount_in, amount_0 is amount_out
        (amount_1, amount_0)
    };

    let actual_amount_in = included_fee_input_amount;

    Some(DexEvent::MeteoraDammV2Swap(MeteoraDammV2SwapEvent {
        metadata,
        pool,
        trade_direction,
        has_referral,
        amount_in,
        minimum_amount_out,
        output_amount,
        next_sqrt_price,
        lp_fee,
        protocol_fee,
        partner_fee: 0, // SwapResult2 没有 partner_fee
        referral_fee,
        actual_amount_in,
        current_timestamp,
        ..Default::default()
    }))
}

/// 解析 Create Position Log 指令
#[allow(unused_variables)]
fn parse_create_position_log_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    rpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    // pool (Pubkey - 32 bytes)
    let pool = read_pubkey(data, offset)?;
    offset += 32;

    // owner (Pubkey - 32 bytes)
    let owner = read_pubkey(data, offset)?;
    offset += 32;

    // position (Pubkey - 32 bytes)
    let position = read_pubkey(data, offset)?;
    offset += 32;

    // positionNftMint (Pubkey - 32 bytes)
    let position_nft_mint = read_pubkey(data, offset)?;

    let metadata =
        create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);

    Some(DexEvent::MeteoraDammV2CreatePosition(MeteoraDammV2CreatePositionEvent {
        metadata,
        pool,
        owner,
        position,
        position_nft_mint,
    }))
}

/// 解析 Close Position Log 指令
#[allow(unused_variables)]
fn parse_close_position_log_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    rpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    // pool (Pubkey - 32 bytes)
    let pool = read_pubkey(data, offset)?;
    offset += 32;

    // owner (Pubkey - 32 bytes)
    let owner = read_pubkey(data, offset)?;
    offset += 32;

    // position (Pubkey - 32 bytes)
    let position = read_pubkey(data, offset)?;
    offset += 32;

    // positionNftMint (Pubkey - 32 bytes)
    let position_nft_mint = read_pubkey(data, offset)?;

    let metadata =
        create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);

    Some(DexEvent::MeteoraDammV2ClosePosition(MeteoraDammV2ClosePositionEvent {
        metadata,
        pool,
        owner,
        position,
        position_nft_mint,
    }))
}

/// 解析 Add Liquidity Log 指令
#[allow(unused_variables)]
fn parse_add_liquidity_log_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    rpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    // pool (Pubkey - 32 bytes)
    let pool = read_pubkey(data, offset)?;
    offset += 32;

    // position (Pubkey - 32 bytes)
    let position = read_pubkey(data, offset)?;
    offset += 32;

    // owner (Pubkey - 32 bytes)
    let owner = read_pubkey(data, offset)?;
    offset += 32;

    // params.liquidityDelta (u128 - 16 bytes)
    let liquidity_delta = read_u128_le(data, offset)?;
    offset += 16;

    // params.tokenAAmountThreshold (u64 - 8 bytes)
    let token_a_amount_threshold = read_u64_le(data, offset)?;
    offset += 8;

    // params.tokenBAmountThreshold (u64 - 8 bytes)
    let token_b_amount_threshold = read_u64_le(data, offset)?;
    offset += 8;

    // tokenAAmount (u64 - 8 bytes)
    let token_a_amount = read_u64_le(data, offset)?;
    offset += 8;

    // tokenBAmount (u64 - 8 bytes)
    let token_b_amount = read_u64_le(data, offset)?;
    offset += 8;

    // totalAmountA (u64 - 8 bytes)
    let total_amount_a = read_u64_le(data, offset)?;
    offset += 8;

    // totalAmountB (u64 - 8 bytes)
    let total_amount_b = read_u64_le(data, offset)?;

    let metadata =
        create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);

    Some(DexEvent::MeteoraDammV2AddLiquidity(MeteoraDammV2AddLiquidityEvent {
        metadata,
        pool,
        position,
        owner,
        liquidity_delta,
        token_a_amount_threshold,
        token_b_amount_threshold,
        token_a_amount,
        token_b_amount,
        total_amount_a,
        total_amount_b,
    }))
}

/// 解析 Add Liquidity Log 指令
#[allow(unused_variables)]
fn parse_remove_liquidity_log_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    rpc_recv_us: i64,
) -> Option<DexEvent> {
    let mut offset = 0;

    // pool (Pubkey - 32 bytes)
    let pool = read_pubkey(data, offset)?;
    offset += 32;

    // position (Pubkey - 32 bytes)
    let position = read_pubkey(data, offset)?;
    offset += 32;

    // owner (Pubkey - 32 bytes)
    let owner = read_pubkey(data, offset)?;
    offset += 32;

    // params.liquidityDelta (u128 - 16 bytes)
    let liquidity_delta = read_u128_le(data, offset)?;
    offset += 16;

    // params.tokenAAmountThreshold (u64 - 8 bytes)
    let token_a_amount_threshold = read_u64_le(data, offset)?;
    offset += 8;

    // params.tokenBAmountThreshold (u64 - 8 bytes)
    let token_b_amount_threshold = read_u64_le(data, offset)?;
    offset += 8;

    // tokenAAmount (u64 - 8 bytes)
    let token_a_amount = read_u64_le(data, offset)?;
    offset += 8;

    // tokenBAmount (u64 - 8 bytes)
    let token_b_amount = read_u64_le(data, offset)?;

    let metadata =
        create_metadata(signature, slot, tx_index, block_time_us.unwrap_or_default(), rpc_recv_us);

    Some(DexEvent::MeteoraDammV2RemoveLiquidity(MeteoraDammV2RemoveLiquidityEvent {
        metadata,
        pool,
        position,
        owner,
        liquidity_delta,
        token_a_amount_threshold,
        token_b_amount_threshold,
        token_a_amount,
        token_b_amount,
    }))
}