How to Create Tokens on Solana in 2024: A Developer's Step-by-Step Guide

·

3 min read

Overview

This document outlines the steps required to create a new token on the Solana blockchain.

Dependencies

To follow this tutorial, you'll need to install the following dependencies:

  1. @solana/web3js

  2. @solana/spl-token

For this tutorial, TypeScript is my language of choice.

Step 1

Imports

import {
  Connection,
  Keypair,
  PublicKey,
  Transaction,
  sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
  TOKEN_PROGRAM_ID,
  createMint,
  createAccount,
  mintTo,
  getOrCreateAssociatedTokenAccount,
} from '@solana/spl-token';

Step 2

Create a connection to the Solana blockchain

For this tutorial, we will be using the Solana devnet.

const connection = new Solana.Connection(
Solana.clusterApiUrl('devnet'),
'confirmed',
);

Step 3

Configure Mint authority

The mint authority is the account that has the power to create new tokens. You can either utilize an existing Solana keypair or generate a new one.

const mintAuthority = Keypair.generate();

Step 4

Create a new token mint with the desired configurations

const decimals = 6; // Set the desired number of decimal places
const mintAddress = await createMint(
  connection,
  mintAuthority, // Mint authority
  mintAuthority.publicKey, // Freeze authority (same as mint authority in this case)
  decimals,
);

Step 5

Configure total supply

The total supply of a token in the Solana Token Program is not explicitly set during the token creation process. Instead, the total supply is determined by the sum of all minted tokens across all token accounts associated with the token mint.

When you create a new token mint using createMint, the initial total supply is effectively zero. As you mint tokens to various accounts using mintTo, the total supply increases accordingly.

To set or cap the total supply of a token, you would need to keep track of the total minted amount and stop minting new tokens once the desired supply is reached.

const totalSupply = 1000000 * Math.pow(10, decimals); // 1,000,000 tokens with 6 decimal places

Step 6

Get or create an associated token account

[!NOTE] Important Context

To receive a token, a user must have an associated account for that specific token, or the transaction will fail.

Lucky for us the spl-token library provides a function that allows you to create or get an associated token account.

const treasuryAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  mintAuthority,
  mintAddress,
  mintAuthority.publicKey // Use the mint authority as the treasury account
);

Step 7

Mint the total supply to the treasury account

const mintTxn = await mintTo(
  connection,
  mintAuthority,
  mintAddress,
  treasuryAccount.address,
  mintAuthority.publicKey,
  totalSupply
);

Step 8

Check account balance

We obtained the treasury account for the token from the mint authority keypair. Therefore, we can retrieve the token's address from the mint authority public key.

const associatedTokenAccount = await getAssociatedTokenAddress(
    tokenAddress,
    addressToCheck,
    true,
    ASSOCIATED_TOKEN_PROGRAM_ID,
    TOKEN_PROGRAM_ID,
);

Get the account info

    const tokenAccountInfo = await getAccount(
      connection,
      fromAssociatedTokenAccount
    );
const mintAuthorityTokenBalance = fromTokenAccountInfo.amount.toNumber() / Math.pow(10, 6);

console.log(mintAuthorityTokenBalance)

Summary

In this tutorial, we have discussed the steps needed to create a new token on the Solana blockchain using the @solana/web3.js and @solana/spl-token libraries.

We began by connecting to the Solana devnet and setting up the mint authority. Then, we made a new token mint with the preferred number of decimal places and set the total supply.

We then showed how to get or create an associated token account, which is needed for receiving tokens. Following that, we minted the total token supply to the Treasury account linked with the mint authority.

Finally, we demonstrated how to check the account balance of the associated token account to confirm the success of the minting process.

Creating a new token on Solana is simple, thanks to the well-designed APIs provided by the Solana team. By following the steps outlined in this guide, developers can easily create and manage their own tokens on the Solana blockchain.