use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed,
};
pub fn spl_token_burn(params: TokenBurnParams<'_, '_>) -> ProgramResult {
let TokenBurnParams {
mint,
source,
authority,
token_program,
amount,
authority_signer_seeds,
} = params;
let mut seeds: Vec<&[&[u8]]> = vec![];
if let Some(seed) = authority_signer_seeds {
seeds.push(seed);
}
invoke_signed(
&spl_token::instruction::burn(
token_program.key,
source.key,
mint.key,
authority.key,
&[authority.key],
amount,
)?,
&[source, mint, authority],
seeds.as_slice(),
)
}
pub struct TokenBurnParams<'a: 'b, 'b> {
pub mint: AccountInfo<'a>,
pub source: AccountInfo<'a>,
pub amount: u64,
pub authority: AccountInfo<'a>,
pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
pub token_program: AccountInfo<'a>,
}
pub fn spl_token_close(params: TokenCloseParams<'_, '_>) -> ProgramResult {
let TokenCloseParams {
account,
destination,
owner,
authority_signer_seeds,
token_program,
} = params;
let mut seeds: Vec<&[&[u8]]> = vec![];
if let Some(seed) = authority_signer_seeds {
seeds.push(seed);
}
invoke_signed(
&spl_token::instruction::close_account(
token_program.key,
account.key,
destination.key,
owner.key,
&[],
)?,
&[account, destination, owner, token_program],
seeds.as_slice(),
)
}
pub struct TokenCloseParams<'a: 'b, 'b> {
pub account: AccountInfo<'a>,
pub destination: AccountInfo<'a>,
pub owner: AccountInfo<'a>,
pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
pub token_program: AccountInfo<'a>,
}
pub fn spl_token_mint_to(params: TokenMintToParams<'_, '_>) -> ProgramResult {
let TokenMintToParams {
mint,
destination,
authority,
token_program,
amount,
authority_signer_seeds,
} = params;
let mut seeds: Vec<&[&[u8]]> = vec![];
if let Some(seed) = authority_signer_seeds {
seeds.push(seed);
}
invoke_signed(
&spl_token::instruction::mint_to(
token_program.key,
mint.key,
destination.key,
authority.key,
&[],
amount,
)?,
&[mint, destination, authority, token_program],
seeds.as_slice(),
)
}
pub struct TokenMintToParams<'a: 'b, 'b> {
pub mint: AccountInfo<'a>,
pub destination: AccountInfo<'a>,
pub amount: u64,
pub authority: AccountInfo<'a>,
pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
pub token_program: AccountInfo<'a>,
}
pub fn spl_token_transfer(params: TokenTransferParams<'_, '_>) -> ProgramResult {
let TokenTransferParams {
mint: _,
source,
destination,
amount,
authority,
token_program,
authority_signer_seeds,
} = params;
let mut seeds: Vec<&[&[u8]]> = vec![];
if let Some(seed) = authority_signer_seeds {
seeds.push(seed);
}
invoke_signed(
&spl_token::instruction::transfer(
token_program.key,
source.key,
destination.key,
authority.key,
&[authority.key],
amount,
)?,
&[source, destination, authority],
seeds.as_slice(),
)
}
#[derive(Debug)]
pub struct TokenTransferParams<'a: 'b, 'b> {
pub mint: AccountInfo<'a>,
pub source: AccountInfo<'a>,
pub destination: AccountInfo<'a>,
pub amount: u64,
pub authority: AccountInfo<'a>,
pub authority_signer_seeds: Option<&'b [&'b [u8]]>,
pub token_program: AccountInfo<'a>,
}