sol-parser-sdk 0.7.4

A lightweight Rust library for real-time event streaming from Solana DEX trading programs. Supports PumpFun, PumpSwap, LaunchLab, 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
//! 指令解析通用工具函数

use crate::core::events::EventMetadata;
use solana_sdk::{pubkey::Pubkey, signature::Signature};
use yellowstone_grpc_proto::prelude::{Transaction, TransactionStatusMeta};

/// 创建事件元数据的通用函数
pub fn create_metadata(
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: i64,
    grpc_recv_us: i64,
) -> EventMetadata {
    EventMetadata { signature, slot, tx_index, block_time_us, grpc_recv_us, recent_blockhash: None }
}

/// 创建事件元数据的兼容性函数(用于指令解析)
#[inline(always)]
pub fn create_metadata_simple(
    signature: Signature,
    slot: u64,
    tx_index: u64,
    block_time_us: Option<i64>,
    _program_id: Pubkey,
) -> EventMetadata {
    let current_time = now_us();

    EventMetadata {
        signature,
        slot,
        tx_index,
        block_time_us: block_time_us.unwrap_or(0),
        grpc_recv_us: current_time,
        recent_blockhash: None,
    }
}

/// 从指令数据中读取 u64(小端序)- SIMD 优化
#[inline(always)]
pub fn read_u64_le(data: &[u8], offset: usize) -> Option<u64> {
    data.get(offset..offset + 8).map(|slice| u64::from_le_bytes(slice.try_into().unwrap()))
}

/// 从指令数据中读取 u32(小端序)- SIMD 优化
#[inline(always)]
pub fn read_u32_le(data: &[u8], offset: usize) -> Option<u32> {
    data.get(offset..offset + 4).map(|slice| u32::from_le_bytes(slice.try_into().unwrap()))
}

/// Read a little-endian `i64` from instruction data.
#[inline(always)]
pub fn read_i64_le(data: &[u8], offset: usize) -> Option<i64> {
    data.get(offset..offset + 8).map(|slice| i64::from_le_bytes(slice.try_into().unwrap()))
}

/// 从指令数据中读取 u16(小端序)- SIMD 优化
#[inline(always)]
pub fn read_u16_le(data: &[u8], offset: usize) -> Option<u16> {
    data.get(offset..offset + 2).map(|slice| u16::from_le_bytes(slice.try_into().unwrap()))
}

/// 从指令数据中读取 u8
#[inline(always)]
pub fn read_u8(data: &[u8], offset: usize) -> Option<u8> {
    data.get(offset).copied()
}

/// 从指令数据中读取 i32(小端序)- SIMD 优化
#[inline(always)]
pub fn read_i32_le(data: &[u8], offset: usize) -> Option<i32> {
    data.get(offset..offset + 4).map(|slice| i32::from_le_bytes(slice.try_into().unwrap()))
}

/// 从指令数据中读取 u128(小端序)- SIMD 优化
#[inline(always)]
pub fn read_u128_le(data: &[u8], offset: usize) -> Option<u128> {
    data.get(offset..offset + 16).map(|slice| u128::from_le_bytes(slice.try_into().unwrap()))
}

/// 从指令数据中读取布尔值
#[inline(always)]
pub fn read_bool(data: &[u8], offset: usize) -> Option<bool> {
    data.get(offset).map(|&b| b != 0)
}

/// IDL 自定义类型 `OptionBool`(Anchor:`struct { bool }`)在 **指令参数** 中与 `bool` 相同,Borsh 仅占 **1 字节**。
/// 勿与 Rust `Option<bool>` 的 Borsh 编码(discriminator + inner,共 2 字节)混淆。
#[inline(always)]
pub fn read_option_bool_idl(data: &[u8], offset: usize) -> Option<bool> {
    match data.get(offset).copied()? {
        0 => Some(false),
        1 => Some(true),
        _ => None,
    }
}

