Book a free call

Testing and Debugging Solana Smart Contracts: A Practical Guide

Testing a Solana smart contract means running its instructions against known inputs before mainnet, so a bug is caught while it is still cheap to fix.

Hertiv engineering teamReviewed by a senior engineer
7 min read
Testing and Debugging Solana Smart Contracts: A Practical Guide
On this page

Short answer

Testing a Solana smart contract means running its instructions against known inputs on a local validator or test harness before deployment, checking that accounts change as expected and invalid calls fail cleanly. Debugging then uses program logs and error codes to trace why a transaction failed, so the fix can be verified with the same test before the program reaches mainnet.

What is testing a Solana smart contract?

Testing a Solana smart contract means running the program's instructions against known inputs before it reaches mainnet, and checking that accounts change the way you expect while invalid calls fail cleanly. A Solana program is a piece of code, usually written in Rust with the Anchor framework, that reads and writes account data when a transaction calls it. A test sends a transaction to that program, then reads the accounts afterwards to confirm the result.

Testing matters more on Solana than in a normal web service because a deployed program is hard to change and a bug can lock funds or break a live product. The usual test layers are:

Test layerWhat it checksWhere it runs
UnitSingle instruction logicLocal test harness
IntegrationProgram, accounts and calls to other programsLocal validator
End to endWallet, frontend and program togetherDevnet
FuzzRandom inputs and edge casesLocal harness

Start with unit and integration tests. They run in seconds, catch most logic errors, and form the base that every later check depends on. For the wider picture of how these programs are built, creating a smart contract on Solana covers the build step that testing follows.

How do you set up a local test environment?

You set up a local test environment by installing the Solana command line tools and the Anchor framework, starting a local validator, and deploying the program to it so tests can run against a private copy of the chain. A local validator behaves like the real network but gives you free test tokens and instant block times, which makes the test loop fast.

A workable sequence is:

  1. Install the Solana CLI and Anchor, then check both report a version.
  2. Start a local validator in a separate terminal with solana-test-validator.
  3. Request test tokens with an airdrop so your test wallet can pay fees.
  4. Build the program and deploy it to the local validator.
  5. Run the test suite with anchor test, which deploys, runs and reports in one step.
  6. Keep the validator running while you work, and restart it for a clean state when a test leaves accounts behind.

Keep test wallets separate from any real wallet. A local validator is private, but the habit of never mixing test keys with mainnet keys prevents an expensive accident later.

What should unit tests cover?

Unit tests should cover the happy path, invalid inputs, account checks and the error codes the program returns, because most Solana bugs live in the checks around an instruction rather than in the happy path itself. A test that only checks the successful case leaves the most dangerous code untested.

Cover these cases for every instruction:

  • The happy path. Correct accounts, correct signer, correct amounts, and the expected account changes afterwards.
  • Wrong signer. A call from an account that should not be allowed, which must fail with the program's own error.
  • Wrong account. Passing an account of the wrong type or the wrong owner, which must be rejected.
  • Bad amounts. Zero, negative where relevant, and amounts larger than the balance, which must fail rather than overflow.
  • Derived addresses. Program derived addresses computed the same way the program computes them, so a mismatch is caught.

Write the failing cases first when the check is the point of the instruction. A test that proves an attack path fails is worth more than a second test of a path that already works.

How do you test with a local validator?

You test with a local validator by deploying the program to it, sending real transactions from a test wallet, and reading the account state afterwards to confirm the result. The local validator runs the same runtime as mainnet, so a transaction that works locally will behave the same way on the real network, apart from timing and congestion.

Use the validator for the checks that a unit harness cannot do: cross-program calls, token transfers, account creation with rent, and sequences of instructions that depend on each other. Send the transactions with the same client code your frontend will use, so client-side mistakes surface early.

Watch the logs while tests run. The command solana logs streams program output, including the messages your program writes and the compute units each instruction used. A sudden rise in compute units after a change is a warning sign, even when the test still passes. Performance tuning after testing is covered by Solana optimisation best practices, which is worth reading once the tests are green.

How do you debug a failing transaction?

You debug a failing transaction by reading the program logs and the error code, reproducing the failure locally, and narrowing the cause to one instruction before changing any code. The error message alone rarely names the cause, but the logs show which instruction failed and what the program printed just before it.

Work through the failure in this order:

  1. Find the failed instruction in the transaction logs and note the program's error code.
  2. Reproduce the exact call on the local validator with the same accounts and amounts.
  3. Add temporary log messages around the check you suspect, then re-run the single failing test.
  4. Check account ownership, signer status and account size, which are the most common causes of failures that look like logic bugs.
  5. Remove the temporary logs once the cause is confirmed, and keep the failing case as a permanent regression test.

