บล็อก

ทุกอย่างที่คุณต้องรู้เพื่อเขียนโค้ดบน Solana

ทุกอย่างที่คุณต้องรู้เพื่อเขียนโค้ดบน Solana

ขณะนี้บทความนี้มีเฉพาะภาษาอังกฤษ

Solana is a public blockchain where anyone can deploy special-purpose programs (smart contracts) that hold and move money, and "writing code on Solana" means building those programs. To do it you need Rust, the Solana CLI (Agave), the Anchor framework, a fast local test harness, and a security toolkit built for Solana, not Ethereum. This post explains what Solana is, then walks through each tool.

What is Solana?

Solana is a blockchain: a shared database run by thousands of independent computers (validators) around the world, with no company in charge. Nobody can quietly edit its records, and anyone can read them or add to them by paying a small fee.

It launched in 2020, founded by Anatoly Yakovenko, and its native currency is SOL. Fees are paid in SOL, and validators stake SOL to earn the right to process transactions.

What sets Solana apart is speed and cost. Ethereum, the best-known programmable blockchain, processes transactions largely one after another; Solana is designed to process many in parallel. Transactions confirm in well under a second and usually cost a fraction of a cent. That makes Solana popular for trading, payments, and apps where users make many small transactions.

What "writing code on Solana" means

On a regular website, the code runs on a server the owner controls. On Solana, the code runs on the blockchain itself, and nobody, not even its author, can secretly change how it behaves.

That code is called a program (Ethereum calls the same idea a "smart contract"). A program is a set of rules that every validator runs identically. Examples:

  • A token: "this account holds 100 coins; only its owner can send them."
  • An exchange: "swap token A for token B at the current pool price."
  • An escrow: "release the payment only when both sides have signed."

These are not ordinary apps. A Solana program can't browse the web, read files, show a screen or run on a timer. It runs only when a transaction calls it, gets a strict compute budget per call, and must give the same result on every validator. In practice it does one job: read and update account data according to its rules.

Writing code on Solana usually means two things:

  1. The on-chain program, written in Rust. It is compiled and uploaded to the blockchain, where it lives at a fixed address. It handles the money and enforces the rules.
  2. The client, usually a website or app written in TypeScript. It shows the user buttons and balances, then asks their wallet to sign transactions that call the program.

The stakes are what make it different from normal software. A program often holds real money, its code is public, and attackers can call it directly. A bug is not a crash; it is a theft. That is why testing and security tools take up so much of this guide.

Why Solana feels different

If you come from Ethereum, three things change at once:

  • Programs, not contracts. On Solana, on-chain code is called a program, and it is stateless. All data lives in separate accounts that are passed into each instruction.
  • Rust, not Solidity. Programs compile from Rust to sBPF bytecode and run in the Solana Virtual Machine (SVM).
  • Explicit accounts. Every transaction lists up front which accounts it reads and writes. That is what lets Solana run transactions in parallel, and it is also where most Solana bugs hide: missing signer checks, unchecked account owners, and spoofed Program Derived Addresses (PDAs).

That last point matters for tooling. Ethereum security tools assume Solidity and the EVM, so most of them simply do not apply to Solana. More on that below.

Languages and frameworks

Most new programs are written in Rust with Anchor; Pinocchio is the choice when every compute unit counts.

LayerToolWhat it does
Program frameworkAnchorThe standard framework. Macros handle account validation, serialization and IDL generation. Version 1.0 shipped in April 2026; the Rust crate is still anchor-lang.
Program frameworkPinocchioZero-dependency library for native programs. Lower compute cost and full control, but no built-in IDL.
IDL and client codegenCodama (with Shank for Pinocchio)Generates typed TypeScript and Rust clients from a program's IDL.
TypeScript client@solana/kitThe modern JavaScript SDK for building transactions and calling programs. Replaces the older @solana/web3.js 1.x for new work.
Python clientsolana-py / soldersPython SDK; solders provides the core types.

If you are new, start with Anchor. It removes a whole class of account-validation bugs and gives you an IDL for free.

Setting up your machine

