solana-streamer-sdk 0.1.6

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
use prost_types::Timestamp;
use solana_sdk::{instruction::CompiledInstruction, pubkey::Pubkey};
use solana_transaction_status::UiCompiledInstruction;

use crate::streaming::event_parser::{
    common::{utils::*, EventMetadata, EventType, ProtocolType},
    core::traits::{EventParser, GenericEventParseConfig, GenericEventParser, UnifiedEvent},
    protocols::bonk::{
        discriminators, BonkPoolCreateEvent, BonkTradeEvent, ConstantCurve, CurveParams,
        FixedCurve, LinearCurve, MintParams, TradeDirection, VestingParams,
    },
};

/// Bonk程序ID
pub const BONK_PROGRAM_ID: Pubkey =
    solana_sdk::pubkey!("LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj");

/// Bonk事件解析器
pub struct BonkEventParser {
    inner: GenericEventParser,
}

impl BonkEventParser {
    pub fn new() -> Self {
        // 配置所有事件类型
        let configs = vec![
            GenericEventParseConfig {
                inner_instruction_discriminator: discriminators::TRADE_EVENT,
                instruction_discriminator: discriminators::BUY_EXACT_IN,
                event_type: EventType::BonkBuyExactIn,
                inner_instruction_parser: Self::parse_trade_inner_instruction,
                instruction_parser: Self::parse_buy_exact_in_instruction,
            },
            GenericEventParseConfig {
                inner_instruction_discriminator: discriminators::TRADE_EVENT,
                instruction_discriminator: discriminators::BUY_EXACT_OUT,
                event_type: EventType::BonkBuyExactOut,
                inner_instruction_parser: Self::parse_trade_inner_instruction,
                instruction_parser: Self::parse_buy_exact_out_instruction,
            },
            GenericEventParseConfig {
                inner_instruction_discriminator: discriminators::TRADE_EVENT,
                instruction_discriminator: discriminators::SELL_EXACT_IN,
                event_type: EventType::BonkSellExactIn,
                inner_instruction_parser: Self::parse_trade_inner_instruction,
                instruction_parser: Self::parse_sell_exact_in_instruction,
            },
            GenericEventParseConfig {
                inner_instruction_discriminator: discriminators::TRADE_EVENT,
                instruction_discriminator: discriminators::SELL_EXACT_OUT,
                event_type: EventType::BonkSellExactOut,
                inner_instruction_parser: Self::parse_trade_inner_instruction,
                instruction_parser: Self::parse_sell_exact_out_instruction,
            },
            GenericEventParseConfig {
                inner_instruction_discriminator: discriminators::POOL_CREATE_EVENT,
                instruction_discriminator: discriminators::INITIALIZE,
                event_type: EventType::BonkInitialize,
                inner_instruction_parser: Self::parse_pool_create_inner_instruction,
                instruction_parser: Self::parse_initialize_instruction,
            },
        ];

        let inner = GenericEventParser::new(BONK_PROGRAM_ID, ProtocolType::Bonk, configs);

        Self { inner }
    }

    /// 解析创建池事件
    fn parse_pool_create_inner_instruction(
        data: &[u8],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if let Ok(event) = borsh::from_slice::<BonkPoolCreateEvent>(data) {
            let mut metadata = metadata;
            metadata.set_id(format!("{}", metadata.signature,));
            Some(Box::new(BonkPoolCreateEvent {
                metadata: metadata,
                ..event
            }))
        } else {
            None
        }
    }

    /// 解析交易事件
    fn parse_trade_inner_instruction(
        data: &[u8],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if let Ok(event) = borsh::from_slice::<BonkTradeEvent>(data) {
            let mut metadata = metadata;
            metadata.set_id(format!(
                "{}-{}",
                metadata.signature,
                event.pool_state.to_string()
            ));
            if metadata.event_type == EventType::BonkBuyExactIn
                || metadata.event_type == EventType::BonkBuyExactOut
            {
                if event.trade_direction != TradeDirection::Buy {
                    return None;
                }
            } else if metadata.event_type == EventType::BonkSellExactIn
                || metadata.event_type == EventType::BonkSellExactOut
            {
                if event.trade_direction != TradeDirection::Sell {
                    return None;
                }
            }
            Some(Box::new(BonkTradeEvent {
                metadata: metadata,
                ..event
            }))
        } else {
            None
        }
    }

