use crate::errors::MultisigError;
use anchor_lang::prelude::*;
use anchor_lang::system_program;
pub fn create_account<'a, 'info>(
payer: &'a AccountInfo<'info>,
new_account: &'a AccountInfo<'info>,
system_program: &'a AccountInfo<'info>,
owner_program: &Pubkey,
rent: &Rent,
space: usize,
seeds: Vec<Vec<u8>>,
) -> Result<()> {
require_keys_eq!(
*system_program.key,
system_program::ID,
MultisigError::InvalidAccount
);
let current_lamports = **new_account.try_borrow_lamports()?;
if current_lamports == 0 {
anchor_lang::system_program::create_account(
CpiContext::new(
system_program.clone(),
anchor_lang::system_program::CreateAccount {
from: payer.clone(),
to: new_account.clone(),
},
)
.with_signer(&[seeds
.iter()
.map(|seed| seed.as_slice())
.collect::<Vec<&[u8]>>()
.as_slice()]),
rent.minimum_balance(space),
space as u64,
owner_program,
)
} else {
require_keys_neq!(
payer.key(),
new_account.key(),
anchor_lang::error::ErrorCode::TryingToInitPayerAsProgramAccount
);
let required_lamports = rent
.minimum_balance(space)
.max(1)
.saturating_sub(current_lamports);
if required_lamports > 0 {
anchor_lang::system_program::transfer(
CpiContext::new(
system_program.clone(),
anchor_lang::system_program::Transfer {
from: payer.clone(),
to: new_account.clone(),
},
),
required_lamports,
)?;
}
anchor_lang::system_program::allocate(
CpiContext::new(
system_program.to_account_info(),
anchor_lang::system_program::Allocate {
account_to_allocate: new_account.clone(),
},
)
.with_signer(&[seeds
.iter()
.map(|seed| seed.as_slice())
.collect::<Vec<&[u8]>>()
.as_slice()]),
space as u64,
)?;
anchor_lang::system_program::assign(
CpiContext::new(
system_program.to_account_info(),
anchor_lang::system_program::Assign {
account_to_assign: new_account.clone(),
},
)
.with_signer(&[seeds
.iter()
.map(|seed| seed.as_slice())
.collect::<Vec<&[u8]>>()
.as_slice()]),
owner_program,
)
}
}
pub fn close<'info>(info: AccountInfo<'info>, sol_destination: AccountInfo<'info>) -> Result<()> {
let dest_starting_lamports = sol_destination.lamports();
**sol_destination.lamports.borrow_mut() =
dest_starting_lamports.checked_add(info.lamports()).unwrap();
**info.lamports.borrow_mut() = 0;
info.assign(&system_program::ID);
info.realloc(0, false).map_err(Into::into)
}