Skip to content

spl: add spl token approve checked wrapper #2401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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