p2w: Monitor and realloc() + rent adjuist on cfg account size change (#305)
* p2w: Monitor and realloc() + rent adjuist on cfg account size change * p2w/set_config: reorder data assignment and realloc()
This commit is contained in:
parent
6d528ffbe2
commit
b59bd702c2
|
@ -144,6 +144,8 @@ pub fn gen_set_config_tx(
|
||||||
AccountMeta::new(owner.pubkey(), true),
|
AccountMeta::new(owner.pubkey(), true),
|
||||||
// payer
|
// payer
|
||||||
AccountMeta::new(payer.pubkey(), true),
|
AccountMeta::new(payer.pubkey(), true),
|
||||||
|
// system_program
|
||||||
|
AccountMeta::new(system_program::id(), false),
|
||||||
];
|
];
|
||||||
|
|
||||||
let ix_data = (
|
let ix_data = (
|
||||||
|
@ -163,7 +165,6 @@ pub fn gen_set_config_tx(
|
||||||
Ok(tx_signed)
|
Ok(tx_signed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn gen_set_is_active_tx(
|
pub fn gen_set_is_active_tx(
|
||||||
payer: Keypair,
|
payer: Keypair,
|
||||||
p2w_addr: Pubkey,
|
p2w_addr: Pubkey,
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
use solana_program::pubkey::Pubkey;
|
use borsh::BorshSerialize;
|
||||||
|
|
||||||
|
use solana_program::{
|
||||||
|
program::invoke,
|
||||||
|
program_error::ProgramError,
|
||||||
|
pubkey::Pubkey,
|
||||||
|
rent::Rent,
|
||||||
|
system_instruction,
|
||||||
|
sysvar::Sysvar,
|
||||||
|
};
|
||||||
use solitaire::{
|
use solitaire::{
|
||||||
trace,
|
trace,
|
||||||
AccountState,
|
AccountState,
|
||||||
|
@ -18,6 +27,8 @@ use crate::config::{
|
||||||
Pyth2WormholeConfig,
|
Pyth2WormholeConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
#[derive(FromAccounts)]
|
#[derive(FromAccounts)]
|
||||||
pub struct SetConfig<'b> {
|
pub struct SetConfig<'b> {
|
||||||
/// Current config used by the program
|
/// Current config used by the program
|
||||||
|
@ -26,11 +37,13 @@ pub struct SetConfig<'b> {
|
||||||
pub current_owner: Mut<Signer<Info<'b>>>,
|
pub current_owner: Mut<Signer<Info<'b>>>,
|
||||||
/// Payer account for updating the account data
|
/// Payer account for updating the account data
|
||||||
pub payer: Mut<Signer<Info<'b>>>,
|
pub payer: Mut<Signer<Info<'b>>>,
|
||||||
|
/// Used for rent adjustment transfer
|
||||||
|
pub system_program: Info<'b>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Alters the current settings of pyth2wormhole
|
/// Alters the current settings of pyth2wormhole
|
||||||
pub fn set_config(
|
pub fn set_config(
|
||||||
_ctx: &ExecutionContext,
|
ctx: &ExecutionContext,
|
||||||
accs: &mut SetConfig,
|
accs: &mut SetConfig,
|
||||||
data: Pyth2WormholeConfig,
|
data: Pyth2WormholeConfig,
|
||||||
) -> SoliResult<()> {
|
) -> SoliResult<()> {
|
||||||
|
@ -45,7 +58,32 @@ pub fn set_config(
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let old_size = accs.config.info().data_len();
|
||||||
|
let new_size = data.try_to_vec()?.len();
|
||||||
|
|
||||||
|
// Realloc if mismatched
|
||||||
|
if old_size != new_size {
|
||||||
|
accs.config.info().realloc(new_size, false)?;
|
||||||
|
}
|
||||||
|
|
||||||
accs.config.1 = data;
|
accs.config.1 = data;
|
||||||
|
|
||||||
|
// Adjust lamports
|
||||||
|
let mut acc_lamports = accs.config.info().lamports();
|
||||||
|
|
||||||
|
let new_lamports = Rent::get()?.minimum_balance(new_size);
|
||||||
|
|
||||||
|
let diff_lamports: u64 = (acc_lamports as i64 - new_lamports as i64).abs() as u64;
|
||||||
|
|
||||||
|
if acc_lamports < new_lamports {
|
||||||
|
// Less than enough lamports, debit the payer
|
||||||
|
let transfer_ix = system_instruction::transfer(
|
||||||
|
accs.payer.info().key,
|
||||||
|
accs.config.info().key,
|
||||||
|
diff_lamports,
|
||||||
|
);
|
||||||
|
invoke(&transfer_ix, ctx.accounts)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue