Sandbox is not supported on mobile, try on a computer
Solana is a high-performance blockchain platform that is renowned for its incredibly quick transaction times and inexpensive transaction costs, making it a great option for many different decentralized apps and projects. Rust is a good choice for smart contract development on Solana. It is a modern, high-performance language with a rich ecosystem of tools and libraries. It is also a popular language among developers, which means that there is a large community of people who can help and support you if you need it.
In this exercise, we will start from basics and follow step-by-step explanations/instructions to do the following activities:
1. Write a Hello World smart-contract using Rust.
2. Compile and deploy the smart-contract in solana test network.
Solana CLI: Solana Command Line Interface utility allows users to interact with Solana networks. It can be used to perform various options like creating/managing keypairs, checking balance, transferring SOL, deploying smart contracts, and much more.
Solana Test Validator Docker Image: Docker image that runs a local Solana network on your local machine or in our sandbox . The validator node validates and processes transactions, maintains consensus, and secures the network. A public network is required to have thousands of validators. However, in a private network few nodes are sufficient. And, in a test network like ours, even one is enough.
Keypair: A keypair consists of a public key and a private key. The public key (or the account address) is used to receive funds, while the private key is used to sign transactions. Hence, while the public key is not confidential information (ignoring the privacy aspect), the private key needs to be protected. We will generate two key pairs in this exercise.
Features of Rust
Speed and Efficiency
Rust is celebrated for its emphasis on speed and memory safety. These characteristics align perfectly with Solana's mission to provide a high-performance blockchain. Rust's memory safety guarantees help prevent common programming errors that can lead to vulnerabilities and crashes, ensuring a stable and secure execution environment for smart contracts.
Ecosystem and Tooling
Rust boasts a vibrant and growing ecosystem, including libraries and frameworks that facilitate smart contract development. Developers benefit from a wealth of resources, enabling them to build, test, and deploy Solana smart contracts efficiently.
Concurrency and Parallelism
Solana's architecture is designed for concurrency and parallelism, making it well-suited for high-throughput applications. Rust's ownership system and support for concurrency align with Solana's capabilities, allowing developers to create smart contracts that can efficiently utilize the platform's performance advantages.
Developer-Friendly Features
Rust offers modern programming features that enhance developer productivity. Pattern matching, strong type inference, and a robust package manager (Cargo) simplify the development process. These features make it easier to write, test, and maintain Solana smart contracts.
Security and Reliability
Rust's strict compiler checks and memory safety features contribute to the security and reliability of Solana smart contracts. Developers can catch potential issues at compile-time, reducing the risk of vulnerabilities and unexpected behavior on the blockchain.
Relevant Resources:
Here are some more relevant resources for your reference:
Step 1: In this lab, we will write a hello world smart-contract for Solana Blockchain using Rust langugage. We will also compile and deploy our program on the local Solana Blockchain.
Step 2: Open 'VS Code' first and then open the terminal in 'VS Code'.
Step 3: The first step is to install the Solana-CLI and setup the local environment, but for your convinience we have already installed and setup the Solana CLI and required tools. If you are interested in learning how to setup test Solana blockchain using Docker, check the link provided.
Step 4: You can confirm your installation by checking the version of the Solana CLI installed on your system, by running the given command.
Step 5: Next, we need Rust and Cargo. Cargo downloads your Rust project's dependencies and compiles the project. Rust and Cargo are already installed.
Step 6: You can confirm your installation by checking the version of the cargo installed on your system, by running the given command.
Step 7: Now we will create a new keypair for our local Solana blockchain. Run the given command to create a new keypair for our local Solana blockchain. Click enter without typing anything when you get this message 'BIP39 Passphrase (empty for none):' This will create a new keypair and save it in the default location. The default location is '~/.config/solana/id.json'.
Step 8: Now we will start the local Solana blockchain. In another terminal tab run the 'solana-test-validator' command. This will start the local Solana blockchain on your system.
Step 9: Switch to the previous tab. Now we will set the Solana CLI to use the local Solana blockchain. Run the given command to set the Solana CLI to use the local Solana blockchain.
Step 10: Now we will airdrop some SOL tokens to our newly created keypair. Run the given command to airdrop 2 SOL tokens to our newly created keypair. This will airdrop 2 SOL tokens to our newly created keypair.
Step 11: Solana programs written in Rust are libraries which are compiled to BPF bytecode and saved in the .so format. So mow we will create a new rust library. Run the given command to create a new rust project named 'hello_world'. The '--lib' parameter is important to make it a library.
Step 12: Change the directory to the newly created project directory.
Step 13: Add the solana-program library to our project. Run the given command to add the solana-program library to our project.
Step 14: Switch to the file explorer in Vs Code. Inside the root directory of our project (hello_world) we have a file named 'Cargo.toml' , this file carries the dependencies and version info, just like 'package.json' and 'package-lock.json' files do in a Node Js project. Inside the 'src' folder we have a 'lib.rs' file this is the main file for our smart-contract.
Step 15: Open the 'Cargo.toml' and add this code. This code adds the required Rust library configuration settings.
Step 16: Now we will start with coding our 'Hello World' smart-contract, open the 'lib.rs' file located inside the 'src' directory (This is the file where we write code for our smart contract) and replace the preloaded content in that file with those given in steps: 16, 17 and 18. The given code will import the solana-program crate and bring our needed items into the local namespace.
Step 17: This part of the program defines a Solana smart contract's entry point. It exports the hello_world function, which takes program information, accounts, and instruction data to execute program-specific logic on the Solana blockchain.
Step 18: This code, logs the message 'Hello world!' to the blockchain as part of the program's execution and then gracefully exits the program by returning a success result.
Step 19: Compile our program to BPF bytecode. Make sure you run this code from the root directory of the project. This step will take 5-10 minutes as there are several dependencies which are to be downloaded and installed.
Step 20: Once the contract is compiled it creates a `target` directory with compiled code and an ABI (Application Binary Interface) definition. The ABI and code are essential for deploying and interacting with the contract on the Solana blockchain. Inside 'target' directory you will have a 'deploy' directory which will have the 'hello_world.so' and 'hello_world.json' file which are the two important files to create a interface for Dapps to interact with our contract.
Step 21: Deploy our program to the local Solana blockchain. Run the given command to deploy our program to the local Solana blockchain.
Step 22: Once the program is deployed you will get the program id, which will look something like Program Id: <program-id>. The ID value will be different for every deployment and is unique identifier for our program on the Solana blockchain.
Step 23: Now we will check the details of our program on the local Solana blockchain. Run the 'solana program show [CONTRACT ADDRESS]' command to check the details of our program on the local Solana blockchain. Remember to change the contract address with the one you got in previous step.
Step 24: We have successfully coded, compiled and deployed our rust smart-contract on Solana Blockchain.