Skip to content

Commit 94aab8a

Browse files
author
Stanisław Drozd
authored
Drozdziak1/p2w init rent adjustment (#269)
* pyth2wormhole: add rent adjustment to the initialize() instruction * pyth2wormhole migrate(): Leave a note about rent adjustment The migrate() ix won't be called in the nearest future - it does not make sense to bloat the program bytecode with the rent adjustment. The note describes the what and why in detail.
1 parent 65c273f commit 94aab8a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

solana/pyth2wormhole/program/src/initialize.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use solana_program::pubkey::Pubkey;
1+
use solana_program::{
2+
program::invoke,
3+
pubkey::Pubkey,
4+
rent::Rent,
5+
system_instruction,
6+
sysvar::Sysvar,
7+
};
28
use solitaire::{
39
trace,
410
AccountState,
@@ -35,5 +41,21 @@ pub fn initialize(
3541
.create(ctx, accs.payer.info().key, CreationLamports::Exempt)?;
3642
accs.new_config.1 = data;
3743

44+
// TODO(2022-09-05): Remove this rent collection after
45+
// sysvar-based rent calculation becomes mainline in Solitaire.
46+
let config_balance = accs.new_config.info().lamports();
47+
let config_rent_exempt = Rent::get()?.minimum_balance(accs.new_config.info().data_len());
48+
49+
if config_balance < config_rent_exempt {
50+
let required_deposit = config_rent_exempt - config_balance;
51+
52+
let transfer_ix = system_instruction::transfer(
53+
accs.payer.key,
54+
accs.new_config.info().key,
55+
required_deposit,
56+
);
57+
invoke(&transfer_ix, ctx.accounts)?
58+
}
59+
3860
Ok(())
3961
}

solana/pyth2wormhole/program/src/migrate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ pub fn migrate(ctx: &ExecutionContext, accs: &mut Migrate, data: ()) -> SoliResu
7070
));
7171
}
7272

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+
7380
// Populate new config
7481
accs.new_config
7582
.create(ctx, accs.payer.info().key, CreationLamports::Exempt)?;

0 commit comments

Comments
 (0)