DEV Community

Samuel Emmanuel
Samuel Emmanuel

Posted on

My First Anchor Program: More Than Just a Counter

Building a counter is one of those projects that looks trivial until you build it yourself.

This week, I completed my first working Anchor program on Solana. At a glance, it only initializes a counter and increments a number, but under the hood it taught me some of the core ideas behind Solana development.

Here's what my program does:

  • Initializes a counter account for a user.
  • Stores the owner's public key and the current count.
  • Allows the owner to increment the counter.
  • Prevents other wallets from modifying someone else's counter through account constraints.
  • Includes tests for both successful transactions and expected failures.

The most valuable lesson wasn't writing the increment function it was understanding why the account constraints matter.

Instead of trusting whatever account a client sends, Anchor lets you describe exactly what a valid account should look like:

  • The account must belong to the correct program.
  • It must be derived from the expected PDA seeds.
  • It must have the correct authority.
  • The signer must match the stored owner.

If any of those checks fail, the instruction never reaches my business logic.

I also learned that good tests aren't just about proving the happy path works. They're about proving the program rejects invalid behavior. Writing tests that intentionally fail helped me understand how Anchor's validation protects my program before my Rust code even executes.

Another takeaway was that debugging taught me as much as coding. I ran into program ID mismatches, malformed macros, Rust compiler errors, and tooling issues with Cargo, Node, and Yarn. Solving those problems forced me to understand how the entire development environment fits together instead of simply copying code until it compiled.

This project may be "just a counter," but it introduced me to concepts I'll use in every future Solana program:

  • Program Derived Addresses (PDAs)
  • Account validation with #[derive(Accounts)]
  • Signers and authorities
  • Safe arithmetic
  • Integration testing
  • Thinking about security before writing business logic

The code runs, the tests pass, and I finally feel like I understand what Anchor is doing instead of treating it as magic.

On to the next day of the challenge. 🚀

Top comments (0)