Select Page

How to build a dApp on Solana?

build defi app

The transaction speed of the blockchain platforms plays a major role in driving the network traffic. A blockchain platform with high transaction speed will offer better exchange facilities to confirm the transactions on-chain. Solana is one of the most preferred high-speed blockchains that possesses cryptographic time-sampling. It can approximately regulate the network traffic and process up to 2500 transactions per second. Solana is, therefore, one of the fastest networks in the world.

Because of Solana’s high transaction speed and low gas fees, many businesses and entrepreneurs prefer to develop their dApps on the Solana platform. Solana overcomes the communication cost and transaction latency with a unique PoH consensus mechanism and PoS Tower BFT. It uses the network’s Proof of History as a reminder before the PoS consensus.

Solana has a dynamic ecosystem with many tools and features like interoperability. It has devised its interoperability protocol known as the Wormhole. With the interoperability protocol and a distinctive consensus mechanism, Solana provides businesses, entrepreneurs and developers with a wider spectrum to securely build and launch their decentralized applications. It minimizes the network congestion and scalability issues that exist while processing a transaction or using a dApp with the help of a unique block propagation protocol and minimum to negligible mempool status.

This article is a brief guide on developing a decentralized application on the Solana network.

About Solana Blockchain

Founded by Anatoly Yakovenko in 2017, Solana is a public blockchain network that is open-source in nature. Its ecosystem is based on the proof-of-history and proof-of-stake consensus mechanism. Solana’s native currency is the SOL and is used to regulate peer-to-peer transactions on the network. The Solana network is ‘censorship-resistant’ and is a fourth-generation blockchain; it also supports the use of smart contracts and NFTs in its ecosystem.

Solana functions according to the time-keeping technique based on the Proof-of-History (PoH). Proof-of-History helps eliminate the problem of scalability and time required to reach the consensus regarding the transaction orders. Solana’s founder, Yakovenko, believes that this robust consensus mechanism can smoothly automate the blockchain process of transaction ordering on the Solana network.

Solana supports multiple wallets with the help of its native command-line program. It advocates using application wallets or a wallet based on the online browser. These wallets give a user-friendly experience for the users rather than utilizing the command-line tools. The wallets based on the command line are for the blockchain experts and developers, as the new attributes on the Solana network will always be supported on the command line first. The wallets based on the command-line tools will then be incorporated into the third-party solutions.

The various wallets supported by the Solana chain are as follows:

  • Mobile application wallets
  • Web wallets
  • SolFlare web wallet
  • Hardware wallets
  • Command-line wallets

Now that we are familiar with the Solana network and its types of wallets, let us understand why blockchain enthusiasts and developers highly prefer it in the next section.

Why should you build on Solana?

The best part about starting development on the Solana ecosystem is that you do not require prior smart contracts or Rust knowledge. You do not need any blockchain experience before starting a development process on the Solana ecosystem, as it is one of the most user-friendly blockchains and can be programmed easily.

On the Solana network, smart contracts are referred to as Programs. Programming languages like Rust C and C++ are used to develop programs for the Solana network.

Solana Programs are important to build unique and robust dApps on-chain. Additionally, there are plenty of ready-made tools in the Solana ecosystem for the new users who are not familiar with the dApp development process on Solana. Solana Labs has developed a convenient Solana web3.js SDk that helps users communicate with the blockchain and with Solana Programs or the Solana smart contracts efficiently.

The foundation of the dApp development on the Solana chain is the JSON RPC API which is equivalent to a layer of communication that allows the users and developers to communicate with the blockchain. There are various benefits of developing on the Solana ecosystem as given below:

Speed

Solana is one of the fastest blockchains globally, with a record of processing approximately 2500 transactions per second.

Cost per transaction

To process a transaction on a blockchain platform, the user must pay a minimum gas fee. This fee varies from blockchain to blockchain and some of them require a good amount of gas fee to process a transaction. In regards to this, Solana is one of the cheapest blockchains in terms of its gas fee, which is equivalent to $0.00025 approximately.

No mempool issue

Mempool in blockchain implicates the waiting area in which the transaction waits after getting processed for it to be accepted by the blockchain. In Solana, there is no such hassle of Mempool status on the network; it directly adds the transactions to the blockchain, therefore saving a lot of time and minimizing the network congestion.

Easier to program

Solana is a user-friendly platform with ready-made tools and has an easy smart contract deployment process. It can exponentially host multiple decentralized applications on its platform. Solana also supports cross-chain transactions, communication and dapp hosting with its bridging protocol known as the Wormhole.

Open-source

Solana is an open-source network with easy-to-use software and codes for building on its ecosystem. Solana users and developers can download Solana software code directly from Solana Lab’s Github and can also own a node up and running instantly.

dApp built on Rust

All the decentralized applications are built using Rust on the Solana ecosystem. Rust is one of the most used programming languages and in Solana, it addresses the issues of memory safety and thread concurrency.

