solana-streamer-sdk 1.3.0

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
use crate::streaming::event_parser::{
    common::{EventMetadata, EventType},
    protocols::meteora_damm_v2::{
        discriminators, meteora_damm_v2_initialize_pool_event_decode,
        meteora_damm_v2_swap_event_decode, MeteoraDammV2InitializeCustomizablePoolEvent,
        MeteoraDammV2InitializePoolEvent, MeteoraDammV2InitializePoolWithDynamicConfigEvent,
        MeteoraDammV2Swap2Event, MeteoraDammV2SwapEvent,
    },
    DexEvent,
};
use solana_sdk::pubkey::Pubkey;

/// Meteora DAMM v2 程序ID
pub const METEORA_DAMM_V2_PROGRAM_ID: Pubkey =
    solana_sdk::pubkey!("cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG");

/// 解析 Meteora DAMM v2 instruction data
///
/// 根据判别器路由到具体的 instruction 解析函数
pub fn parse_meteora_damm_v2_instruction_data(
    discriminator: &[u8],
    data: &[u8],
    accounts: &[Pubkey],
    metadata: EventMetadata,
) -> Option<DexEvent> {
    match discriminator {
        discriminators::SWAP_IX => parse_swap_instruction(data, accounts, metadata),
        discriminators::SWAP2_IX => parse_swap2_instruction(data, accounts, metadata),
        discriminators::INITIALIZE_POOL_IX => {
            parse_initialize_pool_instruction(data, accounts, metadata)
        }
        discriminators::INITIALIZE_CUSTOMIZABLE_POOL_IX => {
            parse_initialize_customizable_pool_instruction(data, accounts, metadata)
        }
        discriminators::INITIALIZE_POOL_WITH_DYNAMIC_CONFIG_IX => {
            parse_initialize_pool_with_dynamic_config_instruction(data, accounts, metadata)
        }
        _ => None,
    }
}

/// 解析 Meteora DAMM v2 inner instruction data (CPI events)
///
/// 根据判别器路由到具体的 inner instruction 解析函数
pub fn parse_meteora_damm_v2_inner_instruction_data(
    discriminator: &[u8],
    data: &[u8],
    metadata: EventMetadata,
) -> Option<DexEvent> {
    match discriminator {
        discriminators::SWAP_EVENT => parse_swap_inner_instruction(data, metadata),
        discriminators::INITIALIZE_POOL_EVENT => {
            parse_initialize_pool_inner_instruction(data, metadata)
        }
        _ => None,
    }
}

/// 解析 swap 指令
fn parse_swap_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    mut metadata: EventMetadata,
) -> Option<DexEvent> {
    metadata.event_type = EventType::MeteoraDammV2Swap;

    if data.len() < 16 || accounts.len() < 14 {
        return None;
    }

    // 跳过 discriminator (8 bytes)
    let amount_in = u64::from_le_bytes(data[0..8].try_into().unwrap());
    let minimum_amount_out = u64::from_le_bytes(data[8..16].try_into().unwrap());

    Some(DexEvent::MeteoraDammV2SwapEvent(MeteoraDammV2SwapEvent {
        metadata,
        pool_authority: accounts[0],
        pool: accounts[1],
        input_token_account: accounts[2],
        output_token_account: accounts[3],
        token_a_vault: accounts[4],
        token_b_vault: accounts[5],
        token_a_mint: accounts[6],
        token_b_mint: accounts[7],
        payer: accounts[8],
        token_a_program: accounts[9],
        token_b_program: accounts[10],
        referral_token_account: Some(accounts[11]),
        event_authority: accounts[12],
        program: accounts[13],
        amount_0: amount_in,
        amount_1: minimum_amount_out,
        ..Default::default()
    }))
}

/// 解析 swap2 指令
fn parse_swap2_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    mut metadata: EventMetadata,
) -> Option<DexEvent> {
    metadata.event_type = EventType::MeteoraDammV2Swap2;

    if data.len() < 16 || accounts.len() < 13 {
        return None;
    }

    // 跳过 discriminator (8 bytes)
    let amount_0 = u64::from_le_bytes(data[0..8].try_into().unwrap());
    let amount_1 = u64::from_le_bytes(data[8..16].try_into().unwrap());
    let swap_mode = data[16];

    // swap2 可能有 15 个账户(带 referral)或 14 个账户
    let has_referral = accounts.len() >= 15;

    Some(DexEvent::MeteoraDammV2Swap2Event(MeteoraDammV2Swap2Event {
        metadata,
        pool_authority: accounts[0],
        pool: accounts[1],
        input_token_account: accounts[2],
        output_token_account: accounts[3],
        token_a_vault: accounts[4],
        token_b_vault: accounts[5],
        token_a_mint: accounts[6],
        token_b_mint: accounts[7],
        payer: accounts[8],
        token_a_program: accounts[9],
        token_b_program: accounts[10],
        referral_token_account: if has_referral && accounts.len() > 11 {
            Some(accounts[11])
        } else {
            None
        },
        event_authority: accounts[if has_referral { 12 } else { 11 }],
        program: accounts[if has_referral { 13 } else { 12 }],
        sysvar: accounts[if has_referral { 14 } else { 13 }],
        amount_0,
        amount_1,
        swap_mode,
        has_referral,
        ..Default::default()
    }))
}

