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

Following best practices for closing accounts #1187

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Changes from all commits
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
24 changes: 15 additions & 9 deletions core/rust/utils/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use solana_program::{
program_error::ProgramError,
pubkey::Pubkey,
rent::Rent,
system_instruction,
system_instruction, system_program,
sysvar::Sysvar,
};

Expand Down Expand Up @@ -66,8 +66,7 @@ pub fn resize_or_reallocate_account_raw<'a>(
) -> ProgramResult {
let rent = Rent::get()?;
let new_minimum_balance = rent.minimum_balance(new_size);
let lamports_diff =
new_minimum_balance.abs_diff(target_account.lamports());
let lamports_diff = new_minimum_balance.abs_diff(target_account.lamports());
if new_size == target_account.data_len() {
return Ok(());
}
Expand Down Expand Up @@ -106,13 +105,20 @@ pub fn close_account_raw<'a>(
src_account_info: &AccountInfo<'a>,
) -> ProgramResult {
let dest_starting_lamports = dest_account_info.lamports();
**dest_account_info.lamports.borrow_mut() = dest_starting_lamports
let mut dest_lamports_mut = dest_account_info
.lamports
.try_borrow_mut()
.map_err(|_| ProgramError::AccountBorrowFailed)?;
**dest_lamports_mut = dest_starting_lamports
.checked_add(src_account_info.lamports())
.unwrap();
**src_account_info.lamports.borrow_mut() = 0;
.ok_or(ProgramError::InvalidRealloc)?;

let mut src_data = src_account_info.data.borrow_mut();
src_data.fill(0);
let mut src_lamports_mut = src_account_info
.lamports
.try_borrow_mut()
.map_err(|_| ProgramError::AccountBorrowFailed)?;
**src_lamports_mut = 0;

Ok(())
src_account_info.assign(&system_program::ID);
src_account_info.realloc(0, false).map_err(Into::into)
}
Loading