Use this file to discover all available pages before exploring further.
The approve() and revoke() functions grant and remove delegate spending authority for compressed tokens. Only the token owner can perform these instructions.
// Approve delegate for spending up to the specified amountconst approveSignature = await approve( rpc, payer, mint, // SPL mint with token pool for compression amount, owner, delegate.publicKey, // delegate account);
In the code examples, use createRpc(RPC_URL) with the devnet URL.
Approve
Revoke
import "dotenv/config";import { Keypair } from "@solana/web3.js";import { createRpc } from "@lightprotocol/stateless.js";import { createMint, mintTo, approve } from "@lightprotocol/compressed-token";import BN from "bn.js";import { homedir } from "os";import { readFileSync } from "fs";// devnet:const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;// localnet:// const RPC_URL = undefined;const payer = Keypair.fromSecretKey( new Uint8Array( JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")) ));(async function () { // devnet: const rpc = createRpc(RPC_URL); // localnet: // const rpc = createRpc(); // Setup: Create mint and mint tokens const { mint } = await createMint(rpc, payer, payer.publicKey, 9); const owner = Keypair.generate(); await mintTo(rpc, payer, mint, owner.publicKey, payer, 1_000_000_000); // Approve delegate const delegate = Keypair.generate(); const tx = await approve(rpc, payer, mint, new BN(500_000_000), owner, delegate.publicKey); console.log("Mint:", mint.toBase58()); console.log("Delegate:", delegate.publicKey.toBase58()); console.log("Tx:", tx);})();
import "dotenv/config";import { Keypair } from "@solana/web3.js";import { createRpc } from "@lightprotocol/stateless.js";import { createMint, mintTo, approve, revoke } from "@lightprotocol/compressed-token";import BN from "bn.js";import { homedir } from "os";import { readFileSync } from "fs";// devnet:const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;// localnet:// const RPC_URL = undefined;const payer = Keypair.fromSecretKey( new Uint8Array( JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")) ));(async function () { // devnet: const rpc = createRpc(RPC_URL); // localnet: // const rpc = createRpc(); // Setup: Create mint, mint tokens, and approve delegate const { mint } = await createMint(rpc, payer, payer.publicKey, 9); const owner = Keypair.generate(); await mintTo(rpc, payer, mint, owner.publicKey, payer, 1_000_000_000); const delegate = Keypair.generate(); await approve(rpc, payer, mint, new BN(500_000_000), owner, delegate.publicKey); // Get delegated accounts and revoke const delegatedAccounts = await rpc.getCompressedTokenAccountsByDelegate(delegate.publicKey, { mint }); const tx = await revoke(rpc, payer, delegatedAccounts.items, owner); console.log("Mint:", mint.toBase58()); console.log("Tx:", tx);})();
Before we approve or revoke delegates, we need:
compressed token accounts to delegate or revoke delegation from, and
an SPL mint with a token pool for compression. This token pool can be created for new SPL mints via createMint() or added to existing SPL mints via createTokenPool().