Book a free call

How to Create a Smart Contract on Solana, Step by Step

A smart contract on Solana is a program written in Rust with the Anchor framework. Here is how to set up the tools, write it, test it and deploy it to devnet.

Hertiv engineering teamReviewed by a senior engineer
7 min read
How to Create a Smart Contract on Solana, Step by Step
On this page

Short answer

To create a smart contract on Solana, install Rust, the Solana CLI and the Anchor CLI, then run anchor init to start a project, write the program logic in Rust, test it with anchor test on a local validator, and deploy it to devnet with anchor deploy. Solana calls smart contracts programs, and Anchor handles the boilerplate, accounts and client code so you can focus on the logic.

What is a smart contract on Solana?

A smart contract on Solana is a program that runs on the Solana network and carries out rules automatically when it is called. Solana calls smart contracts programs, and they are written in Rust, most often with the Anchor framework, which adds the structure, account checks and client code around your logic.

A program does not hold its own state the way a contract on some other chains does. On Solana, the program holds the code and the data lives in separate accounts that the program reads and writes. That split shapes everything about how you design, test and deploy a program.

Programs power the apps people already use on Solana: token swaps, lending, staking, NFT mints and games. If you can describe the rules of your app as a set of instructions that read accounts and update them, you can write them as a Solana program. For the wider picture of the chain itself, see what Solana is and how it works.

What do you need before you start?

You need four tools before you write any code: Rust, the Solana command-line interface, the Anchor command-line interface and Node.js. Anchor builds on Rust and generates a TypeScript test client, so Node.js is part of the standard setup even if your program is pure Rust.

ToolWhat it doesWhy you need it
RustLanguage for the programPrograms are written in Rust
Solana CLIKeys, wallets and clustersCreates wallets and deploys programs
Anchor CLIProject, build and deployScaffolds and builds the program
Node.jsTest client runtimeRuns the TypeScript tests Anchor makes
Devnet SOLTest fundsPays for test deployments

You also need a wallet keypair and some devnet SOL. Create a keypair with solana-keygen new, point the CLI at devnet with solana config set --url devnet, and request test SOL with solana airdrop 2. Devnet SOL has no real value, so you can deploy and break things freely. For the full build setup, see Solana development services.

How do you create a smart contract on Solana, step by step?

Creating a smart contract on Solana takes seven steps, from scaffolding the project to deploying it on devnet. Anchor does most of the setup, so the first deploy is usually a matter of hours rather than days.

  1. Start the project. Run anchor init my-program. Anchor creates a workspace with a Rust program, a test file and an Anchor.toml config file that names the cluster.
  2. Write the program logic. Open programs/my-program/src/lib.rs and add your instructions, each as a function that takes accounts and updates them. Keep each instruction small and name it after what it does.
  3. Define the accounts. For each instruction, list the accounts it reads or writes and mark which ones must sign and which ones the program may change. Anchor checks these rules before your code runs.
  4. Build the program. Run anchor build. Anchor compiles the Rust code into a program file in target/deploy and generates the program ID your tests and client will use.
  5. Test on a local validator. Run anchor test. Anchor starts a local validator, deploys the program, runs your tests and stops the validator, so you can check the logic without spending devnet SOL.
  6. Deploy to devnet. Set the cluster to devnet in Anchor.toml and run anchor deploy. The program is now live on devnet under its program ID, and anyone can call it.
  7. Call it from a client. Use the generated TypeScript client or a small script to send instructions to the program and check the accounts it updated.

Start with the template program Anchor creates and change one instruction at a time. A small program that deploys cleanly teaches you more than a large one that never builds. For a simpler first build on Solana, see how to create a token on Solana.

How do accounts and PDAs work on Solana?

On Solana, every piece of data lives in an account, and a program is itself an account that holds code. Your program reads the accounts passed to each instruction, checks that they are the ones it expects, and writes the new state back into them.

Accounts cost rent, which is a small amount of SOL held in the account to keep it stored on the network. An account that holds enough SOL to cover two years of rent is rent-exempt and never gets charged, so most programs create rent-exempt accounts and forget about rent after that.

A program derived address, or PDA, is an account address that only your program can sign for. PDAs let a program own accounts, such as a vault or a counter, without holding a private key for them. If your program needs to hold funds or keep a single shared record, a PDA is usually the right address to use.

The account model is the hardest part for developers coming from chains where contracts hold their own state. Draw the accounts first, then write the code: for each instruction, list what it reads, what it writes and who must sign. If that list is clear, the Rust code follows it.

