How to Create Smart Contract on Solana?

Solana smart contract - how to create?

Solana was first launched in 2017 with a clear idea and purpose to exclude scalability problems on blockchain platforms. Today, the Solana network has undergone a security audit and can process over 70,000 transactions through Proof of History per second.

Therefore, it has successfully become one of the fast-expanding systems with enormous capabilities and competitive features. And the proof is more than 450 projects spanning NFTs, Defi, and Web3.

Solana is growing rapidly with many various projects. That’s one of the reasons it is essential to understand the Solana system and explore how to deploy smart contracts. And it’s what we are going to do in this article. 

The Definition and Architecture of Solana 

Let’s first understand what Solana is precisely and how it works.

The Solana blockchain is a decentralized platform designed to avoid scalability and congestion issues common in existing blockchain networks. The platform’s primary focus is to improve scalabilities like fast confirmation time and higher TPS.

Solana’s architecture is entirely different from traditional EVM platforms. In the case of a traditional EVM contract, the state and code/logic are combined within one deployed program on a chain. On the contrary, Solana smart contracts remain read-only and contain only program logic. Additionally, after the smart contract is deployed, external accounts can access it and interact with the program account for storing interaction-related information and data. Therefore, the logical division of contract and state logic happens, and it’s the fundamental difference between Solana and EVM-enabled smart contracts.

Solana also offers JSON, RPC, API, CLI, and other available client SDKs to enhance dApps and Solana interaction. 

Solana Development Workflows

For any developer who first enters into the Solana Development Environment, it’s essential to know that the knowledge of Rust or object-oriented programming is not a requirement for beginning smart contract development. Moreover, you don’t even need blockchain experience at all. Instead, you can view Solana as a global computer to freely build and deploy programs.

Solana supports smart contracts known as Solana programs, and Rust C and C++ are the main languages for building smart contracts or on-chain programs. Therefore, Solana Programs are crucial to creating practical tools on-chain. However, you can also use existing programs for development. For instance, Solana developers can build dApps like how Web2, and Web3 app development is done through third-party SDK in centralized applications.

There are two types of Solana development workflows: program development and dApps development. 

A first development workflow is a program development done through Rust, C, and C++ languages. These contracts are deployed on-chain and work via the Solana Runtime. Therefore, anyone who can interact with them will be able to use these programs by submitting transactions with instructions through Solana CLI, JSON, RPC, API, or any other client SDK. They can also use Solana Explorer to search different accounts with the help of various Solana clusters.

The second development workflow is the development of dApps. This workflow includes making applications that can send transactions and interact with deployed programs.

Programs of Solana

As we have already learned about Solana Development workflow types, let’s move on and explore some of the programs created by Solana Labs. The primary software includes two program groups: Solana Program Library and Native Programs.

Solana Program Library consists of various programs like Solana’s Ethereum ERC-20 and Token program. With the help of the token program, you can easily interact with Solana blockchain tokens and NFTs. The program allows you to do different actions, including minting, querying, or transferring tokens. On the other hand, Native Programs are the foundation of Solana, and the most common among them is the System program. It is designed for creating accounts and SOL transactions. Other successful Native Programs are the Voting Program and Skating Program.

You can use them to create your own programs and build fully functional dApps.

How to Deploy a Solana Smart Contract?

Now, let’s see what steps you need to follow for deploying and building a Solana smart contract. We will learn to create a smart contract known as “hello world” written in Rust programming language. 

Solana Development Environment 

Before moving to the primary process of smart contract building, it’s necessary to set up the right development environment. For example, it can be confusing to run the source code directly from Windows. That’s why we suggest setting up a Ubuntu version of WSL, through which you can quickly write the code on Windows.

Here are the required commands for setting up the development environment.

  1. apt  upgrade
  2. apt  install npm

Afterward, it’s necessary to run Hello World to test it.

Using Rust Program for Solana Smart Contract Development

The following installation is necessary to deploy smart contracts:

  • The upgraded stable Rust build
  • NPM and NodJS v14
  • Git
  • Solana CLI v1.7.11

You can already begin setting up the HelloWorld program for the next step.

After that, the function process_instruction accepts the program_id, accountInfo, and a public key where the program assigned is deployed.

The main logic of the program is stored on the ProgramResult.

  1. ) -> ProgramResult {
  2. msg! ( ” Hello World Rust program entrypoint “)

After you complete this, the program will decide whether it has permission to modify the exact account data.

Entrypoint and Control Flow

The client’s entrypoint has five primary functions.

  1. Connection establishment to the cluster: By commanding establishConnection, the client establishes a connection with the cluster.
  2. Creating a paying account for transactions: The new anchor project needs to be connected with your Solana account to make transactions.
  3. Checking if the Hello World on-chain program has been deployed: If the program truly exists, a new account from the assigned program id will be created.
  4. Sending to the on-chain program a “Hello” transaction: The transaction includes the public key and the account of the “greeter” to which the client wishes to tell “Hello.”
  5. Solana Querying: The client queries the “greeter” account’s data, and the program increments the exact number it has been greeted with.

Here, the Javascript client has two functions:

  • sayhello

export async function sayHello():

  • and reportGreetings

export async function reportGreetings():

Deploying the Smart Contract

Cloning the repository is the first step toward deploying the smart contract.

  1. git clone https:// /solana-labs/helloworld 
  2. cd example-HelloWorld

Once finished, you can already set Devnet as your current environment.

  1. solana config set –url

Then, the program asks you to make a new key pair, which will allow the newly generated account to interconnect with the deployed smart contracts or programs on the Solana Devnet. 

The next important step is obtaining SOL tokens. For this, you can use the airdrop program id.

  1. Solana airdrop 5

Now, you can build the hello world. Write the command below to make it done:

  1. npm run build:program-rust

When the process is done, you can deploy it to Devnet.

  1. Solana program deploy dist/program/

And if the deployed program shows “correct program id,” it means you have successfully deployed your Hello World Program and can check the authenticity on Devnet.

Summing Up

Solana is a completely new blockchain platform with innovative techniques and tools. And to keep up with it, developers constantly need to stay up-to-date and follow all the changes so as not to miss out on anything important.