|
3 | 3 | use solana_program::{
|
4 | 4 | program_error::ProgramError,
|
5 | 5 | pubkey::Pubkey,
|
| 6 | + rent::Rent, |
| 7 | + system_instruction, |
6 | 8 | system_program,
|
| 9 | + sysvar::Sysvar, |
| 10 | + program::invoke, |
7 | 11 | };
|
8 | 12 |
|
9 | 13 | use solitaire::{
|
10 | 14 | trace,
|
11 | 15 | AccountState,
|
| 16 | + AccountSize, |
12 | 17 | CreationLamports,
|
13 | 18 | ExecutionContext,
|
14 | 19 | FromAccounts,
|
@@ -70,18 +75,30 @@ pub fn migrate(ctx: &ExecutionContext, accs: &mut Migrate, data: ()) -> SoliResu
|
70 | 75 | ));
|
71 | 76 | }
|
72 | 77 |
|
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 |
| - |
80 | 78 | // Populate new config
|
81 | 79 | accs.new_config
|
82 | 80 | .create(ctx, accs.payer.info().key, CreationLamports::Exempt)?;
|
83 | 81 | accs.new_config.1 = Pyth2WormholeConfig::from(old_config.clone());
|
84 | 82 |
|
| 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 | + |
85 | 102 | // Reclaim old config lamports
|
86 | 103 |
|
87 | 104 | // Save current balance
|
|
0 commit comments