Do not guess and redeploy. A fix that is not backed by a failing-then-passing test will often return in a different form, and mainnet is the wrong place to find out.

What tools catch bugs before mainnet?

The tools that catch bugs before mainnet are the Anchor test framework, the local validator, program logs and a fuzzing pass, followed by an independent audit for anything that will hold user funds. No single tool catches every class of bug, so the layers are meant to be used together.

Anchor's test framework runs TypeScript tests against a local validator and gives you typed clients for your program, which removes a whole class of client-side mistakes. The Solana CLI gives you account inspection, log streaming and transaction simulation. Fuzzing tools send random or mutated inputs to find paths your hand-written tests missed, which is useful for instructions that take amounts, prices or user-supplied data.

An audit is the last layer, not the first. Send the code for review only after your own tests pass, because an auditor's time is best spent on design and edge cases, not on bugs your test suite should have found. If you are choosing a team to build the program, Solana development covers what to look for in a delivery partner.

Example (hypothetical): catching a fee bug before launch

Example (hypothetical): a team builds a token swap program that charges a 0.5% fee on each trade. A unit test swaps 10,000 tokens and expects a fee of 50 tokens, but the test reports a fee of 500 tokens instead. The fee is ten times too high, which would have taken far more from every trader than the product promised.

The team reproduces the failure on the local validator and reads the program logs. The calculation divides by 100 where it should divide by 1,000, because the fee is stored in basis points and the code treats the value as a plain percentage. The fix is a one-line change, and the test is extended to cover fees of 0, 1 and 10,000 tokens so the boundary cases are checked too.

After the fix, the same test reports a fee of 50 tokens on a 10,000-token swap, 0 on a zero swap, and 500 on a 100,000-token swap. The arithmetic now scales correctly: 10,000 times 0.5% is 50, and 100,000 times 0.5% is 500. The team adds the fee cases to the permanent suite, so a later change to the fee logic cannot silently reintroduce the error.

What are the common mistakes?

The most common mistakes in Solana testing are testing only the happy path, testing against mainnet, and treating a passing test suite as proof the program is safe. Each of these leaves a different kind of bug uncaught until users find it.

  • Only happy-path tests. Invalid signers, wrong accounts and bad amounts are where exploits start, so they need tests of their own.
  • Testing on mainnet. Real tokens and real fees make every test expensive, and a failed test can leave accounts in a bad state.
  • Ignoring compute units. A program that passes tests but uses most of the compute budget will fail under real load.
  • No regression tests. A bug fixed without a test for it will come back the next time that code changes.
  • Skipping the audit. Tests prove the program does what you intended; they cannot prove your intention was safe.

If the program itself is still at the design stage, how smart contracts work explains the model that Solana programs follow, which helps you decide what needs testing in the first place.

Key takeaways

  • Test on a local validator first: it is fast, free and behaves like mainnet
  • Unit tests must cover invalid signers, wrong accounts and bad amounts, not just the happy path
  • Read program logs and error codes to find the failing instruction before changing code
  • Anchor tests, the local validator, fuzzing and an audit are layers, not alternatives
  • Every bug you fix deserves a permanent regression test

Frequently asked questions

Do I need Anchor to test a Solana program?

No, but it helps a lot. Anchor gives you a test framework, typed clients and account checks out of the box. You can test a plain Rust program with the Solana CLI and a custom harness, but you will write more setup code for the same coverage.

What is the difference between devnet and a local validator?

A local validator runs on your machine, gives free test tokens and resets when you restart it. Devnet is a shared public test network with real network behaviour and other people's programs, which makes it better for end-to-end checks and worse for fast iteration.

How much of a Solana program should be tested?

Cover every instruction's happy path and its failure cases: wrong signer, wrong account, bad amounts and derived address mismatches. Aim for every check in the program to have at least one test that proves it rejects bad input.

Why did my transaction fail with an account error?

The most common causes are a missing signer, an account owned by the wrong program, an account that is too small for the data it must hold, or a derived address computed differently from the program. Check ownership, signer status and size before looking at the logic.

Can I test a Solana program without deploying it?

Yes, for logic checks. A unit harness can run instruction handlers against mock accounts without a validator. You still need a local validator for cross-program calls, token transfers and anything that depends on the runtime, so use both.

Is an audit a replacement for testing?

No. An audit reviews design and edge cases, but it cannot replace a test suite that proves the program behaves as intended. Auditors expect working tests, and code without them takes longer to review and costs more to audit.

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 want the program tested before mainnet? 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