From 701bbe3bcf6aaf9b3334d44d328750f8ba24cfd0 Mon Sep 17 00:00:00 2001 From: Colin Ogoo Date: Wed, 6 Sep 2023 12:11:05 +0000 Subject: [PATCH 1/7] docs(token 2022): guide for mint close authority extension --- .../guides/token-2022/mint-close-authority.md | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 content/guides/token-2022/mint-close-authority.md diff --git a/content/guides/token-2022/mint-close-authority.md b/content/guides/token-2022/mint-close-authority.md new file mode 100644 index 000000000..f04713448 --- /dev/null +++ b/content/guides/token-2022/mint-close-authority.md @@ -0,0 +1,188 @@ +--- +date: Sep 01, 2023 +title: How to use the Mint Close Authority extension +description: + "The Token program allows owners to close token accounts, but it is impossible + to close mint accounts. In Token-2022, it is possible to close mints by + initializing the MintCloseAuthority extension before initializing the mint." +keywords: + - token 2022 + - token extensions + - token program +difficulty: beginner +tags: + - token 2022 + - token extensions +altRoutes: + - /developers/guides/mint-close-authority +--- + +The Token program allows owners to close token accounts, but is impossible to +close mint accounts. In Token-2022, it is possible to close mints by +initializing the MintCloseAuthority extension before initializing the mint. + +Let's get started! + +## Install dependencies + +```shell +npm i @solana/web3.js @solana/spl-token +``` + +Install the `@solana/web3.js` and `@solana/spl-token` packages. + +## Setting up + +Let's start by setting up our script to create a new token mint. + +First, we will need to: + +- Establish a connection to the devnet cluster +- Generate a payer account and fund it +- Create a new token mint using the Token 2022 program + +```javascript +import { + Connection, + Keypair, + LAMPORTS_PER_SOL, + SystemProgram, + Transaction, + clusterApiUrl, + sendAndConfirmTransaction, +} from "@solana/web3.js"; +import { + ExtensionType, + TOKEN_2022_PROGRAM_ID, + closeAccount, + createInitializeMintCloseAuthorityInstruction, + createInitializeMintInstruction, + getMintLen, +} from "@solana/spl-token"; + +// We establish a connection to the cluster +const connection = new Connection(clusterApiUrl("devnet"), "confirmed"); + +// Next, we create and fund the payer account +const payer = Keypair.generate(); +const airdropSignature = await connection.requestAirdrop( + payer.publicKey, + 2 * LAMPORTS_PER_SOL, +); +await connection.confirmTransaction({ + signature: airdropSignature, + ...(await connection.getLatestBlockhash()), +}); +``` + +## Mint setup + +Next, let's configure the properties of our token mint and generate the +necessary authorities. + +```javascript +const mintKeypair = Keypair.generate(); +// address of our token mint +const mint = mintKeypair.publicKey; +// The amount of decimals for our mint +const decimals = 9; +// authority that can mint new tokens +const mintAuthority = Keypair.generate(); +// authority that can close the mint account +const closeAuthority = Keypair.generate(); + +const mintLen = getMintLen([ExtensionType.MintCloseAuthority]); +const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); +``` + +We get the size of our new account and calculate the amount for rent exemption. +We use the helper `getMinLen` helper function, which takes an array of +extensions we want for this mint. + +## The Instructions + +Now, let's build the set of instructions to: + +- Create a new account +- Initialize the mint close authority extension +- Initialize our new account as a token mint + +```javascript +const createAccountInstruction = SystemProgram.createAccount({ + fromPubkey: payer.publicKey, // account that will transfer lamports to created account + newAccountPubkey: mint, // public key or the created account + space: mintLen, // amount of bytes to allocate to the created account + lamports, // amount of lamports to transfer to created account + programId: TOKEN_2022_PROGRAM_ID, // public key of the program to assign as owner of created account +}); +``` + +We create our Mint account and assign ownership to the token 2022 program. + +```javascript +const initializeMintCloseAuthorityInstruction = + createInitializeMintCloseAuthorityInstruction( + mint, // token mint account + closeAuthority.publicKey, // authority that can close the mint + TOKEN_2022_PROGRAM_ID, // SPL Token program id + ); +``` + +Next, we initialize the Mint Close Authority extension for our mint. + +```javascript +const initializeMintInstruction = createInitializeMintInstruction( + mint, // token mint account + decimals, // number of decimals for token + mintAuthority.publicKey, // authority that can mint new tokens + null, // optional freeze authority + TOKEN_2022_PROGRAM_ID, // SPL Token program id +); +``` + +We then initialize our account as a mint account. + +## Send and confirm + +```javascript +const transaction = new Transaction().add( + createAccountInstruction, + initializeMintCloseAuthorityInstruction, + initializeMintInstruction, +); +await sendAndConfirmTransaction( + connection, + transaction, + [payer, mintKeypair], + undefined, +); +``` + +Finally, we add the instructions to our transaction and send it to the network. +As a result, we've created a mint account with the mint close authority +extension. + +## Closing a mint account + +With the MintCloseAuthority extension on the mint and a valid authority, it's +possible to close the mint account and reclaim the lamports on the mint account. + +```javascript +await closeAccount( + connection, // connection to use + payer, // payer of the transaction fees + mint, // mint account to close + payer.publicKey, // account to receive the lamports + closeAuthority, // authority that can close the mint + [], // signing accounts + undefined, // options for confirming the transaction + TOKEN_2022_PROGRAM_ID, // SPL Token program id +); +``` + +**Note**: The supply on the mint must be 0. + +## Conclusion + +Token 2022's `MintCloseAuthority` extension is quite simple but effective. You +get to reclaim precious SOL that otherwise would have been lost. From 178cfce33806dbd87d5ecb745d0baf3d7e1622bd Mon Sep 17 00:00:00 2001 From: John <75003086+ZYJLiu@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:04:00 -0600 Subject: [PATCH 2/7] Add Solana Playground --- .../guides/token-2022/mint-close-authority.md | 129 ++++++++++-------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/content/guides/token-2022/mint-close-authority.md b/content/guides/token-2022/mint-close-authority.md index f04713448..3bb23d7ee 100644 --- a/content/guides/token-2022/mint-close-authority.md +++ b/content/guides/token-2022/mint-close-authority.md @@ -1,5 +1,5 @@ --- -date: Sep 01, 2023 +date: Dec 04, 2023 title: How to use the Mint Close Authority extension description: "The Token program allows owners to close token accounts, but it is impossible @@ -19,33 +19,44 @@ altRoutes: The Token program allows owners to close token accounts, but is impossible to close mint accounts. In Token-2022, it is possible to close mints by -initializing the MintCloseAuthority extension before initializing the mint. +initializing the `MintCloseAuthority` extension before initializing the mint. +Note that the supply on the mint must be 0 to close the account. -Let's get started! +In this guide, we'll walk through an example using Solana Playground. +Here is a [link](https://beta.solpg.io/656e180ffb53fa325bfd0c45) to the final script. -## Install dependencies +## Getting Started -```shell -npm i @solana/web3.js @solana/spl-token +Start by opening this Solana Playground [link](https://beta.solpg.io/656e19acfb53fa325bfd0c46) with the following starter code. + +```javascript +// Client +console.log("My address:", pg.wallet.publicKey.toString()); +const balance = await pg.connection.getBalance(pg.wallet.publicKey); +console.log(`My balance: ${balance / web3.LAMPORTS_PER_SOL} SOL`); ``` -Install the `@solana/web3.js` and `@solana/spl-token` packages. +If it is your first time using Solana Playground, you'll first need to create a Playground Wallet and fund the wallet with devnet SOL. + +To get devnet SOL, run the `solana airdrop` command in the Playground's terminal, or visit this [devnet faucet](https://faucet.solana.com/). -## Setting up +``` +solana airdrop 5 +``` -Let's start by setting up our script to create a new token mint. +Once you've created and funded the Playground wallet, click the "Run" button to run the starter code. -First, we will need to: +## Add Dependencies -- Establish a connection to the devnet cluster -- Generate a payer account and fund it -- Create a new token mint using the Token 2022 program +Let's start by setting up our script. +We'll be using the `@solana/web3.js` and `@solana/spl-token` libraries. + +Replace the starter code with the following: ```javascript import { Connection, Keypair, - LAMPORTS_PER_SOL, SystemProgram, Transaction, clusterApiUrl, @@ -60,25 +71,19 @@ import { getMintLen, } from "@solana/spl-token"; -// We establish a connection to the cluster +// Establish a connection to the cluster const connection = new Connection(clusterApiUrl("devnet"), "confirmed"); -// Next, we create and fund the payer account -const payer = Keypair.generate(); -const airdropSignature = await connection.requestAirdrop( - payer.publicKey, - 2 * LAMPORTS_PER_SOL, -); -await connection.confirmTransaction({ - signature: airdropSignature, - ...(await connection.getLatestBlockhash()), -}); +// Playground wallet +const payer = pg.wallet.keypair; + +// transaction signature return from sent transaction +let transactionSignature: string; ``` -## Mint setup +## Mint Setup -Next, let's configure the properties of our token mint and generate the -necessary authorities. +Next, let's configure the properties of our token mint and the define the authorities. ```javascript const mintKeypair = Keypair.generate(); @@ -87,19 +92,22 @@ const mint = mintKeypair.publicKey; // The amount of decimals for our mint const decimals = 9; // authority that can mint new tokens -const mintAuthority = Keypair.generate(); +const mintAuthority = payer; // authority that can close the mint account -const closeAuthority = Keypair.generate(); +const closeAuthority = payer; +``` + +Next, let's get the size of the new mint account and calculate the minimum lamports required for rent exemption. +We use the helper `getMinLen` helper function, which takes an array of extensions we want for this mint. +```javascript const mintLen = getMintLen([ExtensionType.MintCloseAuthority]); const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); ``` -We get the size of our new account and calculate the amount for rent exemption. -We use the helper `getMinLen` helper function, which takes an array of -extensions we want for this mint. +With Token 2022, the size of the mint account will vary based on the extensions enabled. -## The Instructions +## Build Instructions Now, let's build the set of instructions to: @@ -107,28 +115,31 @@ Now, let's build the set of instructions to: - Initialize the mint close authority extension - Initialize our new account as a token mint +First, build the instruction to invoke the System Program to create an account and assign ownership to the Token 2022 Program. + ```javascript const createAccountInstruction = SystemProgram.createAccount({ fromPubkey: payer.publicKey, // account that will transfer lamports to created account - newAccountPubkey: mint, // public key or the created account + newAccountPubkey: mint, // public key to use as the address of the created account space: mintLen, // amount of bytes to allocate to the created account lamports, // amount of lamports to transfer to created account programId: TOKEN_2022_PROGRAM_ID, // public key of the program to assign as owner of created account }); ``` -We create our Mint account and assign ownership to the token 2022 program. +Next, build the instruction to initialize the Mint Close Authority extension for the mint account. ```javascript const initializeMintCloseAuthorityInstruction = createInitializeMintCloseAuthorityInstruction( mint, // token mint account closeAuthority.publicKey, // authority that can close the mint - TOKEN_2022_PROGRAM_ID, // SPL Token program id + TOKEN_2022_PROGRAM_ID // SPL Token program id ); ``` -Next, we initialize the Mint Close Authority extension for our mint. +Lastly, build the instruction to initialize the rest of the Mint Account data. +This is the same as with the original Token Program. ```javascript const initializeMintInstruction = createInitializeMintInstruction( @@ -136,39 +147,42 @@ const initializeMintInstruction = createInitializeMintInstruction( decimals, // number of decimals for token mintAuthority.publicKey, // authority that can mint new tokens null, // optional freeze authority - TOKEN_2022_PROGRAM_ID, // SPL Token program id + TOKEN_2022_PROGRAM_ID // SPL Token program id ); ``` -We then initialize our account as a mint account. +## Send Transaction -## Send and confirm +Finally, we add the instructions to a new transaction and send it to the network. This will create a mint account with the `MintCloseAuthority` extension. ```javascript const transaction = new Transaction().add( createAccountInstruction, initializeMintCloseAuthorityInstruction, - initializeMintInstruction, + initializeMintInstruction ); -await sendAndConfirmTransaction( + +transactionSignature = await sendAndConfirmTransaction( connection, transaction, - [payer, mintKeypair], - undefined, + [payer, mintKeypair] +); + +console.log( + "\n", + "Transaction Signature:", + `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet` ); ``` -Finally, we add the instructions to our transaction and send it to the network. -As a result, we've created a mint account with the mint close authority -extension. +Run the script by clicking the `Run` button. You can then inspect the transaction on the Solana Explorer. ## Closing a mint account -With the MintCloseAuthority extension on the mint and a valid authority, it's -possible to close the mint account and reclaim the lamports on the mint account. +With the `MintCloseAuthority` extension on the mint and a valid authority, it's possible to close the mint account and reclaim the lamports on the mint account. ```javascript -await closeAccount( +transactionSignature = await closeAccount( connection, // connection to use payer, // payer of the transaction fees mint, // mint account to close @@ -176,13 +190,18 @@ await closeAccount( closeAuthority, // authority that can close the mint [], // signing accounts undefined, // options for confirming the transaction - TOKEN_2022_PROGRAM_ID, // SPL Token program id + TOKEN_2022_PROGRAM_ID // SPL Token program id +); + +console.log( + "\n", + "Transaction Signature:", + `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet` ); ``` -**Note**: The supply on the mint must be 0. +Run the script by clicking the `Run` button. You can then inspect the transaction on the Solana Explorer. ## Conclusion -Token 2022's `MintCloseAuthority` extension is quite simple but effective. You -get to reclaim precious SOL that otherwise would have been lost. +Token 2022's `MintCloseAuthority` extension is quite simple but effective. You get to reclaim SOL that otherwise would have been lost. From b8e66f7b401fbe611de82b011c674024937ca763 Mon Sep 17 00:00:00 2001 From: John <75003086+ZYJLiu@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:30:46 -0600 Subject: [PATCH 3/7] fix prettier lint error --- .../guides/token-2022/mint-close-authority.md | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/content/guides/token-2022/mint-close-authority.md b/content/guides/token-2022/mint-close-authority.md index 3bb23d7ee..481df0904 100644 --- a/content/guides/token-2022/mint-close-authority.md +++ b/content/guides/token-2022/mint-close-authority.md @@ -22,12 +22,14 @@ close mint accounts. In Token-2022, it is possible to close mints by initializing the `MintCloseAuthority` extension before initializing the mint. Note that the supply on the mint must be 0 to close the account. -In this guide, we'll walk through an example using Solana Playground. -Here is a [link](https://beta.solpg.io/656e180ffb53fa325bfd0c45) to the final script. +In this guide, we'll walk through an example using Solana Playground. Here is a +[link](https://beta.solpg.io/656e180ffb53fa325bfd0c45) to the final script. ## Getting Started -Start by opening this Solana Playground [link](https://beta.solpg.io/656e19acfb53fa325bfd0c46) with the following starter code. +Start by opening this Solana Playground +[link](https://beta.solpg.io/656e19acfb53fa325bfd0c46) with the following +starter code. ```javascript // Client @@ -36,20 +38,23 @@ const balance = await pg.connection.getBalance(pg.wallet.publicKey); console.log(`My balance: ${balance / web3.LAMPORTS_PER_SOL} SOL`); ``` -If it is your first time using Solana Playground, you'll first need to create a Playground Wallet and fund the wallet with devnet SOL. +If it is your first time using Solana Playground, you'll first need to create a +Playground Wallet and fund the wallet with devnet SOL. -To get devnet SOL, run the `solana airdrop` command in the Playground's terminal, or visit this [devnet faucet](https://faucet.solana.com/). +To get devnet SOL, run the `solana airdrop` command in the Playground's +terminal, or visit this [devnet faucet](https://faucet.solana.com/). ``` solana airdrop 5 ``` -Once you've created and funded the Playground wallet, click the "Run" button to run the starter code. +Once you've created and funded the Playground wallet, click the "Run" button to +run the starter code. ## Add Dependencies -Let's start by setting up our script. -We'll be using the `@solana/web3.js` and `@solana/spl-token` libraries. +Let's start by setting up our script. We'll be using the `@solana/web3.js` and +`@solana/spl-token` libraries. Replace the starter code with the following: @@ -83,7 +88,8 @@ let transactionSignature: string; ## Mint Setup -Next, let's configure the properties of our token mint and the define the authorities. +Next, let's configure the properties of our token mint and the define the +authorities. ```javascript const mintKeypair = Keypair.generate(); @@ -97,15 +103,17 @@ const mintAuthority = payer; const closeAuthority = payer; ``` -Next, let's get the size of the new mint account and calculate the minimum lamports required for rent exemption. -We use the helper `getMinLen` helper function, which takes an array of extensions we want for this mint. +Next, let's get the size of the new mint account and calculate the minimum +lamports required for rent exemption. We use the helper `getMinLen` helper +function, which takes an array of extensions we want for this mint. ```javascript const mintLen = getMintLen([ExtensionType.MintCloseAuthority]); const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); ``` -With Token 2022, the size of the mint account will vary based on the extensions enabled. +With Token 2022, the size of the mint account will vary based on the extensions +enabled. ## Build Instructions @@ -115,7 +123,8 @@ Now, let's build the set of instructions to: - Initialize the mint close authority extension - Initialize our new account as a token mint -First, build the instruction to invoke the System Program to create an account and assign ownership to the Token 2022 Program. +First, build the instruction to invoke the System Program to create an account +and assign ownership to the Token 2022 Program. ```javascript const createAccountInstruction = SystemProgram.createAccount({ @@ -127,14 +136,15 @@ const createAccountInstruction = SystemProgram.createAccount({ }); ``` -Next, build the instruction to initialize the Mint Close Authority extension for the mint account. +Next, build the instruction to initialize the Mint Close Authority extension for +the mint account. ```javascript const initializeMintCloseAuthorityInstruction = createInitializeMintCloseAuthorityInstruction( mint, // token mint account closeAuthority.publicKey, // authority that can close the mint - TOKEN_2022_PROGRAM_ID // SPL Token program id + TOKEN_2022_PROGRAM_ID, // SPL Token program id ); ``` @@ -147,39 +157,43 @@ const initializeMintInstruction = createInitializeMintInstruction( decimals, // number of decimals for token mintAuthority.publicKey, // authority that can mint new tokens null, // optional freeze authority - TOKEN_2022_PROGRAM_ID // SPL Token program id + TOKEN_2022_PROGRAM_ID, // SPL Token program id ); ``` ## Send Transaction -Finally, we add the instructions to a new transaction and send it to the network. This will create a mint account with the `MintCloseAuthority` extension. +Finally, we add the instructions to a new transaction and send it to the +network. This will create a mint account with the `MintCloseAuthority` +extension. ```javascript const transaction = new Transaction().add( createAccountInstruction, initializeMintCloseAuthorityInstruction, - initializeMintInstruction + initializeMintInstruction, ); transactionSignature = await sendAndConfirmTransaction( connection, transaction, - [payer, mintKeypair] + [payer, mintKeypair], ); console.log( "\n", "Transaction Signature:", - `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet` + `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`, ); ``` -Run the script by clicking the `Run` button. You can then inspect the transaction on the Solana Explorer. +Run the script by clicking the `Run` button. You can then inspect the +transaction on the Solana Explorer. ## Closing a mint account -With the `MintCloseAuthority` extension on the mint and a valid authority, it's possible to close the mint account and reclaim the lamports on the mint account. +With the `MintCloseAuthority` extension on the mint and a valid authority, it's +possible to close the mint account and reclaim the lamports on the mint account. ```javascript transactionSignature = await closeAccount( @@ -190,18 +204,20 @@ transactionSignature = await closeAccount( closeAuthority, // authority that can close the mint [], // signing accounts undefined, // options for confirming the transaction - TOKEN_2022_PROGRAM_ID // SPL Token program id + TOKEN_2022_PROGRAM_ID, // SPL Token program id ); console.log( "\n", "Transaction Signature:", - `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet` + `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`, ); ``` -Run the script by clicking the `Run` button. You can then inspect the transaction on the Solana Explorer. +Run the script by clicking the `Run` button. You can then inspect the +transaction on the Solana Explorer. ## Conclusion -Token 2022's `MintCloseAuthority` extension is quite simple but effective. You get to reclaim SOL that otherwise would have been lost. +Token 2022's `MintCloseAuthority` extension is quite simple but effective. You +get to reclaim SOL that otherwise would have been lost. From 3792cf8dd8de95f1b9617545f102bb90034a289f Mon Sep 17 00:00:00 2001 From: John <75003086+ZYJLiu@users.noreply.github.com> Date: Wed, 6 Dec 2023 00:01:54 -0600 Subject: [PATCH 4/7] Revision and print SolanaFM links --- .../guides/token-2022/mint-close-authority.md | 148 +++++++++--------- 1 file changed, 78 insertions(+), 70 deletions(-) diff --git a/content/guides/token-2022/mint-close-authority.md b/content/guides/token-2022/mint-close-authority.md index 481df0904..37ea0f51f 100644 --- a/content/guides/token-2022/mint-close-authority.md +++ b/content/guides/token-2022/mint-close-authority.md @@ -1,10 +1,10 @@ --- -date: Dec 04, 2023 +date: Dec 05, 2023 title: How to use the Mint Close Authority extension description: "The Token program allows owners to close token accounts, but it is impossible - to close mint accounts. In Token-2022, it is possible to close mints by - initializing the MintCloseAuthority extension before initializing the mint." + to close mint accounts. With Token Extensions, it is possible to close mints + by initializing the MintCloseAuthority extension before initializing the mint." keywords: - token 2022 - token extensions @@ -17,13 +17,13 @@ altRoutes: - /developers/guides/mint-close-authority --- -The Token program allows owners to close token accounts, but is impossible to -close mint accounts. In Token-2022, it is possible to close mints by -initializing the `MintCloseAuthority` extension before initializing the mint. -Note that the supply on the mint must be 0 to close the account. +With the original Token program, it is not possible to close existing Mint +Accounts. With Token Extensions, the `MintCloseAuthority` extension enables a +designated Close Authority to close a Mint Account if the supply of the mint +is 0. -In this guide, we'll walk through an example using Solana Playground. Here is a -[link](https://beta.solpg.io/656e180ffb53fa325bfd0c45) to the final script. +In this guide, we'll walk through an example of using Solana Playground. Here is +the [final script](https://beta.solpg.io/65700c73fb53fa325bfd0c4a). ## Getting Started @@ -76,75 +76,79 @@ import { getMintLen, } from "@solana/spl-token"; -// Establish a connection to the cluster -const connection = new Connection(clusterApiUrl("devnet"), "confirmed"); - // Playground wallet const payer = pg.wallet.keypair; -// transaction signature return from sent transaction +// Connection to devnet cluster +const connection = new Connection(clusterApiUrl("devnet"), "confirmed"); + +// Transaction signature returned from sent transaction let transactionSignature: string; ``` ## Mint Setup -Next, let's configure the properties of our token mint and the define the -authorities. +First, let's define the properties of the Mint Account we'll be creating in the +following step. ```javascript +// Generate new keypair for Mint Account const mintKeypair = Keypair.generate(); -// address of our token mint +// Address for Mint Account const mint = mintKeypair.publicKey; -// The amount of decimals for our mint -const decimals = 9; -// authority that can mint new tokens -const mintAuthority = payer; -// authority that can close the mint account -const closeAuthority = payer; +// Decimals for Mint Account +const decimals = 2; +// Authority that can mint new tokens +const mintAuthority = pg.wallet.publicKey; +// Authority that can close the Mint Account +const closeAuthority = pg.wallet.publicKey; ``` -Next, let's get the size of the new mint account and calculate the minimum -lamports required for rent exemption. We use the helper `getMinLen` helper -function, which takes an array of extensions we want for this mint. +Next, let's determine the size of the new Mint Account and calculate the minimum +lamports needed for rent exemption. ```javascript +// Size of Mint Account with extension const mintLen = getMintLen([ExtensionType.MintCloseAuthority]); +// Minimum lamports required for Mint Account const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); ``` -With Token 2022, the size of the mint account will vary based on the extensions -enabled. +With Token Extensions, the size of the Mint Account will vary based on the +extensions enabled. ## Build Instructions -Now, let's build the set of instructions to: +Next, let's build the set of instructions to: - Create a new account -- Initialize the mint close authority extension -- Initialize our new account as a token mint +- Initialize the `MintCloseAuthority` extension +- Initialize the remaining Mint Account data First, build the instruction to invoke the System Program to create an account -and assign ownership to the Token 2022 Program. +and assign ownership to the Token Extensions Program. ```javascript +// Instruction to invoke System Program to create new account const createAccountInstruction = SystemProgram.createAccount({ - fromPubkey: payer.publicKey, // account that will transfer lamports to created account - newAccountPubkey: mint, // public key to use as the address of the created account - space: mintLen, // amount of bytes to allocate to the created account - lamports, // amount of lamports to transfer to created account - programId: TOKEN_2022_PROGRAM_ID, // public key of the program to assign as owner of created account + fromPubkey: payer.publicKey, // Account that will transfer lamports to created account + newAccountPubkey: mint, // Address of the account to create + space: mintLen, // Amount of bytes to allocate to the created account + lamports, // Amount of lamports transferred to created account + programId: TOKEN_2022_PROGRAM_ID, // Program assigned as owner of created account }); ``` -Next, build the instruction to initialize the Mint Close Authority extension for -the mint account. +Next, build the instruction to initialize the `MintCloseAuthority` extension for +the Mint Account. ```javascript +// Instruction to initialize the MintCloseAuthority Extension const initializeMintCloseAuthorityInstruction = createInitializeMintCloseAuthorityInstruction( - mint, // token mint account - closeAuthority.publicKey, // authority that can close the mint - TOKEN_2022_PROGRAM_ID, // SPL Token program id + mint, // Mint Account address + closeAuthority, // Designated Close Authority + TOKEN_2022_PROGRAM_ID, // Token Extension Program ID ); ``` @@ -152,72 +156,76 @@ Lastly, build the instruction to initialize the rest of the Mint Account data. This is the same as with the original Token Program. ```javascript +// Instruction to initialize Mint Account data const initializeMintInstruction = createInitializeMintInstruction( - mint, // token mint account - decimals, // number of decimals for token - mintAuthority.publicKey, // authority that can mint new tokens - null, // optional freeze authority - TOKEN_2022_PROGRAM_ID, // SPL Token program id + mint, // Mint Account Address + decimals, // Decimals of Mint + mintAuthority, // Designated Mint Authority + null, // Optional Freeze Authority + TOKEN_2022_PROGRAM_ID, // Token Extension Program ID ); ``` ## Send Transaction -Finally, we add the instructions to a new transaction and send it to the -network. This will create a mint account with the `MintCloseAuthority` -extension. +Next, let's add the instructions to a new transaction and send it to the +network. This will create a Mint Account with the `MintCloseAuthority` extension +enabled. ```javascript +// Add instructions to new transaction const transaction = new Transaction().add( createAccountInstruction, initializeMintCloseAuthorityInstruction, initializeMintInstruction, ); +// Send transaction transactionSignature = await sendAndConfirmTransaction( connection, transaction, - [payer, mintKeypair], + [payer, mintKeypair], // Signers ); console.log( - "\n", - "Transaction Signature:", - `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`, + "\nCreate Mint Account:", + `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`, ); ``` Run the script by clicking the `Run` button. You can then inspect the -transaction on the Solana Explorer. +transaction details on SolanaFM. ## Closing a mint account -With the `MintCloseAuthority` extension on the mint and a valid authority, it's -possible to close the mint account and reclaim the lamports on the mint account. +With the `MintCloseAuthority` extension enabled, the Close Authority can close +the Mint Account to reclaim the lamports from the account. ```javascript +// Send transaction to close Mint Account transactionSignature = await closeAccount( - connection, // connection to use - payer, // payer of the transaction fees - mint, // mint account to close - payer.publicKey, // account to receive the lamports - closeAuthority, // authority that can close the mint - [], // signing accounts - undefined, // options for confirming the transaction - TOKEN_2022_PROGRAM_ID, // SPL Token program id + connection, + payer, // Transaction fee payer + mint, // Mint Account address + payer.publicKey, // Account to receive lamports from closed account + closeAuthority, // Close Authority for Mint Account + undefined, // Additional signers + undefined, // Confirmation options + TOKEN_2022_PROGRAM_ID, // Token Extension Program ID ); console.log( - "\n", - "Transaction Signature:", - `https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`, + "\nClose Mint Account:", + `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`, ); ``` Run the script by clicking the `Run` button. You can then inspect the -transaction on the Solana Explorer. +transaction details on the SolanaFM. ## Conclusion -Token 2022's `MintCloseAuthority` extension is quite simple but effective. You -get to reclaim SOL that otherwise would have been lost. +The `MintCloseAuthority` extension is quite simple but effective. It enables +developers to reclaim SOL that otherwise would have been permanently locked in a +Mint Account. This functionality is particularly useful for applications +designed to produce NFTs that are intended to be burned. From 27d6c2f006af89a19056537fe38b38fff65e033b Mon Sep 17 00:00:00 2001 From: John <75003086+ZYJLiu@users.noreply.github.com> Date: Sat, 9 Dec 2023 20:46:11 -0600 Subject: [PATCH 5/7] proofread --- .../guides/token-2022/mint-close-authority.md | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/content/guides/token-2022/mint-close-authority.md b/content/guides/token-2022/mint-close-authority.md index 37ea0f51f..bc72c7d50 100644 --- a/content/guides/token-2022/mint-close-authority.md +++ b/content/guides/token-2022/mint-close-authority.md @@ -17,10 +17,13 @@ altRoutes: - /developers/guides/mint-close-authority --- -With the original Token program, it is not possible to close existing Mint -Accounts. With Token Extensions, the `MintCloseAuthority` extension enables a -designated Close Authority to close a Mint Account if the supply of the mint -is 0. +Currently, there's no option to close Mint Accounts owned by the Token Program +and reclaim the SOL allocated to these accounts. + +The ` MintCloseAuthority` extension introduces a solution to this limitation by +allowing a designated Close Authority to close a Mint Account if the supply of +the mint is 0. This feature provides a mechanism to recover SOL allocated to +Mint Accounts that are no longer in use. In this guide, we'll walk through an example of using Solana Playground. Here is the [final script](https://beta.solpg.io/65700c73fb53fa325bfd0c4a). @@ -196,7 +199,7 @@ console.log( Run the script by clicking the `Run` button. You can then inspect the transaction details on SolanaFM. -## Closing a mint account +## Close Mint Account With the `MintCloseAuthority` extension enabled, the Close Authority can close the Mint Account to reclaim the lamports from the account. @@ -225,7 +228,8 @@ transaction details on the SolanaFM. ## Conclusion -The `MintCloseAuthority` extension is quite simple but effective. It enables -developers to reclaim SOL that otherwise would have been permanently locked in a -Mint Account. This functionality is particularly useful for applications -designed to produce NFTs that are intended to be burned. +The `MintCloseAuthority` extension enables developers to reclaim SOL that +otherwise would have been permanently locked in a Mint Account. This feature is +particularly useful for applications or games involving single-use NFTs that are +meant to be burned. It ensures that the SOL allocated to Mint Accounts which are +no longer used can be reclaimed and reused. From fcf479bbe92a09336e1d7c303fbcb2f7739d58bb Mon Sep 17 00:00:00 2001 From: nickfrosty Date: Tue, 12 Dec 2023 16:13:11 -0500 Subject: [PATCH 6/7] refactor: changed dir --- .../{token-2022 => token-extensions}/mint-close-authority.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename content/guides/{token-2022 => token-extensions}/mint-close-authority.md (99%) diff --git a/content/guides/token-2022/mint-close-authority.md b/content/guides/token-extensions/mint-close-authority.md similarity index 99% rename from content/guides/token-2022/mint-close-authority.md rename to content/guides/token-extensions/mint-close-authority.md index bc72c7d50..16a07f213 100644 --- a/content/guides/token-2022/mint-close-authority.md +++ b/content/guides/token-extensions/mint-close-authority.md @@ -1,5 +1,6 @@ --- date: Dec 05, 2023 +seoTitle: "Token Extensions: Mint Close Authority" title: How to use the Mint Close Authority extension description: "The Token program allows owners to close token accounts, but it is impossible @@ -13,8 +14,6 @@ difficulty: beginner tags: - token 2022 - token extensions -altRoutes: - - /developers/guides/mint-close-authority --- Currently, there's no option to close Mint Accounts owned by the Token Program From 0154a51575e0397be9260c1683a63a6def0d22e6 Mon Sep 17 00:00:00 2001 From: nickfrosty Date: Tue, 12 Dec 2023 16:20:51 -0500 Subject: [PATCH 7/7] refactor: editoral changes --- .../guides/token-extensions/mint-close-authority.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/content/guides/token-extensions/mint-close-authority.md b/content/guides/token-extensions/mint-close-authority.md index 16a07f213..c4a0b5525 100644 --- a/content/guides/token-extensions/mint-close-authority.md +++ b/content/guides/token-extensions/mint-close-authority.md @@ -3,9 +3,8 @@ date: Dec 05, 2023 seoTitle: "Token Extensions: Mint Close Authority" title: How to use the Mint Close Authority extension description: - "The Token program allows owners to close token accounts, but it is impossible - to close mint accounts. With Token Extensions, it is possible to close mints - by initializing the MintCloseAuthority extension before initializing the mint." + "With Token Extensions, it is possible to close token mint accounts by + initializing the MintCloseAuthority extension before initializing the mint." keywords: - token 2022 - token extensions @@ -16,10 +15,10 @@ tags: - token extensions --- -Currently, there's no option to close Mint Accounts owned by the Token Program -and reclaim the SOL allocated to these accounts. +With the original SPL token program, there was no option to close Mint Accounts +owned by the Token Program and reclaim the SOL allocated to these accounts. -The ` MintCloseAuthority` extension introduces a solution to this limitation by +The `MintCloseAuthority` extension introduces a solution to this limitation by allowing a designated Close Authority to close a Mint Account if the supply of the mint is 0. This feature provides a mechanism to recover SOL allocated to Mint Accounts that are no longer in use.