Skip to main content

sol_parser_sdk/core/
common_filler.rs

1use crate::{core::events::*, instr::read_bool};
2use std::collections::HashMap;
3use yellowstone_grpc_proto::prelude::{Transaction, TransactionStatusMeta};
4
5pub fn fill_data(
6    event: &mut DexEvent,
7    meta: &TransactionStatusMeta,
8    transaction: &Option<Transaction>,
9    program_invokes: &HashMap<&str, Vec<(i32, i32)>>,
10) {
11    // 获取账户的辅助函数
12    match event {
13        DexEvent::PumpSwapBuy(ref mut event) => {
14            if let Some(invoke) = program_invokes
15                .get(crate::grpc::program_ids::PUMPSWAP_FEES_PROGRAM_ID)
16                .as_ref()
17                .and_then(|v| v.last())
18            {
19                let data = get_instruction_data(meta, transaction, invoke);
20                if data.is_some() {
21                    let is_pump_pool = read_bool(&data.unwrap_or_default(), 9).unwrap_or_default();
22                    event.is_pump_pool = is_pump_pool;
23                }
24            }
25        }
26        DexEvent::PumpSwapSell(ref mut event) => {
27            if let Some(invoke) = program_invokes
28                .get(crate::grpc::program_ids::PUMPSWAP_FEES_PROGRAM_ID)
29                .as_ref()
30                .and_then(|v| v.last())
31            {
32                let data = get_instruction_data(meta, transaction, invoke);
33                if data.is_some() {
34                    let is_pump_pool = read_bool(&data.unwrap_or_default(), 9).unwrap_or_default();
35                    event.is_pump_pool = is_pump_pool;
36                }
37            }
38        }
39        _ => {} // 其他事件类型TODO
40    }
41}
42
43pub fn get_instruction_data<'a>(
44    meta: &'a TransactionStatusMeta,
45    transaction: &'a Option<Transaction>,
46    index: &(i32, i32), // (outer_index, inner_index)
47) -> Option<&'a [u8]> {
48    let data = if index.1 >= 0 {
49        meta.inner_instructions
50            .iter()
51            .find(|i| i.index == index.0 as u32)?
52            .instructions
53            .get(index.1 as usize)?
54            .data
55            .as_slice()
56    } else {
57        transaction.as_ref()?.message.as_ref()?.instructions.get(index.0 as usize)?.data.as_slice()
58    };
59    return Some(data);
60}