Skip to content

Commit

Permalink
fixed behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
kstepanovdev committed Mar 5, 2024
1 parent cdc1681 commit 7046136
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions core/rust/utils/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use solana_program::{
entrypoint::ProgramResult,
msg,
program::{invoke, invoke_signed},
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
system_instruction,
Expand Down Expand Up @@ -65,31 +66,38 @@ pub fn resize_or_reallocate_account_raw<'a>(
) -> ProgramResult {
let rent = Rent::get()?;
let new_minimum_balance = rent.minimum_balance(new_size);
let current_ta_lamports = target_account.lamports();
let lamports_diff =
new_minimum_balance.abs_diff(target_account.lamports());
if new_size == target_account.data_len() {
return Ok(());
}

let account_infos = &[
funding_account.clone(),
target_account.clone(),
system_program.clone(),
];

// account will be shrunk
if target_account.data_len() > new_size {
let lamports_diff = new_minimum_balance.saturating_sub(current_ta_lamports);
if new_size > target_account.data_len() {
invoke(
&system_instruction::transfer(funding_account.key, target_account.key, lamports_diff),
account_infos,
)?;
} else {
// account will be extended
let excess_lamports = current_ta_lamports.saturating_sub(new_minimum_balance);
} else if target_account.owner == system_program.owner {
invoke(
&system_instruction::transfer(target_account.key, funding_account.key, excess_lamports),
&system_instruction::transfer(target_account.key, funding_account.key, lamports_diff),
account_infos,
)?;
} else {
(**target_account.try_borrow_mut_lamports()?)
.checked_sub(lamports_diff)
.ok_or(ProgramError::InvalidRealloc)?;
(**funding_account.try_borrow_mut_lamports()?)
.checked_add(lamports_diff)
.ok_or(ProgramError::InvalidRealloc)?;
}
target_account.realloc(new_size, false)?;

Ok(())
target_account.realloc(new_size, false)
}

/// Close src_account and transfer lamports to dst_account, lifted from Solana Cookbook
Expand Down

0 comments on commit 7046136

Please sign in to comment.