Gravitational plate of three masses and a slashed discABC0Static engraved plate. Three-dimensional view is unavailable or reduced motion is requested.

← back to fieldarticle

articleJul 31, 2022

Solana on-chain programs

How Solana separates executable programs from account data, what native and SPL programs provide, and how Rust programs are structured and deployed.

Solana on-chain programs

sol.png
sol.png

Programs

On Solana, developers deploy programs — what other chains call smart contracts. Programs handle instructions from users and from other programs. They power DeFi, NFTs, social apps, and the rest of the on-chain stack.

Unlike Ethereum's model, Solana keeps code and data apart:

  • Programs are stateless. Data they touch lives in separate accounts passed in by each instruction.
  • The program binary itself sits in an account marked executable.
  • All programs are owned by the BPF Loader and run inside the Solana runtime.

Most people write Rust or C++, but any language that targets BPF / LLVM works. Each program exposes a single entrypoint that receives roughly:

  • program_id: pubkey
  • accounts: array
  • instruction_data: byte array

One generic program can operate on many accounts without redeploying. Native and SPL programs lean on that pattern heavily.

Native programs and SPL

Native programs cover validator essentials. The best-known is the System Program, which creates accounts and transfers SOL.

SPL (Solana Program Library) covers tokens, exchanges, lending, stake pools, name service, and related building blocks. Some programs (for example the SPL Token program) are called directly; others (like the Associated Token Account program) usually sit behind user-defined programs.

Writing programs

Rust and C++ dominate. Solidity is also an option through EVM-compat tooling that targets Solana's stack.

Typical Rust layout

FileRole
lib.rsModule registration
entrypoint.rsProgram entrypoint
instruction.rsAPI and instruction (de)serialization
state.rsAccount objects and (de)serialization
error.rsProgram-specific errors

Anchor cuts boilerplate the way Rails does for web apps — especially account deserialization and constraint checks. Develop and test on localhost / Devnet before Testnet or Mainnet.

ClusterRPC URL
Mainnet-Betahttps://api.mainnet-beta.solana.com
Testnethttps://api.testnet.solana.com
Devnethttps://api.devnet.solana.com
Localhostdefault port 8899

Deploy

solana program deploy <PROGRAM_FILEPATH>

Deploy compiles the program to an ELF shared object (BPF bytecode) and uploads it to the cluster. The executable lives in an account owned by the BPF Loader. That account's address is the program_id used in later transactions. Upgradeable BPF Loader manages the program account over time. When invoked, the runtime executes the bytecode.

Other resources

related

  1. Aug 1, 2022/articleSolana programs and Web3 API
  2. Jul 31, 2022/articleSolana Program Derived Addresses
  3. Jul 31, 2022/articleSolana accounts model

graphfeed