    /// 解析买入指令事件
    fn parse_buy_exact_in_instruction(
        data: &[u8],
        accounts: &[Pubkey],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if data.len() < 16 || accounts.len() < 11 {
            return None;
        }

        let amount_in = read_u64_le(data, 0)?;
        let minimum_amount_out = read_u64_le(data, 8)?;
        let share_fee_rate = read_u64_le(data, 16)?;

        let mut metadata = metadata;
        metadata.set_id(format!("{}-{}", metadata.signature, accounts[4]));

        Some(Box::new(BonkTradeEvent {
            metadata,
            amount_in,
            minimum_amount_out,
            share_fee_rate,
            payer: accounts[0],
            pool_state: accounts[4],
            user_base_token: accounts[5],
            user_quote_token: accounts[6],
            base_vault: accounts[7],
            quote_vault: accounts[8],
            base_token_mint: accounts[9],
            quote_token_mint: accounts[10],
            trade_direction: TradeDirection::Buy,
            ..Default::default()
        }))
    }

    fn parse_buy_exact_out_instruction(
        data: &[u8],
        accounts: &[Pubkey],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if data.len() < 16 || accounts.len() < 11 {
            return None;
        }

        let amount_out = read_u64_le(data, 0)?;
        let maximum_amount_in = read_u64_le(data, 8)?;
        let share_fee_rate = read_u64_le(data, 16)?;

        let mut metadata = metadata;
        metadata.set_id(format!("{}-{}", metadata.signature, accounts[4]));

        Some(Box::new(BonkTradeEvent {
            metadata,
            amount_out,
            maximum_amount_in,
            share_fee_rate,
            payer: accounts[0],
            pool_state: accounts[4],
            user_base_token: accounts[5],
            user_quote_token: accounts[6],
            base_vault: accounts[7],
            quote_vault: accounts[8],
            base_token_mint: accounts[9],
            quote_token_mint: accounts[10],
            trade_direction: TradeDirection::Buy,
            ..Default::default()
        }))
    }

    fn parse_sell_exact_in_instruction(
        data: &[u8],
        accounts: &[Pubkey],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if data.len() < 16 || accounts.len() < 11 {
            return None;
        }

        let amount_in = read_u64_le(data, 0)?;
        let minimum_amount_out = read_u64_le(data, 8)?;
        let share_fee_rate = read_u64_le(data, 16)?;

        let mut metadata = metadata;
        metadata.set_id(format!("{}-{}", metadata.signature, accounts[4]));

        Some(Box::new(BonkTradeEvent {
            metadata,
            amount_in,
            minimum_amount_out,
            share_fee_rate,
            payer: accounts[0],
            pool_state: accounts[4],
            user_base_token: accounts[5],
            user_quote_token: accounts[6],
            base_vault: accounts[7],
            quote_vault: accounts[8],
            base_token_mint: accounts[9],
            quote_token_mint: accounts[10],
            trade_direction: TradeDirection::Sell,
            ..Default::default()
        }))
    }

    fn parse_sell_exact_out_instruction(
        data: &[u8],
        accounts: &[Pubkey],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if data.len() < 16 || accounts.len() < 11 {
            return None;
        }

        let amount_out = read_u64_le(data, 0)?;
        let maximum_amount_in = read_u64_le(data, 8)?;
        let share_fee_rate = read_u64_le(data, 16)?;

        let mut metadata = metadata;
        metadata.set_id(format!("{}-{}", metadata.signature, accounts[4]));

        Some(Box::new(BonkTradeEvent {
            metadata,
            amount_out,
            maximum_amount_in,
            share_fee_rate,
            payer: accounts[0],
            pool_state: accounts[4],
            user_base_token: accounts[5],
            user_quote_token: accounts[6],
            base_vault: accounts[7],
            quote_vault: accounts[8],
            base_token_mint: accounts[9],
            quote_token_mint: accounts[10],
            trade_direction: TradeDirection::Sell,
            ..Default::default()
        }))
    }

    /// 解析初始化事件
    fn parse_initialize_instruction(
        data: &[u8],
        accounts: &[Pubkey],
        metadata: EventMetadata,
    ) -> Option<Box<dyn UnifiedEvent>> {
        if data.len() < 24 {
            return None;
        }

        let mut offset = 0;
        let base_mint_param = Self::parse_mint_params(data, &mut offset)?;
        let curve_param = Self::parse_curve_params(data, &mut offset)?;
        let vesting_param = Self::parse_vesting_params(data, &mut offset)?;

        let mut metadata = metadata;
        metadata.set_id(format!("{}", metadata.signature));

        Some(Box::new(BonkPoolCreateEvent {
            metadata,
            payer: accounts[0],
            creator: accounts[1],
            global_config: accounts[2],
            platform_config: accounts[3],
            pool_state: accounts[5],
            base_mint: accounts[6],
            quote_mint: accounts[7],
            base_vault: accounts[8],
            quote_vault: accounts[9],
            base_mint_param,
            curve_param,
            vesting_param,
            ..Default::default()
        }))
    }

