SOLANA SERIES
How to deploy Solana Smart Contract
A guide to built & deploy Solana smart contract using Rust
In this tutorial, we are attempting to create and deploy a sample Solana smart contract using the Rust programming language.
Solana Smart Contracts
Solana is a public blockchain platform that supports smart contracts. SOL is its native cryptocurrency.
A smart contract is a piece of executable code that is programmed to run automatically and is stored on a blockchain.
Smart contracts are referred to as programs in Solana and are stored as Berkeley Packet Filter(BPF) bytecode. They have the ability to read and alter data in Solana accounts.
In Solana Explorer, smart contracts are identified as Program Accounts. Each program has a unique ID that identifies it, as well as a unique owner who can change the program’s code.
Solana supports smart contracts written in Rust, C, and C++. Rust is the most preferred language.
If you are a Windows system user, I recommend installing Windows Subsystem for Linux(WSL) on your machine and proceed with the installations.
Install Rust
Run the following command in your linux terminal.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
The command prompts you to select the installation type once you run it. You can proceed with the default installation.
You will see a notice saying “Rust is Installed” following the installation.
Close your terminal and reopen it to make the default PATH changes.
You can check Rust’s version by following the command below.
rustc --version
If it fails to show you the version, set the PATH variable to $HOME/.cargo/bin.
Install Solana CLI
Run the following command in your linux terminal.
sh -c "$(curl -sSfL https://release.solana.com/v1.14.6/install)"
You can replace v1.14.6 with stable, beta or edge. For more information, refer the solana docs.
Close your terminal and reopen it to make the default PATH changes.
You can check Solana’s version by following the command below.
solana --version
Generate File System Wallet Keypair
Use solana-keygen to generate keypair files.
solana-keygen new -o $PATH/.config/solana/id.json //$PATH specifies the path to .config folder of Solana CLI tools
This will generate an unencrypted keypair along with a wallet address, pubkey.
Connect to Solana Cluster
Use the below command to target a particular cluster.
solana config set --url $cluster_name //$cluster_name specifies the target cluster
For instance: To target the Devnet cluster, run the below command.
solana config set --url https://api.devnet.solana.com
Verify your solana address and balance with the following commands.
solana addresssolana balance
You can drop devnet SOL to your wallet as follows.
solana airdrop 1
We have successfully set up solana in our machine. Now, we can start writing smart contracts.
Create a new Rust library with Cargo
Initialize a new Rust library.
cargo init $project_name --lib
cd $project_name
Add the solana-program crate to your new Rust library.
cargo add solana-program
The Rust Program structure looks like below:
Cargo.toml -> Rust manifest file that contains the project’s metadata and dependencies.
src/lib.rs -> The application code is kept here.
Add the following Rust library configuration settings to your Cargo.toml file.
[lib]
name = "$project_name"
crate-type = ["cdylib", "lib"]
Create Solana program
In src/lib.rs file, first import the solana-program crate and use the below required modules from the solana-program.
- account_info module with a type AccountInfo that defines the structure of accounts data.
- entrypoint is where the program starts.
- entrypoint has a type ProgramResult that defines the result type of the program.
- pubkey module with Pubkey struct type that defines the public key structure.
- msg macro used to log messages from blockchain.
Every Solana program must define an entrypoint that tells the Solana runtime where to start executing your on chain code. Your program's entrypoint should provide a public function with the output type ProgramResult. The function accepts 3 input attributes:
- _program_id -> Public key of the account.
- _accounts -> All accounts required to process the instruction.
- _instruction_data -> serialized instruction-specific data.
When the function completes without error, an OK() statement is returned, indicating that the transaction was successful.
Build Rust Program
The command cargo build-bpf, which is out of date, was initially being used.
Run the command listed below to build the Rust program. It will convert the Rust code to BPF bytecode. A .so file will be generated in a target folder if the build is successful.
cargo build-sbf
During cargo build, you may get the following error:
Run the steps listed below to resolve this Rust build error.
$ sudo apt update
$ sudo apt install build-essential
Deploy Solana Program
Using the Solana CLI, we can deploy the program to the currently selected cluster.
solana program deploy ./target/deploy/$project_name.so
Once the Solana program has been deployed, it will outputs the program’s public address.
The program Id can be verified via Solana Explorer.
We can now check the balance to find out how much SOL was spent deploying this program.
You have successfully deployed your first Solana program using the Rust language!
Contents distributed by Learn.Block6.tech
👉 Discord — Live Talks
👉 Twitter — Latest articles