From 97a4c15b7f8e001f6b9f13565a4dbc32827a1ee3 Mon Sep 17 00:00:00 2001 From: Jeff Wood Date: Sat, 24 Aug 2024 03:12:35 +0000 Subject: [PATCH 1/3] Update the Token Extensions Program link --- content/courses/tokens-and-nfts/token-program.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/courses/tokens-and-nfts/token-program.md b/content/courses/tokens-and-nfts/token-program.md index 08794cf32..fdb4b999a 100644 --- a/content/courses/tokens-and-nfts/token-program.md +++ b/content/courses/tokens-and-nfts/token-program.md @@ -28,7 +28,7 @@ description: - Creating Token Mints and Token Accounts requires allocating **rent** in SOL. The rent for a Token Account can be refunded when the account is closed. Additionally, tokens created with the - [Token Extensions Program](/developers/courses/token-extensions-for-mints/close-mint) + [Token Extensions Program](/content/courses/token-extensions/close-mint) can also close Token Mints. ### Lesson From 2ba6531b7f3723321a0b5c1ae8b072add7f9ad54 Mon Sep 17 00:00:00 2001 From: wuuer Date: Fri, 27 Sep 2024 09:59:44 +0800 Subject: [PATCH 2/3] sync token-program.md from main --- content/courses/tokens-and-nfts/token-program.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/courses/tokens-and-nfts/token-program.md b/content/courses/tokens-and-nfts/token-program.md index fdb4b999a..08794cf32 100644 --- a/content/courses/tokens-and-nfts/token-program.md +++ b/content/courses/tokens-and-nfts/token-program.md @@ -28,7 +28,7 @@ description: - Creating Token Mints and Token Accounts requires allocating **rent** in SOL. The rent for a Token Account can be refunded when the account is closed. Additionally, tokens created with the - [Token Extensions Program](/content/courses/token-extensions/close-mint) + [Token Extensions Program](/developers/courses/token-extensions-for-mints/close-mint) can also close Token Mints. ### Lesson From b5aabef93c9ac1c9b21e66a190a77db0355c47da Mon Sep 17 00:00:00 2001 From: wuuer Date: Fri, 27 Sep 2024 10:48:50 +0800 Subject: [PATCH 3/3] Delete "web3." for "web3.Transaction" in the section Brun Tokens. Using the system program as the delegate in the typescript of Delegating tokens. Add some instructions for Delegating tokens, Revoke Delegate, and Burn Tokens. Fix [creating NFTs with Metaplex] link. Fix [previous chapter] link. Fix [Transferring Tokens] link. Fix the import @solana/web3.js statements in the section Brun Tokens. Fix a wrong argument passed to calling the revoke function in the typescript for Revoke Delegate. --- .../tokens-and-nfts/token-program-advanced.md | 99 +++++++++++++++---- 1 file changed, 82 insertions(+), 17 deletions(-) diff --git a/content/courses/tokens-and-nfts/token-program-advanced.md b/content/courses/tokens-and-nfts/token-program-advanced.md index 16c827c38..aea2a8649 100644 --- a/content/courses/tokens-and-nfts/token-program-advanced.md +++ b/content/courses/tokens-and-nfts/token-program-advanced.md @@ -51,7 +51,7 @@ Under the hood, the `burn` function creates a transaction with instructions obtained from the `createBurnInstruction` function: ```typescript -import { PublicKey, Transaction } from "@solana/web3"; +import { PublicKey, Transaction } from "@solana/web3.js"; import { createBurnInstruction } from "@solana/spl-token"; async function buildBurnTransaction( @@ -105,7 +105,7 @@ Under the hood, the `approve` function creates a transaction with instructions obtained from the `createApproveInstruction` function: ```typescript -import { PublicKey, Transaction } from "@solana/web3"; +import { PublicKey, Transaction } from "@solana/web3.js"; import { createApproveInstruction } from "@solana/spl-token"; async function buildApproveTransaction( @@ -113,7 +113,7 @@ async function buildApproveTransaction( delegate: PublicKey, owner: PublicKey, amount: number, -): Promise { +): Promise { const transaction = new Transaction().add( createApproveInstruction(account, delegate, owner, amount), ); @@ -150,13 +150,13 @@ Under the hood, the `revoke` function creates a transaction with instructions obtained from the `createRevokeInstruction` function: ```typescript -import { PublicKey, Transaction } from "@solana/web3"; +import { PublicKey, Transaction } from "@solana/web3.js"; import { revoke } from "@solana/spl-token"; async function buildRevokeTransaction( account: PublicKey, owner: PublicKey, -): Promise { +): Promise { const transaction = new Transaction().add( createRevokeInstruction(account, owner), ); @@ -181,7 +181,8 @@ previous lab, you can [add a second account on devnet](/content/courses/intro-to-solana/intro-to-cryptography.md) if you like, or find a friend who has a devnet account! -Create a new file `delegate-tokens.ts` +Create a new file `delegate-tokens.ts`. We use the system program account as the +delegate here for demonstration. ```typescript import "dotenv/config"; @@ -204,8 +205,8 @@ console.log( `πŸ”‘ Loaded our keypair securely, using an env file! Our public key is: ${user.publicKey.toBase58()}`, ); -// Add the delegate public key here. -const delegate = new PublicKey("YOUR_DELEGATE_HERE"); +// Use the system program public key +const delegate = new PublicKey("11111111111111111111111111111111"); // Substitute in your token mint account const tokenMintAccount = new PublicKey("YOUR_TOKEN_MINT_ADDRESS_HERE"); @@ -231,7 +232,7 @@ const approveTransactionSignature = await approve( ); console.log( - `Approve Delegate Transaction: ${getExplorerLink( + `βœ… Approve Delegate Transaction: ${getExplorerLink( "transaction", approveTransactionSignature, "devnet", @@ -239,16 +240,54 @@ console.log( ); ``` +Replace `YOUR_TOKEN_MINT_ADDRESS_HERE` with your mint token address obtained +from the previous chapter. + +Run the script using `npx esrun delegate-tokens.ts`. You should see: + +```bash +βœ… Approve Delegate Transaction: https://explorer.solana.com/tx/3sBr62x2VMaoJ4Z3SQMy6ZPzQKaa5Bs9ni9dgwwZZ5qEViKh1gQznCgH489h6pgfruMmqPbc2GgminTPK4UXRRZd?cluster=devnet +``` + +Open the Explorer link, you will see the β€Œapproval information. + #### 2. Revoke Delegate Lets revoke the `delegate` using the `spl-token` library's `revoke` function. -Revoke will set delegate for the token account to null and reset the delegated -amount to 0. +Revoke will set delegate for the associated token account to null and reset the +delegated amount to 0. -All we will need for this function is the token account and user. After the +Create a new file `revoke-approve-tokens.ts`. ```typescript +import "dotenv/config"; +import { + getExplorerLink, + getKeypairFromEnvironment, +} from "@solana-developers/helpers"; +import { Connection, PublicKey, clusterApiUrl } from "@solana/web3.js"; +import { getOrCreateAssociatedTokenAccount, revoke } from "@solana/spl-token"; + +const connection = new Connection(clusterApiUrl("devnet")); + +const user = getKeypairFromEnvironment("SECRET_KEY"); + +console.log( + `πŸ”‘ Loaded our keypair securely, using an env file! Our public key is: ${user.publicKey.toBase58()}`, +); + +// Substitute in your token mint account +const tokenMintAccount = new PublicKey("YOUR_TOKEN_MINT_ADDRESS_HERE"); + +// Get or create the source and destination token accounts to store this token +const sourceTokenAccount = await getOrCreateAssociatedTokenAccount( + connection, + user, + tokenMintAccount, + user.publicKey, +); + const revokeTransactionSignature = await revoke( connection, user, @@ -257,7 +296,7 @@ const revokeTransactionSignature = await revoke( ); console.log( - `Revoke Delegate Transaction: ${getExplorerLink( + `βœ… Revoke Delegate Transaction: ${getExplorerLink( "transaction", revokeTransactionSignature, "devnet", @@ -265,6 +304,17 @@ console.log( ); ``` +Replace `YOUR_TOKEN_MINT_ADDRESS_HERE` with your mint token address obtained +from the previous chapter. + +Run the script using `npx esrun revoke-approve-tokens.ts`. You should see: + +```bash +βœ… Revoke Delegate Transaction: https://explorer.solana.com/tx/5UboxLULHT3pPznBxThfQMc73NNjYNLmvqrB3JVVXPwWxUFWA49WG58sFQP8B5rv4FXxxZm3mur319YNiyYxYgBd?cluster=devnet +``` + +Open the Explorer link, you will see the revoke information. + #### 3. Burn Tokens Finally, let's remove some tokens from circulation by burning them. @@ -272,7 +322,7 @@ Finally, let's remove some tokens from circulation by burning them. Use the `spl-token` library's `burn` function to remove half of your tokens from circulation. -Now call this new function in `main` to burn 25 of the user's tokens. +Create a new file `burn-tokens.ts`. ```typescript import "dotenv/config"; @@ -281,7 +331,11 @@ import { getKeypairFromEnvironment, } from "@solana-developers/helpers"; import { Connection, PublicKey, clusterApiUrl } from "@solana/web3.js"; -import { getOrCreateAssociatedTokenAccount, burn } from "@solana/spl-token"; +import { + getOrCreateAssociatedTokenAccount, + Account, + burn, +} from "@solana/spl-token"; const connection = new Connection(clusterApiUrl("devnet")); @@ -294,7 +348,7 @@ console.log( // Substitute in your token mint account const tokenMintAccount = new PublicKey("YOUR_TOKEN_MINT_ADDRESS_HERE"); -// Get the account where the user stores these tokens +// Get or create the source and destination token accounts to store this token const sourceTokenAccount = await getOrCreateAssociatedTokenAccount( connection, user, @@ -315,7 +369,7 @@ const transactionSignature = await burn( ); console.log( - `Burn Transaction: ${getExplorerLink( + `βœ… Burn Transaction: ${getExplorerLink( "transaction", transactionSignature, "devnet", @@ -323,6 +377,17 @@ console.log( ); ``` +Replace `YOUR_TOKEN_MINT_ADDRESS_HERE` with your mint token address obtained +from the previous chapter. + +Run the script using `npx esrun burn-tokens.ts`. You should see: + +```bash +βœ… Burn Transaction: https://explorer.solana.com/tx/P9JAK7bSAhycccGunDEThgt12QFiqMr9oexenEmRXXKoXsLKr2x64k9BWNppjTxFeVMUYjBEncRKe3gZsyd29JY?cluster=devnet +``` + +Open the Explorer link, you will see the burn information. + Well done! You've now