use dos safe account creation code from spl-token (#20)

* use dos safe account creation code from spl-token

* format
This commit is contained in:
Maximilian Schneider 2024-10-14 13:23:06 +03:00 committed by GitHub
parent cfac317b43
commit 1bf5e6e2b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 20 deletions

View File

@ -0,0 +1,66 @@
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::program::invoke;
use solana_program::program::invoke_signed;
use solana_program::pubkey::Pubkey;
use solana_program::rent::Rent;
use solana_program::system_instruction;
/// Creates associated token account using Program Derived Address for the given
/// seeds
/// source: https://github.com/solana-labs/solana-program-library/blob/87b905c91f9c17581f6696512f20abb43ade13c8/associated-token-account/program/src/tools/account.rs#L19
pub fn create_pda_account<'a>(
payer: &AccountInfo<'a>,
rent: &Rent,
space: usize,
owner: &Pubkey,
system_program: &AccountInfo<'a>,
new_pda_account: &AccountInfo<'a>,
new_pda_signer_seeds: &[&[u8]],
) -> ProgramResult {
if new_pda_account.lamports() > 0 {
let required_lamports = rent
.minimum_balance(space)
.max(1)
.saturating_sub(new_pda_account.lamports());
if required_lamports > 0 {
invoke(
&system_instruction::transfer(payer.key, new_pda_account.key, required_lamports),
&[
payer.clone(),
new_pda_account.clone(),
system_program.clone(),
],
)?;
}
invoke_signed(
&system_instruction::allocate(new_pda_account.key, space as u64),
&[new_pda_account.clone(), system_program.clone()],
&[new_pda_signer_seeds],
)?;
invoke_signed(
&system_instruction::assign(new_pda_account.key, owner),
&[new_pda_account.clone(), system_program.clone()],
&[new_pda_signer_seeds],
)
} else {
invoke_signed(
&system_instruction::create_account(
payer.key,
new_pda_account.key,
rent.minimum_balance(space).max(1),
space as u64,
owner,
),
&[
payer.clone(),
new_pda_account.clone(),
system_program.clone(),
],
&[new_pda_signer_seeds],
)
}
}

View File

@ -1,12 +1,14 @@
use solana_program::account_info::AccountInfo;
use solana_program::entrypoint::ProgramResult;
use solana_program::program::{invoke, invoke_signed};
use solana_program::program::invoke;
use solana_program::program_error::ProgramError;
use solana_program::program_pack::Pack;
use solana_program::pubkey::Pubkey;
use solana_program::rent::Rent;
use solana_program::system_instruction;
use solana_program::system_program;
use solana_program::sysvar::Sysvar;
use crate::create_pda::create_pda_account;
pub fn execute_create_referral(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
if let [payer, referrer, vault, mint, system_program, token_program] = accounts {
@ -34,32 +36,24 @@ pub fn execute_create_referral(accounts: &[AccountInfo], instruction_data: &[u8]
return Err(ProgramError::InvalidSeeds);
}
// fund account with rent
let space = spl_token::state::Account::LEN;
let lamports = Rent::default().minimum_balance(space);
create_pda_account(
payer,
&Rent::get()?,
spl_token::state::Account::LEN,
&spl_token::ID,
system_program,
vault,
&vault_seeds,
)?;
let create_account_ix = system_instruction::create_account(
payer.key,
vault.key,
lamports,
space as u64,
token_program.key,
);
let create_account_infos = [payer.clone(), vault.clone(), system_program.clone()];
invoke_signed(&create_account_ix, &create_account_infos, &[&vault_seeds])?;
// Initialize the token account for the vault
let initialize_ix = spl_token::instruction::initialize_account3(
token_program.key,
&spl_token::ID,
vault.key,
mint.key,
vault.key,
)?;
let initialize_account_infos = [vault.clone(), mint.clone(), token_program.clone()];
invoke(&initialize_ix, &initialize_account_infos)?;
Ok(())

View File

@ -1,4 +1,5 @@
mod instructions;
pub mod create_pda;
pub mod logs;
pub mod swap_ix;
pub mod utils;