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
//! Raydium CLMM Inner Instruction 解析器
//!
//! ## 解析器插件系统
#![allow(unused_imports)]
//!
//! 本模块提供两种可插拔的解析器实现:
//!
//! ### 1. Borsh 反序列化解析器(默认,推荐)
//! - **启用**: `cargo build --features parse-borsh` (默认)
//! - **优点**: 类型安全、代码简洁、易维护、自动验证
//! - **适用**: 一般场景、需要稳定性和可维护性的项目
//!
//! ### 2. 零拷贝解析器(高性能)
//! - **启用**: `cargo build --features parse-zero-copy --no-default-features`
//! - **优点**: 最快、零拷贝、无验证开销、适合超高频场景
//! - **适用**: 性能关键路径、每秒数万次解析的场景

use crate::core::events::*;
use crate::instr::inner_common::*;
use solana_sdk::pubkey::Pubkey;

/// Raydium CLMM inner instruction discriminators (16 bytes)
pub mod discriminators {
    /// SwapEvent
    pub const SWAP: [u8; 16] =
        [248, 198, 158, 145, 225, 117, 135, 200, 155, 167, 108, 32, 122, 76, 173, 64];

    /// IncreaseLiquidityEvent
    pub const INCREASE_LIQUIDITY: [u8; 16] =
        [133, 29, 89, 223, 69, 238, 176, 10, 155, 167, 108, 32, 122, 76, 173, 64];

    /// DecreaseLiquidityEvent
    pub const DECREASE_LIQUIDITY: [u8; 16] =
        [160, 38, 208, 111, 104, 91, 44, 1, 155, 167, 108, 32, 122, 76, 173, 64];

    /// CreatePoolEvent
    pub const CREATE_POOL: [u8; 16] =
        [233, 146, 209, 142, 207, 104, 64, 188, 155, 167, 108, 32, 122, 76, 173, 64];

    /// CollectFeeEvent
    pub const COLLECT_FEE: [u8; 16] =
        [164, 152, 207, 99, 187, 104, 171, 119, 155, 167, 108, 32, 122, 76, 173, 64];
}

#[inline]
pub fn parse_raydium_clmm_inner_instruction(
    discriminator: &[u8; 16],
    data: &[u8],
    metadata: EventMetadata,
) -> Option<DexEvent> {
    match discriminator {
        &discriminators::SWAP => parse_swap_inner(data, metadata),
        &discriminators::INCREASE_LIQUIDITY => parse_increase_liquidity_inner(data, metadata),
        &discriminators::DECREASE_LIQUIDITY => parse_decrease_liquidity_inner(data, metadata),
        &discriminators::CREATE_POOL => parse_create_pool_inner(data, metadata),
        &discriminators::COLLECT_FEE => parse_collect_fee_inner(data, metadata),
        _ => None,
    }
}

// ============================================================================
// Swap 事件解析器
// ============================================================================

/// 解析 Swap 事件(统一入口)
///
/// 根据编译时的 feature flag 自动选择解析器实现
#[inline(always)]
fn parse_swap_inner(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    #[cfg(feature = "parse-borsh")]
    {
        parse_swap_inner_borsh(data, metadata)
    }

    #[cfg(feature = "parse-zero-copy")]
    {
        parse_swap_inner_zero_copy(data, metadata)
    }
}

/// Borsh 反序列化解析器 - Swap 事件
///
/// **优点**: 类型安全、代码简洁、自动验证
#[cfg(feature = "parse-borsh")]
#[inline(always)]
fn parse_swap_inner_borsh(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    // 数据结构:
    // pool_state: Pubkey (32 bytes)
    // token_account_0: Pubkey (32 bytes)
    // token_account_1: Pubkey (32 bytes)
    // amount_0: u64 (8 bytes)
    // amount_1: u64 (8 bytes)
    // zero_for_one: bool (1 byte)
    // sqrt_price_x64: u128 (16 bytes)
    // liquidity: u128 (16 bytes)
    // Total: 145 bytes
    const SWAP_EVENT_SIZE: usize = 32 + 32 + 32 + 8 + 8 + 1 + 16 + 16;

    if data.len() < SWAP_EVENT_SIZE {
        return None;
    }

    let event = borsh::from_slice::<RaydiumClmmSwapEvent>(&data[..SWAP_EVENT_SIZE]).ok()?;

    Some(DexEvent::RaydiumClmmSwap(RaydiumClmmSwapEvent { metadata, ..event }))
}