/// IDL custom type `OptionU64` is a one-field struct and uses the same 8-byte
/// little-endian representation as `u64` when present.
#[inline(always)]
pub fn read_option_u64_idl(data: &[u8], offset: usize) -> Option<u64> {
    read_u64_le(data, offset)
}

/// 从指令数据中读取公钥 - SIMD 优化
#[inline(always)]
pub fn read_pubkey(data: &[u8], offset: usize) -> Option<Pubkey> {
    data.get(offset..offset + 32).and_then(|slice| Pubkey::try_from(slice).ok())
}

/// 从账户列表中获取账户
#[inline(always)]
pub fn get_account(accounts: &[Pubkey], index: usize) -> Option<Pubkey> {
    accounts.get(index).copied()
}

/// 计算滑点基点
pub fn calculate_slippage_bps(amount_in: u64, amount_out_min: u64) -> u16 {
    if amount_in == 0 {
        return 0;
    }

    // 简化的滑点计算
    let slippage = ((amount_in.saturating_sub(amount_out_min)) * 10000) / amount_in;
    slippage.min(10000) as u16
}

/// 计算价格影响基点
pub fn calculate_price_impact_bps(_amount_in: u64, amount_out: u64, expected_out: u64) -> u16 {
    if expected_out == 0 {
        return 0;
    }

    let impact = ((expected_out.saturating_sub(amount_out)) * 10000) / expected_out;
    impact.min(10000) as u16
}

/// Read bytes from instruction data
pub fn read_bytes(data: &[u8], offset: usize, length: usize) -> Option<&[u8]> {
    if data.len() < offset + length {
        return None;
    }
    Some(&data[offset..offset + length])
}

/// `create_v2` instruction payload without the discriminator. All fields after
/// `is_mayhem_mode` are trailing and optional for backward compatibility.
/// `mint` / `bonding_curve` / `user` 在账户里,不在 data 中。
#[inline]
pub fn parse_create_v2_tail_fields(
    data_after_discriminator: &[u8],
) -> Option<(Pubkey, bool, bool, u64, bool)> {
    let mut offset = 0usize;
    let (_, l) = read_str_unchecked(data_after_discriminator, offset)?;
    offset += l;
    let (_, l) = read_str_unchecked(data_after_discriminator, offset)?;
    offset += l;
    let (_, l) = read_str_unchecked(data_after_discriminator, offset)?;
    offset += l;
    if data_after_discriminator.len() < offset + 32 + 1 {
        return None;
    }
    let creator = read_pubkey(data_after_discriminator, offset)?;
    offset += 32;
    let is_mayhem_mode = read_bool(data_after_discriminator, offset)?;
    offset += 1;
    let is_cashback_enabled = if offset < data_after_discriminator.len() {
        read_option_bool_idl(data_after_discriminator, offset).unwrap_or(false)
    } else {
        false
    };
    if offset < data_after_discriminator.len() {
        offset += 1;
    }
    let creator_fee_bps = read_option_u64_idl(data_after_discriminator, offset).unwrap_or_default();
    if offset + 8 <= data_after_discriminator.len() {
        offset += 8;
    }
    let is_holder_reward =
        read_option_bool_idl(data_after_discriminator, offset).unwrap_or_default();
    Some((creator, is_mayhem_mode, is_cashback_enabled, creator_fee_bps, is_holder_reward))
}

/// Read string with 4-byte length prefix (Borsh format)
/// Returns (string slice, total bytes consumed including length prefix)
#[inline]
pub fn read_str_unchecked(data: &[u8], offset: usize) -> Option<(&str, usize)> {
    if data.len() < offset + 4 {
        return None;
    }
    let len = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?) as usize;
    if data.len() < offset + 4 + len {
        return None;
    }
    let string_bytes = &data[offset + 4..offset + 4 + len];
    let s = std::str::from_utf8(string_bytes).ok()?;
    Some((s, 4 + len))
}

