Create a mint that can be closed once the tokens are burnt.
close mint
extension which allows mint
accounts to be closed.close mint
extension, the supply of said mint needs
to be 0.mintCloseAuthority
can be updated by calling setAuthority
close mint
extension. This simply
allows a mint account to be closed and the lamports refunded. The only caveat,
is the supply of said mint needs to be 0.
This extension is a nice improvement for developers, who may have thousands of
mint accounts that could be cleaned up and be refunded for. Additionally it’s
great for NFT holders who wish to burn their NFT. They will now be able to
recuperate all of the costs, ie closing the mint, metadata and token accounts.
Whereas before, if someone burned an NFT would only recuperate the metadata and
token account’s rents. Note, the burner would also have to be the
mintCloseAuthority
.
The close mint
extension, adds an additional field mintCloseAuthority
to the
mint account. This is the address of the authority to actually close the
account.
Again, for a mint to be closed with this extension, the supply has to be 0. So
if any of this token is minted, it will have to be burned first.
SystemProgram.createAccount
createInitializeMintCloseAuthorityInstruction
createInitializeMintInstruction
SystemProgram.createAccount
allocates space on the
blockchain for the mint account. However like all Token Extensions Program
mints, we need to calculate the size and cost of the mint. This can be
accomplished by using getMintLen
and getMinimumBalanceForRentExemption
. In
this case, we’ll call getMintLen
with only the
ExtensionType.MintCloseAuthority
.
To get the mint length and create account instruction, do the following:
createInitializeMintCloseAuthorityInstruction
initializes the close authority extension. The only notable parameter is the
mintCloseAuthority
in the second position. This is the address that can close
the mint.
createInitializeMintInstruction
initializes the mint.
close mint
extension, all that is needed is to call
the closeAccount
function.
Remember, that to close the mint account, the total supply has to be 0. So if
any tokens exist, they have to be burned first. You can do this with the burn
function.
closeMintAuthority
you can call the setAuthority
function and
pass in the right accounts, as well as the authorityType
, which in this case
is AuthorityType.CloseMint
close mint
extension. We will then
mint some of the tokens and see what happens when we try to close it with a
non-zero supply (hint, the close transaction will fail). Lastly, we will burn
the supply and close the account.
close-mint
and navigate to it.
We’ll be initializing a brand new project. Run npm init
and follow through the
prompts.
Next, we’ll need to add our dependencies. Run the following to install the
required packages:
src
. In this directory, create a file named
index.ts
. This is where we will run checks against the rules of this
extension. Paste the following code in index.ts
:
index.ts
creates a connection to the specified validator node and calls
initializeKeypair
. This is where we’ll end up calling the rest of our script
once we’ve written it.
Go ahead and run the script. You should see the payer
and mint
public key
logged to your terminal.
initializeKeypair
with airdropping, follow the
next step.
solana-test-validator
. This
will run the node and also log out some keys and values. The value we need to
retrieve and use in our connection is the JSON RPC URL, which in this case is
http://127.0.0.1:8899
. We then use that in the connection to specify to use
the local RPC URL.
clusterApiUrl
from @nexis-network/web3.js
and pass it to the connection as such:
keypairPath
parameter to initializeKeypair
. You can get this from
running solana config get
in your terminal. And then go to
faucet.nexis.network and airdrop some NZT to your
address. You can get your address from running solana address
in your
terminal.
createClosableMint
in a
new file src/create-mint.ts
.
To create a closable mint, we need several instructions:
getMintLen
: Gets the space needed for the mint accountSystemProgram.getMinimumBalanceForRentExemption
: Tells us the cost of the
rent for the mint accountSystemProgram.createAccount
: Creates the instruction to allocates space on
Nexis Native Chain for the mint accountcreateInitializeMintCloseAuthorityInstruction
: Creates the instruction to
initialize the close mint extension - this takes the closeMintAuthority
as a
parameter.createInitializeMintInstruction
: Creates the instruction to initialize the
mintsendAndConfirmTransaction
: Sends the transaction to the blockchaincreateClosableMint
function:
connection: Connection
: The connection objectpayer: Keypair
: Payer for the transactionmintKeypair: Keypair
: Keypair for new mintdecimals: number
: Mint decimalssrc/index.ts
. First you’ll need to import our
new function. Then paste the following under the right comment section:
src/index.ts
, create an account and mint 1 token to that account.
We can accomplish this by calling 2 functions: createAccount
and mintTo
:
close mint
extension requires a non-zero supply. So
to see the resulting error message, we’ll wrap the closeAccount
function in a
try catch and log out the error:
burn
:
closeAccount
, however, for the sake of visualizing how this works,
we’ll do the following:
closeAccount
function, we check the account status once more to confirm that it has indeed been closed successfully.
getMint
: Grabs the mint account and deserializes the informationgetAccountInfo
: Grabs the mint account, so we can check it exists - we’ll
call this before and after the close.closeAccount
: Closes the mintsolution
branch of
this repository.