Book a free call

How to Build and Deploy a Solana Smart Contract

Solana programs are written in Rust with Anchor and deployed with the Solana CLI. This guide covers the path from first program to live mainnet deployment.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How to Build and Deploy a Solana Smart Contract
On this page

Short answer

To build and deploy a Solana smart contract, write the program in Rust using Anchor, test it on a local validator, then deploy it to devnet and mainnet using the Solana CLI. A Solana smart contract is a program: it holds the logic, while accounts hold the data. Keep it upgradeable at first, verify it on an explorer, and move the upgrade authority to a multisig before real funds arrive.

What is a Solana smart contract?

A Solana smart contract is called a program. It is code that runs on the Solana network and holds the logic of an application, while accounts hold the data the program reads and writes.

That split is the first thing to learn. On some chains, a contract holds both its code and its state. On Solana, the program is stateless: the same program can serve many users because each user's data lives in a separate account that the program is given when it runs.

Programs are usually written in Rust, most often with the Anchor framework, which handles the account checks and the interface for you. The program is compiled, deployed to the network, and given a program ID that other programs and wallets use to call it. For the testing side, testing and debugging Solana smart contracts covers what happens before deployment.

What tools do you need to build on Solana?

You need five tools to build on Solana: a Rust toolchain, the Anchor framework, the Solana CLI, a local validator for testing, and a wallet for signing. The table shows what each one does.

ToolWhat it doesWhen you use it
RustLanguage the program is written inWriting and compiling
AnchorFramework for accounts and checksWriting and testing
Solana CLIDeploys programs and manages keysDeploying and checking
Local validatorRuns a test network on your machineTesting before devnet
WalletSigns deployments and transactionsDeploying and calling
ExplorerShows the program and its activityVerifying after deploy

Install the tools in that order. A working local validator before you write any code saves more time than any later debugging session.

How do you write a Solana program with Anchor?

You write a Solana program with Anchor by defining instructions, the accounts each instruction needs, and the checks that run before any state changes. Anchor generates the interface from those definitions, so a wallet or a front end can call the program without hand-written plumbing.

Each instruction names the accounts it touches and states what it will do with them. Anchor checks that the right accounts were passed, that the signer is who the instruction expects, and that the account data has the shape the program expects. Those checks run before your logic, which is what stops many common errors.

Keep the program small. One program that does one job well is easier to test, audit and upgrade than a program that tries to do everything. Split a big application into several programs that call each other, and keep shared data in accounts both can read.

How do you test a program before deploying it?

You test a program before deploying it by running it on a local validator, then on devnet. The local validator is a Solana network on your own machine, so tests run fast and cost nothing in real funds.

Anchor's test command runs your tests against the local validator. Write tests for the happy path first, then for the failures: a missing account, a wrong signer, an amount that is too large. A program that only works when everything is correct is not ready.

Devnet is the shared public test network. Deploy there to check that the program works with real network timing, real wallets and real explorers. Devnet SOL has no value, so testing there costs nothing. Only move to mainnet when the devnet version has passed every test you wrote.

How do you deploy a program to devnet?

You deploy a program to devnet by building it with Anchor, pointing the Solana CLI at devnet, and running the deploy command. The steps are short, but each one has a check worth doing.

  1. Build the program. Run the Anchor build command and confirm it compiles without errors or warnings you have not read.
  2. Point the CLI at devnet. Set the Solana CLI to the devnet network and check the setting before you deploy.
  3. Fund the deployer wallet. Request devnet SOL from a faucet, which is free test currency with no value.
  4. Deploy. Run the deploy command and note the program ID it returns.
  5. Check the explorer. Look up the program ID on a Solana explorer and confirm the program is there with the right size.
  6. Call it once. Run one real transaction against the deployed program and confirm it behaves as your tests predicted.

Deploying to another chain follows the same shape with different tools. For comparison, deploying smart contracts on Ethereum shows the equivalent steps on that chain.