/// 解析 initialize_pool 指令
fn parse_initialize_pool_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    mut metadata: EventMetadata,
) -> Option<DexEvent> {
    metadata.event_type = EventType::MeteoraDammV2InitializePool;

    if accounts.len() < 20 {
        return None;
    }

    // 解析 instruction data (不包含 discriminator,已被调用者移除)
    // 结构: liquidity (u128 = 16 bytes) + sqrt_price (u128 = 16 bytes) + activation_point (Option<u64> = 1 + 8 bytes)
    if data.len() < 33 {
        return None;
    }

    let mut offset = 0;

    // 读取 liquidity (u128)
    let liquidity = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 sqrt_price (u128)
    let sqrt_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 activation_point (Option<u64>)
    let option_tag = data[offset];
    offset += 1;
    let _activation_point = if option_tag == 1 && data.len() >= offset + 8 {
        Some(u64::from_le_bytes(data[offset..offset + 8].try_into().ok()?))
    } else {
        None
    };

    Some(DexEvent::MeteoraDammV2InitializePoolEvent(MeteoraDammV2InitializePoolEvent {
        metadata,
        creator: accounts[0],
        position_nft_mint: accounts[1],
        position_nft_account: accounts[2],
        payer: accounts[3],
        config: accounts[4],
        pool_authority: accounts[5],
        pool: accounts[6],
        position: accounts[7],
        token_a_mint: accounts[8],
        token_b_mint: accounts[9],
        token_a_vault: accounts[10],
        token_b_vault: accounts[11],
        payer_token_a: accounts[12],
        payer_token_b: accounts[13],
        token_a_program: accounts[14],
        token_b_program: accounts[15],
        event_authority: accounts[18],
        program: accounts[19],
        remaining_accounts: accounts[20..].to_vec(),
        liquidity,
        sqrt_price,
        ..Default::default()
    }))
}

/// 解析 initialize_customizable_pool 指令
fn parse_initialize_customizable_pool_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    mut metadata: EventMetadata,
) -> Option<DexEvent> {
    metadata.event_type = EventType::MeteoraDammV2InitializeCustomizablePool;

    if accounts.len() < 19 {
        return None;
    }

    // 解析 instruction data (不包含 discriminator)
    // 结构: PoolFeeParameters + sqrt_min_price + sqrt_max_price + has_alpha_vault + liquidity + sqrt_price + activation_type + collect_fee_mode + activation_point
    if data.len() < 99 {
        return None;
    }

    let mut offset = 0;

    // 解析 PoolFeeParameters
    use crate::streaming::event_parser::protocols::meteora_damm_v2::PoolFeeParameters;
    use borsh::BorshDeserialize;

    // PoolFeeParameters size: 8 + 2 + 8 + 8 + 1 + 3 + 1 + (optional DynamicFee)
    // 先读取前 31 bytes (不包含 dynamic_fee option tag)
    let pool_fees = PoolFeeParameters::deserialize(&mut &data[offset..]).ok()?;

    // 计算 pool_fees 消耗的字节数
    // BaseFee: 8 + 2 + 8 + 8 + 1 = 27 bytes
    // padding: 3 bytes
    // option tag: 1 byte
    // 如果 dynamic_fee 存在: 2 + 16 + 2 + 2 + 2 + 4 + 4 = 32 bytes
    let pool_fees_size = 31 + if pool_fees.dynamic_fee.is_some() { 32 } else { 0 };
    offset += pool_fees_size;

    // 读取 sqrt_min_price (u128)
    let sqrt_min_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 sqrt_max_price (u128)
    let sqrt_max_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 has_alpha_vault (bool)
    let _has_alpha_vault = data[offset];
    offset += 1;

    // 读取 liquidity (u128)
    let liquidity = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 sqrt_price (u128)
    let sqrt_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 activation_type (u8)
    let activation_type = data[offset];
    offset += 1;

    // 读取 collect_fee_mode (u8)
    let collect_fee_mode = data[offset];
    offset += 1;

    // 读取 activation_point (Option<u64>)
    let option_tag = data[offset];
    let _activation_point = if option_tag == 1 && data.len() >= offset + 9 {
        Some(u64::from_le_bytes(data[offset + 1..offset + 9].try_into().ok()?))
    } else {
        None
    };

    Some(DexEvent::MeteoraDammV2InitializeCustomizablePoolEvent(
        MeteoraDammV2InitializeCustomizablePoolEvent {
            metadata,
            creator: accounts[0],
            position_nft_mint: accounts[1],
            position_nft_account: accounts[2],
            payer: accounts[3],
            pool_authority: accounts[4],
            pool: accounts[5],
            position: accounts[6],
            token_a_mint: accounts[7],
            token_b_mint: accounts[8],
            token_a_vault: accounts[9],
            token_b_vault: accounts[10],
            payer_token_a: accounts[11],
            payer_token_b: accounts[12],
            token_a_program: accounts[13],
            token_b_program: accounts[14],
            token_2022_program: accounts[15],
            system_program: accounts[16],
            event_authority: accounts[17],
            program: accounts[18],
            remaining_accounts: accounts[19..].to_vec(),
            pool_fees,
            sqrt_min_price,
            sqrt_max_price,
            activation_type,
            collect_fee_mode,
            liquidity,
            sqrt_price,
            ..Default::default()
        },
    ))
}

