forked from coal-digital/coal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
close.rs
35 lines (29 loc) · 1.11 KB
/
close.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use coal_api::{loaders::*, state::Proof};
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
system_program,
};
use crate::utils::AccountDeserialize;
/// Close closes a proof account and returns the rent to the owner.
pub fn process_close<'a, 'info>(accounts: &'a [AccountInfo<'info>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let [signer, proof_info, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_proof(proof_info, signer.key, true)?;
load_program(system_program, system_program::id())?;
// Validate balance is zero.
let proof_data = proof_info.data.borrow();
let proof = Proof::try_from_bytes(&proof_data)?;
if proof.balance.gt(&0) {
return Err(ProgramError::InvalidAccountData);
}
drop(proof_data);
// Realloc data to zero.
proof_info.realloc(0, true)?;
// Send remaining lamports to signer.
**signer.lamports.borrow_mut() += proof_info.lamports();
**proof_info.lamports.borrow_mut() = 0;
Ok(())
}