You need four things installed: Rust, the Solana CLI, Anchor, and a local validator. The Solana Foundation publishes a one-line installer that sets up all of them; the steps below are the manual route. Windows users should work inside WSL.

  1. Rust, using rustup. rustup is Rust's official installer. Run curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh (or the Windows installer from rust-lang.org) and you get two things: rustc, the Rust compiler, and Cargo, Rust's build tool and package manager. You need both: Anchor installs through cargo install, and Solana programs are compiled with cargo build-sbf, a Cargo command the Solana CLI adds in the next step. rustup also keeps Rust updated (rustup update). Check with cargo --version.
  2. Solana CLI (Agave). Agave is Anza's validator client, formerly the Solana Labs client, but the command is still solana. Check with solana --version and upgrade with agave-install update.
  3. Anchor through the Anchor Version Manager:
    • cargo install --git https://github.com/solana-foundation/anchor avm --force
    • avm install latest, then anchor --version
  4. Surfpool, the newer replacement for solana-test-validator. Anchor 1.0 uses it by default for anchor test and anchor localnet.
  5. A wallet keypair: solana-keygen new, then solana config set --url devnet and solana airdrop 2 to get free devnet SOL.

From there, anchor init my_program gives you a working project with a program, tests and a TypeScript client.

Testing

Solana testing is a three-layer stack: Mollusk for single instructions, LiteSVM for full program flows, and Surfpool for integration tests against realistic network state.

LayerToolUse it for
Single instructionMollusk (Anza)Pure instruction logic, compute-unit benchmarking, fixture-based regression tests.
Multi-instructionLiteSVMRuns a Solana VM inside your test process. Fast, with full bank state, sysvars and cross-program calls. Works from Rust, TypeScript and Python.
IntegrationSurfpoolA local network that can mirror mainnet accounts, for end-to-end and client tests.

Two older tools you will see in tutorials: solana-program-test still works but LiteSVM is its successor for new projects, and solana-bankrun has been deprecated by its own author in favor of LiteSVM (source).

Security tools: what you can and can't borrow from Ethereum

Slither, Echidna, Halmos and Foundry are excellent, but they are Ethereum (EVM) tools and do not run on Solana programs. They read Solidity or EVM bytecode; Solana programs are Rust compiled to sBPF.

What each one is, and its closest Solana counterpart:

EVM toolWhat it isSolana counterpart
SlitherTrail of Bits' static analyzer for Solidity and Vyper, written in Python, with 90+ built-in vulnerability detectors. Usually the first tool wired into CI.cargo clippy plus Anchor's account constraints; commercial Solana scanners and audit firms fill the gap.
EchidnaTrail of Bits' property-based fuzzer. You write invariants ("total supply never exceeds X") and it hunts for inputs that break them.Trident by Ackee Blockchain: stateful, guided fuzzing for Solana programs, backed by the Solana Foundation.
Halmosa16z's symbolic testing tool. It reuses Foundry-style tests but checks them over all possible inputs, a step toward formal verification.No mainstream equivalent yet; property tests in Trident and careful audits cover most of this ground.
FoundryThe leading Solidity dev framework: forge (build and test, with built-in fuzzing), cast (chain CLI), anvil (local node) and chisel (REPL).Anchor + Solana CLI + LiteSVM/Surfpool together play the same role.

On Solana, the bugs that matter most are account bugs: a missing signer check, a missing owner check, an unvalidated PDA, or an unchecked arithmetic overflow. Anchor's constraint macros prevent many of these by default, which is a strong reason to start with it. Fuzz anything that holds funds with Trident, and get an independent audit before mainnet.

Deploying and verifying

Deploy to devnet first, then publish a verified build so anyone can confirm your on-chain program matches your public source.

  1. Deploy: anchor deploy or solana program deploy target/deploy/<name>.so.
  2. Verified build: install the Solana Verify CLI (cargo install solana-verify --locked), build inside Docker with solana-verify build, deploy that exact binary, then run solana-verify verify-from-repo against your GitHub repository. Don't rebuild with anchor build in between, or the hashes won't match.
  3. Upgrade authority: decide who can upgrade the program. A multisig or governance program is safer than a single hot key.

A verified build proves the source matches the deployed code. It does not prove the code is safe.

Your starter checklist

  • Install Rust, Solana CLI (Agave), Anchor via AVM, and Surfpool
  • Create a keypair and get devnet SOL
  • anchor init a project and read the generated program
  • Learn the account model: accounts, PDAs, cross-program invocations, rent
  • Write unit tests in LiteSVM or Mollusk
  • Fuzz anything that holds value with Trident
  • Deploy to devnet, then publish a verified build
  • Get an audit before mainnet

Best places to keep learning: the official Solana docs, the Anchor book, and the Solana Foundation's program templates.

Sources

← บทความทั้งหมด