Create instructions
Build instructions that create a new Solana token
A new token
Now that our Client helper is ready, let's use it to build a new transaction. In this tutorial, we'll build a simple transaction that creates a new Solana token using the Token program.
More precisely, we will create a Mint account which is the main account that represents a Solana token. We will need at least two instructions to create a new Mint account:
- The CreateAccountinstruction of the System program. This will allocate a new account on the Solana blockchain and assign it to the Token program.
- The InitializeMintinstruction of the Token program. This will initialize the previously allocated account as a newMintaccount.
The createMint helper
Before we start constructing these instructions, let's create a new create-mint.ts file under our src directory. We'll export a createMint function that will be responsible for building and sending the transaction that creates a new Mint account.
We'll accept our Client object as the first argument and some options as the second argument. To keep things simple, we'll only support customizing the number of decimals for the new Mint account.
Next, let's start building our instructions inside that createMint helper.
The CreateAccount instruction
To allocate a new account on the Solana blockchain, we need to use the CreateAccount instruction of the System program. We can build this instruction using the getCreateAccountInstruction helper from the @solana-program/system library.
As you can see, the getCreateAccountInstruction helper requires a handful of inputs that we need to provide:
- The payersigner that will pay for the storage fees of the newly allocated account. In our case, we'll use thewalletsigner of ourClientobject.
- The newAccountitself as a signer. Since this can be any address, we'll generate a new one and save it as amintvariable.
- The spacewe want to allocated for the new account. For this, we can use thegetMintSizehelper from the@solana-program/tokenlibrary.
- The number of lamportsto transfer to the new account in order to pay for the storage fees. This can be deduced from the size we want to allocate for the account by usinggetMinimumBalanceForRentExemptionRPC method. This method returns the minimum lamports required for an account of the given size.
- The programAddressof the program that will own the new account. In our case, that's the Token program whose address is accessible via theTOKEN_PROGRAM_ADDRESSconstant of the@solana-program/tokenlibrary.
We end up with the following code. Notice how we wrapped a few promises that could be executed in parallel in a Promise.all to make the code more efficient.
The InitializeMint instruction
Now that we have an empty account allocated on chain, let's initialize it as a Mint account. For that, we can use the getInitializeMintInstruction helper from the @solana-program/token library.
This time, the instruction requires we provide the following inputs:
- The address of the mintaccount we just allocated. Since we already generated a newmintsigner, we just need to providemint.address.
- The number of decimalsfor the newMintaccount. We'll use theoptions.decimalsargument or default to0.
- The authority that can mint new tokens. Here, we'll use the walletsigner of ourClientobject.
- The optional authority that can freeze tokens. Let's also use the walletsigner for this.
We end up with the following code:
Compute budget instructions
On top of any instructions we might need in our transactions, we may also use instructions from the Compute Budget program to configure our transaction's compute allocation. For instance, we may use the SetComputeUnitLimit instruction to set the maximum number of compute units (CUs) that our transaction can consume. The lower that number is, the higher the chances that our transaction will be included in the next block. Additionally, the SetComputeUnitPrice instruction can be used to provide additional priority fees for our transactions in the form of micro-lamports per CU.
If we wanted to include these instructions explicitly in our transaction, we could use the getSetComputeUnitLimitInstruction and getSetComputeUnitPriceInstruction helpers from the @solana-program/compute-budget library.
However, for this tutorial, we won't set up priority fees and we will use another helper to estimate the compute unit our transaction will consume by simulating it. We will tackle this — as well as the transaction message construction — in the next article.