Skip to content

Commit

Permalink
spl: Add burn_checked, mint_to_checked and approve_checked inst…
Browse files Browse the repository at this point in the history
…ructions (#3186)
  • Loading branch information
deanmlittle authored Aug 21, 2024
1 parent 546945b commit 733b77a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Support non-8-byte discriminators ([#3165](https://github.com/coral-xyz/anchor/pull/3165)).
- idl: Disallow empty discriminators ([#3166](https://github.com/coral-xyz/anchor/pull/3166)).
- cli: Add `--no-idl` option to the `test` command ([#3175](https://github.com/coral-xyz/anchor/pull/3175)).
- spl: Add `burn_checked`, `mint_to_checked` and `approve_checked` instructions ([#3186]([https://github.com/coral-xyz/anchor/pull/3186)).

### Fixes

Expand Down
94 changes: 94 additions & 0 deletions spl/src/token_2022.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ pub fn mint_to<'info>(
.map_err(Into::into)
}

pub fn mint_to_checked<'info>(
ctx: CpiContext<'_, '_, '_, 'info, MintToChecked<'info>>,
amount: u64,
decimals: u8,
) -> Result<()> {
let ix = spl_token_2022::instruction::mint_to_checked(
ctx.program.key,
ctx.accounts.mint.key,
ctx.accounts.to.key,
ctx.accounts.authority.key,
&[],
amount,
decimals,
)?;
anchor_lang::solana_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 = spl_token_2022::instruction::burn(
ctx.program.key,
Expand All @@ -96,6 +118,28 @@ pub fn burn<'info>(ctx: CpiContext<'_, '_, '_, 'info, Burn<'info>>, amount: u64)
.map_err(Into::into)
}

pub fn burn_checked<'info>(
ctx: CpiContext<'_, '_, '_, 'info, BurnChecked<'info>>,
amount: u64,
decimals: u8,
) -> Result<()> {
let ix = spl_token_2022::instruction::burn_checked(
ctx.program.key,
ctx.accounts.from.key,
ctx.accounts.mint.key,
ctx.accounts.authority.key,
&[],
amount,
decimals,
)?;
anchor_lang::solana_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,
Expand All @@ -120,6 +164,34 @@ pub fn approve<'info>(
.map_err(Into::into)
}

pub fn approve_checked<'info>(
ctx: CpiContext<'_, '_, '_, 'info, ApproveChecked<'info>>,
amount: u64,
decimals: u8,
) -> Result<()> {
let ix = spl_token_2022::instruction::approve_checked(
ctx.program.key,
ctx.accounts.to.key,
ctx.accounts.mint.key,
ctx.accounts.delegate.key,
ctx.accounts.authority.key,
&[],
amount,
decimals,
)?;
anchor_lang::solana_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 = spl_token_2022::instruction::revoke(
ctx.program.key,
Expand Down Expand Up @@ -411,20 +483,42 @@ pub struct MintTo<'info> {
pub authority: AccountInfo<'info>,
}

#[derive(Accounts)]
pub struct MintToChecked<'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 BurnChecked<'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>,
Expand Down

0 comments on commit 733b77a

Please sign in to comment.