Solana Development(1): Basic Operation of Solana-CLI

Solana Development(1): Basic Operation of Solana-CLI

By Brochain | Solana Development | 23 Apr 2022


If 2020 is defined as the year of Ethereum Defi. Then, 2021 will definitely be the year of Solana ecosystem. According to Solana’s official website, there are currently 400 projects laucnhed in Solana. This is certainly not easy for a blockchain newly launched its mainnet in April 2020. In the next few articles, we will try to explore some concepts related to Solana blockchain development. In this first article, we first discuss some basic operations using Solana-CLI such as how to use it to create a wallet and transfer funds etc.

Basic Setup

Examples in this article are operated on Ubuntu 20.04.

First, we need to download Solana’s CLI tools from Solana’s official website. At the time of writing this article, the latest version of Solana CLI-tools is 1.8.0. You may download using below command:

sh -c "$(curl -sSfL https://release.solana.com/v1.8.0/install)"

After the download is complete, reboot your workstation, and then check whether it is installed properly:

$ solana --version solana-cli 1.8.0 (src:4a8ff62a; feat:1813598585)

Then, we need to download Rust, the main language used by Solana:

$ curl https://sh.rustup.rs -sSf | sh
$ source $HOME/.cargo/env
$ rustup component add rustfmt

Like other blockchains, in Solana, you need a wallet to store your token, tranfer your token and other actions.

If you are new to Blockchain, wallet can be simply understood as a pair of public key and private key. You can think Public Key as your account number. For example, if your friend needs to transfer some tokens to you, he needs to put your public key as the destination of the funds. Private key can be simply understood as the signature of your account, which is used to check whether the transfer information is sent from your account and is authorised by you. Therefore, your private key must be kept well and safe. Otherwise, if you lose it, you will not be able to use any of your funds, or if it is leaked, other people can transfer the funds in your account at will.

According to Solana’s official documentation, Solana Wallet can be setup in three different modes, namely Paper Wallet, Hardware Wallet and File System Wallet. This article mainly introduces how to create File System Wallet and use it for other operations.

If you need to create a new wallet account, you can create it through the following instructions:

$ solana-keygen new

The above command will automatically generate a pair of public and private keys and store them in the following preset locations:

/root/.config/solana/id.json

You can also instruct the newly generated public and private keys to be stored in a specific file:

$ mkdir solana
$ solana-keygen new --outfile /root/solana/my_wallet.json

Then, you need to declare to the Solana network that you own the wallet’s private key. First, you need to obtain the public key of this wallet:

$ solana-keygen pubkey ~/solana/my_wallet.json FEBxPgsTXTdWkifpiGUSjfzS7ztBFJKPnaHT8A7iUdFc

Then use this public key to pair with the wallet file:

solana-keygen verify FEBxPgsTXTdWkifpiGUSjfzS7ztBFJKPnaHT8A7iUdFc /root/solana/my_wallet.json

We can use the following command to view the current configuration:

$ solana config get Config File: /root/.config/solana/cli/config.yml
RPC URL: http://api.devnet.solana.com
WebSocket URL: ws://api.devnet.solana.com/ (computed)
Keypair Path: /root/.config/solana/id.json
Commitment: confirmed

Now, let me walkthrough the configuration data stated above:

Config File: The location of the configuration file;

RPC URL: URL of the Cluster you are currently connected to. In Solana, network is called Cluster. Currently, there are a few Clusters for you to connect in Solana such as Testnet, Mainnet-Beta, Mainnet etc. You can also create your own private Solana Cluster on your localhost. The above configuration shows http://api.devnet.solana.com which is Solana’s Testnet;

WebSocket URL: Websocket URL of the Solana Cluster you connected to. It is automatically generated from RPC URL;

Keypair: The location of the wallet that is currently operating

Since the wallet display location is the default location of Solana CLI, we need to set the wallet location to /root/solana/my_wallet.json:

$ solana config set --keypair /root/solana/my_wallet.json

You can also use this command to switch between wallets on same workstation.

As mentioned above, Solana has multiple Clusters to choose from. In this article, all operations would be carried out on Testnet. The setup of Testnet is basically the same as Mainnet. The only difference is that all tokens and transactions on Testnet are not real. So everyone can freely to try anything on Testnet.

After you download Solana-CLI, its default configuration should be pointed to Testnet. If not, you can use the following command to setup:

$ solana config set --url https://api.devnet.solana.com

In addition, on Testnet, you can obtain Sol coins through Airdrop. After completing the above configuration, you can obtain 2 Sol coins through the following instructions:

$ solana airdrop 2

Now, let’s create another wallet and name it my_wallet2.json. The instructions are the same as the previous ones:

$ solana-keygen new --outfile /root/solana/my_wallet2.json

Let’s extract its public key:

$ solana-keygen pubkey ~/solana/my_wallet2.json Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56

and verify your wallet:

solana-keygen verify Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56 /root/solana/my_wallet2.json

So, we now have two wallets on Solana’s Testnet, which are saved in /root/solana/my_wallet.json and /root/solana/my_wallet2.json respectively. I took two Sols through Airdrop in the wallet of /root/solana/my_wallet.json. Now, let’s try to send one of them to the wallet of /root/solana/my_wallet2.json.

First use the following command to check the current configuration:

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

According to the above information, we are using /root/solana/my_wallet.json as our wallet. Then, we enter the public key of the receiving account to transfer 1 Sol to this account:

$ solana transfer Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56 1 Error: The recipient address (Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56) is not funded. Add `--allow-unfunded-recipient` to complete the transfer

You will find that there is an error in the first transfer. The main reason is that the balance of the receiving account is 0, so the instruction needs to be changed to:

$ solana transfer Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56 1 -allow-unfunded-recipient

After success, you can use the following command to check the balance of the receiving account:

$ solana balance Ec2x4xwfxgLuZBwvuv1HmhFgujWNJ16Lkf92Zu81hv56
1 SOL

We can also check the balance of the original account:

$ solana balance FEBxPgsTXTdWkifpiGUSjfzS7ztBFJKPnaHT8A7iUdFc
0.9985234 SOL

You will find that after your first account has transferred 1Sol out, its remaining balance only has 0.9985234Sol instead of 1Sol. The reason is that Solana transactions are the same as other blockchains, each transaction requires payment to the Validator of the network. Therefore, the missing part is the processing fee of this transaction.

This article mainly introduces the common usage of Solana CLI interface. The next article will begin to discuss how to mint your own tokens on Solana.

How do you rate this article?

31


Brochain
Brochain

Web3 Developer. The best way to learn is to share what you learnt. Medium Blog: https://medium.com/@lianxiongdi


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!

Publish0x

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.

Page not displaying correctly?