C 701: Making a Solana Smart Contract (15 pts.)

What You Need

Purpose

To learn how Solana works, the newer blockchain intended to compete with Ethereum by being faster and cheaper. Solana is the 9th largest cryptocurrency with a market capitalization of $13 billion as of June 2, 2022.

Rather than Proof of Work or Proof of Stake, Solana uses "Proof of History". This is intended to achieve higher performance. However, Solana experienced nine network outages in the first half of 2022, and fell from a peak of $258 to $40, so this system has not been an unqualified success so far.

Installing Node 16

First execute this command to see if you already have node installed:
node -v
If you have a version below 14 installed, remove it with this command:
sudo apt remove nodejs npm
Execute these commands to install node.js 16:
sudo apt update 
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
sudo apt install nodejs -y
node -v
You should now have node version 16.

Installing Rust

Execute these commands to install Rust:
sudo apt install rustc -y
rustc -V
If you see a version number, as shown below, Rust is ready.

Installing Git

Execute these commands to install Node.js, npm, Rust, and git:
sudo apt install git -y 

Installing Solana Tools

Execute this command to install Solana tools:
sh -c "$(curl -sSfL https://release.solana.com/v1.10.23/install)"
The output specifies an "export" command, highlighted in the image below.

Execute that command.

Execute this command to test your installation:

solana --version
You should see a version number, as shown below.

Cloning the Repository

Execute these commands:
git clone https://github.com/solana-labs/example-helloworld
cd example-helloworld
ls
You see several files and a src directory, as shown below.

Execute these commands:

git clone https://github.com/solana-labs/example-helloworld
cd example-helloworld
ls
You see several files and a src directory, as shown below.

Reading the Contract

Execute this command:
less src/program-rust/src/lib.rs
You see the start of the contract, as shown below.

Note these features:

Use the down-arrow key to scroll down and see these items, as shown below:

Use the down-arrow key to scroll down and see this item, as shown below:

Press Q to exit from "less".

Using devnet

Execute this command to set your environment to devnet, Solana's test network:
solana config set --url https://api.devnet.solana.com
The command works, as shown below

Creating a Keypair

Execute this command create a keypair:
solana-keygen new --force
It prompts you for a passphrase. Press ENTER for none (an insecure choice, but this is only a testnet).

You see a "seed phrase" consisting of several words, as shown below

Getting SOL Tokens

Execute this command to get some SOL tokens from the "airdrop program":
solana airdrop 2
You get 2 tokens, as shown below

Building the Program

Execute this command build the program:
npm run build:program-rust
The process takes several minutes. When it's done, you see a command to execute to deploy the contract, highlighted in the image below.

Deploying the Program

Execute the command output by the compiler.

You get a Program Id, as shown below.

Using the Solana Devnet Explorer

In a Web browser, go to

https://explorer.solana.com/?cluster=devnet

Search for your Program Id. You should find it on the blockchain, as shown below.

Flag C 701.1 Field Name (10 pts)

The flag is covered by a green rectangle in the image below.

Understanding the Client

To interact with the deployed contract, we need a client.

Execute this command to see the client's code:

less src/client/main.ts
As shown below, the flow is simple, with these steps:
  1. Establish a connection
  2. Establish a payer
  3. Check that the program has been deployed
  4. Say hello
  5. Get the report

Press Q to exit from "less".

Running the Client

Execute this command install the client's dependencies:
npm install
Then execute this command to run the client:
npm run start
The client succeeds, as shown below.

Flag C 701.2 Message (5 pts)

The flag is covered by a green rectangle in the image below.

Sources

How to Build and Deploy a Solana Smart Contract

Posted 5-20-22 by Sam Bowne