@@ -160,6 +160,59 @@ fn create_transient_stake_account<'a>(
160
160
)
161
161
}
162
162
163
+ /// Create an account on a program-derived address
164
+ fn create_pda_account < ' a > (
165
+ payer : & AccountInfo < ' a > ,
166
+ required_lamports : u64 ,
167
+ space : usize ,
168
+ owner : & Pubkey ,
169
+ system_program : & AccountInfo < ' a > ,
170
+ new_pda_account : & AccountInfo < ' a > ,
171
+ new_pda_signer_seeds : & [ & [ u8 ] ] ,
172
+ ) -> ProgramResult {
173
+ if new_pda_account. lamports ( ) > 0 {
174
+ let required_lamports = required_lamports. saturating_sub ( new_pda_account. lamports ( ) ) ;
175
+ if required_lamports > 0 {
176
+ invoke (
177
+ & system_instruction:: transfer ( payer. key , new_pda_account. key , required_lamports) ,
178
+ & [
179
+ payer. clone ( ) ,
180
+ new_pda_account. clone ( ) ,
181
+ system_program. clone ( ) ,
182
+ ] ,
183
+ ) ?;
184
+ }
185
+
186
+ invoke_signed (
187
+ & system_instruction:: allocate ( new_pda_account. key , space as u64 ) ,
188
+ & [ new_pda_account. clone ( ) , system_program. clone ( ) ] ,
189
+ & [ new_pda_signer_seeds] ,
190
+ ) ?;
191
+
192
+ invoke_signed (
193
+ & system_instruction:: assign ( new_pda_account. key , owner) ,
194
+ & [ new_pda_account. clone ( ) , system_program. clone ( ) ] ,
195
+ & [ new_pda_signer_seeds] ,
196
+ )
197
+ } else {
198
+ invoke_signed (
199
+ & system_instruction:: create_account (
200
+ payer. key ,
201
+ new_pda_account. key ,
202
+ required_lamports,
203
+ space as u64 ,
204
+ owner,
205
+ ) ,
206
+ & [
207
+ payer. clone ( ) ,
208
+ new_pda_account. clone ( ) ,
209
+ system_program. clone ( ) ,
210
+ ] ,
211
+ & [ new_pda_signer_seeds] ,
212
+ )
213
+ }
214
+ }
215
+
163
216
/// Program state handler.
164
217
pub struct Processor { }
165
218
impl Processor {
@@ -798,22 +851,19 @@ impl Processor {
798
851
] ;
799
852
800
853
// Fund the stake account with the minimum + rent-exempt balance
801
- let required_lamports = MINIMUM_ACTIVE_STAKE
802
- + rent. minimum_balance ( std :: mem :: size_of :: < stake :: state :: StakeState > ( ) ) ;
854
+ let space = std :: mem :: size_of :: < stake :: state :: StakeState > ( ) ;
855
+ let required_lamports = MINIMUM_ACTIVE_STAKE + rent. minimum_balance ( space ) ;
803
856
804
857
// Create new stake account
805
- invoke_signed (
806
- & system_instruction:: create_account (
807
- funder_info. key ,
808
- stake_info. key ,
809
- required_lamports,
810
- std:: mem:: size_of :: < stake:: state:: StakeState > ( ) as u64 ,
811
- & stake:: program:: id ( ) ,
812
- ) ,
813
- & [ funder_info. clone ( ) , stake_info. clone ( ) ] ,
814
- & [ stake_account_signer_seeds] ,
858
+ create_pda_account (
859
+ funder_info,
860
+ required_lamports,
861
+ space,
862
+ & stake:: program:: id ( ) ,
863
+ system_program_info,
864
+ stake_info,
865
+ stake_account_signer_seeds,
815
866
) ?;
816
-
817
867
invoke (
818
868
& stake:: instruction:: initialize (
819
869
stake_info. key ,
0 commit comments