accounts_index: Add SPL Token account indexing for token-2022 accounts (#23043)

This commit is contained in:
Michael Vines
2022-02-16 16:23:25 -08:00
committed by GitHub
parent bca1d51735
commit a102453bae
8 changed files with 174 additions and 84 deletions

View File

@@ -1,4 +1,6 @@
// Partial SPL Token declarations inlined to avoid an external dependency on the spl-token crate
/// Partial SPL Token declarations inlined to avoid an external dependency on the spl-token crate
use solana_sdk::pubkey::{Pubkey, PUBKEY_BYTES};
solana_sdk::declare_id!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
pub(crate) mod new_token_program {
@@ -19,15 +21,54 @@ pub(crate) mod new_token_program {
*/
pub const SPL_TOKEN_ACCOUNT_MINT_OFFSET: usize = 0;
pub const SPL_TOKEN_ACCOUNT_OWNER_OFFSET: usize = 32;
const SPL_TOKEN_ACCOUNT_LENGTH: usize = 165;
pub mod state {
const LEN: usize = 165;
pub struct Account;
impl Account {
pub fn get_packed_len() -> usize {
LEN
pub(crate) trait GenericTokenAccount {
fn valid_account_data(account_data: &[u8]) -> bool;
// Call after account length has already been verified
fn unpack_account_owner_unchecked(account_data: &[u8]) -> &Pubkey {
Self::unpack_pubkey_unchecked(account_data, SPL_TOKEN_ACCOUNT_OWNER_OFFSET)
}
// Call after account length has already been verified
fn unpack_account_mint_unchecked(account_data: &[u8]) -> &Pubkey {
Self::unpack_pubkey_unchecked(account_data, SPL_TOKEN_ACCOUNT_MINT_OFFSET)
}
// Call after account length has already been verified
fn unpack_pubkey_unchecked(account_data: &[u8], offset: usize) -> &Pubkey {
bytemuck::from_bytes(&account_data[offset..offset + PUBKEY_BYTES])
}
fn unpack_account_owner(account_data: &[u8]) -> Option<&Pubkey> {
if Self::valid_account_data(account_data) {
Some(Self::unpack_account_owner_unchecked(account_data))
} else {
None
}
}
fn unpack_account_mint(account_data: &[u8]) -> Option<&Pubkey> {
if Self::valid_account_data(account_data) {
Some(Self::unpack_account_mint_unchecked(account_data))
} else {
None
}
}
}
pub struct Account;
impl Account {
pub fn get_packed_len() -> usize {
SPL_TOKEN_ACCOUNT_LENGTH
}
}
impl GenericTokenAccount for Account {
fn valid_account_data(account_data: &[u8]) -> bool {
account_data.len() == SPL_TOKEN_ACCOUNT_LENGTH
}
}
pub mod native_mint {