Browse Source

spl: Add `burn_checked`, `mint_to_checked` and `approve_checked` instructions (#3186)

Dean 利迪恩 1 year ago
parent
commit
733b77a990
2 changed files with 95 additions and 0 deletions
  1. 1 0
      CHANGELOG.md
  2. 94 0
      spl/src/token_2022.rs

+ 1 - 0
CHANGELOG.md

@@ -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
 

+ 94 - 0
spl/src/token_2022.rs

@@ -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,
@@ -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,
@@ -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,
@@ -411,6 +483,13 @@ 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>,
@@ -418,6 +497,13 @@ pub struct Burn<'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>,
@@ -425,6 +511,14 @@ pub struct Approve<'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>,