Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix money returns caused by an account shrinking #1173

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions core/rust/utils/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,28 @@ 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 account_infos = &[
funding_account.clone(),
target_account.clone(),
system_program.clone(),
];

let lamports_diff = new_minimum_balance.saturating_sub(target_account.lamports());
invoke(
&system_instruction::transfer(funding_account.key, target_account.key, lamports_diff),
&[
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);
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);
invoke(
&system_instruction::transfer(target_account.key, funding_account.key, excess_lamports),
account_infos,
)?;
}
target_account.realloc(new_size, false)?;

Ok(())
Expand Down
Loading