DEV Community

Cover image for Build a Simple Counter Smart Contract in Solidity
karthikaaa-x
karthikaaa-x

Posted on

Build a Simple Counter Smart Contract in Solidity

Hello again, Geeksters!

So if you read my Hello World blog (and didn’t fall asleep halfway), you probably know I’m still figuring out this whole Solidity thing.

But today, I’m back with another beginner-friendly smart contract. This time we’re building a Counter. A simple contract that counts up and down like a digital toddler learning numbers.

But first, what’s a Counter Contract?
A counter smart contract is one of the easiest ways to get your hands dirty with:

  • State variables
  • Function creation
  • Function visibility
  • Increment and decrement logic

It's a tiny contract that holds a number and lets you increase or decrease it. Nothing fancy, but a solid way to practice.

Let’s Start:
Open Remix! Hop onto Remix IDE, that magical place where all smart contracts are born.

Create a new file called Counter.sol. Paste the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Counter {
    uint public count;

    constructor() {
        count = 0;
    }

    function increment() public {
        count += 1;
    }

    function decrement() public {
        require(count > 0, "Counter can't go below 0");
        count -= 1;
    }
}
Enter fullscreen mode Exit fullscreen mode

So… What’s Happening Here?
Let’s break this down line by line because I like knowing why things work — not just copy-pasting and hoping Remix is in a good mood today.

-// SPDX-License-Identifier: MIT - It tells people what license your code uses.

-pragma solidity ^0.8.0; - This tells Solidity: “Hey, I want you to compile this with version 0.8.0 or anything newer but not breaking.” We don’t want syntax tantrums later.

-contract Counter { - We’re creating a contract (like a class in Java or Python). Name? Counter. Duh.

-uint public count; - We’re making a public unsigned integer called count. Public means anyone (yes, anyone!) can view it without needing a separate function.

-constructor() { count = 0; } - When this contract is deployed, we set count to zero. Because a counter usually starts… well… at zero. Unless you’re a chaotic dev.

-function increment() public { count += 1; } - A simple function that adds 1 to our count. It’s public, so anyone can call it and bump the number.

-function decrement() public { require(count > 0, "Counter can't go below 0"); count -= 1; } - This one subtracts 1 but only if the count is above zero. We don’t want it to go negative — that’s not how counters work in normal human land.

Time to Deploy!

  • Okay, now that you’ve typed (or shamelessly copied) the code:
  • Hit the Compile button (left sidebar).
  • Go to the Deploy & Run Transactions tab.
  • Click Deploy.
  • Click the count button — it should show 0.
  • Click increment() a few times, then check count again. Boom — it’s counting!
  • Try decrement() — if it reaches 0 and you try again, Remix will yell at you (nicely).

Final Thoughts
This might seem super basic — and it is — but that’s the point. I’m still at the “clumsy but curious” stage of learning Solidity, and small wins like this really help.

You don’t need to write the next DeFi app right away. You just need to start, mess up a little, and build one block at a time.

If you got your counter working, that’s HUGE. I’m proud of you. Let’s keep learning, one contract at a time!

See you in the next blog — and maybe we’ll build something even cooler.

Top comments (0)