Solana

Zero-to-Solana First Deploy

Going from Zero to Solana; running your first deploy with no pre-requisites

A. Introduction

Pre-requisites:

  • OS-specific: No. From laptop unboxing to deploy, regardless of Linux, Mac, or Windows
  • Rust knowledge: None. Will install the toolchain and get you access to the commands
  • Solana knowledge: None. Will set up the Solana keys and explain it

Goal for this is to be copy-paste in-order from a fresh system install to a deployed contract on Solana devnet, all on a single page with numbered steps (so you can get help debugging)

It includes fixes to the references (missing dependencies and out of order items) with a simpler explanations on what’s happening. See references section to dive deeper into the details.

The dependency packages will also be split up and organized by section, so you can skip ahead if you want to use your own infra to test it out.

B. Set up a VM using Multipass

The only way to get away with the OS-agnostic approach is to run everything on a a Ubuntu 20.04 and access via SSH command-line and tunneling.

We’ll use multipass, as this is cross-platform. Feel free to use VirtualBox, Parallels, or a cloud VM from AWS to load up the Ubuntu system.

On your host system open up a terminal (or Powershell):

After installation, in your terminal, run:

multipass launch -n zerotosolana -m 8G -d 100G
  • This will create an Ubuntu 20.04 VM named zerotosolana with 8GB of ram and 100GB of (virtual) disk space. Feel free to change these

Get into your shell like so:

multipass shell zerotosolana
  • Note: This uses the autogenerated SSH keys in your multipass setup. You may override these and simply use ssh with a cloud-init.yaml file, which is out of scope for this doc

C. Install npm in Ubuntu VM

  1. Update apt database
    sudo apt update
  2. Add apt source list
    curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
  3. Install nodejs
    sudo apt -y install nodejs

D. Install Anchor Pre-requisites in Ubuntu VM

Anchor is a toolchain to let you work with Solana. We will reference Anchor docs and set up the dependencies, and fix a few out of order items specific to Ubuntu 20.04

  1. Install rust, and enter 1 to proceed with the default:
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    • It will create ~/.cargo/env file
  2. Get access to rustup command:
    source $HOME/.cargo/env
  3. Confirm we have rustfmt
    rustup component add rustfmt
    • It will tell you info: component 'rustfmt' for target 'x86_64-unknown-linux-gnu' is up to date
  4. Install solana, around 240MB
    sh -c "$(curl -sSfL https://release.solana.com/v1.7.11/install)"
    • It will update the PATH in your .profile file to this:
      export PATH="/home/ubuntu/.local/share/solana/install/active_release/bin:$PATH"
  5. Get access to solana
    source ~/.profile
  6. Confirm access to solana binary:
    solana --version
    • You will see solana-cli 1.7.11 (src:bdb77b0c; feat:1140394761)
  7. Install mocha, used for unit/integration tests. We will be skipping it in this doc, but let’s get it installed anyway
    sudo npm install -g mocha
  8. install Linux requirements; Ubuntu additionally requires: libssl-dev
    sudo apt-get install -y pkg-config build-essential libudev-dev libssl-dev
  9. use cargo (rust toolchain) to install anchor (solana toolchain). This will take a while
    cargo install --git https://github.com/project-serum/anchor --tag v0.14.0 anchor-cli --locked
  10. Install npm anchor project globally
    sudo npm install -g @project-serum/anchor
  11. Add NODE_PATH and update environment:
    echo export NODE_PATH=$(npm root --quiet -g) >> ~/.profile
    source ~/.profile
  12. Confirm anchor is available globally
    anchor --version
    • You will see something like: anchor-cli 0.14.0

E. Solana Keys, Validator, and Devnet Airdrop

  1. Open another terminal and get into the VM (multipass shell zerotosolana) to generate a wallet
    solana-keygen new
    • Hit enter for empty passphrase
    • You will see something like:
    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 /home/ubuntu/.config/solana/id.json
    =============================================================================
    pubkey: xiYwvdFBbYYZBu2o9CBcHZK7G7vBqdmcWAPiLs6HtHb
    =============================================================================
    Save this seed phrase and your BIP39 passphrase to recover your new keypair:
    fake seed lightcycle research is the best fake seed fake seed fake
    =============================================================================
  2. Run the solana validator and keep it running, we’ll use it in the next section
    solana-test-validator
    • You will see something like:
    Ledger location: test-ledger
    Log: test-ledger/validator.log
    Identity: 8apBQTsqURHu2rfJryMP3qqq5NANYCwaVQjoMKxkX9BS
    Genesis Hash: BasSkGswmE8Zfuj7AFYJXcq2FBnsKpBfxbDamruw5hFP
    Version: 1.7.11
    Shred Version: 22293
    Gossip Address: 127.0.0.1:1024
    TPU Address: 127.0.0.1:1027
    JSON RPC URL: http://127.0.0.1:8899
    ⠠ 00:00:10 | Processed Slot: 22 | Confirmed Slot: 22 | Finalized Slot: 0 | Snaps

F. Deploy Program to Localnet

Ensure you have solana-test-validator running in another window

  1. Clone the anchor repository to access the basic tutorials
    git clone https://github.com/project-serum/anchor --depth 1
  2. Change directory into the example folder
    cd anchor/examples/tutorial/basic-0
  3. Build the code; this will download a 180MB SDK first:
    anchor build
  4. Run the deploy:
    anchor deploy
    • You will see something like:
    Deploying workspace: http://127.0.0.1:8899
    Upgrade authority: /home/ubuntu/.config/solana/id.json
    Deploying program "basic-0"...
    Program path: /home/ubuntu/anchor/examples/tutorial/basic-0/target/deploy/basic_0.so...
    ⠙ [664/664] Transactions confirmed. Retrying in 280 blocks
    Program Id: 4MTEyvTpoELqqZfehi1ZbQBtktnSuCUedwNhkT83XpSx
    
    Deploy success

Continue to next section to deploy to devnet

G. Deploy Program to devnet

  1. Get your pubkey address:
    solana-keygen pubkey
    • You will see a Solana address, e.g. xiYwvdFBbYYZBu2o9CBcHZK7G7vBqdmcWAPiLs6HtHb
  2. Get an Airdrop of 10 SOL on devnet.
    solana --url devnet airdrop 10
    • Alternatively, you can do this in a browser by going to https://solfaucet.com/
      1. Enter your Solana account address from previous step
      2. Enter 10 for SOL
      3. Click DEVNET
  3. Edit Anchor.toml and change localnet to devnet, so it looks like this:
    [provider]
    cluster = "devnet"
    wallet = "~/.config/solana/id.json"
    
    [scripts]
    test = "mocha -t 1000000 tests/"
  4. Run anchor build if you haven’t already from the previous section
  5. Run anchor deploy and you’ll see something like this:
    Deploying workspace: https://api.devnet.solana.com
    Upgrade authority: /home/ubuntu/.config/solana/id.json
    Deploying program "basic-0"...
    Program path: /home/ubuntu/anchor/examples/tutorial/basic-0/target/deploy/basic_0.so...
    Program Id: 9Tn8HZEpS9cUtQrBcJ5BbSVcArAWpi7P2nrKdHFcakd
    
    Deploy success

H. Expore More

Here’s a list of things you may research more or try to learn about the ecosystem:

  • Things we skipped: running unit/integration tests. You’ll want to turn off solana-test-validator for this, since the test runner will handle it for you. Check out the Anchor docs in references
  • Research further on how to interact with the contract
  • Set default environment to devnet when using solana
  • Use a config management tool to set up the environment, or docker

References