pyth_min/
byte_utils.rs

1/// The discriminator of Price Feed Accounts on mainnet
2pub const DISCRIMINATOR_AS_HEX: &str = "22f123639d7ef4cd";
3pub const DISCRIMINATOR_AS_BYTES: &[i32; 8] = &[0x22, 0xF1, 0x23, 0x63, 0x9D, 0x7E, 0xF4, 0xCD];
4
5/// Bytes that will be a Pubkey when decoded (this crate has dependencies and therefore does not
6/// read Pubkeys)
7pub type PubkeyBytes = [u8; 32];
8
9/// A very minimal tool to convert a hex string like "22f123639" into the byte equivalent.
10pub fn hex_to_bytes(hex: &str) -> Vec<u8> {
11    hex.as_bytes()
12        .chunks(2)
13        .map(|chunk| {
14            let high = chunk[0] as char;
15            let low = chunk[1] as char;
16            let high = high.to_digit(16).expect("Invalid hex character") as u8;
17            let low = low.to_digit(16).expect("Invalid hex character") as u8;
18            (high << 4) | low
19        })
20        .collect()
21}
22
23/// A very minimal utility to interpret some bytes as an i64
24pub fn interpret_bytes_as_i64(bytes: &[u8]) -> i64 {
25    let mut arr = [0u8; 8];
26    arr.copy_from_slice(bytes);
27    i64::from_le_bytes(arr)
28}
29
30/// A very minimal utility to interpret some bytes as an i32
31pub fn interpret_bytes_as_i32(bytes: &[u8]) -> i32 {
32    let mut arr = [0u8; 4];
33    arr.copy_from_slice(bytes);
34    i32::from_le_bytes(arr)
35}
36
37/// A very minimal utility to interpret some bytes as an u64
38pub fn interpret_bytes_as_u64(bytes: &[u8]) -> u64 {
39    let mut arr = [0u8; 8];
40    arr.copy_from_slice(bytes);
41    u64::from_le_bytes(arr)
42}