How to Build and Deploy a Solana Smart Contract

·

Solana is rapidly emerging as one of the most powerful blockchains for high-performance decentralized applications. With support for fast, low-cost transactions and native smart contract capabilities, it offers developers a scalable platform to build next-generation Web3 solutions. This guide walks you through the complete process of building, deploying, and interacting with Solana smart contracts—also known as programs—using Rust and the Anchor framework. You’ll also learn how to integrate Chainlink Price Feeds for real-world data.

Whether you're new to Solana or expanding your blockchain development skills, this tutorial provides hands-on experience with core concepts like accounts, program logic, and on-chain interactions.

Understanding Solana’s Architecture and Smart Contract Model

Solana stands out in the blockchain space due to its unique architecture designed for speed and scalability. It achieves thousands of transactions per second (TPS) through a combination of innovative consensus mechanisms and a distinct programming model.

Proof of History: A Cryptographic Clock

At the heart of Solana’s performance is Proof of History (PoH), a cryptographic mechanism that sequences events before they’re validated by the network. Unlike traditional blockchains that rely on nodes to agree on time and order, PoH uses a verifiable delay function (VDF) to create a timestamped record of transactions.

Think of it like a digital notary: each transaction is hashed in sequence, producing an immutable timeline. This allows validators to process transactions in parallel rather than waiting for full consensus, significantly reducing latency.

👉 Discover how high-speed blockchain execution can accelerate your dApp development.

How Solana Smart Contracts Differ from EVM-Based Chains

In Ethereum-like blockchains, smart contracts bundle both code and state within a single entity. Solana takes a different approach:

This separation enables greater flexibility and efficiency. For example, multiple programs can interact with the same account, and account data persists independently of program upgrades.

Additionally, Solana supports development via CLI tools, JSON-RPC APIs, and SDKs in JavaScript/TypeScript (web3.js) and Rust, making it accessible for various developer backgrounds.

Step-by-Step: Deploying Your First Solana Program

Let’s create a simple “Hello World” program on Solana’s Devnet—a test environment ideal for learning and experimentation.

Prerequisites

Before starting, ensure you have the following installed:

You can verify installations with:

node --version
rustc --version
solana --version
git --version

Writing the HelloWorld Program in Rust

The program increments a counter each time it's called and logs the result. Here's a breakdown of key components:

use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint,
    msg,
    program_error::ProgramError,
    pubkey::Pubkey,
};

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct GreetingAccount {
    pub counter: u32,
}

entrypoint!(process_instruction);

pub fn process_instruction(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    _instruction_data: &[u8],
) -> ProgramResult {
    msg!("Hello World Rust program entrypoint");

    let account = next_account_info(&mut accounts.iter())?;

    if account.owner != program_id {
        return Err(ProgramError::IncorrectProgramId);
    }

    let mut greeting_account = GreetingAccount::try_from_slice(&account.data.borrow())?;
    greeting_account.counter += 1;
    greeting_account.serialize(&mut &mut account.data.borrow_mut()[..])?;
    msg!("Greeted {} time(s)!", greeting_account.counter);
    Ok(())
}

This code defines a struct GreetingAccount to hold state (the counter), and the process_instruction function handles incoming calls.

Deploying to Devnet

  1. Clone the example repository:

    git clone https://github.com/solana-labs/example-helloworld
    cd example-helloworld
  2. Switch to Devnet:

    solana config set --url https://api.devnet.solana.com
  3. Generate a keypair:

    solana-keygen new --force
  4. Airdrop SOL for transaction fees:

    solana airdrop 2
  5. Build and deploy:

    npm run build:program-rust
    solana program deploy dist/program/helloworld.so

After deployment, note the Program ID—you’ll use it to interact with your contract.

Interacting with the Program Using a Client

The provided TypeScript client performs these steps:

Run the client with:

npm install
npm run start

Each execution increases the greeting count stored on-chain.

Integrating Chainlink Price Feeds on Solana

