How to Build and Deploy Smart Contracts in Solana Blockchain

Solana is a new blockchain focusing on performance. It supports smart contracts like Ethereum which they call Programs.

Thu Mar 10 2022

How to Build and Deploy Smart Contracts in Solana Blockchain

As someone who just started learning Solidity and its ecosystem about 6 months ago, I kind of assumed it couldn't be that much harder to get up and running with. I was wrong.

Parts of the developer tooling are really nice and polished (the Solana CLI and Anchor), while the rest of the ecosystem, and even the documentation for Anchor (which to be fair, is very new), leaves a decent amount to be desire

Prerequisites

  • NodJS v14 or higher
  • The latest stable Rust build
  • Git

1. Set Up Solana Development Environment

1.1 Use Solana's Install Tool

Let us start with installing all necessary components in Mac OS, which is OS we currently have; hence it is used for this tutorial.

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

Once the installation is successful you will see logs like the following

in Code/
› sh -c "$(curl -sSfL https://release.solana.com/v1.9.12/install)"
downloading v1.9.12 installer
  ✨ 1.9.12 initialized
Adding
export PATH="/Users/xxx/.local/share/solana/install/active_release/bin:$PATH" to /Users/xxx/.profile

Close and reopen your terminal to apply the PATH changes or run the following in your existing shell:

export PATH="/Users/xxx/.local/share/solana/install/active_release/bin:$PATH"

Now, you have to run the command and add Solana to your $PATH so that system can invoke it.

export PATH="/Users/xxx/.local/share/solana/install/active_release/bin:$PATH"

1.2 Use the prebuilt binaries

Download the binaries by navigating to @solana-labs/solana/releases/latest, download solana-release-x86_64-apple-darwin.tar.bz2, then extract the archive:

tar jxf solana-release-x86_64-apple-darwin.tar.bz2
cd solana-release/
export PATH=$PWD/bin:$PATH

There is one more option which is to install the software from source; you can find more information here

If you have installed it correctly you will see the following message when solana --version is entered in the terminal

› solana --version
solana-cli 1.9.12 (src:3875bc91; feat:1070292356)

Compile and run Solana Program

For this tutorial we are going to use @solana-labs/example-helloworld; the original Hello World example from Solana team

Clone the example source code

git clone https://github.com/solana-labs/example-helloworld.git solana-hello
cd solana-hello/
yarn install
yarn build:program-rust

Set up solana network

We are connecting to Solana Devnet for this tutorial; which can be achieved by running the following command in terminal.

solana config set --url https://api.devnet.solana.com

Which will print a few things like the following

in solana-hello/ on master
› solana config set --url https://api.devnet.solana.com
Config File: /Users/xxxx/.config/solana/cli/config.yml
RPC URL: https://api.devnet.solana.com
WebSocket URL: wss://api.devnet.solana.com/ (computed)
Keypair Path: /Users/xxxx/.config/solana/id.json
Commitment: confirmed

After this, we need to create a new keypair. This keypair allows our account to interact with the deployed programs or smart contracts on the Solana Devnet. This method is insecure for storing keys and is only useful for demo purposes. Thus, you will be promoted to passphrase for security reasons.

Generate Solana Keypair

› solana-keygen new --force
Generating a new keypair

For added security, enter a BIP39 passphrase

NOTE! This passphrase improves security of the recovery seed phrase NOT the
keypair file itself, which is stored as insecure plain text

BIP39 Passphrase (empty for none):

Wrote new keypair to /Users/xxx/.config/solana/id.json
========================================================================
pubkey: 9gSSWsnXBG1r5xxxxxxxxx9Fno8XXdpuF1Rz
========================================================================
Save this seed phrase and your BIP39 passphrase to recover your new keypair:
nice xxxx chunk xxxx royal jar xxxx shove xxxx salmon xxxx decrease
========================================================================

Obtain test tokens

Now as in every Blockchain network we need tokens to deploy smart contracts. We can get our tokens from the cli itself by running solana airdrop 50

Now that we have all the necessary things we can go ahead and build. We can use the following command.

yarn build:program-rust

It will take a while, depends on your machine.

Once the build process is successful you will see something like this

To deploy this program:
  $ solana program deploy /Users/xxx/Code/yyyyy/solana-hello/dist/program/helloworld.so
The program address will default to this keypair (override with --program-id):
  /Users/xxx/Code/yyyyy/solana-hello/dist/program/helloworld-keypair.json
✨  Done in 46.00s.

Deploy the compiled contract

To deploy to testnet we just run the following command

solana program deploy /Users/xxx/Code/yyyyy/solana-hello/dist/program/helloworld.so

References

Leave a comment

To make a comment, please send an e-mail using the button below. Your e-mail address won't be shared and will be deleted from our records after the comment is published. If you don't want your real name to be credited alongside your comment, please specify the name you would like to use. If you would like your name to link to a specific URL, please share that as well. Thank you.

Comment via email
Nikhil M
Nikhil M

Entrepreneur / Privacy Freak / Humanist / Blockchain / Ethereum / Elixir / Digital Security / Online Privacy

Tags Recent Blogs