Skip to content

Commit c81a420

Browse files
author
Stanisław Drozd
authored
pyth2wormhole/migrate: add pythnet rent adjustment (#315)
* pyth2wormhole/migrate: add pythnet rent adjustment * p2w/migrate: leave a note about rent calculation
1 parent 983813d commit c81a420

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

solana/pyth2wormhole/program/src/migrate.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
use solana_program::{
44
program_error::ProgramError,
55
pubkey::Pubkey,
6+
rent::Rent,
7+
system_instruction,
68
system_program,
9+
sysvar::Sysvar,
10+
program::invoke,
711
};
812

913
use solitaire::{
1014
trace,
1115
AccountState,
16+
AccountSize,
1217
CreationLamports,
1318
ExecutionContext,
1419
FromAccounts,
@@ -70,18 +75,30 @@ pub fn migrate(ctx: &ExecutionContext, accs: &mut Migrate, data: ()) -> SoliResu
7075
));
7176
}
7277

73-
// NOTE(2022-09-06): This instruction does not contain the temporary rent
74-
// adjustment snippet necessary for PythNet compatibility. This
75-
// was done to minimize the footprint of this rather hacky
76-
// workaround. In case the migrate ix needs to be ran in a
77-
// weird-rent environment, copy the rent due snippet and adjust it
78-
// to work against `accs.new_config`.
79-
8078
// Populate new config
8179
accs.new_config
8280
.create(ctx, accs.payer.info().key, CreationLamports::Exempt)?;
8381
accs.new_config.1 = Pyth2WormholeConfig::from(old_config.clone());
8482

83+
// Adjust new config lamports
84+
// NOTE(2022-09-29): Necessary due to PythNet rent calculation
85+
// differences, remove when solitaire supports Rent::get()?
86+
let mut acc_lamports = accs.new_config.info().lamports();
87+
88+
let new_lamports = Rent::get()?.minimum_balance(accs.new_config.size());
89+
90+
let diff_lamports: u64 = (acc_lamports as i64 - new_lamports as i64).abs() as u64;
91+
92+
if acc_lamports < new_lamports {
93+
// Less than enough lamports, debit the payer
94+
let transfer_ix = system_instruction::transfer(
95+
accs.payer.info().key,
96+
accs.new_config.info().key,
97+
diff_lamports,
98+
);
99+
invoke(&transfer_ix, ctx.accounts)?;
100+
}
101+
85102
// Reclaim old config lamports
86103

87104
// Save current balance

0 commit comments

Comments
 (0)