/// 零拷贝解析器 - Swap 事件
///
/// **优点**: 最快、零拷贝、无验证开销
#[cfg(feature = "parse-zero-copy")]
#[inline(always)]
fn parse_swap_inner_zero_copy(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    // 数据结构:
    // pool_state: Pubkey (32 bytes)
    // token_account_0: Pubkey (32 bytes)
    // token_account_1: Pubkey (32 bytes)
    // amount_0: u64 (8 bytes)
    // amount_1: u64 (8 bytes)
    // zero_for_one: bool (1 byte)
    // sqrt_price_x64: u128 (16 bytes)
    // liquidity: u128 (16 bytes)
    unsafe {
        if !check_length(data, 32 + 32 + 32 + 8 + 8 + 1 + 16 + 16) {
            return None;
        }

        let mut offset = 0;
        let pool_id = read_pubkey_unchecked(data, offset);
        offset += 32;
        let input_vault = read_pubkey_unchecked(data, offset);
        offset += 32;
        let output_vault = read_pubkey_unchecked(data, offset);
        offset += 32;
        let input_amount = read_u64_unchecked(data, offset);
        offset += 8;
        let output_amount = read_u64_unchecked(data, offset);
        offset += 8;
        let zero_for_one = read_bool_unchecked(data, offset);
        offset += 1;
        let sqrt_price_x64 = read_u128_unchecked(data, offset);
        offset += 16;
        let liquidity = read_u128_unchecked(data, offset);

        Some(DexEvent::RaydiumClmmSwap(RaydiumClmmSwapEvent {
            metadata,
            pool_state: pool_id,
            sender: Pubkey::default(),
            token_account_0: input_vault,
            token_account_1: output_vault,
            amount_0: input_amount,
            transfer_fee_0: 0,
            amount_1: output_amount,
            transfer_fee_1: 0,
            zero_for_one,
            sqrt_price_x64,
            liquidity,
            tick: 0,
        }))
    }
}

// ============================================================================
// IncreaseLiquidity 事件解析器
// ============================================================================

/// 解析 IncreaseLiquidity 事件(统一入口)
#[inline(always)]
fn parse_increase_liquidity_inner(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    #[cfg(feature = "parse-borsh")]
    {
        parse_increase_liquidity_inner_borsh(data, metadata)
    }

    #[cfg(feature = "parse-zero-copy")]
    {
        parse_increase_liquidity_inner_zero_copy(data, metadata)
    }
}

/// Borsh 反序列化解析器 - IncreaseLiquidity 事件
#[cfg(feature = "parse-borsh")]
#[inline(always)]
fn parse_increase_liquidity_inner_borsh(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    // 数据结构:
    // pool: Pubkey (32 bytes)
    // position_nft_mint: Pubkey (32 bytes)
    // amount0_max: u64 (8 bytes)
    // amount1_max: u64 (8 bytes)
    // liquidity: u128 (16 bytes)
    // Total: 96 bytes
    const EVENT_SIZE: usize = 32 + 32 + 8 + 8 + 16;

    if data.len() < EVENT_SIZE {
        return None;
    }

    let event = borsh::from_slice::<RaydiumClmmIncreaseLiquidityEvent>(&data[..EVENT_SIZE]).ok()?;

    Some(DexEvent::RaydiumClmmIncreaseLiquidity(RaydiumClmmIncreaseLiquidityEvent {
        metadata,
        ..event
    }))
}

