Solana is a public blockchain with high performance, scalability and low transaction cost when compared to Ethereum or Cardano.
In this tutorial, we will look at how we can create tokens from scratch!
Step 1: Local Environment Setup
--> Install Rust, Rustup and Cargo
For MacOC or Linux, copy & paste this command.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
For Windows, download the rustup-init.exe file from https://doc.rust-lang.org/cargo/getting-started/installation.html#:~:text=On%20Windows%2C%20download%20and%20run,channels%20for%20Rust%20and%20Cargo.
Now we have installed Rust language and Cargo (the Rust package manager)which is required for compiling Rust-based Solana programs.
--> Install Solana Tool Suite
Installing Solana on Windows can be tricky, we would recommend using a Linux machine.
a: Installing for MacOS & Linux(using homebrew) - RECOMMENDED
This option is easier but it requires you to have Homebrew package manager on your MacOS or Linux machine.
brew install solana
You can confirm you have the desired version of Solana installed by running:
solana --version
b: Installing for MacOS & Linux(without homebrew)
Install the Solana on your machine by running:
sh -c "$(curl -sSfL
https://release.solana.com/v1.17.1/install
)"
You can confirm you have the desired version of Solana installed by running:
solana --version
c )Installing for Windows
Open a Command Prompt (cmd.exe) as an Administrator and paste this command
cmd /c "curl
https://release.solana.com/v1.16.3/solana-install-init-x86_64-pc-windows-msvc.exe
--output C:\solana-install-tmp\solana-install-init.exe --create-dirs"
You can confirm you have the desired version of Solana installed by running:
solana --version
Step 2: Configuring Solana CLI
--> Create Wallet
Open up a terminal or command prompt and use the following command to create a new key pair:
solana-keygen new
This command will ask for a BIP39 passphrase, we can just press enter and skip that.
Once that is finished, we should see 2 outputs. First is the pubkey, which will be our account's public key. The second one will be our seed phrase which we should copy and store somewhere else.
--> Set your config file
Lastly, since we will work with devnet throughout this course, we need to set our config to connect to devnet. We can achieve this by
solana config set --url devnet
--> Get devnet tokens
Now we have our Solana CLI connected to devnet and we have an account. Yet, our account has no SOL, so we need to request an airdrop with this command:
solana airdrop 1
You can request as many airdrops as you need, and after that, you can check your balance with the command:
solana balance
Note: In case we need more SOL and our limit is reached, we can go to Sol Faucet to ask from any browser. This website will ask for our public address which can be found by executing:
solana address
Step 3: Create Fungible Token
In Solana, tokens are not just digital currencies but can be a representation of any asset with value. Solana's token standard, SPL (Solana Program Library), allows us to create, trade, and manage tokens on its high-speed blockchain.
--> Create a token using SPL
Now, execute the following command from a terminal/command prompt to create a new token
spl-token create-token
The response from the create-token command will include an address, which is the TOKEN_ADDRESS. This address is unique to the token you just created. By default, it will have 9 decimals.
--> Create an Associated Token Account
In Solana, each token we want to hold needs to have a unique ATA (). For example, if I want to hold Token A and Token B, they will have two separate ATAs, one for each token. Therefore, let's create an account that is eligible to hold the tokens you just created:
spl-token create-account <TOKEN_ADDRESS>
--> Mint
Now we are ready to mint new tokens into this account using:
spl-token mint <TOKEN_ADDRESS> 1
Now we have a basic understanding of how tokens in Solana work and successfully created our own fully functioning token which has a supply of 5.
You can verify the balance of the account before and after the mint operation from the explorer by searching you public address or editing this URL: https://explorer.solana.com/address/<YOUR_PUBLIC_ADDRESS>/tokens?cluster=devnet
Step 4: Create Non-Fungible Token
NFTs, or Non-Fungible Tokens, can be thought of as standard tokens with uniqueness. It is achieved by disabling minting capability after their initial creation, ensuring they are one of a kind and distinguishable.
Additionally, they come equipped with metadata, a feature that allows them to store additional information. With this metadata, NFTs can represent anything from a physical item, like a piece of art, to virtual services as we will dive deeper in the next section.
--> Create Token
To create non-fungible tokens (NFTs), we need to specify 0 decimals when creating the token:
spl-token create-token --decimals 0
The decimals parameter determines a token's divisibility. Fungible tokens often have decimals for fractional values, like a token with 2 decimals being split into 0.01 units. Using --decimals 0 with the spl-token command means the token can't be divided. This makes it unique and indivisible. Considering the actual aim of NFT is to represent a one-of-a-kind asset, it should not be divisible.
--> Create ATA for our NFT
Just like in the token section, we can create an Associated Token Account, which will be eligible to hold our NFT, using this command:
spl-token create-account <NFT_TOKEN_ADDRESS>
--> Mint NFT
Mint a new token into this account:
spl-token mint <TOKEN_ADDRESS> 1
--> Disable Further Minting
Finally, to ensure that no one else can mint more of your NFT, disable the mint authority:
spl-token authorize <NFT_TOKEN_ADDRESS> mint --disable
And there you have it! You've just minted your own NFT on Solana using the CLI. The current NFT doesn't have metadata but in the next section, we will implement our own NFT with metadata.