9
9
check_valid_fresh_account,
10
10
get_rent,
11
11
pyth_assert,
12
- send_lamports,
13
12
try_convert,
14
13
} ,
15
14
} ,
23
22
program_error:: ProgramError ,
24
23
program_memory:: sol_memset,
25
24
pubkey:: Pubkey ,
26
- system_instruction:: {
27
- allocate,
28
- assign,
29
- } ,
25
+ system_instruction:: create_account,
30
26
} ,
31
27
std:: {
32
28
borrow:: BorrowMut ,
@@ -107,7 +103,9 @@ pub trait PythAccount: Pod {
107
103
load_account_as_mut :: < Self > ( account)
108
104
}
109
105
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.
111
109
fn initialize_pda < ' a > (
112
110
account : & AccountInfo < ' a > ,
113
111
funding_account : & AccountInfo < ' a > ,
@@ -118,52 +116,36 @@ pub trait PythAccount: Pod {
118
116
) -> Result < ( ) , ProgramError > {
119
117
let target_rent = get_rent ( ) ?. minimum_balance ( Self :: MINIMUM_SIZE ) ;
120
118
121
- if account. lamports ( ) < target_rent {
122
- send_lamports (
119
+ if account. data_len ( ) == 0 {
120
+ create (
123
121
funding_account,
124
122
account,
125
123
system_program,
126
- target_rent - account. lamports ( ) ,
124
+ program_id,
125
+ Self :: MINIMUM_SIZE ,
126
+ target_rent,
127
+ seeds,
127
128
) ?;
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) ?;
133
129
Self :: initialize ( account, version) ?;
134
130
}
131
+
135
132
Ok ( ( ) )
136
133
}
137
134
}
138
135
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 > ,
143
139
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 > ,
159
140
owner : & Pubkey ,
160
- system_program : & AccountInfo < ' a > ,
141
+ space : usize ,
142
+ lamports : u64 ,
161
143
seeds : & [ & [ u8 ] ] ,
162
144
) -> 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) ;
164
146
invoke_signed (
165
- & assign_instruction ,
166
- & [ account . clone ( ) , system_program. clone ( ) ] ,
147
+ & create_instruction ,
148
+ & [ from . clone ( ) , to . clone ( ) , system_program. clone ( ) ] ,
167
149
& [ seeds] ,
168
150
) ?;
169
151
Ok ( ( ) )
0 commit comments