From 3d030ce44e2f07faa7e656b34e4087ee8d9afb5d Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 6 Sep 2022 21:33:17 +0800 Subject: [PATCH] feat: Use new canonical close account pattern (#2169) --- CHANGELOG.md | 1 + lang/src/common.rs | 14 +++----------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9e440ec08..fc89299412 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi * spl: Add `create_metadata_accounts_v3` and `set_collection_size` wrappers ([#2119](https://github.com/coral-xyz/anchor/pull/2119)) * spl: Add `MetadataAccount` account deserialization. ([#2014](https://github.com/coral-xyz/anchor/pull/2014)). * lang: Add parsing for consts from impl blocks for IDL PDA seeds generation ([#2128](https://github.com/coral-xyz/anchor/pull/2014)) +* lang: Account closing reassigns to system program and reallocates ([#2169](https://github.com/coral-xyz/anchor/pull/2169)). * ts: Add coders for SPL programs ([#2143](https://github.com/coral-xyz/anchor/pull/2143)). ### Fixes diff --git a/lang/src/common.rs b/lang/src/common.rs index 365d1281e6..db58366574 100644 --- a/lang/src/common.rs +++ b/lang/src/common.rs @@ -1,9 +1,6 @@ -use crate::bpf_writer::BpfWriter; -use crate::error::ErrorCode; -use crate::prelude::error; use crate::Result; use solana_program::account_info::AccountInfo; -use std::io::Write; +use solana_program::system_program; pub fn close<'info>(info: AccountInfo<'info>, sol_destination: AccountInfo<'info>) -> Result<()> { // Transfer tokens from the account to the sol_destination. @@ -12,11 +9,6 @@ pub fn close<'info>(info: AccountInfo<'info>, sol_destination: AccountInfo<'info dest_starting_lamports.checked_add(info.lamports()).unwrap(); **info.lamports.borrow_mut() = 0; - // Mark the account discriminator as closed. - let mut data = info.try_borrow_mut_data()?; - let dst: &mut [u8] = &mut data; - let mut writer = BpfWriter::new(dst); - writer - .write_all(&crate::__private::CLOSED_ACCOUNT_DISCRIMINATOR) - .map_err(|_| error!(ErrorCode::AccountDidNotSerialize)) + info.assign(&system_program::ID); + info.realloc(0, false).map_err(Into::into) }