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.
approve grants a delegate permission to transfer up to a specified amount of tokens.
Find the source code here.
1

Approve a delegate

Install packages in your working directory:
npm install @lightprotocol/stateless.js@beta \
            @lightprotocol/compressed-token@beta
Install the CLI globally:
npm install -g @lightprotocol/zk-compression-cli@beta
# start local test-validator in a separate terminal
light test-validator
In the code examples, use createRpc() without arguments for localnet.
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);
})();
2

Revoke a delegate

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);
})();

Next Steps

Go back to the overview for the Light Token standard