Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit a6b53b6

Browse files
authored
rafactor: Add trait types (#3)
* Add trait types * Use load functions * Fix decimals value
1 parent 80f09a7 commit a6b53b6

25 files changed

+299
-358
lines changed

interface/src/state/account.rs

Lines changed: 35 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use pinocchio::{
2-
account_info::{AccountInfo, Ref},
3-
program_error::ProgramError,
4-
pubkey::Pubkey,
5-
};
1+
use pinocchio::pubkey::Pubkey;
62

7-
use crate::program::ID;
3+
use super::{account_state::AccountState, COption, Initializable, RawType};
84

9-
use super::{account_state::AccountState, COption};
5+
/// Incinerator address.
6+
const INCINERATOR_ID: Pubkey =
7+
pinocchio_pubkey::pubkey!("1nc1nerator11111111111111111111111111111111");
8+
9+
/// System program id.
10+
const SYSTEM_PROGRAM_ID: Pubkey = pinocchio_pubkey::pubkey!("11111111111111111111111111111111");
1011

1112
/// Internal representation of a token account data.
1213
#[repr(C)]
@@ -44,89 +45,28 @@ pub struct Account {
4445
}
4546

4647
impl Account {
47-
pub const LEN: usize = core::mem::size_of::<Account>();
48-
49-
/// Return a `TokenAccount` from the given account info.
50-
///
51-
/// This method performs owner and length validation on `AccountInfo`, safe borrowing
52-
/// the account data.
53-
#[inline]
54-
pub fn from_account_info(account_info: &AccountInfo) -> Result<Ref<Account>, ProgramError> {
55-
if account_info.data_len() != Self::LEN {
56-
return Err(ProgramError::InvalidAccountData);
57-
}
58-
if account_info.owner() != &ID {
59-
return Err(ProgramError::InvalidAccountData);
60-
}
61-
Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe {
62-
Self::from_bytes(data)
63-
}))
64-
}
65-
66-
/// Return a `TokenAccount` from the given account info.
67-
///
68-
/// This method performs owner and length validation on `AccountInfo`, but does not
69-
/// perform the borrow check.
70-
///
71-
/// # Safety
72-
///
73-
/// The caller must ensure that it is safe to borrow the account data – e.g., there are
74-
/// no mutable borrows of the account data.
75-
#[inline]
76-
pub unsafe fn from_account_info_unchecked(
77-
account_info: &AccountInfo,
78-
) -> Result<&Account, ProgramError> {
79-
if account_info.data_len() != Self::LEN {
80-
return Err(ProgramError::InvalidAccountData);
81-
}
82-
if account_info.owner() != &ID {
83-
return Err(ProgramError::InvalidAccountData);
84-
}
85-
Ok(Self::from_bytes(account_info.borrow_data_unchecked()))
86-
}
87-
88-
/// Return a `TokenAccount` from the given bytes.
89-
///
90-
/// # Safety
91-
///
92-
/// The caller must ensure that `bytes` contains a valid representation of `TokenAccount`.
93-
#[inline(always)]
94-
pub unsafe fn from_bytes(bytes: &[u8]) -> &Self {
95-
&*(bytes.as_ptr() as *const Account)
96-
}
97-
98-
/// Return a mutable `Mint` reference from the given bytes.
99-
///
100-
/// # Safety
101-
///
102-
/// The caller must ensure that `bytes` contains a valid representation of `Mint`.
10348
#[inline(always)]
104-
pub unsafe fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self {
105-
&mut *(bytes.as_mut_ptr() as *mut Account)
106-
}
107-
108-
#[inline]
10949
pub fn set_amount(&mut self, amount: u64) {
11050
self.amount = amount.to_le_bytes();
11151
}
11252

113-
#[inline]
53+
#[inline(always)]
11454
pub fn amount(&self) -> u64 {
11555
u64::from_le_bytes(self.amount)
11656
}
11757

118-
#[inline]
58+
#[inline(always)]
11959
pub fn clear_delegate(&mut self) {
12060
self.delegate.0[0] = 0;
12161
}
12262

123-
#[inline]
63+
#[inline(always)]
12464
pub fn set_delegate(&mut self, delegate: &Pubkey) {
12565
self.delegate.0[0] = 1;
12666
self.delegate.1 = *delegate;
12767
}
12868