Example (hypothetical): a tipping program

Example (hypothetical): a team builds a tipping program that lets viewers send tips to a creator, with the program keeping a 5% fee. A viewer sends a tip of 0.05 SOL, and the program splits it: 95% goes to the creator and 5% stays with the program.

Over one week, 200 viewers each send one tip of 0.05 SOL. The total collected is 200 x 0.05 = 10 SOL. The program fee is 10 x 5% = 0.5 SOL, so the creator receives 10 - 0.5 = 9.5 SOL and the program keeps 0.5 SOL.

The program stores the totals in a PDA account owned by the program, so the creator can read the running balance without trusting a separate server. If tips double to 400 in the next week, the total is 400 x 0.05 = 20 SOL, the fee is 20 x 5% = 1 SOL, and the creator receives 20 - 1 = 19 SOL.

The numbers stay simple because the fee is a fixed share of each tip. The lesson is to fix the fee rule in the program before launch: changing it later means redeploying the program and asking every user to trust the new rules.

What are the common mistakes?

The most common mistakes when creating a smart contract on Solana are skipping the local tests, forgetting account checks, and deploying straight to mainnet.

  • Skipping anchor test. A program that was never run against a local validator hides basic errors, such as missing signers, that the tests would have caught in seconds.
  • Missing account checks. If an instruction does not check that an account is the one it expects, a caller can pass a different account and change data they should not touch. Anchor helps, but only if you write the checks.
  • Deploying to mainnet first. Mainnet programs handle real SOL. Deploy to devnet, call every instruction, and only move to mainnet when the tests and the devnet runs both pass.
  • Ignoring rent. Accounts that are not rent-exempt can be closed when their rent runs out. Create rent-exempt accounts from the start.
  • One giant instruction. An instruction that does everything is hard to test and hard to fix. Split the logic into small instructions with one job each.
  • No upgrade plan. Programs can be upgraded by redeploying to the same program ID, but only if the upgrade authority is kept safe. Decide who holds it before launch.

What do you do after the contract is live?

After the program is live on devnet, the next step is to call it from a real client and watch how it behaves. Build a small front end or script that sends each instruction, reads the accounts it updated, and confirms the state matches what you expected.

Then get the program reviewed before mainnet. A review by someone who did not write the code catches account-check gaps and logic errors that the author stops seeing. For anything that will hold real funds, an audit is worth the cost: a bug in a live program cannot be undone, only fixed by an upgrade.

Finally, plan the upgrade path. Keep the upgrade authority in a safe place, record the program ID and the deployed version, and test every upgrade on devnet first. For a guide to building on the chain more broadly, see how to build on Solana.

Key takeaways

  • A Solana smart contract is a program written in Rust
  • Anchor scaffolds, builds, tests and deploys it
  • Accounts hold the data; the program holds the code
  • Test on a local validator, then deploy to devnet
  • Check every account each instruction touches

Frequently asked questions

What language do you use to write a smart contract on Solana?

Rust, most often with the Anchor framework. Anchor adds the project structure, account checks and test client around your Rust code, and it handles building and deploying. You can also write programs in plain Rust or C, but Anchor is the usual starting point for a first program.

What is the difference between a program and a smart contract?

On Solana they are the same thing under different names. Solana calls them programs, while most other chains call them smart contracts. The difference that matters is the account model: a Solana program holds code, and the data it works with lives in separate accounts.

Do I need real SOL to deploy a smart contract on Solana?

No, not while you are learning. Devnet SOL is free from the faucet and pays for test deployments. You only need real SOL when you deploy to mainnet, where the program handles real funds and the deployment cost is paid in SOL.

What is a PDA on Solana?

A program derived address is an account address that only your program can sign for. It lets a program own accounts, such as a vault or a counter, without holding a private key. If your program needs to hold funds or keep a shared record, a PDA is usually the right address.

Can I update a smart contract after deploying it on Solana?

Yes, by redeploying the program to the same program ID, as long as you still hold the upgrade authority. Test every upgrade on devnet first, and keep the upgrade authority safe, because whoever holds it can change the program.

How long does it take to build a first smart contract on Solana?

A first program with one or two instructions usually takes a few days, including setting up Rust, the Solana CLI and Anchor. Most of the time goes to learning the account model, not to writing Rust, so start with a small program and add instructions one at a time.

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

Planning a smart contract on Solana or another chain? 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 [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call