solana_geyser_plugin_postgres/
inline_spl_token.rs

1/// Partial SPL Token declarations inlined to avoid an external dependency on the spl-token crate
2/// Copied from solana-runtime
3use solana_sdk::pubkey::{Pubkey, PUBKEY_BYTES};
4
5solana_sdk::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
6
7pub(crate) mod new_token_program {
8    solana_sdk::declare_id!("nTok2oJvx1CgbYA2SznfJLmnKLEL6sYdh2ypZms2nhm");
9}
10
11/*
12    /// The SPL token definition -- we care about only the mint and owner fields for now.
13    /// at offset 0 and 32 respectively.
14    spl_token::state::Account {
15        mint: Pubkey,
16        owner: Pubkey,
17        amount: u64,
18        delegate: COption<Pubkey>,
19        state: AccountState,
20        is_native: COption<u64>,
21        delegated_amount: u64,
22        close_authority: COption<Pubkey>,
23    }
24*/
25pub const SPL_TOKEN_ACCOUNT_MINT_OFFSET: usize = 0;
26pub const SPL_TOKEN_ACCOUNT_OWNER_OFFSET: usize = 32;
27const SPL_TOKEN_ACCOUNT_LENGTH: usize = 165;
28
29pub(crate) trait GenericTokenAccount {
30    fn valid_account_data(account_data: &[u8]) -> bool;
31
32    // Call after account length has already been verified
33    fn unpack_account_owner_unchecked(account_data: &[u8]) -> &Pubkey {
34        Self::unpack_pubkey_unchecked(account_data, SPL_TOKEN_ACCOUNT_OWNER_OFFSET)
35    }
36
37    // Call after account length has already been verified
38    fn unpack_account_mint_unchecked(account_data: &[u8]) -> &Pubkey {
39        Self::unpack_pubkey_unchecked(account_data, SPL_TOKEN_ACCOUNT_MINT_OFFSET)
40    }
41
42    // Call after account length has already been verified
43    fn unpack_pubkey_unchecked(account_data: &[u8], offset: usize) -> &Pubkey {
44        bytemuck::from_bytes(&account_data[offset..offset + PUBKEY_BYTES])
45    }
46
47    fn unpack_account_owner(account_data: &[u8]) -> Option<&Pubkey> {
48        if Self::valid_account_data(account_data) {
49            Some(Self::unpack_account_owner_unchecked(account_data))
50        } else {
51            None
52        }
53    }
54
55    fn unpack_account_mint(account_data: &[u8]) -> Option<&Pubkey> {
56        if Self::valid_account_data(account_data) {
57            Some(Self::unpack_account_mint_unchecked(account_data))
58        } else {
59            None
60        }
61    }
62}
63
64pub struct Account;
65impl Account {
66    pub fn get_packed_len() -> usize {
67        SPL_TOKEN_ACCOUNT_LENGTH
68    }
69}
70
71impl GenericTokenAccount for Account {
72    fn valid_account_data(account_data: &[u8]) -> bool {
73        account_data.len() == SPL_TOKEN_ACCOUNT_LENGTH
74    }
75}
76
77pub mod native_mint {
78    solana_sdk::declare_id!("So11111111111111111111111111111111111111112");
79
80    /*
81        Mint {
82            mint_authority: COption::None,
83            supply: 0,
84            decimals: 9,
85            is_initialized: true,
86            freeze_authority: COption::None,
87        }
88    */
89    pub const ACCOUNT_DATA: [u8; 82] = [
90        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93    ];
94}