/// 零拷贝解析器 - IncreaseLiquidity 事件
#[cfg(feature = "parse-zero-copy")]
#[inline(always)]
fn parse_increase_liquidity_inner_zero_copy(
    data: &[u8],
    metadata: EventMetadata,
) -> Option<DexEvent> {
    unsafe {
        if !check_length(data, 32 + 32 + 8 + 8 + 16) {
            return None;
        }

        let mut offset = 0;
        let pool_id = read_pubkey_unchecked(data, offset);
        offset += 32;
        let position = read_pubkey_unchecked(data, offset);
        offset += 32;
        let token_0_amount = read_u64_unchecked(data, offset);
        offset += 8;
        let token_1_amount = read_u64_unchecked(data, offset);
        offset += 8;
        let liquidity = read_u128_unchecked(data, offset);

        Some(DexEvent::RaydiumClmmIncreaseLiquidity(RaydiumClmmIncreaseLiquidityEvent {
            metadata,
            pool: pool_id,
            position_nft_mint: position,
            user: Pubkey::default(),
            liquidity,
            amount0_max: token_0_amount,
            amount1_max: token_1_amount,
        }))
    }
}

// ============================================================================
// DecreaseLiquidity 事件解析器
// ============================================================================

/// 解析 DecreaseLiquidity 事件(统一入口)
#[inline(always)]
fn parse_decrease_liquidity_inner(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    #[cfg(feature = "parse-borsh")]
    {
        parse_decrease_liquidity_inner_borsh(data, metadata)
    }

    #[cfg(feature = "parse-zero-copy")]
    {
        parse_decrease_liquidity_inner_zero_copy(data, metadata)
    }
}

/// Borsh 反序列化解析器 - DecreaseLiquidity 事件
#[cfg(feature = "parse-borsh")]
#[inline(always)]
fn parse_decrease_liquidity_inner_borsh(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    // 数据结构:
    // pool: Pubkey (32 bytes)
    // position_nft_mint: Pubkey (32 bytes)
    // amount0_min: u64 (8 bytes)
    // amount1_min: u64 (8 bytes)
    // liquidity: u128 (16 bytes)
    // Total: 96 bytes
    const EVENT_SIZE: usize = 32 + 32 + 8 + 8 + 16;

    if data.len() < EVENT_SIZE {
        return None;
    }

    let event = borsh::from_slice::<RaydiumClmmDecreaseLiquidityEvent>(&data[..EVENT_SIZE]).ok()?;

    Some(DexEvent::RaydiumClmmDecreaseLiquidity(RaydiumClmmDecreaseLiquidityEvent {
        metadata,
        ..event
    }))
}

/// 零拷贝解析器 - DecreaseLiquidity 事件
#[cfg(feature = "parse-zero-copy")]
#[inline(always)]
fn parse_decrease_liquidity_inner_zero_copy(
    data: &[u8],
    metadata: EventMetadata,
) -> Option<DexEvent> {
    unsafe {
        if !check_length(data, 32 + 32 + 8 + 8 + 16) {
            return None;
        }

        let mut offset = 0;
        let pool_id = read_pubkey_unchecked(data, offset);
        offset += 32;
        let position = read_pubkey_unchecked(data, offset);
        offset += 32;
        let token_0_amount = read_u64_unchecked(data, offset);
        offset += 8;
        let token_1_amount = read_u64_unchecked(data, offset);
        offset += 8;
        let liquidity = read_u128_unchecked(data, offset);

        Some(DexEvent::RaydiumClmmDecreaseLiquidity(RaydiumClmmDecreaseLiquidityEvent {
            metadata,
            pool: pool_id,
            position_nft_mint: position,
            user: Pubkey::default(),
            liquidity,
            amount0_min: token_0_amount,
            amount1_min: token_1_amount,
        }))
    }
}

// ============================================================================
// CreatePool 事件解析器
// ============================================================================

/// 解析 CreatePool 事件(统一入口)
#[inline(always)]
fn parse_create_pool_inner(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    #[cfg(feature = "parse-borsh")]
    {
        parse_create_pool_inner_borsh(data, metadata)
    }

    #[cfg(feature = "parse-zero-copy")]
    {
        parse_create_pool_inner_zero_copy(data, metadata)
    }
}

