1
0

mint.rs 2.6 KB

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