99 check_valid_fresh_account,
1010 get_rent,
1111 pyth_assert,
12- send_lamports,
1312 try_convert,
1413 } ,
1514 } ,
2322 program_error:: ProgramError ,
2423 program_memory:: sol_memset,
2524 pubkey:: Pubkey ,
26- system_instruction:: {
27- allocate,
28- assign,
29- } ,
25+ system_instruction:: create_account,
3026 } ,
3127 std:: {
3228 borrow:: BorrowMut ,
@@ -107,7 +103,9 @@ pub trait PythAccount: Pod {
107103 load_account_as_mut :: < Self > ( account)
108104 }
109105
110- // Creates PDA accounts only when needed, and initializes it as one of the Pyth accounts
106+ /// Creates PDA accounts only when needed, and initializes it as one of the Pyth accounts.
107+ /// This PDA initialization assumes that the account has 0 lamports.
108+ /// TO DO: Fix this once we can resize the program.
111109 fn initialize_pda < ' a > (
112110 account : & AccountInfo < ' a > ,
113111 funding_account : & AccountInfo < ' a > ,
@@ -118,52 +116,36 @@ pub trait PythAccount: Pod {
118116 ) -> Result < ( ) , ProgramError > {
119117 let target_rent = get_rent ( ) ?. minimum_balance ( Self :: MINIMUM_SIZE ) ;
120118
121- if account. lamports ( ) < target_rent {
122- send_lamports (
119+ if account. data_len ( ) == 0 {
120+ create (
123121 funding_account,
124122 account,
125123 system_program,
126- target_rent - account. lamports ( ) ,
124+ program_id,
125+ Self :: MINIMUM_SIZE ,
126+ target_rent,
127+ seeds,
127128 ) ?;
128- }
129-
130- if account. data_len ( ) == 0 {
131- allocate_data ( account, system_program, Self :: MINIMUM_SIZE , seeds) ?;
132- assign_owner ( account, program_id, system_program, seeds) ?;
133129 Self :: initialize ( account, version) ?;
134130 }
131+
135132 Ok ( ( ) )
136133 }
137134}
138135
139- /// Given an already empty `AccountInfo`, allocate the data field to the given size. This make no
140- /// assumptions about owner.
141- fn allocate_data < ' a > (
142- account : & AccountInfo < ' a > ,
136+ fn create < ' a > (
137+ from : & AccountInfo < ' a > ,
138+ to : & AccountInfo < ' a > ,
143139 system_program : & AccountInfo < ' a > ,
144- space : usize ,
145- seeds : & [ & [ u8 ] ] ,
146- ) -> Result < ( ) , ProgramError > {
147- let allocate_instruction = allocate ( account. key , try_convert ( space) ?) ;
148- invoke_signed (
149- & allocate_instruction,
150- & [ account. clone ( ) , system_program. clone ( ) ] ,
151- & [ seeds] ,
152- ) ?;
153- Ok ( ( ) )
154- }
155-
156- /// Given a newly created `AccountInfo`, assign the owner to the given program id.
157- fn assign_owner < ' a > (
158- account : & AccountInfo < ' a > ,
159140 owner : & Pubkey ,
160- system_program : & AccountInfo < ' a > ,
141+ space : usize ,
142+ lamports : u64 ,
161143 seeds : & [ & [ u8 ] ] ,
162144) -> Result < ( ) , ProgramError > {
163- let assign_instruction = assign ( account . key , owner) ;
145+ let create_instruction = create_account ( from . key , to . key , lamports , try_convert ( space ) ? , owner) ;
164146 invoke_signed (
165- & assign_instruction ,
166- & [ account . clone ( ) , system_program. clone ( ) ] ,
147+ & create_instruction ,
148+ & [ from . clone ( ) , to . clone ( ) , system_program. clone ( ) ] ,
167149 & [ seeds] ,
168150 ) ?;
169151 Ok ( ( ) )
0 commit comments