A Guide to Writing and Deploying your Smart Contract to the Solana Blockchain

A Guide to Writing and Deploying your Smart Contract to the Solana Blockchain

Solana was created in 2017 by Anatoly Yakavenko to resolve the problem of scalability and cost on other blockchain networks. The Solana blockchain can process up to 65,000 transactions per second while still providing maximum security; its enormous capabilities and many unique features make Solana one of the fastest-growing blockchain ecosystems with global adoption.

Solana programs or smart contracts in Solana are written using low-level programming language, specifically using Rust, C, or C++. It is open-source and supports smart contracts, Non-fungible tokens (NFTs), and a variety of decentralized(dApps).

In this technical article, we will be looking at some basic concepts in the Solana ecosystem, writing our first Solana Rust programs from scratch, as well as deploying our smart contract.

What is Solana Blockchain?

Solana is a crypto computing platform that aims for high transaction speeds while maintaining decentralization. It employs a new novel cryptographic function known as "Proof of History" mechanism, which is a Byzantine Fault Tolerant (BFT) consensus technique. The main aim of Solana is to ensure that the decentralized network of nodes matches the characteristics of a single node, thus increasing the synchronization speed of the blockchain.

We would also be looking at some essential terminologies in the Solana smart contract architecture.

What are Smart Contracts?

Smart contracts are defined as self-enforcing software items administered by a peer-to-peer network of devices. Smart contracts provide a framework for agreement coordination and enforcement among network players without the need for third parties or traditional legal contracts. They are frequently used to codify simple agreements between parties, produce digital assets, and assist organizational procedures. In Solana smart contracts are called 'programs'.

Solana's Network Architecture?

Solana differs from traditional EVM-based blockchains in its smart contract paradigm. Contract code/logic and state are bundled into a single contract deployed on-chain in classic EVM-based networks. However, a smart contract (or program) in Solana is read-only or stateless, containing only program logic.

This allows for the logical separation of state (accounts) and contracts logic (programs), which distinguishes typical EVM-enabled smart contracts from Solana smart contracts. Furthermore, accounts on Solana and other blockchains (like Ethereum) are very different. In contrast to Ethereum accounts, which are simply references to users' wallets, Solana accounts store data (such as wallet information).

Transaction-Ntwork-solana-part-2-copy@2x-100-1024x657.jpg

Proof of History

Proof-of-history is a technique for demonstrating that transactions are in the appropriate order and were discovered by the right person. As a result, Solana can push the boundaries of confirmation times, allowing the network to give an experience comparable to that of a centralized system while maintaining security and decentralization.

Using Proof of History, you can build a historical record that confirms an event that happened at a certain point in time. Unlike other blockchains, which require validators to communicate with one another in order to agree that time has passed, each Solana validator keeps track of time by encoding it in a simple SHA-256, sequential-hashing verifiable delay function(VDF).

Solana JSON RPCs?

Solana JSON RPCs are ways by which Solana nodes accept HTTP requests. They do so by using the JSON-RPC 2.0 specification. You can interact with a Solana node by using the Solana web3.js library in your javascript application, as this provides an interface for all RPC methods.

Screen Shot 2021-07-12 at 9.47.04 AM.png

To learn more about JSON-RPCs, check out my post. Here are also more learning resources if you want to know about how Solana works:

Project Setup and Installation

We would start by installing all the dependencies together with a framework called anchor that makes development on the Solana Rust program easier. But, first, we would need to install Rust. Rust is the programming language used for building Solana Programs, and we can install it by copying the following code to our terminal:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Then, set the path to bin directory by running:

export PATH="$HOME/.cargo/bin:$PATH"

Next, we would run the following command to check the version of the Rust compiler rustc we have installed:

rustc --version

Cargo is Rust"s package manager. It allows us to install and manage dependencies. It is similar to the npm for the Node Javascript platform.

cargo --version

Next, you can continue by installing Solana itself with this command:

sh -c "$(curl -sSfL https://release.solana.com/v1.9.8/install)"

Then to confirm it we have installed Solana successfully, we run the following command:

Solana --version

Finally, we need to install anchor. Anchor is a framework that allows us to build programs on Solana much faster and easier, providing several convenient developer tools. We can install anchor by running:

cargo install --git https://github.com/project-serum/anchor
anchor-cli --locked

We would be using Cargo which we installed earlier, to install anchor from the GitHub repo above, and then we confirm by running the following command:

anchor --version

That should be the end of the dependency installation. Let's get started by setting up the project so we can start working on the program!

Getting Started

To get started, open your terminal to the folder you want your program to be in. Then create a new program using this command.

anchor block_contract

In the project structure, you would find the following folders:

  • programs - This is a directory of Solana smart contract or program

  • test - This is where our Javascript code is stored

  • migrations - This contains the deploy script

  • app - This is where our frontend is going to be built

Now let's write some code: You would open the lib.rs file in the project folder where our program is stored. The smart contract is basically just a simple messaging contract that interacts with another user and performs certain functions.

use solana_program::{
   account_info::{next_account_info, AccountInfo},
   entrypoint::ProgramResult,
   msg,
   program::invoke,
   program_error::ProgramError,
   pubkey::Pubkey,
};


entrypoint!(process_instruction);

pub fn process_instruction(
    program_id: &Pubkey, 
    accounts: &[AccountInfo], 
    _instruction_data: &[u8], 
          ) -> ProgramResult {

 msg!("Hello John!");

    let accounts_iter = &mut accounts.iter();
    let account = next_account_info(accounts_iter)?;

    if account.owner != program_id {
        msg!("Alicia would be back on Friday!");
        return Err(ProgramError::IncorrectProgramId);
    }

    let mut messaging_account = MessagingAccount::try_from_slice(&account.data.borrow())?;
    messaging_account.counter += 1;
    messaging_account.serialize(&mut &mut account.data.borrow_mut()[..])?;

    msg!("Messaging {} time(s)", messaging_account.counter);

    Ok(())
}

We are done with this wonderful program, and then we need to test our program with anchor:

anchor test

Lastly, we would deploy the program by using the following command:

anchor deploy

Conclusion

Solana is one of the most prominent blockchain networks that is becoming one of the fastest-growing ecosystems in the crypto world, and we are only seeing the beginning.

To learn more about web3, NFTs, blockchain, and smart contracts in other languages, check out my other blog here.

I would absolutely like to connect with you on Twitter. I hope you enjoyed this. See you in the next post.