/// 解析 initialize_pool_with_dynamic_config 指令
fn parse_initialize_pool_with_dynamic_config_instruction(
    data: &[u8],
    accounts: &[Pubkey],
    mut metadata: EventMetadata,
) -> Option<DexEvent> {
    metadata.event_type = EventType::MeteoraDammV2InitializePoolWithDynamicConfig;

    if accounts.len() < 21 {
        return None;
    }

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

    let mut offset = 0;

    // 解析 PoolFeeParameters
    use crate::streaming::event_parser::protocols::meteora_damm_v2::PoolFeeParameters;
    use borsh::BorshDeserialize;

    let pool_fees = PoolFeeParameters::deserialize(&mut &data[offset..]).ok()?;

    // 计算 pool_fees 消耗的字节数
    // BaseFee: 8 + 2 + 8 + 8 + 1 = 27 bytes
    // padding: 3 bytes
    // option tag: 1 byte
    // 如果 dynamic_fee 存在: 2 + 16 + 2 + 2 + 2 + 4 + 4 = 32 bytes
    let pool_fees_size = 31 + if pool_fees.dynamic_fee.is_some() { 32 } else { 0 };
    offset += pool_fees_size;

    // 读取 sqrt_min_price (u128)
    let sqrt_min_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 sqrt_max_price (u128)
    let sqrt_max_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 has_alpha_vault (bool)
    let _has_alpha_vault = data[offset];
    offset += 1;

    // 读取 liquidity (u128)
    let liquidity = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 sqrt_price (u128)
    let sqrt_price = u128::from_le_bytes(data[offset..offset + 16].try_into().ok()?);
    offset += 16;

    // 读取 activation_type (u8)
    let activation_type = data[offset];
    offset += 1;

    // 读取 collect_fee_mode (u8)
    let collect_fee_mode = data[offset];
    offset += 1;

    // 读取 activation_point (Option<u64>)
    let option_tag = data[offset];
    let _activation_point = if option_tag == 1 && data.len() >= offset + 9 {
        Some(u64::from_le_bytes(data[offset + 1..offset + 9].try_into().ok()?))
    } else {
        None
    };

    Some(DexEvent::MeteoraDammV2InitializePoolWithDynamicConfigEvent(
        MeteoraDammV2InitializePoolWithDynamicConfigEvent {
            metadata,
            creator: accounts[0],
            position_nft_mint: accounts[1],
            position_nft_account: accounts[2],
            payer: accounts[3],
            pool_creator_authority: accounts[4],
            pool_authority: accounts[6],
            pool: accounts[7],
            position: accounts[8],
            token_a_mint: accounts[9],
            token_b_mint: accounts[10],
            token_a_vault: accounts[11],
            token_b_vault: accounts[12],
            payer_token_a: accounts[13],
            payer_token_b: accounts[14],
            token_a_program: accounts[15],
            token_b_program: accounts[16],
            token_2022_program: accounts[17],
            system_program: accounts[18],
            event_authority: accounts[19],
            program: accounts[20],
            config: accounts[5],
            pool_fees,
            sqrt_min_price,
            sqrt_max_price,
            activation_type,
            collect_fee_mode,
            liquidity,
            sqrt_price,
            ..Default::default()
        },
    ))
}

/// 解析 swap inner instruction (CPI event)
fn parse_swap_inner_instruction(data: &[u8], metadata: EventMetadata) -> Option<DexEvent> {
    if let Some(event) = meteora_damm_v2_swap_event_decode(data) {
        Some(DexEvent::MeteoraDammV2SwapEvent(MeteoraDammV2SwapEvent { metadata, ..event }))
    } else {
        None
    }
}

/// 解析 initialize pool inner instruction (CPI event)
fn parse_initialize_pool_inner_instruction(
    data: &[u8],
    mut metadata: EventMetadata,
) -> Option<DexEvent> {
    metadata.event_type = EventType::MeteoraDammV2InitializePool;
    if let Some(event) = meteora_damm_v2_initialize_pool_event_decode(data) {
        Some(DexEvent::MeteoraDammV2InitializePoolEvent(MeteoraDammV2InitializePoolEvent {
            metadata,
            ..event
        }))
    } else {
        None
    }
}