Solana is a high-performance blockchain platform designed for decentralized applications and crypto projects. It uses a unique Proof of History consensus mechanism combined with Proof of Stake to achieve fast transaction speeds and low costs. In this guide, I’ll walk through setting up a Solana development environment and building a basic smart contract using the Anchor framework.
Step 01: Install Dependencies
We start by installing the following deps:
Install the Solana development tools. Running this command will install mucho, rust, solana, avm, anchor, solana-verify, yarn.
Instead of a quickinstall we can also do a detailed installation using the Solana docs .
npx mucho install
# On MacOS, we can use this command to install all dependencies.
$ curl --proto '=https' --tlsv1.2 -sSfL https://solana-install.solana.workers.dev | bash
# Verify that we got anchor installed
$ anchor -V # will outputs: anchor-cli 0.31.0
# To install latest and use it
# avm install latest
# avm use latest
# Initialize a Anchor project template
# You may have to install Yarn before this next step.
# I'm on a Mac, so I simply did: brew install yarn
$ anchor init solana_webpage
Optional: AI Development Tools
We can setup AI tooling for Solana development
- MCP - Solana has an MCP server that we can connect to improve Solana AI assisted development.
- LLMs.txt LLM optimized documentation that we can use to train LLMs on Solana docs.
Anyway - if all went well with our installation then we can move on to building with Solana.
Step 02: Select a blockchain network
The solana cli will be our main tool during our build process. We can use it to configure our build environment.
Configuration
- config file
- rpc url and websockets url
- keypair path (By default, this file is stored at ~/.config/solana/id.json.)
# To check our current configuration we run:
solana config get
Network
On Solana we have a few different networks to choose from. Let’s go through them:
- Mainnet Beta : main net production
- Testnet
- Devnet : if we’re building an application then this is the net we want to use.
- Local : Solan comes with a built-in validator for local development.
# Choose network
solana config set --url mainnet-beta # -um
solana config set --url testnet # -ut
solana config set --url devnet # -ud
solana config set --url localhost # -ul
Wallet
We need a wallet with SOL before we can do anything on any chain. So, let’s create a wallet and generate a keypair.
solana-keygen new
# View the wallet's public address.
solana address
# Request Devnet faucet to airdrop max 5 SOL to wallet
solana config set --url devnet
solana airdrop 5
# Check wallet balance
solana balance
We’l use the local network as our validator.
# First we need to configure the Solana CLI to use
# the local cluster
solana config set -ul
# Next, we start the local validator.
solana-test-validator
If everything went well, then Solana will respond with some inormation:
$ solana-test-validator
Notice! No wallet available. `solana airdrop` localnet SOL after creating one
Ledger location: test-ledger
Log: test-ledger/validator.log
â „ Initializing... Waiting for fees to stabilize 1...
Identity: 6Xm6q5QAN2anQUW42Gs9qsq7BYTgSeabwy91MRys6s5f
Genesis Hash: 2a3zWsgHgQB94tW4w1WgRAnnMd1tZmfJgLXUKcrw1zD3
Version: 2.1.19
Shred Version: 21999
Gossip Address: 127.0.0.1:1024
TPU Address: 127.0.0.1:1027
JSON RPC URL: http://127.0.0.1:8899
WebSocket PubSub URL: ws://127.0.0.1:8900
Anchor CLI
Anchor is a framework which makes it easier to build secure Solana smart contracts (sc), called programs in Solana.
The Anchor CLI assits us in writing, testing, deploying, and interacting with Solana smart contracts. Coming from Rust, we can think of it as cargo but for smart contracts development.
# The following command generates a directory with
# 1) a Rust program (smart contract)
# 2) a TypeScript test template
anchor init <project-name>
cd <project-name>
anchor build
# the compiled binary is in /target/deploy
# Deploy the smart contract to the network that is
# specified in Anchor.toml
anchor deploy
# Test the smart contract
# Note: if we are building on localnet then Anchor
# will start a local validator, deploy the sc, run tests
# and when finished, stop the validator.
anchor test
We can see that we don’t have a wallet installed.
Step 02: Build
We open our project (solana_webapp) in an editor. What’s called a Smart Contract in Bitcoin and Ethereum, is called a program in Solana. Anchor scaffolding left us with a base that we can buld on:
- app : this is where we will put our frontend code
- migrations
- node_modules
- programs : this is where we will put our smart contracts.
- target
- tests
- Anchor.toml
- Cargo.toml
- package.json
- tsconfig.json
- yarn.lock
Step 03: lib.rs
Let’s have a look at the code that Anchor generates for us when we call anchor init.
// The Anchor library is imported.
use anchor_lang::prelude::*;
// Our smart contract is provdided a unique id, which consists of a Base58
// encoding of the public key of the smart contract.
declare_id!("H7h2mdumYg5ssTq3e1TfU86sFwDa6UPoAWGjPF8hxuNE");
#[program]
pub mod solana_webapp {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
msg!("Greetings from: {:?}", ctx.program_id);
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize {}
#[account]
pub struct MyAccount {
pub data: u64,
}
Step 4: build early, build often
We can now build the project to make sure that everything in our toolchain and dependencies works:
$ anchor build
The build process will generate an Interface Description Language (IDL) file and compile your program to the BPF (Berkeley Packet Filter) bytecode format that Solana uses.