/// 从指令数据中读取u64向量(简化版本)
pub fn read_vec_u64(_data: &[u8], _offset: usize) -> Option<Vec<u64>> {
    // 简化版本:返回默认的两个元素向量
    // 实际实现需要根据具体的数据格式来解析
    Some(vec![0, 0])
}

/// 快速读取 Pubkey(从字节数组)
#[inline(always)]
pub fn read_pubkey_fast(bytes: &[u8]) -> Pubkey {
    crate::logs::utils::read_pubkey(bytes, 0).unwrap_or_default()
}

/// 获取指令账户访问器
/// 返回一个可以通过索引获取 Pubkey 的闭包
pub fn get_instruction_account_getter<'a>(
    meta: &'a TransactionStatusMeta,
    transaction: &'a Option<Transaction>,
    account_keys: Option<&'a Vec<Vec<u8>>>,
    // 地址表
    loaded_writable_addresses: &'a [Vec<u8>],
    loaded_readonly_addresses: &'a [Vec<u8>],
    index: &(i32, i32), // (outer_index, inner_index)
) -> Option<impl Fn(usize) -> Pubkey + 'a> {
    // 1. 获取指令的账户索引数组
    let accounts = if index.1 >= 0 {
        // 内层指令 - 使用二分查找优化 (inner_instructions 按 index 升序排列)
        let outer_idx = index.0 as u32;
        meta.inner_instructions
            .binary_search_by_key(&outer_idx, |i| i.index)
            .ok()
            .and_then(|pos| meta.inner_instructions.get(pos))
            .or_else(|| {
                // 回退到线性查找(以防数据未排序)
                meta.inner_instructions.iter().find(|i| i.index == outer_idx)
            })?
            .instructions
            .get(index.1 as usize)?
            .accounts
            .as_slice()
    } else {
        // 外层指令
        transaction
            .as_ref()?
            .message
            .as_ref()?
            .instructions
            .get(index.0 as usize)?
            .accounts
            .as_slice()
    };

    // 2. 创建高性能的账户查找闭包
    Some(move |acc_index: usize| -> Pubkey {
        // 获取账户在交易中的索引
        let account_index = match accounts.get(acc_index) {
            Some(&idx) => idx as usize,
            None => return Pubkey::default(),
        };
        // 早期返回优化
        let Some(keys) = account_keys else {
            return Pubkey::default();
        };
        // 主账户列表
        if let Some(key_bytes) = keys.get(account_index) {
            return read_pubkey_fast(key_bytes);
        }
        // 可写地址
        let writable_offset = account_index.saturating_sub(keys.len());
        if let Some(key_bytes) = loaded_writable_addresses.get(writable_offset) {
            return read_pubkey_fast(key_bytes);
        }
        // 只读地址
        let readonly_offset = writable_offset.saturating_sub(loaded_writable_addresses.len());
        if let Some(key_bytes) = loaded_readonly_addresses.get(readonly_offset) {
            return read_pubkey_fast(key_bytes);
        }
        Pubkey::default()
    })
}

use crate::core::clock::now_us;
/// 预构建的 inner_instructions 索引,用于 O(1) 查找
use std::collections::HashMap;

/// InnerInstructions 索引缓存
pub struct InnerInstructionsIndex<'a> {
    /// outer_index -> &InnerInstructions
    index_map: HashMap<u32, &'a yellowstone_grpc_proto::prelude::InnerInstructions>,
}

impl<'a> InnerInstructionsIndex<'a> {
    /// 从 TransactionStatusMeta 构建索引
    #[inline]
    pub fn new(meta: &'a TransactionStatusMeta) -> Self {
        let mut index_map = HashMap::with_capacity(meta.inner_instructions.len());
        for inner in &meta.inner_instructions {
            index_map.insert(inner.index, inner);
        }
        Self { index_map }
    }

    /// O(1) 查找 inner_instructions
    #[inline]
    pub fn get(
        &self,
        outer_index: u32,
    ) -> Option<&'a yellowstone_grpc_proto::prelude::InnerInstructions> {
        self.index_map.get(&outer_index).copied()
    }
}

