Introduction
Solana is a high-performance blockchain platform designed for decentralized applications and cryptocurrencies. Known for its fast transaction speeds and low costs, Solana has become a popular choice for developers looking to build scalable blockchain applications. One of the essential features of any blockchain ecosystem is the ability to create and manage tokens, which can represent various assets, currencies, or utilities within the network.
In this article, we will learn how to create a token on the Solana blockchain using the command-line interface (CLI). By the end of this guide, we'll have a new token minted and ready for use.
Prerequisites
Before moving ahead, make sure you have the following tools installed on your system.
Follow this article to learn how to set the development environment: How to Setup the Solana Development Environment?
How do you create a token using CLI?
Following are the steps that you need to follow to create a token using CLI.
Step 1. Setting Up a Solana Wallet
If you have already created a wallet, you can skip this step. In your terminal, enter the following command.
solana-keygen new
This will create a key pair. Keep your passphrase in a secure place.
If you already have a wallet and want to create another, you can forcefully do this by following the command.
solana-keygen new --force
This will create a new key pair forcefully.
You can check the balance of your wallet by following command.
solana balance
You can also airdrop some tokens by following the command.
solana airdrop 2 F4aPqHpcrZXDyb6MBdBLG**************** --url devnet
This will airdrop 2 tokens in your devnet account having the above public key. Sometimes the airdrop may fail due to the limit, you can try again and it will work.
If you want to get information about your account, use this command.
solana account ************XDyb6MBdBLGr********
This will give you the information associated with the above account.
Step 2. Installing the Token Program CLI
Now that you have your wallet ready. Let's install the SPL token CLI tool, which is necessary for creating and managing tokens using CLI.
cargo install spl-token-cli
If you are getting any pkg-config error, you should install pkg-config by following the command.
sudo apt install pkg-config
This will resolve this error.
To verify whether the spl token program is installed properly, you can check the version by following the command.
spl-token --version
This ensures we have properly installed the spl token program.
Step 3. Creating the Token
Now we have a wallet and token program, it's time to create our own token. To do so follow the below command.
spl-token create-token --url devnet
This will create a token using the existing spl token program. You will get a token address in the terminal, which represents your new token. Store this in a safe place.
Step 4. Creating a Token Account
Now that our token is created. we need to create an associated token account to hold our tokens. To create an account for our token, use the following command.
spl-token create-account <CREATED_TOKEN> --url devnet
This will create an account for the above token. Make sure to use your correct token address in the above command.
Now you can check the balance of the created token.
spl-token balance <CREATED_TOKEN> --url devnet
Step 5. Minting Tokens
Now you can mint new tokens to the created account by the following command.
spl-token mint <TOKEN_ADDRESS> <amount> --url devnet
This will mint the given amount of tokens in your token account.
To check the tokens in the supply, you can use the following command.
spl-token supply <CREATED_TOKEN> --url devnet
This command will show you the tokens that are currently present in the supply.
How to manage the created token in Solana?
Now that we have created our token, we can perform various actions on the created token.
Limit the supply of the token
Although we are currently creating tokens for our development and testing purposes, if in the future we create our token, it will be important to limit the supply of the token as an unlimited supply is not good for your token value and reputation and there are many more things to it.
So, to limit the supply of your token, you can use the following command.
spl-token authorize <CREATED_TOKEN> mint --disable --url devnet
this command will disable the further minting of tokens and limit the supply. Currently, our token is on devnet so, I am using devnet in the command, you can change it accordingly.
Transferring Tokens
We can transfer our created token to others by following the command.
spl-token transfer <TOKEN_ADDRESS> <AMOUNT> <RECIPIENT_WALLET_ADDRESS> --url devnet
If you are having issues like unfunded recipient, you can use the following command.
spl-token transfer <TOKEN_ADDRESS> <AMOUNT> <RECIPIENT_WALLET_ADDRESS> --url devnet --allow-unfunded-recipient --fund-recipient
This will solve the unfunded recipient issue.
Make sure to verify the transaction to ensure tokens are transferred correctly.
Managing the Token by setting permissions
You can set various permissions on your token, such as giving mint authority, and freezing or thawing accounts. Before setting these permissions make sure you were to do this and give permissions to only the most trusted person.
Freeze an Account: Freezing an account prevents any further transactions involving the token from that account. To freeze use the following command.
spl-token authorize <TOKEN_ACCOUNT> freeze <AUTHORITY_ADDRESS> --url devnet
In the above command, replace <TOKEN_ACCOUNT>
with the account to be frozen and <AUTHORITY_ADDRESS>
with the authority’s public key.
Thaw an Account: Thawing reverses freezing action, allowing transactions to resume. To thaw use the following command.
spl-token authorize <TOKEN_ACCOUNT> thaw <AUTHORITY_ADDRESS> --url devnet
In the above command, replace <TOKEN_ACCOUNT> with
the account to be thawed and <AUTHORITY_ADDRESS>
with the authority’s public key.
Mint Authority of an Account: If you want to give mint authority of your token to someone else, you can use the following command.
spl-token authorize <TOKEN_ADDRESS> mint <NEW_AUTHORITY>
In the above command, replace <NEW_AUTHORITY> with the new authority's public key.
Burning Tokens: Burning is useful for managing token supply and maintaining value. To burn tokens and reduce the supply, you can use the following command.
spl-token burn <TOKEN_ACCOUNT> <AMOUNT> --url devnet
This will burn the given amount of tokens from the above token account. Note that, the tokens will be burned from the remaining tokens of your token account. If you previously sent some tokens to others, their token will remain unaffected by this action.
Conclusion
We’ve covered the entire process of creating, minting, and managing a token on Solana using the CLI. From setting up the environment to transferring and burning tokens, these steps equip you with the knowledge to launch your custom token. Custom tokens on Solana open up many possibilities, from decentralized finance (DeFi) applications to unique digital assets. Continue exploring Solana’s ecosystem and leverage its capabilities for innovative blockchain projects.