Introduction to Solana and the spl-token command line

This article is an introduction to the Solana ecosystem, the Solana Tool Suite and the spl-token command-line utility. It was made by following the Moralis course: Solana Programming 101

  • Create your own key with the command line
  • Create your own key with Phantom
  • Operation on token with the cli

If you want to explore Solana more, I have written 2 other articles: Solana Core Concept & Solana Programs - Basic Security with Anchor

[TOC]

Installation

You can install Solana in your OS by following the official installation guide: https://docs.solana.com/cli/install-solana-cli-tools

Key generation

The first thing to do is to create a pair of public/private keys.

When you use the spl-token command line, this generated key will be used

  • Key generation
solana-keygen new --force

My result: Wrote new keypair to /home//.config/solana/id.json

My public key is BgAoyQvL1ejQNrj4BewgJxfTHZ2cLjgxq8tza5oTcxyK

  • Getting the public key of a wallet
solana-keygen pubkey

Install spl-token-cli

The spl-token command-line utility can be used to experiment with SPL tokens.

You can find the documentation here: https://spl.solana.com/token

  • Install the cli tools
cargo install spl-token-cli

During the installation, it is possible you have some errors with libudev. You can find more information with this link: stackoverflow.com - libudev-development-package-not-found

the solution for me was to install libudev with this command:

sudo apt-get install libudev-dev

Airdrop

To perform operations, you need to have some SOL in your wallet. It is possible to have some SOL on devnet with the command airdrop

  • Command
solana airdrop 1 --url devnet
  • Checking the balance
solana balance --url devnet
  • Screenshot

solana-airdrop-dev

There is a limitation on the number of SOL you can obtain. It is currently one by call

On the screenshot below, you can see the balance is unchanged.

solana-airdrop-dev-unchanged


Spl-token

Creating a token

spl-token create-token -url devnet

spl-create-token

The address of the token is: H3goZSZ99PjQCBmFqy93jX683G3hgSE1BSnyY5DBEvws

Solana explorer

Information about the token on Solana explorer:

explorer.solana.com - tx

info-token-solana-explorer

Create an account

Account in a wallet holds a token. Accounts must be created within wallets

Reminder: In Solana, everything is an account

spl-token create-account <TOKEN_ADDRESS> --url devnet

The returned id is the token account address

spl-create-account

There is one account by token. An error is generated if you try to create a second accountspl-create-account-2

Solana explorer

create-account-solana-explorer


Mint

spl-token mint <TOKEN_ADDRESS> <NUMBER> --url devnet

spl-mint-token

  • Check the balance
spl-token balance H3goZSZ99PjQCBmFqy93jX683G3hgSE1BSnyY5DBEvws --url devnet

spl-mint-token-balance

  • Check the supply
spl-token supply H3goZSZ99PjQCBmFqy93jX683G3hgSE1BSnyY5DBEvws --url devnet

spl-token-supply

Solana explorer

Check on Solana explorer

Link: explorer.solana.com - tx

mint-token-solana-explorer


Disable authorization

  • Renouncing the ability to mint tokens:
spl-token authorize <TOKEN_ADDRESS> mint --disable --url devnet

spl-authorize-mint-disable

  • Check the result:

spl-authorize-mint-disable-check


Burn token

Only our own tokens can be burned

spl-token burn <ACCOUNT_ADDRESS> <number> --url devnet

spl-token-burn


Send token to another account

You can create a second account with phantom wallet to test this command

spl-token transfer <SOURCE_TOKEN_ADDRESS> <AMOUNT> <TARGET_TOKEN_ADDRESS> --url devnet --fund-recipient

Without the fund-recipient flag, you’d not be able to add balance to an unfunded address

Transfer token to a phantom wallet

For the next test, I created a new token with an account because I had revoked the authorizations for the previous one (not very smart haha)

Token address: EHNLcqxdBLGGMwqVSR3tf1PsSo6HxNgxftD4j3wnHNQL

Account: 2dWvomUHStpCZbdVfLvxKC4neJmUYNEaozpru65kfHyH

  • Add the token to Phantom

phantom-add-custom-token

  • Transfer some tokens to the phantom wallet

We transfer some tokens

spl-transfer-token

phantom-token-transfer

spl-transfer-check

Solana explorer

See on Solana explorer: explorer.solana - tx

solana-explorer-transfer

References

Main references are:

You might also enjoy