account.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. use bytemuck::{Pod, Zeroable};
  2. use pinocchio::pubkey::Pubkey;
  3. use super::PodCOption;
  4. /// Account data.
  5. #[repr(C)]
  6. #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
  7. pub struct Account {
  8. /// The mint associated with this account
  9. pub mint: Pubkey,
  10. /// The owner of this account.
  11. pub owner: Pubkey,
  12. /// The amount of tokens this account holds.
  13. pub amount: [u8; 8],
  14. /// If `delegate` is `Some` then `delegated_amount` represents
  15. /// the amount authorized by the delegate
  16. pub delegate: PodCOption<Pubkey>,
  17. /// The account's state
  18. pub state: u8,
  19. /// If is_native.is_some, this is a native token, and the value logs the
  20. /// rent-exempt reserve. An Account is required to be rent-exempt, so
  21. /// the value is used by the Processor to ensure that wrapped SOL
  22. /// accounts do not drop below this threshold.
  23. pub is_native: PodCOption<[u8; 8]>,
  24. /// The amount delegated
  25. pub delegated_amount: [u8; 8],
  26. /// Optional authority to close the account.
  27. pub close_authority: PodCOption<Pubkey>,
  28. }
  29. impl Account {
  30. #[inline]
  31. pub fn is_initialized(&self) -> bool {
  32. self.state != AccountState::Uninitialized as u8
  33. }
  34. #[inline]
  35. pub fn is_frozen(&self) -> bool {
  36. self.state == AccountState::Frozen as u8
  37. }
  38. }
  39. /// Account state.
  40. #[repr(u8)]
  41. #[derive(Clone, Copy, Debug, Default, PartialEq)]
  42. pub enum AccountState {
  43. /// Account is not yet initialized
  44. #[default]
  45. Uninitialized,
  46. /// Account is initialized; the account owner and/or delegate may perform
  47. /// permitted operations on this account
  48. Initialized,
  49. /// Account has been frozen by the mint freeze authority. Neither the
  50. /// account owner nor the delegate are able to perform operations on
  51. /// this account.
  52. Frozen,
  53. }
  54. impl From<u8> for AccountState {
  55. fn from(value: u8) -> Self {
  56. match value {
  57. 0 => AccountState::Uninitialized,
  58. 1 => AccountState::Initialized,
  59. 2 => AccountState::Frozen,
  60. _ => panic!("invalid account state value: {value}"),
  61. }
  62. }
  63. }
  64. impl From<AccountState> for u8 {
  65. fn from(value: AccountState) -> Self {
  66. match value {
  67. AccountState::Uninitialized => 0,
  68. AccountState::Initialized => 1,
  69. AccountState::Frozen => 2,
  70. }
  71. }
  72. }