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
4647impl 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}
0 commit comments