Solana DApp Development (Part-II)-Deploying Smart Contract

Kerala Blockchain Academy
5 min readApr 11, 2024

By Lekshmi PG, R&D Engineer Kerala Blockchain Academy

Prefer watching videos over reading text? Check out our YouTube video on Solana DApp Development-Deploying Smart Contract.

In the journey, we started from the Solana shore, we felt the breeze and small waves, and now we are ready for a deep dive to explore the beauty of the deep sea. So, in the previous article, we set up the development environment for developing a DApp in the Solana ecosystem. There, we installed Rust & Solana CLI. You can read the article here. So here, we will deploy a smart contract in Solana Devnet using Solana CLI & Anchor.

Using Solana CLI

We are going to deploy smart contracts using Solana CLI. First, we are going to check whether we have installed everything correctly, for that let’s check the Solana CLI version using the command

solana --version

Now, let’s check the configuration. For that, you can use the command,

solana config get

As you can see, we are connected to Devnet. Now, let’s check the balance.

Now create a project named hello_world using the command

cargo init hello_world --lib

A new project, hello_world is created, and it contains,

Now move inside this project and install solana program dependencies using the command

cargo add solana-program

Now, we need to edit the Cargo.toml file. For that, open it using the command

nano Cargo.toml

It will look like this

Now, we need to add Rust library configuration settings by adding crate-type.

Config.toml

[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
solana-program = "1.17.0"
[lib]
crate-type = ["cdylib","lib"]

Now, we are going to write the hello_world smart contract. For that, move to src folder in hello_world project. Inside, there is a file, lib.rs.

Open it in the editor. Delete the content present in the file and copy our hello-world contract, which we used at Solana Playground.

Next, we are going to build the project using the command

cargo build-bpf

Wait for it to finish

Finally, we are going to deploy a smart contract using the command.

solana program deploy ./target/deploy/hello_world.so

So we deployed the contract in Solana Devnet, and we got the Program Id. We can search this Program Id on Solscan.

Using Anchor

Anchor is a framework where we can write, compile & deploy our smart contract into the Solana ecosystem. Here, we are going to install Anchor using avm. We are installing avm using cargo.

cargo install --git https://github.com/coral-xyz/anchor avm --locked --force

Now install anchor using the command,

avm install latest

The whole procedure will take some time, so wait patiently. After completion, you can check the anchor version using the command

anchor --version

Inorder to create an anchor project, we need yarn. You can install yarn using nvm.

sudo npm i -g yarn

So, let’s create an anchor project hello_anchor.

anchor init hello_anchor

Now move to hello_anchor folder. The folder contains these files.

Here, the Anchor.toml file is the configuration file. The app folder contains the DApp files and the programs folder contains the smart contract. So move to programs folder, there is a hello_anchor folder, move inside src folder we have lib.rs file.

Now, we are removing the current content of lib.rs file with the hello_world smart contract.

Save the contract. Now, we are going to install solana program dependencies using the command,

cargo add solana-program

Now, let’s build the contract using Anchor framework.

anchor build

This process will take some time, so wait patiently. Now let’s configure Anchor.toml file, inorder to communicate with Devnet. The Anchor.toml file will be like this.

We have to make some changes.

> First remove [programs.localnet]

> Then on [provider] replace Localnet with Devnet

> Add one more field [cluster] and add Devnet url on it.

So the Anchor.toml file will be like this.

Now, we will deploy our contract to Solana Devnet using Anchor. For that, use the command,

anchor deploy

So, our smart contract was deployed successfully, and we got the Program Id. We can view this Program Id on Solscan.

Conclude

So yes, we have deployed our smart contract using Solana CLI and Devnet. For small projects, you can go with Solana CLI, and for bigger project, Anchor will be appropriate. As of now, we deployed our smart contract. Is it the end? No, we need to interact with it. So let’s do it at the next meet.

References

  1. https://www.anchor-lang.com/docs/manifest
  2. https://solanacookbook.com/#contributing
  3. https://solana.com/developers

--

--

Kerala Blockchain Academy

One-stop solution for quality blockchain education and research. Offers best in class blockchain certification programs in multiple blockchain domains.