Skip to content

Commit

Permalink
Following best practices for closing accounts (#1187)
Browse files Browse the repository at this point in the history
* Following best practices for closing accounts.

* Removing unwrap.

* Switching to try borrow.
  • Loading branch information
blockiosaurus authored Oct 31, 2024
1 parent 520e448 commit 0ca5009
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 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 @@ -108,13 +108,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)
}

0 comments on commit 0ca5009

Please sign in to comment.