1
- use solana_program:: pubkey:: Pubkey ;
1
+ use borsh:: BorshSerialize ;
2
+
3
+ use solana_program:: {
4
+ program:: invoke,
5
+ program_error:: ProgramError ,
6
+ pubkey:: Pubkey ,
7
+ rent:: Rent ,
8
+ system_instruction,
9
+ sysvar:: Sysvar ,
10
+ } ;
2
11
use solitaire:: {
3
12
trace,
4
13
AccountState ,
@@ -18,6 +27,8 @@ use crate::config::{
18
27
Pyth2WormholeConfig ,
19
28
} ;
20
29
30
+ use std:: cmp:: Ordering ;
31
+
21
32
#[ derive( FromAccounts ) ]
22
33
pub struct SetConfig < ' b > {
23
34
/// Current config used by the program
@@ -26,11 +37,13 @@ pub struct SetConfig<'b> {
26
37
pub current_owner : Mut < Signer < Info < ' b > > > ,
27
38
/// Payer account for updating the account data
28
39
pub payer : Mut < Signer < Info < ' b > > > ,
40
+ /// Used for rent adjustment transfer
41
+ pub system_program : Info < ' b > ,
29
42
}
30
43
31
44
/// Alters the current settings of pyth2wormhole
32
45
pub fn set_config (
33
- _ctx : & ExecutionContext ,
46
+ ctx : & ExecutionContext ,
34
47
accs : & mut SetConfig ,
35
48
data : Pyth2WormholeConfig ,
36
49
) -> SoliResult < ( ) > {
@@ -45,7 +58,32 @@ pub fn set_config(
45
58
) ) ;
46
59
}
47
60
61
+ let old_size = accs. config . info ( ) . data_len ( ) ;
62
+ let new_size = data. try_to_vec ( ) ?. len ( ) ;
63
+
64
+ // Realloc if mismatched
65
+ if old_size != new_size {
66
+ accs. config . info ( ) . realloc ( new_size, false ) ?;
67
+ }
68
+
48
69
accs. config . 1 = data;
49
70
71
+ // Adjust lamports
72
+ let mut acc_lamports = accs. config . info ( ) . lamports ( ) ;
73
+
74
+ let new_lamports = Rent :: get ( ) ?. minimum_balance ( new_size) ;
75
+
76
+ let diff_lamports: u64 = ( acc_lamports as i64 - new_lamports as i64 ) . abs ( ) as u64 ;
77
+
78
+ if acc_lamports < new_lamports {
79
+ // Less than enough lamports, debit the payer
80
+ let transfer_ix = system_instruction:: transfer (
81
+ accs. payer . info ( ) . key ,
82
+ accs. config . info ( ) . key ,
83
+ diff_lamports,
84
+ ) ;
85
+ invoke ( & transfer_ix, ctx. accounts ) ?;
86
+ }
87
+
50
88
Ok ( ( ) )
51
89
}
0 commit comments