From 9044b9b8cde7be87cc9c1ca1867b9a5f2791e103 Mon Sep 17 00:00:00 2001 From: Matthew Callens Date: Tue, 21 Feb 2023 02:49:07 -0500 Subject: [PATCH] spl: add spl token approve checked wrapper (#2401) * add spl token approve checked wrapper * update changelog --- CHANGELOG.md | 1 + spl/src/token.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e44f669992..aadec8fadc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi - cli: Add `idl close` command to close a program's IDL account ([#2329](https://github.com/coral-xyz/anchor/pull/2329)). - cli: `idl init` now supports very large IDL files ([#2329](https://github.com/coral-xyz/anchor/pull/2329)). - spl: Add `transfer_checked` function ([#2353](https://github.com/coral-xyz/anchor/pull/2353)). +- spl: Add `approve_checked` function ([#2401](https://github.com/coral-xyz/anchor/pull/2401)). - cli: Add `--skip-build` option to the verify command ([#2387](https://github.com/coral-xyz/anchor/pull/2387)). - client: Add support for multithreading to the rust client: use flag `--multithreaded` ([#2321](https://github.com/coral-xyz/anchor/pull/2321)). - client: Add `async_rpc` a method which returns a nonblocking solana rpc client ([2322](https://github.com/coral-xyz/anchor/pull/2322)). diff --git a/spl/src/token.rs b/spl/src/token.rs index 14ad1179e4..fbb501cb9c 100644 --- a/spl/src/token.rs +++ b/spl/src/token.rs @@ -130,6 +130,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::instruction::approve_checked( + &spl_token::ID, + ctx.accounts.to.key, + ctx.accounts.mint.key, + ctx.accounts.delegate.key, + ctx.accounts.authority.key, + &[], + amount, + decimals, + )?; + solana_program::program::invoke_signed( + &ix, + &[ + ctx.accounts.to.clone(), + ctx.accounts.mint.clone(), + ctx.accounts.delegate.clone(), + ctx.accounts.authority.clone(), + ], + ctx.signer_seeds, + ) + .map_err(Into::into) +} + pub fn revoke<'info>(ctx: CpiContext<'_, '_, '_, 'info, Revoke<'info>>) -> Result<()> { let ix = spl_token::instruction::revoke( &spl_token::ID, @@ -355,6 +383,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>,