Skip to main content

  1. 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.
  2. Revoke removes all delegate permissions from a light-token account.
  3. Only the token account owner can approve or revoke delegates.
1

Prerequisites

Cargo.toml
[dependencies]
light-compressed-token-sdk = "0.1"
light-client = "0.1"
light-token-types = "0.1"
solana-sdk = "2.2"
borsh = "0.10"
tokio = { version = "1.36", features = ["full"] }

[dev-dependencies]
light-program-test = "0.1"  # For in-memory tests with LiteSVM
Test with Lite-SVM (…)
# Initialize project
cargo init my-light-project
cd my-light-project

# Run tests
cargo test
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());
}
2

Approve or revoke delegates

View Source Code or find full examples with tests: examples-light-token.
mod shared;

use light_client::rpc::Rpc;
use light_token_sdk::token::Approve;
use shared::SetupContext;
use solana_sdk::{signature::Keypair, signer::Signer};

#[tokio::test(flavor = "multi_thread")]
async fn approve_delegate() {
    // Setup creates mint and ATA with tokens
    let SetupContext {
        mut rpc,
        payer,
        ata,
        ..
    } = shared::setup().await;

    let delegate = Keypair::new();
    let delegate_amount = 500_000u64;

    let approve_ix = Approve {
        token_account: ata,
        delegate: delegate.pubkey(),
        owner: payer.pubkey(),
        amount: delegate_amount,
    }
    .instruction()
    .unwrap();

    rpc.create_and_send_transaction(&[approve_ix], &payer.pubkey(), &[&payer])
        .await
        .unwrap();
}

Next Steps

Use the Unified Transfer Interface for Light-Token, SPL and Token 22 Transfers