129-
#[inline]
69+
#[inline(always)]
13070
pub fn delegate(&self) -> Option<&Pubkey> {
13171
if self.delegate.0[0] == 1 {
13272
Some(&self.delegate.1)
@@ -135,17 +75,17 @@ impl Account {
13575
}
13676
}
13777

138-
#[inline]
78+
#[inline(always)]
13979
pub fn set_native(&mut self, value: bool) {
14080
self.is_native[0] = value as u8;
14181
}
14282

143-
#[inline]
83+
#[inline(always)]
14484
pub fn is_native(&self) -> bool {
14585
self.is_native[0] == 1
14686
}
14787

148-
#[inline]
88+
#[inline(always)]
14989
pub fn native_amount(&self) -> Option<u64> {
15090
if self.is_native() {
15191
Some(u64::from_le_bytes(self.native_amount))
@@ -154,28 +94,28 @@ impl Account {
15494
}
15595
}
15696

157-
#[inline]
97+
#[inline(always)]
15898
pub fn set_delegated_amount(&mut self, amount: u64) {
15999
self.delegated_amount = amount.to_le_bytes();
160100
}
161101

162-
#[inline]
102+
#[inline(always)]
163103
pub fn delegated_amount(&self) -> u64 {
164104
u64::from_le_bytes(self.delegated_amount)
165105
}
166106

167-
#[inline]
107+
#[inline(always)]
168108
pub fn clear_close_authority(&mut self) {
169109
self.close_authority.0[0] = 0;
170110
}
171111

172-
#[inline]
112+
#[inline(always)]
173113
pub fn set_close_authority(&mut self, value: &Pubkey) {
174114
self.close_authority.0[0] = 1;
175115
self.close_authority.1 = *value;
176116
}
177117

178-
#[inline]
118+
#[inline(always)]
179119
pub fn close_authority(&self) -> Option<&Pubkey> {
180120
if self.close_authority.0[0] == 1 {
181121
Some(&self.close_authority.1)
@@ -185,12 +125,23 @@ impl Account {
185125
}
186126

187127
#[inline(always)]
188-
pub fn is_initialized(&self) -> bool {
189-
self.state != AccountState::Uninitialized
128+
pub fn is_frozen(&self) -> bool {
129+
self.state == AccountState::Frozen
190130
}
191131

192132
#[inline(always)]
193-
pub fn is_frozen(&self) -> bool {
194-
self.state == AccountState::Frozen
133+
pub fn is_owned_by_system_program_or_incinerator(&self) -> bool {
134+
SYSTEM_PROGRAM_ID == self.owner || INCINERATOR_ID == self.owner
135+
}
136+
}
137+
138+
impl RawType for Account {
139+
const LEN: usize = core::mem::size_of::<Account>();
140+
}
141+
142+
impl Initializable for Account {
143+
#[inline(always)]
144+
fn is_initialized(&self) -> bool {
145+
self.state != AccountState::Uninitialized
195146
}
196147
}

interface/src/state/mint.rs

Lines changed: 23 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
use pinocchio::{
2-
account_info::{AccountInfo, Ref},
3-
program_error::ProgramError,
4-
pubkey::Pubkey,
5-
};
1+
use pinocchio::pubkey::Pubkey;
62

7-
use crate::program::ID;
8-
9-
use super::COption;
3+
use super::{COption, Initializable, RawType};
104

115
/// Internal representation of a mint data.
126
#[repr(C)]
@@ -33,100 +27,33 @@ pub struct Mint {
3327
}
3428

3529
impl Mint {
36-
/// The length of the `Mint` account data.
37-
pub const LEN: usize = core::mem::size_of::<Mint>();
38-
39-
/// Return a `Mint` from the given account info.
40-
///
41-
/// This method performs owner and length validation on `AccountInfo`, safe borrowing
42-
/// the account data.
43-
#[inline]
44-
pub fn from_account_info(account_info: &AccountInfo) -> Result<Ref<Mint>, ProgramError> {
45-
if account_info.data_len() != Self::LEN {
46-
return Err(ProgramError::InvalidAccountData);
47-
}
48-
if account_info.owner() != &ID {
49-
return Err(ProgramError::InvalidAccountOwner);
50-
}
51-
Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe {
52-
Self::from_bytes(data)
53-
}))
54-
}
55-
56-
/// Return a `Mint` from the given account info.
57-
///
58-
/// This method performs owner and length validation on `AccountInfo`, but does not
59-
/// perform the borrow check.
60-
///
61-
/// # Safety
62-
///
63-
/// The caller must ensure that it is safe to borrow the account data – e.g., there are
64-
/// no mutable borrows of the account data.
65-
#[inline]
66-
pub unsafe fn from_account_info_unchecked(
67-
account_info: &AccountInfo,
68-
) -> Result<&Self, ProgramError> {
69-
if account_info.data_len() != Self::LEN {
70-
return Err(ProgramError::InvalidAccountData);
71-
}
72-
if account_info.owner() != &ID {
73-
return Err(ProgramError::InvalidAccountOwner);
74-
}
75-
Ok(Self::from_bytes(account_info.borrow_data_unchecked()))
76-
}
77-
78-
/// Return a `Mint` reference from the given bytes.
79-
///
80-
/// # Safety
81-
///
82-
/// The caller must ensure that `bytes` contains a valid representation of `Mint`.
83-
#[inline]
84-
pub unsafe fn from_bytes(bytes: &[u8]) -> &Self {
85-
&*(bytes.as_ptr() as *const Mint)
86-
}
87-
88-
/// Return a mutable `Mint` reference from the given bytes.
89-
///
90-
/// # Safety
91-
///
92-
/// The caller must ensure that `bytes` contains a valid representation of `Mint`.
93-
#[inline]
94-
pub unsafe fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self {
95-
&mut *(bytes.as_mut_ptr() as *mut Mint)
96-
}
97-
98-
#[inline]
30+
#[inline(always)]
9931
pub fn set_supply(&mut self, supply: u64) {
10032
self.supply = supply.to_le_bytes();
10133
}
10234

103-
#[inline]
35+
#[inline(always)]
10436
pub fn supply(&self) -> u64 {
10537
u64::from_le_bytes(self.supply)
10638
}
10739

108-
#[inline]
40+
#[inline(always)]
10941
pub fn set_initialized(&mut self, value: bool) {
11042
self.is_initialized = value as u8;
11143
}
11244

113-
#[inline]
114-
pub fn is_initialized(&self) -> bool {
115-
self.is_initialized == 1
116-
}
117-
118-
#[inline]
45+
#[inline(always)]
11946
pub fn clear_mint_authority(&mut self) {
12047
self.mint_authority.0[0] = 0;
12148
}
12249

123-
#[inline]
50+
#[inline(always)]
12451
pub fn set_mint_authority(&mut self, mint_authority: &Pubkey) {
12552
self.mint_authority.0[0] = 1;
12653
self.mint_authority.1 = *mint_authority;
12754
}
12855

129-
#[inline]
56+
#[inline(always)]
13057
pub fn mint_authority(&self) -> Option<&Pubkey> {
13158
if self.mint_authority.0[0] == 1 {
13259
Some(&self.mint_authority.1)
@@ -135,18 +62,18 @@ impl Mint {
13562
}
13663
}
13764

138-
#[inline]
65+
#[inline(always)]
13966
pub fn clear_freeze_authority(&mut self) {
14067
self.freeze_authority.0[0] = 0;
14168
}
14269

143-
#[inline]
70+
#[inline(always)]
14471
pub fn set_freeze_authority(&mut self, freeze_authority: &Pubkey) {
14572
self.freeze_authority.0[0] = 1;
14673
self.freeze_authority.1 = *freeze_authority;
14774
}
14875

149-
#[inline]
76+
#[inline(always)]
15077
pub fn freeze_authority(&self) -> Option<&Pubkey> {
15178
if self.freeze_authority.0[0] == 1 {
15279
Some(&self.freeze_authority.1)
@@ -155,3 +82,15 @@ impl Mint {
15582
}
15683
}
15784
}
85+
86+
impl RawType for Mint {
87+
/// The length of the `Mint` account data.
88+
const LEN: usize = core::mem::size_of::<Mint>();
89+
}
90+
91+
impl Initializable for Mint {
92+
#[inline(always)]
93+
fn is_initialized(&self) -> bool {
94+
self.is_initialized == 1
95+
}
96+
}

0 commit comments

Comments
 (0)