How To Initialize Mint Accounts and Mint Tokens Using the Anchor Framework | by Senudajayalath | Aug, 2022

A brief guide to experiment with this program

0*xQgLKAb eWFLGKws
Photo by shehan levusara on Unsplash

This article will show you how to use derive macros in Anchor to initialize mint accounts and mint tokens in the Solana Network.

Solana (SOL) is the native token for the Solana network. But if you need to create another token on the Solana network, it provides you with all the resources you need. The anchor framework, used to write smart contracts (programmes), has simplified this process using derive macros.

Photo by Shubham Dhage on Unsplash

Derive macros is a special feature of the Anchor frameworkis used to simplify the serialization/deserialization of structs. According to the official documentation as found here, derive macros can provide further functionality through the use of attributes and instructions.

In this article, let’s see how the the Anchor framework has made the process of mint accounts and minting tokens to a specific associated token account(ATA) very easy for developers.

VS Code — Code editor
Anchor version — 0.24.2

Previous knowledge of Anchor framework

This is the rust code to initialize a new mint account:

Let us see how this code works. The struct InitializeMint is what gets passed into the function initailize_mint. The code from line 17 to 24 initializes a mint account with a given address. When initializing a mint account we can specify who will be paying for this transaction using the field payer. In addition to that, the authority and the freeze_authority can be specified as well.

Here we have used payer for all three fields. But if you want to use another address as authority or freeze_authority you are more than welcome to do so. The payer is defined in line 26 as a signer account. This is because in line 19, I have named payer = payer , and the payer must sign the transaction.

The authority account mentioned here will be given the authority to mint and burn tokens after it has been created. The freeze_authority will be the account that can freeze the functionality of the mint account at any time. To complete the transaction, there are some helper accounts that needs to be passed in to the struct. They are the system_program,token_program and rent.

Let us see how we can test this piece of code from the JS side:

An important thing to notice here is that both the fromwallet and the mint who are the payer and mint respectively has to sign the transaction. The fromwallet has to sign because it is paying for the transaction and the mint has to sign, to confirm that the private key of the account we have given to initialize the mint account is owned by us.

This is the rust code to mint tokens to an ATA:

MintToken is the struct that gets passed into the function mint_token to mint tokens to a specified ATA. In this struct, you need to specify the address of the mint in the field mint. The token_program is an helper account that needs to be specified. token_account is the ATA that you will be minting tokens into.

The authority field needs to include the authority of the mint account that was set when initializing the mint account. token_account and mint are set to mutable (#[account(mut)]) because after the transaction has occurred the balances of the account will be changed.

If you look at the mint_token function, it would be clear that a cross-program invocation (CPI) is happening inside this function call. The token_program account is being called from this program in line 18. token_program is the governing body of all the tokens in Solana.

Therefore, any minting, burning or anything related tokens on Solana should be executed by calling the token_program function.

Now, let’s look at the JS code for minting tokens:

It is important to note that the field tokeAccount should be given an ATA as input. Therefore, in line 8, we have derived the associated token account for fromWallet for that specific mint.

This way, you can mint any amount of tokens from your mint account to your target ATA.

Thanks for reading!

I will follow up this tutorial with another on how to burn tokens in Solana and how this can be done with the Anchor framework.


  1. Anchor Lang document —

News Credit

%d bloggers like this: