- Approve grants a delegate permission to transfer up to a specified amount of tokens from your account.
- Each token account can have only one delegate at a time.
- Any new approval overwrites the previous one.
- Revoke removes all delegate permissions from a Light Token account.
- Only the token account owner can approve or revoke delegates.
- TypeScript Client
- Rust Client
- Program
- Approve
- Revoke
approve grants a delegate permission to transfer up to a specified amount of tokens.revoke removes all delegate permissions from a Light Token account.Find the source code
here.
Approve a delegate
Installation
Installation
- npm
- yarn
- pnpm
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
npm install @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
- Localnet
- Devnet
Report incorrect code
Copy
Ask AI
# start local test-validator in a separate terminal
light test-validator
In the code examples, use
createRpc() without arguments for localnet.Get an API key from Helius and add to
.env:.env
Report incorrect code
Copy
Ask AI
API_KEY=<your-helius-api-key>
In the code examples, use
createRpc(RPC_URL) with the devnet URL.Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import {
createMintInterface,
mintToCompressed,
approve,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
// const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
// const rpc = createRpc(RPC_URL);
// localnet:
const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
);
(async function () {
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
await mintToCompressed(rpc, payer, mint, payer, [{ recipient: payer.publicKey, amount: 1000n }]);
const delegate = Keypair.generate();
const tx = await approve(
rpc,
payer,
mint,
500,
payer,
delegate.publicKey,
);
console.log("Tx:", tx);
})();
Revoke a delegate
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import {
createMintInterface,
mintToCompressed,
approve,
revoke,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
// const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
// const rpc = createRpc(RPC_URL);
// localnet:
const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
);
(async function () {
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
await mintToCompressed(rpc, payer, mint, payer, [{ recipient: payer.publicKey, amount: 1000n }]);
const delegate = Keypair.generate();
await approve(rpc, payer, mint, 500, payer, delegate.publicKey);
const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate(
delegate.publicKey,
{ mint },
);
const tx = await revoke(rpc, payer, delegatedAccounts.items, payer);
console.log("Tx:", tx);
})();
- Approve
- Revoke
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-token = "0.4.0"
light-client = { version = "0.19.0", features = ["v2"] }
solana-sdk = "2"
borsh = "0.10.4"
tokio = { version = "1", features = ["full"] }
Developer Environment
Developer Environment
- In-Memory (LightProgramTest)
- Localnet (LightClient)
- Devnet (LightClient)
Test with Lite-SVM (…)
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Run tests
cargo test
Report incorrect code
Copy
Ask AI
use light_program_test::{LightProgramTest, ProgramTestConfig};
use solana_sdk::signer::Signer;
#[tokio::test]
async fn test_example() {
// In-memory test environment
let mut rpc = LightProgramTest::new(ProgramTestConfig::default())
.await
.unwrap();
let payer = rpc.get_payer().insecure_clone();
println!("Payer: {}", payer.pubkey());
}
Connects to a local test validator.
- npm
- yarn
- pnpm
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Start local test validator (in separate terminal)
light test-validator
Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connects to http://localhost:8899
let rpc = LightClient::new(LightClientConfig::local()).await?;
let slot = rpc.get_slot().await?;
println!("Current slot: {}", slot);
Ok(())
}
Replace
<your-api-key> with your actual API key. Get your API key here.Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc_url = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
let rpc = LightClient::new(
LightClientConfig::new(rpc_url.to_string(), None, None)
).await?;
println!("Connected to Devnet");
Ok(())
}
Approve or revoke delegates
View the source code and full example with shared test utilities.
- Approve
- Revoke
- Action
- Instruction
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token_client::actions::Approve;
use rust_client::{setup, SetupContext};
use solana_sdk::{signature::Keypair, signer::Signer};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint and associated token account with tokens
let SetupContext {
mut rpc,
payer,
associated_token_account,
..
} = setup().await;
let delegate = Keypair::new();
let sig = Approve {
token_account: associated_token_account,
delegate: delegate.pubkey(),
amount: 500_000,
owner: Some(payer.pubkey()),
}
.execute(&mut rpc, &payer)
.await?;
let data = rpc.get_account(associated_token_account).await?.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Delegate: {:?} Tx: {sig}", token.delegate);
Ok(())
}
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token::instruction::Approve;
use rust_client::{setup, SetupContext};
use solana_sdk::{signature::Keypair, signer::Signer};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint and associated token account with tokens
let SetupContext {
mut rpc,
payer,
associated_token_account,
..
} = setup().await;
let delegate = Keypair::new();
let delegate_amount = 500_000u64;
let approve_instruction = Approve {
token_account: associated_token_account,
delegate: delegate.pubkey(),
owner: payer.pubkey(),
amount: delegate_amount,
}
.instruction()?;
let sig = rpc
.create_and_send_transaction(&[approve_instruction], &payer.pubkey(), &[&payer])
.await?;
let data = rpc.get_account(associated_token_account).await?.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Delegate: {:?} Tx: {sig}", token.delegate);
Ok(())
}
- Action
- Instruction
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token_client::actions::Revoke;
use rust_client::{setup, SetupContext};
use solana_sdk::signer::Signer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint and associated token account with approved delegate
let SetupContext {
mut rpc,
payer,
associated_token_account,
..
} = setup().await;
let sig = Revoke {
token_account: associated_token_account,
owner: Some(payer.pubkey()),
}
.execute(&mut rpc, &payer)
.await?;
let data = rpc.get_account(associated_token_account).await?.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Delegate: {:?} Tx: {sig}", token.delegate);
Ok(())
}
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token::instruction::Revoke;
use rust_client::{setup, SetupContext};
use solana_sdk::signer::Signer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint, associated token account with tokens, and approves delegate
let SetupContext {
mut rpc,
payer,
associated_token_account,
..
} = setup().await;
let revoke_instruction = Revoke {
token_account: associated_token_account,
owner: payer.pubkey(),
}
.instruction()?;
let sig = rpc
.create_and_send_transaction(&[revoke_instruction], &payer.pubkey(), &[&payer])
.await?;
let data = rpc.get_account(associated_token_account).await?.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Delegate: {:?} Tx: {sig}", token.delegate);
Ok(())
}
- Approve
- Revoke
Build Account Infos and CPI the Light Token Program
Useinvoke for external signers or invoke_signed when the authority is a PDA.- invoke (External signer)
- invoke_signed (PDA owner)
Report incorrect code
Copy
Ask AI
use light_token::instruction::ApproveCpi;
ApproveCpi {
token_account: token_account.clone(),
delegate: delegate.clone(),
owner: owner.clone(),
system_program: system_program.clone(),
amount,
}
.invoke()?;
Report incorrect code
Copy
Ask AI
use light_token::instruction::ApproveCpi;
let approve_cpi = ApproveCpi {
token_account: token_account.clone(),
delegate: delegate.clone(),
owner: owner.clone(),
system_program: system_program.clone(),
amount,
};
let signer_seeds: &[&[u8]] = &[TOKEN_ACCOUNT_SEED, &[bump]];
approve_cpi.invoke_signed(&[signer_seeds])?;
Build Account Infos and CPI the Light Token Program
Useinvoke for external signers or invoke_signed when the authority is a PDA.- invoke (External signer)
- invoke_signed (PDA owner)
Report incorrect code
Copy
Ask AI
use light_token::instruction::RevokeCpi;
RevokeCpi {
token_account: token_account.clone(),
owner: owner.clone(),
system_program: system_program.clone(),
}
.invoke()?;
Report incorrect code
Copy
Ask AI
use light_token::instruction::RevokeCpi;
let revoke_cpi = RevokeCpi {
token_account: token_account.clone(),
owner: owner.clone(),
system_program: system_program.clone(),
};
let signer_seeds: &[&[u8]] = &[TOKEN_ACCOUNT_SEED, &[bump]];
revoke_cpi.invoke_signed(&[signer_seeds])?;
Full Code Example
- Approve
- Revoke
View the source code and full example with shared test utilities.
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs, deprecated)]
use anchor_lang::prelude::*;
use light_token::instruction::ApproveCpi;
declare_id!("37XmzKqSG2VD1ZBvzyfbt1HN1mT1bqVAmfzX2ziB3KT1");
#[program]
pub mod light_token_anchor_approve {
use super::*;
pub fn approve(ctx: Context<ApproveAccounts>, amount: u64) -> Result<()> {
ApproveCpi {
token_account: ctx.accounts.token_account.to_account_info(),
delegate: ctx.accounts.delegate.to_account_info(),
owner: ctx.accounts.owner.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
amount,
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct ApproveAccounts<'info> {
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub token_account: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
pub delegate: AccountInfo<'info>,
pub owner: Signer<'info>,
pub system_program: Program<'info, System>,
}
View the source code and full example with shared test utilities.
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs, deprecated)]
use anchor_lang::prelude::*;
use light_token::instruction::RevokeCpi;
declare_id!("G3ph4MK5qaSdxYnfxToETg31AHEMMqVhPuMRgBhk38tQ");
#[program]
pub mod light_token_anchor_revoke {
use super::*;
pub fn revoke(ctx: Context<RevokeAccounts>) -> Result<()> {
RevokeCpi {
token_account: ctx.accounts.token_account.to_account_info(),
owner: ctx.accounts.owner.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct RevokeAccounts<'info> {
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub token_account: AccountInfo<'info>,
pub owner: Signer<'info>,
pub system_program: Program<'info, System>,
}