1use crate::core::events::EventMetadata;
4use solana_sdk::{pubkey::Pubkey, signature::Signature};
5use yellowstone_grpc_proto::prelude::{Transaction, TransactionStatusMeta};
6
7pub fn create_metadata(
9 signature: Signature,
10 slot: u64,
11 tx_index: u64,
12 block_time_us: i64,
13 grpc_recv_us: i64,
14) -> EventMetadata {
15 EventMetadata { signature, slot, tx_index, block_time_us, grpc_recv_us, recent_blockhash: None }
16}
17
18#[inline(always)]
20pub fn create_metadata_simple(
21 signature: Signature,
22 slot: u64,
23 tx_index: u64,
24 block_time_us: Option<i64>,
25 _program_id: Pubkey,
26) -> EventMetadata {
27 let current_time = now_us();
28
29 EventMetadata {
30 signature,
31 slot,
32 tx_index,
33 block_time_us: block_time_us.unwrap_or(0),
34 grpc_recv_us: current_time,
35 recent_blockhash: None,
36 }
37}
38
39#[inline(always)]
41pub fn read_u64_le(data: &[u8], offset: usize) -> Option<u64> {
42 data.get(offset..offset + 8).map(|slice| u64::from_le_bytes(slice.try_into().unwrap()))
43}
44
45#[inline(always)]
47pub fn read_u32_le(data: &[u8], offset: usize) -> Option<u32> {
48 data.get(offset..offset + 4).map(|slice| u32::from_le_bytes(slice.try_into().unwrap()))
49}
50
51#[inline(always)]
53pub fn read_u16_le(data: &[u8], offset: usize) -> Option<u16> {
54 data.get(offset..offset + 2).map(|slice| u16::from_le_bytes(slice.try_into().unwrap()))
55}
56
57#[inline(always)]
59pub fn read_u8(data: &[u8], offset: usize) -> Option<u8> {
60 data.get(offset).copied()
61}
62
63#[inline(always)]
65pub fn read_i32_le(data: &[u8], offset: usize) -> Option<i32> {
66 data.get(offset..offset + 4).map(|slice| i32::from_le_bytes(slice.try_into().unwrap()))
67}
68
69#[inline(always)]
71pub fn read_u128_le(data: &[u8], offset: usize) -> Option<u128> {
72 data.get(offset..offset + 16).map(|slice| u128::from_le_bytes(slice.try_into().unwrap()))
73}
74
75#[inline(always)]
77pub fn read_bool(data: &[u8], offset: usize) -> Option<bool> {
78 data.get(offset).map(|&b| b != 0)
79}
80
81#[inline(always)]
84pub fn read_option_bool_idl(data: &[u8], offset: usize) -> Option<bool> {
85 match data.get(offset).copied()? {
86 0 => Some(false),
87 1 => Some(true),
88 _ => None,
89 }
90}
91
92#[inline(always)]
94pub fn read_pubkey(data: &[u8], offset: usize) -> Option<Pubkey> {
95 data.get(offset..offset + 32).and_then(|slice| Pubkey::try_from(slice).ok())
96}
97
98#[inline(always)]
100pub fn get_account(accounts: &[Pubkey], index: usize) -> Option<Pubkey> {
101 accounts.get(index).copied()
102}
103
104pub fn calculate_slippage_bps(amount_in: u64, amount_out_min: u64) -> u16 {
106 if amount_in == 0 {
107 return 0;
108 }
109
110 let slippage = ((amount_in.saturating_sub(amount_out_min)) * 10000) / amount_in;
112 slippage.min(10000) as u16
113}
114
115pub fn calculate_price_impact_bps(_amount_in: u64, amount_out: u64, expected_out: u64) -> u16 {
117 if expected_out == 0 {
118 return 0;
119 }
120
121 let impact = ((expected_out.saturating_sub(amount_out)) * 10000) / expected_out;
122 impact.min(10000) as u16
123}
124
125pub fn read_bytes(data: &[u8], offset: usize, length: usize) -> Option<&[u8]> {
127 if data.len() < offset + length {
128 return None;
129 }
130 Some(&data[offset..offset + length])
131}
132
133#[inline]
137pub fn parse_create_v2_tail_fields(
138 data_after_discriminator: &[u8],
139) -> Option<(Pubkey, bool, bool)> {
140 let mut offset = 0usize;
141 let (_, l) = read_str_unchecked(data_after_discriminator, offset)?;
142 offset += l;
143 let (_, l) = read_str_unchecked(data_after_discriminator, offset)?;
144 offset += l;
145 let (_, l) = read_str_unchecked(data_after_discriminator, offset)?;
146 offset += l;
147 if data_after_discriminator.len() < offset + 32 + 1 {
148 return None;
149 }
150 let creator = read_pubkey(data_after_discriminator, offset)?;
151 offset += 32;
152 let is_mayhem_mode = read_bool(data_after_discriminator, offset)?;
153 offset += 1;
154 let is_cashback_enabled = if offset < data_after_discriminator.len() {
155 read_option_bool_idl(data_after_discriminator, offset).unwrap_or(false)
156 } else {
157 false
158 };
159 Some((creator, is_mayhem_mode, is_cashback_enabled))
160}
161
162#[inline]
165pub fn read_str_unchecked(data: &[u8], offset: usize) -> Option<(&str, usize)> {
166 if data.len() < offset + 4 {
167 return None;
168 }
169 let len = u32::from_le_bytes(data[offset..offset + 4].try_into().ok()?) as usize;
170 if data.len() < offset + 4 + len {
171 return None;
172 }
173 let string_bytes = &data[offset + 4..offset + 4 + len];
174 let s = std::str::from_utf8(string_bytes).ok()?;
175 Some((s, 4 + len))
176}
177
178pub fn read_vec_u64(_data: &[u8], _offset: usize) -> Option<Vec<u64>> {
180 Some(vec![0, 0])
183}
184
185#[inline(always)]
187pub fn read_pubkey_fast(bytes: &[u8]) -> Pubkey {
188 crate::logs::utils::read_pubkey(bytes, 0).unwrap_or_default()
189}
190
191pub fn get_instruction_account_getter<'a>(
194 meta: &'a TransactionStatusMeta,
195 transaction: &'a Option<Transaction>,
196 account_keys: Option<&'a Vec<Vec<u8>>>,
197 loaded_writable_addresses: &'a Vec<Vec<u8>>,
199 loaded_readonly_addresses: &'a Vec<Vec<u8>>,
200 index: &(i32, i32), ) -> Option<impl Fn(usize) -> Pubkey + 'a> {
202 let accounts = if index.1 >= 0 {
204 let outer_idx = index.0 as u32;
206 meta.inner_instructions
207 .binary_search_by_key(&outer_idx, |i| i.index)
208 .ok()
209 .and_then(|pos| meta.inner_instructions.get(pos))
210 .or_else(|| {
211 meta.inner_instructions.iter().find(|i| i.index == outer_idx)
213 })?
214 .instructions
215 .get(index.1 as usize)?
216 .accounts
217 .as_slice()
218 } else {
219 transaction
221 .as_ref()?
222 .message
223 .as_ref()?
224 .instructions
225 .get(index.0 as usize)?
226 .accounts
227 .as_slice()
228 };
229
230 Some(move |acc_index: usize| -> Pubkey {
232 let account_index = match accounts.get(acc_index) {
234 Some(&idx) => idx as usize,
235 None => return Pubkey::default(),
236 };
237 let Some(keys) = account_keys else {
239 return Pubkey::default();
240 };
241 if let Some(key_bytes) = keys.get(account_index) {
243 return read_pubkey_fast(key_bytes);
244 }
245 let writable_offset = account_index.saturating_sub(keys.len());
247 if let Some(key_bytes) = loaded_writable_addresses.get(writable_offset) {
248 return read_pubkey_fast(key_bytes);
249 }
250 let readonly_offset = writable_offset.saturating_sub(loaded_writable_addresses.len());
252 if let Some(key_bytes) = loaded_readonly_addresses.get(readonly_offset) {
253 return read_pubkey_fast(key_bytes);
254 }
255 Pubkey::default()
256 })
257}
258
259use crate::core::clock::now_us;
260use std::collections::HashMap;
262
263pub struct InnerInstructionsIndex<'a> {
265 index_map: HashMap<u32, &'a yellowstone_grpc_proto::prelude::InnerInstructions>,
267}
268
269impl<'a> InnerInstructionsIndex<'a> {
270 #[inline]
272 pub fn new(meta: &'a TransactionStatusMeta) -> Self {
273 let mut index_map = HashMap::with_capacity(meta.inner_instructions.len());
274 for inner in &meta.inner_instructions {
275 index_map.insert(inner.index, inner);
276 }
277 Self { index_map }
278 }
279
280 #[inline]
282 pub fn get(
283 &self,
284 outer_index: u32,
285 ) -> Option<&'a yellowstone_grpc_proto::prelude::InnerInstructions> {
286 self.index_map.get(&outer_index).copied()
287 }
288}
289
290pub fn get_instruction_account_getter_indexed<'a>(
292 inner_index: &InnerInstructionsIndex<'a>,
293 transaction: &'a Option<Transaction>,
294 account_keys: Option<&'a Vec<Vec<u8>>>,
295 loaded_writable_addresses: &'a Vec<Vec<u8>>,
296 loaded_readonly_addresses: &'a Vec<Vec<u8>>,
297 index: &(i32, i32),
298) -> Option<impl Fn(usize) -> Pubkey + 'a> {
299 let accounts = if index.1 >= 0 {
300 inner_index.get(index.0 as u32)?.instructions.get(index.1 as usize)?.accounts.as_slice()
302 } else {
303 transaction
304 .as_ref()?
305 .message
306 .as_ref()?
307 .instructions
308 .get(index.0 as usize)?
309 .accounts
310 .as_slice()
311 };
312
313 Some(move |acc_index: usize| -> Pubkey {
314 let account_index = match accounts.get(acc_index) {
315 Some(&idx) => idx as usize,
316 None => return Pubkey::default(),
317 };
318 let Some(keys) = account_keys else {
319 return Pubkey::default();
320 };
321 if let Some(key_bytes) = keys.get(account_index) {
322 return read_pubkey_fast(key_bytes);
323 }
324 let writable_offset = account_index.saturating_sub(keys.len());
325 if let Some(key_bytes) = loaded_writable_addresses.get(writable_offset) {
326 return read_pubkey_fast(key_bytes);
327 }
328 let readonly_offset = writable_offset.saturating_sub(loaded_writable_addresses.len());
329 if let Some(key_bytes) = loaded_readonly_addresses.get(readonly_offset) {
330 return read_pubkey_fast(key_bytes);
331 }
332 Pubkey::default()
333 })
334}
335
336#[cfg(test)]
337mod option_bool_tests {
338 use super::*;
339
340 #[test]
341 fn read_option_bool_idl_strict() {
342 assert_eq!(read_option_bool_idl(&[0], 0), Some(false));
343 assert_eq!(read_option_bool_idl(&[1], 0), Some(true));
344 assert_eq!(read_option_bool_idl(&[2], 0), None);
345 }
346
347 #[test]
348 fn parse_create_v2_tail_matches_anchor_len() {
349 let mut p = Vec::new();
351 p.extend_from_slice(&(1u32.to_le_bytes()));
352 p.push(b'a');
353 p.extend_from_slice(&(1u32.to_le_bytes()));
354 p.push(b'b');
355 p.extend_from_slice(&(1u32.to_le_bytes()));
356 p.push(b'c');
357 p.extend_from_slice(&[0u8; 32]);
358 p.push(1u8); p.push(1u8); assert_eq!(p.len(), 49);
361 let (creator, mayhem, cb) = parse_create_v2_tail_fields(&p).expect("parse");
362 assert_eq!(creator, Pubkey::default());
363 assert!(mayhem);
364 assert!(cb);
365 }
366}