Now that we are familiar with the advantages of building on the Solana platform, let us dive into the steps to create a decentralized application.

How to build a dApp on Solana?

Step 1

Install Rust, Yarn, Solana and Anchor

Install the programming language Rust used to write the Solana smart contracts by using the following command:

curl — proto ‘=https’ — tlsv1.2 -sSf https://sh.rustup.rs | sh

In case you want to do it on windows, then install GitBash from Git – Downloads This is a terminal that allows running unique commands on windows that are otherwise not available on windows. After you have installed the GitBash, run the Rust installation command mentioned above on the GitBash terminal.

Then use the following command to add the Rust executables:

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

Check your installation by running the following commands:

rustup --version
rustc --version
cargo --version

In the next step, you will install Solana with this command:

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

After a successful installation, run the following command:

solana --version

Step 2

Install node.js

The next step is to install the node.js along with the yarn. This is required to function with the Anchor programs. Install the LTS version from Node.js .

Confirm the installation by running the following command:

node --version
npm --version

Then install the yarn by running the following command:

npm i -g yarn

Step 3

Install Anchor

Now install the Anchor, a protocol that helps build programs on the Solana platform.

Run the following command:

cargo install --git GitHub - project-serum/anchor: ⚓ Solana Sealevel Framework  anchor-cli --locked

Then confirm the installation with:

anchor --version
cargo install --git GitHub - project-serum/anchor: ⚓ Solana Sealevel Framework  anchor-cli --locked

Then confirm the installation with:

anchor --version

Step 4

Setup Project

Solana is already configured to function on the mainnet by default. This implies that each transaction has a unique SOL coin in the form of a gas fee. You do not want to engage in the same thing while developing decentralized applications on Solana.

Now let us do the following:

  • Configure Solana CLI Utility to utilize the devnet
  • Init project with the Anchor framework

Set up Solana to work with the devnet network using the following command:

solana config set --url devnet

Then get access to your wallet, which you will need to run and deploy your programs with:

solana-keygen new --force

Check the address of your wallet using the following command:

solana address

Take some test Solana coins by inserting the airdrop command:

In case you want to your account balance, then run the following command:

solana balance

Setup the Anchor framework now by running the following command on your desktop:

anchor init solana-global-article
cd solana-global-article

 

Modular blockchain solutions for new-age enterprise software

Build a dapp on Solana with LeewayHertz.

Step 5

Code the Rust program

Open the global article project via your code editor and go to Programs > src > Lib.rs — home for Rust crates

Open that file and make a note of Lib.rs — home for Rust crates being a starter and the main file for the Solana program

Then create the data structures to store the content and add the Article struct by running the following command:

#[account]pub struct Article { pub content: String,}

Then continue to create the Initialize struct by running the following command:

#[derive(Accounts)]
pub struct Initialize<'info> {
 #[account(
 init,
 payer = person_that_pays,
 space = 8 // account discriminator
 + 32 // pubkey
 + 10000 // make the message max 10k bytes long
 )]
 pub article: Account<'info, Article>,
 #[account(mut)]
 pub person_that_pays: Signer<'info>,
 pub system_program: Program<'info, System>,
}

After this, visit the #program section and develop the main function using the following command:

#[program]
pub mod solana_global_article {
    use super::*;
    pub fn initialize(ctx: Context) -> ProgramResult {
        // Get the article
        let article_account = &mut ctx.accounts.article;
        // Initialize the variables (this is required)
        article_account.content = ("").to_string(); Ok(())
    }
}

Now our Solana programs are ready for the dApp development.

Step 6

Update Anchor on Solana

Open programs/mysolanaapp/src/lib.rs and update it using the following code:

use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
mod mysolanaapp {
    use super::*;
    pub fn create(ctx: Context) -> ProgramResult {
        let base_account = &mut ctx.accounts.base_account;
        base_account.count = 0;
        Ok(())
    }
    pub fn increment(ctx: Context) -> ProgramResult {
        let base_account = &mut ctx.accounts.base_account;
        base_account.count += 1;
        Ok(())
    }
}
// Transaction instructions
#[derive(Accounts)]
pub struct Create<'info> {
    #[account(init, payer = user, space = 16 + 16)]
    pub base_account: Account<'info, BaseAccount>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program <'info, System>,
}
// Transaction instructions
#[derive(Accounts)]
pub struct Increment<'info> {
    #[account(mut)]
    pub base_account: Account<'info, BaseAccount>,
}
// An account that goes inside a transaction instruction
#[account]
pub struct BaseAccount {
    pub count: u64,

Step 7

Build the React app

At the base of the Anchor project, build a new great application and overwrite the existing application directory by using the following command:

npx create-react-app app

Then, install the dependencies required for Anchor and Solana Web3 by running the following command:

cd app
npm install @project-serum/anchor @solana/web3.js

Step 8

Install the Wallet Adapter

Install the Solana wallet adapter to manage the Solana wallet by running the following command:

npm install @solana/wallet-adapter-react \
@solana/wallet-adapter-react-ui @solana/wallet-adapter-wallets \
@solana/wallet-adapter-base

Then create a new file idl.json. in the src directory. In this file, copy the IDL JSON created in the main project folder.

Step 9

Switch the wallet network

Before interacting with a program on the localhost network, switch over the wallet to a proper network. To change the network of your wallet, open your wallet and click on the Settings option, then directly go to the change network and select the localhost. Then airdrop some tokens to this wallet. Click on the address given at the top of the wallet interface and copy it to the clipboard.

After that, open the terminal and run the following command:

solana airdrop 10

This will airdrop the required number of tokens for running and testing the dApp.

After receiving the tokens, change to the app directory and insert the following command:

npm start

Step 10

Deploy
To deploy your dApp, copy the new IDL to the frontend by running the following command:

anchor run copy-idl

Now serve the frontend application locally with the following command:

npm run serve

After this, switch to the cluster of devnet to deploy using the following command:

solana config set --url devnet
and update the Anchor.toml file.

Now finally deploy it to the mainnet using:

anchor deploy

Let us now look at some of the main features of a Solana dApp in the next section.

What are the features of a Solana dApp?

There are some specific features of a Solana dApp as given below:

Proof-of-History

Solana dApps are based on the proof-of-history consensus mechanism to establish a secure and cryptographic source of time all through the network. This consensus mechanism eliminates the problem of trusting timestamps on messages received from the other nodes in a distributed network.

PoH is a high-frequency Verifiable Delay Function, also known as VDF, that allows the nodes to generate blocks in continuation where they do not have to align themselves with the complete network on a prior basis.

Turbine- block propagation protocol

Solana has its block propagation protocol called Turbine, which is used to transmit the blocks and is independent of the consensus mechanism. It splits the block into small packets during the transmission, thereby simplifying the data transaction between the nodes.

Gulf Stream

Solana dApps function based on the Gulf Stream mechanism where there is no delay and negligible mempool status. Solana pushes the transactions caching and forwarding to the periphery of the network. Gulf Stream helps minimize the total number of transactions in the mempool section. It permits the validators to process the transactions instantly, thereby reducing the overall time of the dApp functioning.

Pipeline

The pipeline is a transaction processing unit for validation and optimization on Solana. It is a CPU design upgrade where the validation instantly occurs between various nodes.

Sealevel

Sealevel is another distinctive technique of Solana blockchain that incorporates the simultaneous functioning of Solana Programs or the Solana smart contracts. In the case of dApps, simultaneous triggering of Solana smart contracts using the Sealevel technique increases the overall speed and validation of the dApp processes carried out by the smart contracts.

What Solana dApp development services does Leewayhertz provide

LeewayHertz specializes in developing robust Solana dApps for distinctive NFT marketplaces, payment solutions, token swaps, P2P lending, gaming, stable coins, etc; to serve the businesses and enterprises with end-to-end Solana development and consultancy solutions.

Solana blockchain consultancy

Our dApp experts provide a systematic plan and analysis to build your Solana dApps with end-to-end development facilities in a sink with the current dApp trends.

DEX development

We build highly robust and scalable Solana decentralized exchange platforms with custom DEX features like swaps, aggregators and orderbooks.

Solana smart contract development

We design, develop and implement Solana smart contracts known as Solana programs to accelerate the speed of your business audit and architecture.

dApp Maintenance

We upgrade and maintain by integrating the current micro-services to the Solana dApps for their optimal performance in your business.

dApp Portability

Our dApp experts specialize in relocating the current ongoing apps to your newly developed Solana platform using the reusable codebases.

Closing Note

Solana is an open-source platform with a high processing speed. It functions based on some of the most distinctive consensus mechanisms and techniques like PoH, Sealevel and Gulf Stream. Solana also provides interoperability with its unique bridging Protocol WormHole. It has a negligible mempool status and a low gas fee. Solana is, therefore, one of the most preferred platforms for decentralized application development.

LeewayHertz holds strong expertise in decentralized application development and has some of the most skilled Solana developers onboard. For further guidance on Solana dApp development, connect with our Solana experts.

Webinar Details

Author’s Bio

Akash Takyar
Akash Takyar
CEO LeewayHertz
Akash Takyar is the founder and CEO at LeewayHertz. The experience of building over 100+ platforms for startups and enterprises allows Akash to rapidly architect and design solutions that are scalable and beautiful.
Akash's ability to build enterprise-grade technology solutions has attracted over 30 Fortune 500 companies, including Siemens, 3M, P&G and Hershey’s. Akash is an early adopter of new technology, a passionate technology enthusiast, and an investor in AI and IoT startups.

Start a conversation by filling the form

Once you let us know your requirement, our technical expert will schedule a call and discuss your idea in detail post sign of an NDA.

All information will be kept confidential.

 

Insights