- 1 Presentation
- 2 Introduction
- 3 Requirements/Installation
- 4 Command Line Interface(CLI)
- 5 Deploy your first Solana Program!
- 5.1 Structure of an Anchor Project
- 5.2 Hello Solana World!
- 5.3 Basic Counter π
- 5.4 Advanced Counter
- 5.5 Extra:Lottery
In this workshop you will be hands-on building and deploying Solana Programs!
This workshop will be conducted by cleon in the ewhachain Bootcamp at the Dreamplus Gangnam building. Will be an introductory workshop to start playing with CLI commands, and also to start creating Solana Programs, understanding the logic behind them.
The requirements to get started will depend on your OS.
First, you will need to install wsl(Windows Subsystem for Linux) on your computer because Anchor is not windows friendly. When you have completed that, you will have to integrate the wsl on Visual Studio Code to be able to work with the terminal. Now you are ready to install everything :)
You will have to install Rust Programming language, Solana, Yarn and Anchor. There is a brief tutorial in: https://book.anchor-lang.com/getting_started/installation.html
The CLI is a very important tool for Blockchain Devs and will be useful to check multiple data on-chain and manage different instructions. To get started correctly, let's begin in the Visual Studio Code environment!
We will have to open an empty folder that will be used for our Solana Project. After that, we will open a new Terminal.
Nice, now let's Initialize our Anchor Environment! For our CLI commands will not be necessary at all, but will be useful in order to know where is your Solana Wallet inside your computer. And in the Anchor.toml file we will see where is our wallet located.
anchor init hello-world
Good! Now we are ready to play with CLI commands :)
In this workshop we will be working on Localhost, this means that we will be running a Solana Blockchain inside our computer. For that we will also need to create a validator. First, let's set our network to Localhost.
solana config set --url localhost
After that, let's create a Validator on our Localhost to initialize our own Solana Blockchain
solana-test-validator
It should appear something like this:
Good! The Validator will be running in that terminal, so let's open another terminal.
Now, let's try to send some SOL to our wallet address with the airdrop command.
solana airdrop 200 ~/.config/solana/id.json
solana account ~/.config/solana/id.json
And also, will be important to set our keypair to create the Tokens from next steps!
solana config set --keypair ~/.config/solana/id.json
We will be using SPL token libraries to create Fungible and Non Fungible Tokens. Let's create a token:
spl-token create-token
You should see something like:
Note that this doesn't have a supply yet You can test this with
spl-token supply <Token ID>
Now create an account to hold the token
spl-token create-account <Token ID>
Mint some tokens into that account
spl-token mint <Token ID> 21000000
You can check the token balance for an account with
spl-token balance <Token ID>
and the supply with
spl-token supply <Token ID>
If you want a summary of the tokens that you own use:
spl-token accounts
spl-token transfer <Token ID> <amount> <destination>
Note that the destination account must already be set up for that token If the account is not already setup for that token you can use:
spl-token transfer --fund-recipient <Token ID> <amount> <destination> --allow-unfunded-recipient
To create an NFT we must create a Token with 0 decimals.
spl-token create-token --decimals 0
Setup an associated account as a fungible token
spl-token create-account <Token ID>
Then, we must mint 1 Token to that account
spl-token mint <Token ID> 1 <Account>
And finally, we must disable future minting
spl-token authorize <Token ID> mint --disable
You can check the token details with
spl-token account-info <Token ID>
We already have created our Anchor environment, so the next step will be to understand the most important parts!
So, our environment look something like this:
In the picture, you can see three different colors representing the most relevant parts of the project: red-Smart Contract, blue-tests, green-project configuration.
For building and compiling our smart contract we will use
anchor build
And for deploying to the Solana Blockchain we will use
anchor deploy
(make sure you have enough SOL in the wallet, if not it will fail)
Finally, for testing we will use
anchor run test
Also need to mention that if we are working on Localhost, we must have a validator running.
Noice. Now we are ready for our First solana program :D Basically the First Program will consist of a simple message inside the Blockchain.
Now let's build and compile it
anchor build
Everything good? Now let's get some SOL Airdrop to our Project Wallet before deploying our program to the Blockchain.
solana airdrop 200 ~/.config/solana/id.json
anchor deploy
After you deploy your program, you will receive a new Program ID. This ID will be the account that is storing the program in our blockchain! So basically it will recognise our Program using this ID.
Now let's copy this Program ID and add it to our files. We will have to change the Program ID inside the lib.rs file, and the Program ID inside Anchor.toml
Important - We will have to compile and deploy again to make it work
anchor build && anchor deploy
Before testing it, let's open an additional terminal and write
solana logs
Using this command we will see all the interactions that are happening on our Localhost blockchain. So, we will see what is happening in the transactions on-chain.
Let's test our program then
anchor run test
And now, let's see what happened on Solana Logs π
Here it is! We have just log a message on Solana.
To get started for the next programs, please git clone this project.
git clone https://github.com/cleon30/Solana-Bootcamp-EWHA.git
Explanation will be given on-site. We will create a counter that every time is called, it adds 1 to the count!
anchor build
anchor deploy
anchor run counter
Explanation will be given on-site. We will create a similar version to the previous counter but this time we will allow to select the operation and number we want to apply to the counter.
anchor build
anchor deploy
anchor run calculator
This was an example made by Encode Club <3
anchor build
anchor deploy
anchor run lottery