Skip to content

Commit

Permalink
spl: add spl token approve checked wrapper (#2401)
Browse files Browse the repository at this point in the history
* add spl token approve checked wrapper

* update changelog
  • Loading branch information
callensm authored Feb 21, 2023
1 parent 37cc99c commit 9044b9b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
36 changes: 36 additions & 0 deletions spl/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>,
Expand Down

1 comment on commit 9044b9b

@vercel
Copy link

@vercel vercel bot commented on 9044b9b Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-lang.com
anchor-docs-200ms.vercel.app
www.anchor-lang.com

Please sign in to comment.