    /// 解析 MintParams 结构
    fn parse_mint_params(data: &[u8], offset: &mut usize) -> Option<MintParams> {
        // 读取decimals (1字节)
        let decimals = read_u8(data, *offset)?;
        *offset += 1;

        // 读取name字符串长度和内容
        let name_len = read_u32_le(data, *offset)? as usize;
        *offset += 4;
        if data.len() < *offset + name_len {
            return None;
        }
        let name = String::from_utf8(data[*offset..*offset + name_len].to_vec()).ok()?;
        *offset += name_len;

        // 读取symbol字符串长度和内容
        let symbol_len = read_u32_le(data, *offset)? as usize;
        *offset += 4;
        if data.len() < *offset + symbol_len {
            return None;
        }
        let symbol = String::from_utf8(data[*offset..*offset + symbol_len].to_vec()).ok()?;
        *offset += symbol_len;

        // 读取uri字符串长度和内容
        let uri_len = read_u32_le(data, *offset)? as usize;
        *offset += 4;
        if data.len() < *offset + uri_len {
            return None;
        }
        let uri = String::from_utf8(data[*offset..*offset + uri_len].to_vec()).ok()?;
        *offset += uri_len;

        Some(MintParams {
            decimals,
            name,
            symbol,
            uri,
        })
    }

    /// 解析 CurveParams 结构
    fn parse_curve_params(data: &[u8], offset: &mut usize) -> Option<CurveParams> {
        // 读取curve类型标识符 (1字节)
        let curve_type = read_u8(data, *offset)?;
        *offset += 1;

        match curve_type {
            0 => {
                // Constant curve
                let supply = read_u64_le(data, *offset)?;
                *offset += 8;
                let total_base_sell = read_u64_le(data, *offset)?;
                *offset += 8;
                let total_quote_fund_raising = read_u64_le(data, *offset)?;
                *offset += 8;
                let migrate_type = read_u8(data, *offset)?;
                *offset += 1;

                Some(CurveParams::Constant {
                    data: ConstantCurve {
                        supply,
                        total_base_sell,
                        total_quote_fund_raising,
                        migrate_type,
                    },
                })
            }
            1 => {
                // Fixed curve
                let supply = read_u64_le(data, *offset)?;
                *offset += 8;
                let total_quote_fund_raising = read_u64_le(data, *offset)?;
                *offset += 8;
                let migrate_type = read_u8(data, *offset)?;
                *offset += 1;

                Some(CurveParams::Fixed {
                    data: FixedCurve {
                        supply,
                        total_quote_fund_raising,
                        migrate_type,
                    },
                })
            }
            2 => {
                // Linear curve
                let supply = read_u64_le(data, *offset)?;
                *offset += 8;
                let total_quote_fund_raising = read_u64_le(data, *offset)?;
                *offset += 8;
                let migrate_type = read_u8(data, *offset)?;
                *offset += 1;

                Some(CurveParams::Linear {
                    data: LinearCurve {
                        supply,
                        total_quote_fund_raising,
                        migrate_type,
                    },
                })
            }
            _ => None,
        }
    }

    /// 解析 VestingParams 结构
    fn parse_vesting_params(data: &[u8], offset: &mut usize) -> Option<VestingParams> {
        let total_locked_amount = read_u64_le(data, *offset)?;
        *offset += 8;
        let cliff_period = read_u64_le(data, *offset)?;
        *offset += 8;
        let unlock_period = read_u64_le(data, *offset)?;
        *offset += 8;

        Some(VestingParams {
            total_locked_amount,
            cliff_period,
            unlock_period,
        })
    }
}

#[async_trait::async_trait]
impl EventParser for BonkEventParser {
    fn parse_events_from_inner_instruction(
        &self,
        inner_instruction: &UiCompiledInstruction,
        signature: &str,
        slot: u64,
        block_time: Option<Timestamp>,
        program_received_time_ms: i64,
        index: String,
    ) -> Vec<Box<dyn UnifiedEvent>> {
        self.inner.parse_events_from_inner_instruction(
            inner_instruction,
            signature,
            slot,
            block_time,
            program_received_time_ms,
            index,
        )
    }

    fn parse_events_from_instruction(
        &self,
        instruction: &CompiledInstruction,
        accounts: &[Pubkey],
        signature: &str,
        slot: u64,
        block_time: Option<Timestamp>,
        program_received_time_ms: i64,
        index: String,
    ) -> Vec<Box<dyn UnifiedEvent>> {
        self.inner.parse_events_from_instruction(
            instruction,
            accounts,
            signature,
            slot,
            block_time,
            program_received_time_ms,
            index,
        )
    }

    fn should_handle(&self, program_id: &Pubkey) -> bool {
        self.inner.should_handle(program_id)
    }

    fn supported_program_ids(&self) -> Vec<Pubkey> {
        self.inner.supported_program_ids()
    }
}