Solana Development(2): Create Your Own Token

By Brochain | Solana Development | 23 Apr 2022


After the previous introduction of Solana CLI basic operations, we will move to talk about how to create our own token. Owning your own token has many advantages. For example, if you have your own business, you can ask customers to get your tokens on the exchange, and then exchange the tokens for your company’s services. In this way, price of your company’s services will be determined by the market. This is also a new economic model since the popularization of blockchain. And just like the previous article, all operation in this articles will be carried out on Testnet first. We will demonstrate how to put tokens on the mainnet in the future.

To create and manage your own token, apart from installing Solana-CLI and Rust, you also need to download spl-token-cli. It is also written in Rust. So, we can download it with Cargo loaded in Rust. Cargo is similar to npm in Javascript, and the download instructions are as follows:

$ cargo install spl-token-cli

If you don’t know how to make your own wallet with Solana-CLI yet, I suggest you to checkout our previous article first.

Now, let’s check the configuration first:

$ solana config getConfig File: /root/.config/solana/cli/config.yml
RPC URL: http://api.devnet.solana.com
WebSocket URL: ws://api.devnet.solana.com/ Keypair Path: /root/solana/my_wallet.json
Commitment: confirmed

According to the configuration, we currently set /root/solana/my_wallet.json as our operating wallet, and our wallet is successfully connected to Testnet. Since each of the following operations requires payment of a handling fee, if you do not have any Sol in this account, you can get it through airdrop on Testnet. The instructions are as follows:

$ solana airdrop 1

In Solana, everything requires an account, including the tokens you are going to create. Your token account is mainly used to store token-related data, including information such as issuing amount, token owner etc. You can create a token account through spl-token. The relevant instructions are as follows:

$ spl-token create-token Creating token Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff
Signature: 3MYfw3ECeWnD72UmJY5gLkGXeDk3DoiWYGEdJEdCtcHzBKnCER9kL8kDyz6Dq8QLugcebgyvPgViqYvL5ivQf9hY

As the above information, Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff is your created token account. At this time, the wallet that created the token (ie /root/solana/my_wallet.json) is token owner, and can perform operations including minting new tokens etc.

In Solana, any token has its Associated Token Account in a wallet. It is like a sub-account under the same wallet. It is used for identify different tokens in the same wallet. You can use the following commands to create an Associated Token Account for our newly minted tokens:

$ spl-token create-account Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff Creating account A5hYTYPM8Q6VtFBz178FSdjefPU6DoM5oHuD8BrTR4nr

Then, you can start minting 100 tokens and store them in our wallet:

$ spl-token mint Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff 100 A5hYTYPM8Q6VtFBz178FSdjefPU6DoM5oHuD8BrTR4nr Minting 100 tokens Token: Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff Recipient: A5hYTYPM8Q6VtFBz178FSdjefPU6DoM5oHuD8BrTR4nr

We can now view the token information in this wallet:

$ spl-token accounts Token Balance 
--------------------------------------------------------------- Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff 100

Remember in the previous article, we created another account stored in /root/solana/my_wallet2.json (Public Key is Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56)? Now, let’s try to send him the newly minted tokens:

$ spl-token transfer Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff 50 Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56 --fund-recipient Transfer 50 tokens Sender: A5hYTYPM8Q6VtFBz178FSdjefPU6DoM5oHuD8BrTR4nr Recipient: Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56 Recipient associated token account: 2vV8LkrspyYDA8rqa2sAaRZjtwAb7B9fUhi5jeomHDgM Funding recipient: 2vV8LkrspyYDA8rqa2sAaRZjtwAb7B9fUhi5jeomHDgM (0.00203928 SOL)Signature: 4jDpJ6QPsrTmkycQLZgob1f2go6jKpxmkbeRLqBBXFDd6aN66JDiDhcTzYtqJ9KULcUzYByJxUWoVdHRx1x3Axbt

As the beneficiary account has not established an Associated Token Account for the new token. Therefore, we need to add — fund-recipient in the transfer to help the other party establish a token account when receiving it. Let’s take a look at the current account balance:

$ spl-token accounts Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff -v Account Balance 
------------------------------------------------------------- A5hYTYPM8Q6VtFBz178FSdjefPU6DoM5oHuD8BrTR4nr 50

Now switch to the beneficiary account and check the new token balance again

$ solana config set --keypair /root/solana/my_wallet2.jsonConfig File: /root/.config/solana/cli/config.yml
RPC URL: http://api.devnet.solana.com
WebSocket URL: ws://api.devnet.solana.com/ Keypair Path: /root/solana/my_wallet2.json
Commitment: confirmed $ spl-token accounts Hqp1TVZ7LQPHWj4SuMycUoWYBessWAC5PnhU3hKqHJff -v Account Balance ------------------------------------------------------------- 2vV8LkrspyYDA8rqa2sAaRZjtwAb7B9fUhi5jeomHDgM 50

We can find that the newly minted tokens have been successfully transferred.
Although it should not be difficult to find applications that use GUI to creae tokens on Github or on the website, learning to operate through the CLI interface can give you a deeper understanding of the operation of Solana, such as what is spl-account, etc. In the following articles, we will introduce how to mint non-fungible tokens, issue your own tokens on the mainnet, and interact with other smart contracts in Solana.

Thank you everyone! See you next time!

How do you rate this article?


1

0

Brochain
Brochain

Web3 Developer. The best way to learn is to share what you learnt.


Solana Development
Solana Development

Welcome to my blog. This blog would be about Solana Development. So, if you are interested to know how to write smart contract, web3 programs in Solana, stay tuned! Cheers!

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.