The same shape applies on newer chains too. Deploying a smart contract on Base shows the equivalent steps there, with different tools and a different network.

How do you move a program to mainnet safely?

You move a program to mainnet safely by treating the deployment as a release, not a single command. Mainnet is where real funds live, so every step needs a check and a way back.

First, audit the program. A review by someone who did not write the code will find issues the author missed, and the audit should cover the account checks, the arithmetic and the upgrade path. Fix what the audit finds and re-test on devnet.

Second, decide who can upgrade the program. A new program is usually upgradeable, which means the holder of the upgrade authority can replace it. Before real funds arrive, move that authority to a multisig wallet so no single key can change the program. Record the program ID, the upgrade authority and the audit report in one place your team can find.

Example (hypothetical): a team ships a ticket program in 6 weeks

Example (hypothetical): a team of 3 developers builds a ticket program on Solana in 6 weeks. The program sells tickets for events, checks that each ticket is used once, and sends the payment to the organiser.

In weeks 1 and 2 they write the program with Anchor and test it on a local validator. In weeks 3 and 4 they deploy to devnet 3 times, fixing a signer check and an account size error they found there. In week 5 an outside reviewer audits the program and finds 2 issues, which the team fixes and re-tests. In week 6 they deploy to mainnet and sell the first 100 tickets.

The arithmetic: 6 weeks, 3 devnet deployments, 2 audit fixes and 100 tickets sold in the first week. The team spent 2 weeks writing, 2 weeks testing on devnet, 1 week on audit fixes and 1 week on the mainnet launch. The devnet deployments caught the errors that would have been expensive on mainnet.

What are the common mistakes?

The most common Solana deployment mistakes are skipping the account checks, deploying to the wrong network, and leaving the upgrade authority on one key.

  • Skipping account checks. A program that trusts any account it is given will be exploited. Anchor's checks exist for a reason; do not bypass them.
  • Deploying to the wrong network. A deploy aimed at mainnet when you meant devnet is hard to undo. Check the CLI network setting before every deploy.
  • One key holds the upgrade authority. A single lost or stolen key can replace the program. Move the authority to a multisig before real funds arrive.
  • No audit before mainnet. A program that was never reviewed by another person will carry bugs the author cannot see. Audit first, then deploy.
  • Testing only the happy path. A program that works when everything is correct is not ready. Test the failures: wrong signer, missing account, bad amount.

If you need a team to build and ship the program, Solana development services cover the build, audit and deployment.

Key takeaways

  • A Solana program holds logic; accounts hold the data
  • Test on a local validator, then devnet, then mainnet — in that order
  • Anchor's account checks run before your logic and stop many common errors
  • Move the upgrade authority to a multisig before real funds arrive
  • Check the CLI network setting before every deploy

Frequently asked questions

What is a Solana smart contract called?

It is called a program. The program holds the logic of the application, while accounts hold the data it reads and writes. The same program can serve many users because each user's data lives in a separate account.

Do I need to know Rust to build on Solana?

Yes, for the program itself. Solana programs are written in Rust, usually with the Anchor framework, which handles the account checks and the interface. The front end can use any language that talks to the network.

What is the difference between devnet and mainnet?

Devnet is the shared public test network, where SOL has no value and testing costs nothing. Mainnet is the live network where real funds move. Deploy to devnet first, and only move to mainnet after testing and audit.

Can a deployed Solana program be changed?

Yes, while it is upgradeable. The holder of the upgrade authority can replace the program code. Before real funds arrive, move that authority to a multisig wallet so no single key can change the program.

How much does it cost to deploy a Solana program?

Deployment costs a fee in SOL that varies with the size of the program and network conditions. The fee is small compared with the audit and testing effort, which is where most of the budget goes.

What is Anchor and do I need it?

Anchor is a framework for writing Solana programs in Rust. It handles account checks, the program interface and testing, so you write less plumbing. You can write a program without it, but most teams use it.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building on Solana and need the program shipped safely? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Solana Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call