/// Borsh 反序列化解析器 - CreatePool 事件
#[cfg(feature = "parse-borsh")]
#[inline(always)]
fn parse_create_pool_inner_borsh(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    // 数据结构:
    // pool: Pubkey (32 bytes)
    // token_0_mint: Pubkey (32 bytes)
    // token_1_mint: Pubkey (32 bytes)
    // tick_spacing: u16 (2 bytes)
    // fee_rate: u32 (4 bytes)
    // sqrt_price_x64: u128 (16 bytes)
    // Total: 118 bytes
    const EVENT_SIZE: usize = 32 + 32 + 32 + 2 + 4 + 16;

    if data.len() < EVENT_SIZE {
        return None;
    }

    let event = borsh::from_slice::<RaydiumClmmCreatePoolEvent>(&data[..EVENT_SIZE]).ok()?;

    Some(DexEvent::RaydiumClmmCreatePool(RaydiumClmmCreatePoolEvent { metadata, ..event }))
}

/// 零拷贝解析器 - CreatePool 事件
#[cfg(feature = "parse-zero-copy")]
#[inline(always)]
fn parse_create_pool_inner_zero_copy(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    unsafe {
        if !check_length(data, 32 + 32 + 32 + 2 + 4 + 16) {
            return None;
        }

        let mut offset = 0;
        let pool_id = read_pubkey_unchecked(data, offset);
        offset += 32;
        let token_0_mint = read_pubkey_unchecked(data, offset);
        offset += 32;
        let token_1_mint = read_pubkey_unchecked(data, offset);
        offset += 32;
        let tick_spacing = read_u16_unchecked(data, offset);
        offset += 2;
        let fee_rate = read_u32_unchecked(data, offset);
        offset += 4;
        let sqrt_price_x64 = read_u128_unchecked(data, offset);

        Some(DexEvent::RaydiumClmmCreatePool(RaydiumClmmCreatePoolEvent {
            metadata,
            pool: pool_id,
            token_0_mint,
            token_1_mint,
            tick_spacing,
            fee_rate,
            creator: Pubkey::default(),
            sqrt_price_x64,
            open_time: 0,
        }))
    }
}

// ============================================================================
// CollectFee 事件解析器
// ============================================================================

/// 解析 CollectFee 事件(统一入口)
#[inline(always)]
fn parse_collect_fee_inner(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    #[cfg(feature = "parse-borsh")]
    {
        parse_collect_fee_inner_borsh(data, metadata)
    }

    #[cfg(feature = "parse-zero-copy")]
    {
        parse_collect_fee_inner_zero_copy(data, metadata)
    }
}

/// Borsh 反序列化解析器 - CollectFee 事件
#[cfg(feature = "parse-borsh")]
#[inline(always)]
fn parse_collect_fee_inner_borsh(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    // 数据结构:
    // pool_state: Pubkey (32 bytes)
    // position_nft_mint: Pubkey (32 bytes)
    // amount_0: u64 (8 bytes)
    // amount_1: u64 (8 bytes)
    // Total: 80 bytes
    const EVENT_SIZE: usize = 32 + 32 + 8 + 8;

    if data.len() < EVENT_SIZE {
        return None;
    }

    let event = borsh::from_slice::<RaydiumClmmCollectFeeEvent>(&data[..EVENT_SIZE]).ok()?;

    Some(DexEvent::RaydiumClmmCollectFee(RaydiumClmmCollectFeeEvent { metadata, ..event }))
}

/// 零拷贝解析器 - CollectFee 事件
#[cfg(feature = "parse-zero-copy")]
#[inline(always)]
fn parse_collect_fee_inner_zero_copy(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    unsafe {
        if !check_length(data, 32 + 32 + 8 + 8) {
            return None;
        }

        let mut offset = 0;
        let pool_id = read_pubkey_unchecked(data, offset);
        offset += 32;
        let position = read_pubkey_unchecked(data, offset);
        offset += 32;
        let token_0_fee = read_u64_unchecked(data, offset);
        offset += 8;
        let token_1_fee = read_u64_unchecked(data, offset);

        Some(DexEvent::RaydiumClmmCollectFee(RaydiumClmmCollectFeeEvent {
            metadata,
            pool_state: pool_id,
            position_nft_mint: position,
            amount_0: token_0_fee,
            amount_1: token_1_fee,
        }))
    }
}