Solana CookbookTokens
How to Delegate Token Accounts
You can set a delegate with an allowed amount. After you setting, the delegate is like an another owner of your token account.
A token account can only delegate to one account at a time.
import {Connection,Keypair,Transaction,sendAndConfirmTransaction,SystemProgram,LAMPORTS_PER_SOL} from "@solana/web3.js";import {approveChecked,createApproveCheckedInstruction,revoke,createRevokeInstruction,createMint,getOrCreateAssociatedTokenAccount,createInitializeMintInstruction,createAssociatedTokenAccountInstruction,getAssociatedTokenAddress,MINT_SIZE,TOKEN_PROGRAM_ID} from "@solana/spl-token";(async () => {const connection = new Connection("http://127.0.0.1:8899", "confirmed");const tokenOwner = Keypair.generate();const airdropSignature = await connection.requestAirdrop(tokenOwner.publicKey,LAMPORTS_PER_SOL);await connection.confirmTransaction(airdropSignature);// Create delegate addressconst delegate = Keypair.generate();console.log(`Delegate pubkey: ${delegate.publicKey.toBase58()}`);// 1) Create mint using build-in functionconsole.log("\n1. Creating mint using build-in function...");const mint = await createMint(connection,tokenOwner,tokenOwner.publicKey, // mint authoritytokenOwner.publicKey, // freeze authority9 // decimals);console.log(`Mint created: ${mint.toBase58()}`);// 2) Create mint by composing transactionconsole.log("\n2. Creating mint by composing transaction...");const mintKeypair = Keypair.generate();const mintRent =await connection.getMinimumBalanceForRentExemption(MINT_SIZE);const createMintTx = new Transaction().add(// Create account for mintSystemProgram.createAccount({fromPubkey: tokenOwner.publicKey,newAccountPubkey: mintKeypair.publicKey,space: MINT_SIZE,lamports: mintRent,programId: TOKEN_PROGRAM_ID}),// Initialize mintcreateInitializeMintInstruction(mintKeypair.publicKey,9, // decimalstokenOwner.publicKey, // mint authoritytokenOwner.publicKey // freeze authority));console.log(`Create mint txhash: ${await sendAndConfirmTransaction(connection,createMintTx,[tokenOwner, mintKeypair])}`);// 3) Create token account using build-in functionconsole.log("\n3. Creating token account using build-in function...");const tokenAccount = await getOrCreateAssociatedTokenAccount(connection,tokenOwner,mint, // minttokenOwner.publicKey // owner);console.log(`Token account created: ${tokenAccount.address.toBase58()}`);// 4) Create token account by composing transactionconsole.log("\n4. Creating token account by composing transaction...");const associatedTokenAddress = await getAssociatedTokenAddress(mintKeypair.publicKey, // minttokenOwner.publicKey // owner);const createTokenAccountTx = new Transaction().add(createAssociatedTokenAccountInstruction(tokenOwner.publicKey, // payerassociatedTokenAddress, // token addresstokenOwner.publicKey, // ownermintKeypair.publicKey // mint));console.log(`Create token account txhash: ${await sendAndConfirmTransaction(connection,createTokenAccountTx,[tokenOwner])}`);// Use the created mint and token account for delegationconst mintPubkey = mint;const tokenAccountPubkey = tokenAccount.address;// 5) Approve delegation using build-in function{console.log("\n5. Approving delegation with specific amount...");const txhash = await approveChecked(connection,tokenOwner,mintPubkey,tokenAccountPubkey,delegate.publicKey,tokenOwner,500000000,9);console.log(`Approve delegation txhash: ${txhash}`);}// 6) Approve delegation by composing transaction{console.log("\n2. Approving delegation by composing transaction...");const tx = new Transaction().add(createApproveCheckedInstruction(tokenAccountPubkey, // token accountmintPubkey, // mintdelegate.publicKey, // delegatetokenOwner.publicKey, // owner of token account500000000, // amount, if your decimals is 9, 0.5 tokens9 // decimals));console.log(`Approve delegation txhash: ${await sendAndConfirmTransaction(connection,tx,[tokenOwner])}`);}})();
Is this page helpful?