#account #generics #solana #sanctum

solana-readonly-account

Readonly solana account field getter traits extendable for both on-chain and off-chain structs

5 stable releases

2.0.0 Aug 3, 2024
1.2.1 Nov 22, 2024
1.2.0 Jul 2, 2024
1.1.0 Jan 3, 2024
1.0.0 Dec 11, 2023

#91 in #generics

Download history 261/week @ 2024-07-29 56/week @ 2024-08-05 70/week @ 2024-08-12 34/week @ 2024-08-19 57/week @ 2024-08-26 17/week @ 2024-09-02 1/week @ 2024-09-09 44/week @ 2024-09-16 32/week @ 2024-09-23 70/week @ 2024-09-30 47/week @ 2024-10-07 57/week @ 2024-10-14 60/week @ 2024-10-21 28/week @ 2024-10-28 86/week @ 2024-11-04 13/week @ 2024-11-11

189 downloads per month

MIT/Apache

21KB
433 lines

solana-readonly-account

Reimplementation of ReadableAccount to enable code reuse across off-chain clients (solana-sdk) and on-chain programs (solana-program)

Why was this crate created?

  • You cannot use the original ReadableAccount trait from solana-sdk in on-chain programs because the solana-sdk feature flags don't work properly and it won't compile with build-sbf
  • Rc<RefCell<>>s in AccountInfo make it incompatible with &[u8] for .data

Library

The 6 main account fields (key, lamports, data, owner, is_executable, rent_epoch) are split into individual getter traits. This splitting allows for greater trait composability and flexibility.

For example, say you had a function that only requires the account's owner and this is a known static pubkey. Instead of having to fetch the full Account just to read its already-known owner field, or creating a dummy Account, you can simply define a newtype that only needs to implement ReadonlyAccountOwner, while still maintaining the ability to use this function with on-chain AccountInfos.

Usage

Importing the respective traits from the crate now enables you to write generic functions that work both on-chain and off-chain

use solana_program::{
    program_error::ProgramError, program_pack::Pack,
};
use solana_readonly_account::ReadonlyAccountData;
use spl_token_2022::state::Account;

pub fn try_deserialize_token_account<A: ReadonlyAccountData>(
    acc: A,
) -> Result<Account, ProgramError> {
    Account::unpack(&acc.data())
}

By default, this crate has zero dependencies and only provides the trait definitions.

Crate Features

keyed

Since many offchain account structs such as solana_sdk::Account don't have a pubkey field, the following Keyed wrapper struct is defined to impl ReadonlyAccountPubkey and ReadonlyAccountPubkeyBytes for:

pub struct Keyed<T> {
    pub pubkey: Pubkey,
    pub account: T,
}

keyed-bytes

Similar to keyed but using [u8; 32] instead of Pubkey for zero dependencies.

solana-pubkey

Enables support for solana's Pubkey types on top of the raw [u8; 32] types.

solana-program

impls the traits for AccountInfo

solana-sdk

impls the traits for Account and AccountSharedData.

Do NOT enable this feature in an on-chain program crate, or cargo-build-sbf will fail.

Testing

cargo test --all-features

Dependencies

~0–9.5MB
~89K SLoC