Approve and Revoke Instructions
Approve grants a delegate the authority to transfer a specific amount of tokens on behalf of the account owner. This enables programmatic token transfers without giving full account control.
Revoke removes the current delegate's authority over the account, returning full control to the account owner.
Before we can delegate or revoke any token account, we'll need to already have:
- Initialized a
Mint
account. - Initialized a
Token
account orAssociated Token
account where we're going to take control of
This is how the CPI to the approve()
instruction looks like:
approve(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
Approve {
to: ctx.accounts.token_account.to_account_info(),
delegate: ctx.accounts.delegate.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
},
),
&1_000_000,
)?;
And this is how the CPI to the revoke()
instruction looks like:
revoke(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
Revoke {
pub source: ctx.accounts.token_account.to_account_info(),
authority: ctx.accounts.authority.to_account_info(),
},
),
)?;