@@ -65,17 +65,28 @@ pub fn resize_or_reallocate_account_raw<'a>(
65
65
) -> ProgramResult {
66
66
let rent = Rent :: get ( ) ?;
67
67
let new_minimum_balance = rent. minimum_balance ( new_size) ;
68
+ let current_ta_lamports = target_account. lamports ( ) ;
69
+ let account_infos = & [
70
+ funding_account. clone ( ) ,
71
+ target_account. clone ( ) ,
72
+ system_program. clone ( ) ,
73
+ ] ;
68
74
69
- let lamports_diff = new_minimum_balance. saturating_sub ( target_account. lamports ( ) ) ;
70
- invoke (
71
- & system_instruction:: transfer ( funding_account. key , target_account. key , lamports_diff) ,
72
- & [
73
- funding_account. clone ( ) ,
74
- target_account. clone ( ) ,
75
- system_program. clone ( ) ,
76
- ] ,
77
- ) ?;
78
-
75
+ // account will be shrunk
76
+ if target_account. data_len ( ) > new_size {
77
+ let lamports_diff = new_minimum_balance. saturating_sub ( current_ta_lamports) ;
78
+ invoke (
79
+ & system_instruction:: transfer ( funding_account. key , target_account. key , lamports_diff) ,
80
+ account_infos,
81
+ ) ?;
82
+ } else {
83
+ // account will be extended
84
+ let excess_lamports = current_ta_lamports. saturating_sub ( new_minimum_balance) ;
85
+ invoke (
86
+ & system_instruction:: transfer ( target_account. key , funding_account. key , excess_lamports) ,
87
+ account_infos,
88
+ ) ?;
89
+ }
79
90
target_account. realloc ( new_size, false ) ?;
80
91
81
92
Ok ( ( ) )
0 commit comments