Sol Purge

How to Write and Deploy Smart Contracts on Solana

September 22, 2025 β€’ By SolPurge Team

Solana smart contracts, known as "programs," are written in Rust and deployed on-chain. If you're a developer looking to build on Solana, this guide will walk you through the essential steps to write and deploy your first program.

Understanding Solana Programs

Unlike Ethereum's smart contracts, Solana uses a different terminology and architecture:

EthereumSolana
Smart ContractProgram
Contract StateAccount
TransactionInstruction
SolidityRust

Key differences:

  • πŸ”Ή Programs are statelessβ€”data is stored in accounts
  • πŸ”Ή Multiple accounts can be passed to a single instruction
  • πŸ”Ή Programs can be upgraded (with proper authority)

Prerequisites

Before starting, you'll need:

  • πŸ”Ή Basic Rust knowledge
  • πŸ”Ή Command line familiarity
  • πŸ”Ή Node.js (for testing and front-end)
  • πŸ”Ή Some SOL for deployment

Steps to Writing and Deploying Solana Smart Contracts

Step 1: Install the Solana CLI

Download and install the Solana command-line tools:

# Install Solana CLI
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"

# Add to PATH (follow the prompts)
export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH"

# Verify installation
solana --version

Configure for devnet while learning:

solana config set --url devnet

Step 2: Install Rust and Anchor

Anchor is the most popular framework for Solana development:

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install Anchor
cargo install --git https://github.com/coral-xyz/anchor avm --locked
avm install latest
avm use latest

# Verify installation
anchor --version

Step 3: Create Your First Project

Initialize a new Anchor project:

anchor init my_first_program
cd my_first_program

This creates a project structure:

my_first_program/
β”œβ”€β”€ Anchor.toml
β”œβ”€β”€ programs/
β”‚   └── my_first_program/
β”‚       └── src/
β”‚           └── lib.rs
β”œβ”€β”€ tests/
└── app/

Step 4: Write Your First Smart Contract

Edit programs/my_first_program/src/lib.rs:

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod my_first_program {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {
        let my_account = &mut ctx.accounts.my_account;
        my_account.data = data;
        msg!("Account initialized with data: {}", data);
        Ok(())
    }

    pub fn update(ctx: Context<Update>, new_data: u64) -> Result<()> {
        let my_account = &mut ctx.accounts.my_account;
        my_account.data = new_data;
        msg!("Account updated with new data: {}", new_data);
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init, payer = user, space = 8 + 8)]
    pub my_account: Account<'info, MyAccount>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct Update<'info> {
    #[account(mut)]
    pub my_account: Account<'info, MyAccount>,
}

#[account]
pub struct MyAccount {
    pub data: u64,
}

Step 5: Build Your Program

Compile the program:

anchor build

This generates:

  • πŸ”Ή Program binary in target/deploy/
  • πŸ”Ή IDL (Interface Description Language) for client interaction
  • πŸ”Ή TypeScript types for your program

Step 6: Deploy Your Program

Get some devnet SOL first:

solana airdrop 2

Deploy to devnet:

anchor deploy

You'll see output like:

Deploying program "my_first_program"...
Program Id: <YOUR_PROGRAM_ID>

Step 7: Interact with Your Contract

Write a test in tests/my_first_program.ts:

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { MyFirstProgram } from "../target/types/my_first_program";

describe("my_first_program", () => {
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.MyFirstProgram as Program<MyFirstProgram>;

  it("Initializes the account", async () => {
    const myAccount = anchor.web3.Keypair.generate();
    
    await program.methods
      .initialize(new anchor.BN(42))
      .accounts({
        myAccount: myAccount.publicKey,
        user: provider.wallet.publicKey,
        systemProgram: anchor.web3.SystemProgram.programId,
      })
      .signers([myAccount])
      .rpc();

    const account = await program.account.myAccount.fetch(myAccount.publicKey);
    console.log("Account data:", account.data.toString());
  });
});

Run the test:

anchor test

Key Concepts to Master

Account Model

  • πŸ”Ή All state lives in accounts, not in the program
  • πŸ”Ή Programs are stateless executors
  • πŸ”Ή Accounts have owners (programs that can modify them)

PDAs (Program Derived Addresses)

  • πŸ”Ή Deterministic addresses derived from seeds
  • πŸ”Ή Allow programs to "sign" for accounts
  • πŸ”Ή Essential for complex program logic

CPIs (Cross-Program Invocations)

  • πŸ”Ή Programs can call other programs
  • πŸ”Ή Enable composability in DeFi
  • πŸ”Ή Require proper account forwarding

Resources for Learning

Mastering Solana smart contracts opens the door to building powerful decentralized applications. Start with simple programs and gradually tackle more complex logic!

Find & Claim Your Locked SOL

Unused accounts may be holding your SOL. Scan your wallet now and reclaim your funds easily.