use satellite_lang::arch_program::account::AccountInfo;
use satellite_lang::arch_program::program_pack::Pack;
use satellite_lang::arch_program::pubkey::Pubkey;
use satellite_lang::Result;
use satellite_lang::{context::CpiContext, Accounts};
use std::ops::Deref;
pub use apl_token;
pub use apl_token::ID;
pub fn transfer<'info>(
ctx: CpiContext<'_, '_, '_, 'info, Transfer<'info>>,
amount: u64,
) -> Result<()> {
let ix = apl_token::instruction::transfer(
&apl_token::id(),
ctx.accounts.from.key,
ctx.accounts.to.key,
ctx.accounts.authority.key,
&[],
amount,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.from, ctx.accounts.to, ctx.accounts.authority],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn transfer_checked<'info>(
ctx: CpiContext<'_, '_, '_, 'info, TransferChecked<'info>>,
amount: u64,
decimals: u8,
) -> Result<()> {
let ix = apl_token::instruction::transfer_checked(
&apl_token::id(),
ctx.accounts.from.key,
ctx.accounts.mint.key,
ctx.accounts.to.key,
ctx.accounts.authority.key,
&[],
amount,
decimals,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.from,
ctx.accounts.mint,
ctx.accounts.to,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn mint_to<'info>(
ctx: CpiContext<'_, '_, '_, 'info, MintTo<'info>>,
amount: u64,
) -> Result<()> {
let ix = apl_token::instruction::mint_to(
&apl_token::id(),
ctx.accounts.mint.key,
ctx.accounts.to.key,
ctx.accounts.authority.key,
&[],
amount,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.to, ctx.accounts.mint, ctx.accounts.authority],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn burn<'info>(ctx: CpiContext<'_, '_, '_, 'info, Burn<'info>>, amount: u64) -> Result<()> {
let ix = apl_token::instruction::burn(
&apl_token::id(),
ctx.accounts.from.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
&[],
amount,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.from, ctx.accounts.mint, ctx.accounts.authority],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn approve<'info>(
ctx: CpiContext<'_, '_, '_, 'info, Approve<'info>>,
amount: u64,
) -> Result<()> {
let ix = apl_token::instruction::approve(
&apl_token::id(),
ctx.accounts.to.key,
ctx.accounts.delegate.key,
ctx.accounts.authority.key,
&[],
amount,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.to,
ctx.accounts.delegate,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn approve_checked<'info>(
ctx: CpiContext<'_, '_, '_, 'info, ApproveChecked<'info>>,
amount: u64,
decimals: u8,
) -> Result<()> {
let ix = apl_token::instruction::approve_checked(
&apl_token::id(),
ctx.accounts.to.key,
ctx.accounts.mint.key,
ctx.accounts.delegate.key,
ctx.accounts.authority.key,
&[],
amount,
decimals,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.to,
ctx.accounts.mint,
ctx.accounts.delegate,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn revoke<'info>(ctx: CpiContext<'_, '_, '_, 'info, Revoke<'info>>) -> Result<()> {
let ix = apl_token::instruction::revoke(
&apl_token::id(),
ctx.accounts.source.key,
ctx.accounts.authority.key,
&[],
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.source, ctx.accounts.authority],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn initialize_account<'info>(
ctx: CpiContext<'_, '_, '_, 'info, InitializeAccount<'info>>,
) -> Result<()> {
let ix = apl_token::instruction::initialize_account(
&apl_token::id(),
ctx.accounts.account.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.account,
ctx.accounts.mint,
ctx.accounts.authority,
ctx.accounts.rent,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn initialize_account3<'info>(
ctx: CpiContext<'_, '_, '_, 'info, InitializeAccount3<'info>>,
) -> Result<()> {
let ix = apl_token::instruction::initialize_account3(
&apl_token::id(),
ctx.accounts.account.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.account, ctx.accounts.mint],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn close_account<'info>(ctx: CpiContext<'_, '_, '_, 'info, CloseAccount<'info>>) -> Result<()> {
let ix = apl_token::instruction::close_account(
&apl_token::id(),
ctx.accounts.account.key,
ctx.accounts.destination.key,
ctx.accounts.authority.key,
&[], )?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.account,
ctx.accounts.destination,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn freeze_account<'info>(
ctx: CpiContext<'_, '_, '_, 'info, FreezeAccount<'info>>,
) -> Result<()> {
let ix = apl_token::instruction::freeze_account(
&apl_token::id(),
ctx.accounts.account.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
&[], )?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.account,
ctx.accounts.mint,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn thaw_account<'info>(ctx: CpiContext<'_, '_, '_, 'info, ThawAccount<'info>>) -> Result<()> {
let ix = apl_token::instruction::thaw_account(
&apl_token::id(),
ctx.accounts.account.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
&[], )?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[
ctx.accounts.account,
ctx.accounts.mint,
ctx.accounts.authority,
],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn initialize_mint<'info>(
ctx: CpiContext<'_, '_, '_, 'info, InitializeMint<'info>>,
decimals: u8,
authority: &Pubkey,
freeze_authority: Option<&Pubkey>,
) -> Result<()> {
let ix = apl_token::instruction::initialize_mint(
&apl_token::id(),
ctx.accounts.mint.key,
authority,
freeze_authority,
decimals,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.mint, ctx.accounts.rent],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn initialize_mint2<'info>(
ctx: CpiContext<'_, '_, '_, 'info, InitializeMint2<'info>>,
decimals: u8,
authority: &Pubkey,
freeze_authority: Option<&Pubkey>,
) -> Result<()> {
let ix = apl_token::instruction::initialize_mint2(
&apl_token::id(),
ctx.accounts.mint.key,
authority,
freeze_authority,
decimals,
)?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.mint],
ctx.signer_seeds,
)
.map_err(Into::into)
}
pub fn set_authority<'info>(
ctx: CpiContext<'_, '_, '_, 'info, SetAuthority<'info>>,
authority_type: apl_token::instruction::AuthorityType,
new_authority: Option<Pubkey>,
) -> Result<()> {
let ix = apl_token::instruction::set_authority(
&apl_token::id(),
ctx.accounts.account_or_mint.key,
new_authority.as_ref(),
authority_type,
ctx.accounts.current_authority.key,
&[], )?;
satellite_lang::arch_program::program::invoke_signed(
&ix,
&[ctx.accounts.account_or_mint, ctx.accounts.current_authority],
ctx.signer_seeds,
)
.map_err(Into::into)
}
#[derive(Accounts)]
pub struct Transfer<'info> {
pub from: AccountInfo<'info>,
pub to: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct TransferChecked<'info> {
pub from: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub to: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct MintTo<'info> {
pub mint: AccountInfo<'info>,
pub to: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct Burn<'info> {
pub mint: AccountInfo<'info>,
pub from: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct Approve<'info> {
pub to: AccountInfo<'info>,
pub delegate: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct ApproveChecked<'info> {
pub to: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub delegate: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct Revoke<'info> {
pub source: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct InitializeAccount<'info> {
pub account: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
pub rent: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct InitializeAccount3<'info> {
pub account: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct CloseAccount<'info> {
pub account: AccountInfo<'info>,
pub destination: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct FreezeAccount<'info> {
pub account: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct ThawAccount<'info> {
pub account: AccountInfo<'info>,
pub mint: AccountInfo<'info>,
pub authority: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct InitializeMint<'info> {
pub mint: AccountInfo<'info>,
pub rent: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct InitializeMint2<'info> {
pub mint: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct SetAuthority<'info> {
pub current_authority: AccountInfo<'info>,
pub account_or_mint: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct SyncNative<'info> {
pub account: AccountInfo<'info>,
}
#[derive(Clone, Debug, Default, PartialEq, Copy)]
pub struct TokenAccount(apl_token::state::Account);
impl TokenAccount {
pub const LEN: usize = apl_token::state::Account::LEN;
}
impl satellite_lang::AccountDeserialize for TokenAccount {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> satellite_lang::Result<Self> {
apl_token::state::Account::unpack(buf)
.map(TokenAccount)
.map_err(Into::into)
}
}
impl satellite_lang::AccountSerialize for TokenAccount {}
impl satellite_lang::Owner for TokenAccount {
fn owner() -> Pubkey {
apl_token::id()
}
}
static TOKEN_ACCOUNT_OWNERS: [Pubkey; 1] = [apl_token::ID];
impl satellite_lang::Owners for TokenAccount {
fn owners() -> &'static [Pubkey] {
&TOKEN_ACCOUNT_OWNERS
}
}
impl Deref for TokenAccount {
type Target = apl_token::state::Account;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Clone, Debug, Default, PartialEq, Copy)]
pub struct Mint(apl_token::state::Mint);
impl Mint {
pub const LEN: usize = apl_token::state::Mint::LEN;
}
impl satellite_lang::AccountDeserialize for Mint {
fn try_deserialize_unchecked(buf: &mut &[u8]) -> satellite_lang::Result<Self> {
apl_token::state::Mint::unpack(buf)
.map(Mint)
.map_err(Into::into)
}
}
impl satellite_lang::AccountSerialize for Mint {}
impl satellite_lang::Owner for Mint {
fn owner() -> Pubkey {
apl_token::id()
}
}
static MINT_OWNERS: [Pubkey; 1] = [apl_token::ID];
impl satellite_lang::Owners for Mint {
fn owners() -> &'static [Pubkey] {
&MINT_OWNERS
}
}
impl Deref for Mint {
type Target = apl_token::state::Mint;
fn deref(&self) -> &Self::Target {
&self.0
}
}
static IDS: [Pubkey; 1] = [Pubkey::new_from_array(*b"apl-token00000000000000000000000")];
#[derive(Clone)]
pub struct Token;
impl satellite_lang::Id for Token {
fn id() -> Pubkey {
apl_token::id()
}
}
impl satellite_lang::Ids for Token {
fn ids() -> &'static [Pubkey] {
&IDS
}
}
pub mod accessor {
use super::*;
pub fn amount(account: &AccountInfo) -> Result<u64> {
let bytes = account.try_borrow_data()?;
let mut amount_bytes = [0u8; 8];
amount_bytes.copy_from_slice(&bytes[64..72]);
Ok(u64::from_le_bytes(amount_bytes))
}
pub fn mint(account: &AccountInfo) -> Result<Pubkey> {
let bytes = account.try_borrow_data()?;
let mut mint_bytes = [0u8; 32];
mint_bytes.copy_from_slice(&bytes[..32]);
Ok(Pubkey::new_from_array(mint_bytes))
}
pub fn authority(account: &AccountInfo) -> Result<Pubkey> {
let bytes = account.try_borrow_data()?;
let mut owner_bytes = [0u8; 32];
owner_bytes.copy_from_slice(&bytes[32..64]);
Ok(Pubkey::new_from_array(owner_bytes))
}
}