Getting Started with Solana Smart Contracts from Scratch

·

Building decentralized applications on Solana begins with understanding how to write, build, and deploy smart contracts—known as programs in the Solana ecosystem. This guide walks you through setting up your development environment, creating a simple "Hello World" program, deploying it locally, and interacting with it using the Solana CLI and Rust. Whether you're new to blockchain development or expanding into Solana, this step-by-step tutorial equips you with foundational skills for on-chain programming.

Understanding the Solana CLI

The Solana Command Line Interface (CLI) is a powerful tool that enables developers to interact directly with Solana clusters—whether local, devnet, or mainnet. It supports essential operations like managing accounts, checking balances, deploying programs, and viewing logs.

To explore available commands at any time, run:

solana --help

This returns a full list of CLI capabilities, from configuration to transaction submission.

Configuring the Solana CLI

The CLI uses a configuration file to store default settings. View your current setup with:

solana config get

This displays key details:

You can update these settings using solana config set. The most common change is switching clusters:

solana config set --url localhost
solana config set --url devnet
solana config set --url mainnet-beta

👉 Learn how to securely manage your blockchain keys and interact with Solana programs.

To change the default keypair:

solana config set --keypair ~/path/to/keypair.json

Setting Up a Local Test Validator

For efficient testing and debugging, use the built-in test validator instead of deploying to Devnet repeatedly.

Run this command in a dedicated terminal window:

solana-test-validator

This starts a local Solana cluster on localhost, preloaded with test SOL and ready for immediate deployment. It mimics mainnet behavior without cost or network latency.

Streaming Program Logs

While developing, monitoring real-time logs helps debug program execution. Open another terminal and run:

solana logs

This streams all program-related activity from the cluster specified in your CLI config. When targeting localhost, it automatically connects to your running test validator.

On public clusters like Devnet, include a program ID to filter output:

solana logs <PROGRAM_ID>

Alternatively, use grep to isolate specific program invocations:

solana logs | grep "<PROGRAM_ID> invoke" -A 5

This shows the next five lines after each invocation, making it easier to trace execution flow.

Managing Keys and Balances

Secure identity management is crucial in blockchain development.

Generate a new keypair:

solana-keygen new --outfile ~/my-keypair.json

Check your current configured public address:

solana address

View your account balance:

solana balance

Request test SOL on Devnet or localhost:

solana airdrop 5
Note: Airdrops on Devnet are limited to 5 SOL per request. Use them wisely during testing.

Common issues during development include insufficient funds, incorrect keypairs, or misconfigured clusters. Properly setting up your environment avoids these pitfalls early.

👉 Discover secure ways to handle cryptographic keys and deploy blockchain programs.

Creating Your First Solana Program

Solana programs are written in Rust and compiled to Berkeley Packet Filter (BPF) bytecode for execution on the blockchain.

Start by creating a new Rust library project:

cargo new --lib solana-hello-world-local

Navigate into the project directory and update Cargo.toml:

[package]
name = "solana-hello-world-local"
version = "0.1.0"
edition = "2021"

[dependencies]
solana-program = "~1.8.14"

[lib]
crate-type = ["cdylib", "lib"]

The solana-program crate provides core types and macros needed for on-chain logic. The crate-type ensures compatibility with BPF targets.

Writing the "Hello World" Program

Edit src/lib.rs with the following code:

use solana_program::{
    account_info::AccountInfo,
    entrypoint,
    entrypoint::ProgramResult,
    pubkey::Pubkey,
    msg,
};

entrypoint!(process_instruction);

pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
) -> ProgramResult {
    msg!("Hello, world!");
    Ok(())
}

This minimal program defines an entrypoint that prints a message to the program log whenever invoked.

Building and Deploying the Program

Compile the program for BPF architecture:

cargo build-bpf

Upon success, the CLI outputs a deployment command similar to:

solana program deploy /path/to/program.so

Before deploying, ensure your CLI targets the correct cluster:

solana config set --url localhost

Then deploy:

solana program deploy target/deploy/solana_hello_world_local.so

The response includes your Program ID, a unique public key identifying your deployed contract.

Verifying Deployment

After deployment, verify your program on Solana Explorer by selecting “Custom” as the cluster and entering http://localhost:8899 as the RPC URL. Paste your Program ID to view its details.

Testing Program Execution

To see your program in action:

  1. Start streaming logs in a separate terminal:

    solana logs
  2. Use a client script (e.g., Node.js with @solana/web3.js) to send an instruction to your program.

Example client connection setup:

let connection = new web3.Connection("http://127.0.0.1:8899");

After sending a transaction, check both the terminal running solana logs and the Solana Explorer URL for confirmation that "Hello, world!" appears in the logs.

Congratulations! You've successfully built, deployed, and tested your first Solana smart contract.

Challenge: Deploy to Devnet

Now try deploying your custom message program to Devnet:

  1. Update your CLI configuration:

    solana config set --url devnet
  2. Airdrop test SOL:

    solana airdrop 5
  3. Rebuild and deploy:

    cargo build-bpf
    solana program deploy target/deploy/your_program.so
  4. Update your client script to connect to Devnet:

    let connection = new web3.Connection(web3.clusterApiUrl("devnet"));
  5. Monitor logs selectively:

    solana logs <YOUR_PROGRAM_ID> | grep "invoke" -A 5

This practice strengthens your understanding of cross-environment deployment workflows.

👉 Explore advanced tools for analyzing and optimizing Solana smart contracts.


Frequently Asked Questions

Q: What is a Solana program?
A: A Solana program is a smart contract deployed on the blockchain, written in Rust and compiled to BPF bytecode. It defines how data is processed and stored across accounts.

Q: Can I redeploy a program after updates?
Yes, but only if you reuse the same Program ID. Use --program-id during deployment to specify a custom keypair as the program address.

Q: Why do I need BPF compilation?
Solana uses BPF for efficient execution of on-chain code. It allows high-performance processing while maintaining security and determinism.

Q: How do I fix “Insufficient funds” errors?
Ensure your wallet has enough SOL for transaction fees. On Devnet or localhost, use solana airdrop to receive test tokens.

Q: Is it safe to use solana-test-validator for security testing?
While excellent for functional testing, always audit code separately for vulnerabilities. The test validator does not simulate all network-level threats.

Q: What’s the difference between Devnet and Mainnet Beta?
Devnet is a public test network where anyone can get free SOL; Mainnet Beta is the live network with real economic value.


Core Keywords: Solana smart contract, Rust blockchain development, Solana CLI, BPF program, deploy Solana program, test validator, program logs, Solana development