Decentralized Finance (DeFi) applications require accurate, tamper-proof price data. Chainlink Price Feeds provide exactly that—secure, decentralized market data updated frequently on-chain.

On Solana Devnet, Chainlink feeds offer real-time prices for assets like SOL/USD, enabling developers to build robust DeFi protocols.

Using the Chainlink Solana Starter Kit

This demo retrieves the latest price from a Chainlink feed and stores it in a user account.

Key Components

#[program]
pub mod chainlink_solana_demo {
    use super::*;
    pub fn execute(ctx: Context) -> ProgramResult {
        let round = chainlink::latest_round_data(
            ctx.accounts.chainlink_program.to_account_info(),
            ctx.accounts.chainlink_feed.to_account_info(),
        )?;
        let decimals = chainlink::decimals(...)?;
        let decimal: &mut Account = &mut ctx.accounts.decimal;
        decimal.value = round.answer;
        decimal.decimals = u32::from(decimals);
        msg!("Price is {}", Decimal::new(round.answer, u32::from(decimals)));
        Ok(())
    }
}

The execute function fetches price data using Chainlink’s Solana library.

Deployment Steps

  1. Clone the starter kit:

    git clone https://github.com/smartcontractkit/solana-starter-kit
    cd solana-starter-kit
  2. Install dependencies:

    npm install
  3. For Apple M1 users:

    cargo install --git https://github.com/project-serum/anchor --tag v0.20.1 anchor-cli --locked
  4. Create wallet:

    solana-keygen new -o id.json
  5. Airdrop 4 SOL (two requests):

    solana airdrop 2 $(solana-keygen pubkey ./id.json) --url https://api.devnet.solana.com && solana airdrop 2 $(solana-keygen pubkey ./id.json) --url https://api.devnet.solana.com
  6. Build and deploy:

    anchor build
    # Update lib.rs and Anchor.toml with generated Program ID
    anchor build
    anchor deploy --provider.cluster devnet

👉 Learn how integrating oracle networks can enhance your smart contract reliability.

Running the Off-Chain Client

Set environment variables:

export ANCHOR_PROVIDER_URL='https://api.devnet.solana.com'
export ANCHOR_WALLET='./id.json'

Execute the client:

node client.js --program $(solana address -k ./target/deploy/chainlink_solana_demo-keypair.json) --feed HgTtcbcmp5BeThax5AU8vg4VwK79qAvAKKFMs8txMLW6

The output displays the latest SOL/USD price fetched from Chainlink.

Frequently Asked Questions (FAQ)

Q: What is the difference between a Solana program and an EVM smart contract?
A: Solana programs are stateless and contain only logic. State is stored separately in accounts. In contrast, EVM contracts combine code and state in one entity.

Q: Can I use languages other than Rust to write Solana programs?
A: Yes. While Rust is most common, Solana also supports C and C++. Higher-level frameworks like Anchor (used here) simplify development with TypeScript clients.

Q: Why do I need two airdrops when deploying with Anchor?
A: Deploying larger programs requires more lamports (fractional SOL). The Devnet faucet limits single requests to 2 SOL, so two are needed to cover costs.

Q: Are Chainlink Price Feeds available on Solana mainnet?
A: Yes. Chainlink Price Feeds are live on Solana mainnet, providing secure price data for production DeFi applications.

Q: How do I upgrade a deployed Solana program?
A: Programs can be upgraded if created with upgradeable permissions. Use solana program deploy --program-id with the same ID to update logic while preserving state.

Q: What is Anchor, and why use it?
A: Anchor is a framework that simplifies Solana development with features like automated serialization, testing utilities, and easier account management—reducing boilerplate code.

Final Thoughts

Building on Solana offers unmatched speed and cost-efficiency for decentralized applications. By combining Solana’s high-throughput architecture with reliable external data from Chainlink Price Feeds, developers can create scalable, secure DeFi platforms capable of competing with traditional financial systems.

Whether you're building a simple counter or a complex lending protocol, mastering Solana’s programming model is a valuable skill in today’s Web3 landscape.

👉 Start building performant dApps on one of the fastest blockchains today.