mint.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use pinocchio::pubkey::Pubkey;
  2. use super::{COption, Initializable, Transmutable};
  3. /// Internal representation of a mint data.
  4. #[repr(C)]
  5. pub struct Mint {
  6. /// Optional authority used to mint new tokens. The mint authority may only
  7. /// be provided during mint creation. If no mint authority is present
  8. /// then the mint has a fixed supply and no further tokens may be
  9. /// minted.
  10. pub mint_authority: COption<Pubkey>,
  11. /// Total supply of tokens.
  12. supply: [u8; 8],
  13. /// Number of base 10 digits to the right of the decimal place.
  14. pub decimals: u8,
  15. /// Is `true` if this structure has been initialized.
  16. is_initialized: u8,
  17. // Indicates whether the freeze authority is present or not.
  18. //freeze_authority_option: [u8; 4],
  19. /// Optional authority to freeze token accounts.
  20. pub freeze_authority: COption<Pubkey>,
  21. }
  22. impl Mint {
  23. #[inline(always)]
  24. pub fn set_supply(&mut self, supply: u64) {
  25. self.supply = supply.to_le_bytes();
  26. }
  27. #[inline(always)]
  28. pub fn supply(&self) -> u64 {
  29. u64::from_le_bytes(self.supply)
  30. }
  31. #[inline(always)]
  32. pub fn set_initialized(&mut self) {
  33. self.is_initialized = 1;
  34. }
  35. #[inline(always)]
  36. pub fn clear_mint_authority(&mut self) {
  37. self.mint_authority.0[0] = 0;
  38. }
  39. #[inline(always)]
  40. pub fn set_mint_authority(&mut self, mint_authority: &Pubkey) {
  41. self.mint_authority.0[0] = 1;
  42. self.mint_authority.1 = *mint_authority;
  43. }
  44. #[inline(always)]
  45. pub fn mint_authority(&self) -> Option<&Pubkey> {
  46. if self.mint_authority.0[0] == 1 {
  47. Some(&self.mint_authority.1)
  48. } else {
  49. None
  50. }
  51. }
  52. #[inline(always)]
  53. pub fn clear_freeze_authority(&mut self) {
  54. self.freeze_authority.0[0] = 0;
  55. }
  56. #[inline(always)]
  57. pub fn set_freeze_authority(&mut self, freeze_authority: &Pubkey) {
  58. self.freeze_authority.0[0] = 1;
  59. self.freeze_authority.1 = *freeze_authority;
  60. }
  61. #[inline(always)]
  62. pub fn freeze_authority(&self) -> Option<&Pubkey> {
  63. if self.freeze_authority.0[0] == 1 {
  64. Some(&self.freeze_authority.1)
  65. } else {
  66. None
  67. }
  68. }
  69. }
  70. impl Transmutable for Mint {
  71. /// The length of the `Mint` account data.
  72. const LEN: usize = core::mem::size_of::<Mint>();
  73. }
  74. impl Initializable for Mint {
  75. #[inline(always)]
  76. fn is_initialized(&self) -> bool {
  77. self.is_initialized == 1
  78. }
  79. }