/// 使用预构建索引的账户获取器(O(1) 查找)
pub fn get_instruction_account_getter_indexed<'a>(
    inner_index: &InnerInstructionsIndex<'a>,
    transaction: &'a Option<Transaction>,
    account_keys: Option<&'a Vec<Vec<u8>>>,
    loaded_writable_addresses: &'a [Vec<u8>],
    loaded_readonly_addresses: &'a [Vec<u8>],
    index: &(i32, i32),
) -> Option<impl Fn(usize) -> Pubkey + 'a> {
    let accounts = if index.1 >= 0 {
        // O(1) 查找
        inner_index.get(index.0 as u32)?.instructions.get(index.1 as usize)?.accounts.as_slice()
    } else {
        transaction
            .as_ref()?
            .message
            .as_ref()?
            .instructions
            .get(index.0 as usize)?
            .accounts
            .as_slice()
    };

    Some(move |acc_index: usize| -> Pubkey {
        let account_index = match accounts.get(acc_index) {
            Some(&idx) => idx as usize,
            None => return Pubkey::default(),
        };
        let Some(keys) = account_keys else {
            return Pubkey::default();
        };
        if let Some(key_bytes) = keys.get(account_index) {
            return read_pubkey_fast(key_bytes);
        }
        let writable_offset = account_index.saturating_sub(keys.len());
        if let Some(key_bytes) = loaded_writable_addresses.get(writable_offset) {
            return read_pubkey_fast(key_bytes);
        }
        let readonly_offset = writable_offset.saturating_sub(loaded_writable_addresses.len());
        if let Some(key_bytes) = loaded_readonly_addresses.get(readonly_offset) {
            return read_pubkey_fast(key_bytes);
        }
        Pubkey::default()
    })
}

#[cfg(test)]
mod option_bool_tests {
    use super::*;

    #[test]
    fn read_option_bool_idl_strict() {
        assert_eq!(read_option_bool_idl(&[0], 0), Some(false));
        assert_eq!(read_option_bool_idl(&[1], 0), Some(true));
        assert_eq!(read_option_bool_idl(&[2], 0), None);
    }

    #[test]
    fn parse_create_v2_tail_matches_anchor_len() {
        // name "a", "b", "c" + creator (32) + mayhem (1) + OptionBool cashback (1) = 49 bytes payload
        let mut p = Vec::new();
        p.extend_from_slice(&(1u32.to_le_bytes()));
        p.push(b'a');
        p.extend_from_slice(&(1u32.to_le_bytes()));
        p.push(b'b');
        p.extend_from_slice(&(1u32.to_le_bytes()));
        p.push(b'c');
        p.extend_from_slice(&[0u8; 32]);
        p.push(1u8); // mayhem
        p.push(1u8); // cashback
        assert_eq!(p.len(), 49);
        let (creator, mayhem, cb, creator_fee_bps, holder_reward) =
            parse_create_v2_tail_fields(&p).expect("parse");
        assert_eq!(creator, Pubkey::default());
        assert!(mayhem);
        assert!(cb);
        assert_eq!(creator_fee_bps, 0);
        assert!(!holder_reward);
    }

    #[test]
    fn parse_create_v2_tail_reads_holder_rewards_fields() {
        let mut p = Vec::new();
        for value in ["a", "b", "c"] {
            p.extend_from_slice(&(value.len() as u32).to_le_bytes());
            p.extend_from_slice(value.as_bytes());
        }
        p.extend_from_slice(&[0u8; 32]);
        p.push(0); // mayhem
        p.push(0); // cashback (deprecated)
        p.extend_from_slice(&250u64.to_le_bytes());
        p.push(1); // holder rewards

        let (_, mayhem, cashback, creator_fee_bps, holder_reward) =
            parse_create_v2_tail_fields(&p).expect("parse");
        assert!(!mayhem);
        assert!(!cashback);
        assert_eq!(creator_fee_bps, 250);
        assert!(holder_reward);
    }
}