Simple Solana Counter Program
A minimal native Rust Solana program that increments and decrements an on-chain counter owned by the user's keypair.
A lightweight, native Solana smart contract for incrementing and decrementing a counter, written in Rust.
The program is a minimal Solana on-chain program written in native Rust. The core functionality is a counter:
- Increment the counter by sending a transaction
- Decrement the counter in the same way
- State is stored on-chain and owned by the user’s keypair
- Built using low-level Solana primitives, with no Anchor framework on-chain, which makes it a good way to learn the core mechanics
It suits developers who want to:
- Understand how to work with Solana’s native runtime
- Learn the basics of account handling, transaction flow and state management
- Use it as a boilerplate to build more complex programs
How to get started
-
Start a local Solana validator:
solana-test-validator -
Build and deploy the program:
anchor build && anchor deploy
Anchor is used for deployment and local testing convenience, although the on-chain code is native Rust.
-
Set up your wallet:
export ANCHOR_WALLET=~/.config/solana/id.json -
Interact with the program. Once deployed, you can send transactions to:
- Increment the stored counter
- Decrement the counter
- Fetch and log the current value
These actions can be scripted in TypeScript/JavaScript using the Anchor client or done manually with the
solanaCLI.
License
This project is open-source and available under the terms of the MIT License. Feel free to fork, modify, and use it for both personal and commercial purposes.
Neighbors in time
Related: the posts that share this one's subject, and where the thread goes next.