deregister fixes

This commit is contained in:
Hardhat Chad
2024-05-13 15:06:14 +00:00
parent 56e20bf78e
commit a71024cfa1
3 changed files with 26 additions and 36 deletions

View File

@@ -1,14 +1,13 @@
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed,
program_error::ProgramError, pubkey::Pubkey, system_program,
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey, system_program,
};
use crate::{loaders::*, state::Proof, utils::AccountDeserialize, PROOF};
use crate::{loaders::*, state::Proof, utils::AccountDeserialize};
/// Deregister closes a proof account and returns the rent to the owner. Its responsibilities include:
/// 1. Realloc proof account size to 0.
/// 2. Transfer lamports to the owner.
/// 3. Reassign the account owner back to the system program.
///
/// Safety requirements:
/// - Deregister is a permissionless instruction and can be invoked by any singer.
@@ -33,31 +32,14 @@ pub fn process_deregister<'a, 'info>(
if proof.balance.gt(&0) {
return Err(ProgramError::InvalidAccountData);
}
// Generate bump
let bump = Pubkey::find_program_address(&[PROOF, signer.key.as_ref()], &crate::id()).1;
drop(proof_data);
// Realloc data to zero
drop(proof_data);
proof_info.realloc(0, false)?;
proof_info.realloc(0, true)?;
// Send lamports to signer
invoke_signed(
&solana_program::system_instruction::transfer(
proof_info.key,
signer.key,
proof_info.lamports(),
),
&[proof_info.clone(), signer.clone(), system_program.clone()],
&[&[PROOF, signer.key.as_ref(), &[bump]]],
)?;
// Reassign back to system program
solana_program::program::invoke_signed(
&solana_program::system_instruction::assign(proof_info.key, &system_program::id()),
&[proof_info.clone(), system_program.clone()],
&[&[PROOF, signer.key.as_ref(), &[bump]]],
)?;
**signer.lamports.borrow_mut() = signer.lamports().saturating_add(proof_info.lamports());
**proof_info.lamports.borrow_mut() = 0;
Ok(())
}