Skip to content

Commit

Permalink
pod: Add pod-compatible mint / account / multisig types
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Mar 5, 2024
1 parent c4239ba commit cdd354c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions token/program-2022/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use {
transfer_fee::{TransferFeeAmount, TransferFeeConfig},
transfer_hook::{TransferHook, TransferHookAccount},
},
pod::{PodAccount, PodMint},
state::{Account, Mint, Multisig, PackedSizeOf},
},
bytemuck::{Pod, Zeroable},
Expand Down Expand Up @@ -1226,6 +1227,12 @@ impl BaseState for Account {
impl BaseState for Mint {
const ACCOUNT_TYPE: AccountType = AccountType::Mint;
}
impl BaseState for PodAccount {
const ACCOUNT_TYPE: AccountType = AccountType::Account;
}
impl BaseState for PodMint {
const ACCOUNT_TYPE: AccountType = AccountType::Mint;
}

/// Trait to be implemented by all extension states, specifying which extension
/// and account type they are associated with
Expand Down
1 change: 1 addition & 0 deletions token/program-2022/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod instruction;
pub mod native_mint;
pub mod offchain;
pub mod onchain;
pub mod pod;
pub mod processor;
pub mod proof;
#[cfg(feature = "serde-traits")]
Expand Down
99 changes: 99 additions & 0 deletions token/program-2022/src/pod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! Rewrites of the base state types represented as Pods

use {
crate::{instruction::MAX_SIGNERS, state::PackedSizeOf},
bytemuck::{Pod, Zeroable},
solana_program::{program_pack::IsInitialized, pubkey::Pubkey},
spl_pod::primitives::PodU64,
};

/// Mint data stored as a Pod type
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct PodMint {
/// Optional authority used to mint new tokens. The mint authority may only
/// be provided during mint creation. If no mint authority is present
/// then the mint has a fixed supply and no further tokens may be
/// minted.
pub mint_authority: PodCOption<Pubkey>,
/// Total supply of tokens.
pub supply: PodU64,
/// Number of base 10 digits to the right of the decimal place.
pub decimals: u8,
/// Is not 0 if this structure has been initialized
pub is_initialized: u8,
/// Optional authority to freeze token accounts.
pub freeze_authority: PodCOption<Pubkey>,
}
impl IsInitialized for PodMint {
fn is_initialized(&self) -> bool {
self.is_initialized != 0
}
}
impl PackedSizeOf for PodMint {
const SIZE_OF: usize = std::mem::size_of::<Self>();
}

/// Account data stored as a Pod type
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct PodAccount {
/// The mint associated with this account
pub mint: Pubkey,
/// The owner of this account.
pub owner: Pubkey,
/// The amount of tokens this account holds.
pub amount: PodU64,
/// If `delegate` is `Some` then `delegated_amount` represents
/// the amount authorized by the delegate
pub delegate: PodCOption<Pubkey>,
/// The account's state, stored as u8
pub state: u8,
/// If is_some, this is a native token, and the value logs the rent-exempt
/// reserve. An Account is required to be rent-exempt, so the value is
/// used by the Processor to ensure that wrapped SOL accounts do not
/// drop below this threshold.
pub is_native: PodCOption<PodU64>,
/// The amount delegated
pub delegated_amount: PodU64,
/// Optional authority to close the account.
pub close_authority: PodCOption<Pubkey>,
}
impl IsInitialized for PodAccount {
fn is_initialized(&self) -> bool {
self.state != 0
}
}
impl PackedSizeOf for PodAccount {
const SIZE_OF: usize = std::mem::size_of::<Self>();
}

/// Multisignature data.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct PodMultisig {
/// Number of signers required
pub m: u8,
/// Number of valid signers
pub n: u8,
/// If not 0, this structure has been initialized
pub is_initialized: u8,
/// Signer public keys
pub signers: [Pubkey; MAX_SIGNERS],
}
impl IsInitialized for PodMultisig {
fn is_initialized(&self) -> bool {
self.is_initialized != 0
}
}
impl PackedSizeOf for PodMultisig {
const SIZE_OF: usize = std::mem::size_of::<Self>();
}

/// COption<T> stored as a Pod type
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct PodCOption<T: Pod> {
option: [u8; 4],
value: T,
}

0 comments on commit cdd354c

Please sign in to comment.