Skip to content

Commit

Permalink
fix prettier lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYJLiu authored Dec 4, 2023
1 parent 178cfce commit b8e66f7
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions content/guides/token-2022/mint-close-authority.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:

Expand Down Expand Up @@ -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();
Expand All @@ -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

Expand All @@ -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({
Expand All @@ -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
);
```

Expand All @@ -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(
Expand All @@ -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.

0 comments on commit b8e66f7

Please sign in to comment.