Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀

Introduction

Welcome to my 30 Days of Solidity Challenge hosted by Web3 Compass! 🌐

Today, we’re starting simple but powerful: building a digital click counter smart contract. This beginner-friendly exercise will teach you how to store and m…


This content originally appeared on DEV Community and was authored by Saurav Kumar

Introduction

Welcome to my 30 Days of Solidity Challenge hosted by Web3 Compass! 🌐

Today, we’re starting simple but powerful: building a digital click counter smart contract. This beginner-friendly exercise will teach you how to store and modify data on-chain, the foundation of interactive blockchain applications.

By the end of this tutorial, you’ll know how to:

  • Declare and use state variables (uint)
  • Write functions to increment and decrement numbers
  • Emit events for off-chain tracking
  • Build your first interactive smart contract

Challenge: Build a Click Counter

Imagine a digital clicker: each time someone clicks a button (calls a function), the counter increases. We’ll create:

  1. A variable to store the number of clicks
  2. A function click() to increase the counter
  3. A function unclick() to decrease the counter
  4. An optional reset() function to set the counter back to zero

ClickCounter.sol

Here’s a clean, professional implementation:

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

contract ClickCounter {
    uint256 private _count;

    event Clicked(address indexed caller, uint256 newCount);
    event Unclicked(address indexed caller, uint256 newCount);
    event Reset(address indexed caller);

    function getCount() external view returns (uint256) {
        return _count;
    }

    function click() external {
        _count += 1;
        emit Clicked(msg.sender, _count);
    }

    function unclick() external {
        require(_count > 0, "ClickCounter: count already zero");
        _count -= 1;
        emit Unclicked(msg.sender, _count);
    }

    function reset() external {
        _count = 0;
        emit Reset(msg.sender);
    }
}

How It Works

  • _count stores the current number of clicks
  • click() increments the counter
  • unclick() decrements the counter (but never below zero)
  • reset() sets the counter to 0
  • Events allow front-end apps or dApps to track clicks in real-time

Next Steps

  • Deploy on Ethereum testnets like Goerli or Sepolia
  • Build a web interface using React + ethers.js to call click()
  • Add ownership control so only you can reset the counter
  • Experiment with more interactive functions in Solidity

Learning Outcomes

  • Master Solidity state variables (uint)
  • Write functions to modify state on-chain
  • Understand events and their role in dApps
  • Learn basic smart contract structure

💡 This is your first step into building interactive blockchain apps. Each click on this contract is a step toward mastering Solidity!

Stay tuned for Day 2 of the challenge, where we’ll explore more advanced interactive patterns in smart contracts.


This content originally appeared on DEV Community and was authored by Saurav Kumar


Print Share Comment Cite Upload Translate Updates
APA

Saurav Kumar | Sciencx (2025-10-01T18:31:05+00:00) Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀. Retrieved from https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/

MLA
" » Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀." Saurav Kumar | Sciencx - Wednesday October 1, 2025, https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/
HARVARD
Saurav Kumar | Sciencx Wednesday October 1, 2025 » Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀., viewed ,<https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/>
VANCOUVER
Saurav Kumar | Sciencx - » Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/
CHICAGO
" » Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀." Saurav Kumar | Sciencx - Accessed . https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/
IEEE
" » Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀." Saurav Kumar | Sciencx [Online]. Available: https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» Day 1 of 30 Days of Solidity Challenge: Build Your First Click Counter Smart Contract 🚀 | Saurav Kumar | Sciencx | https://www.scien.cx/2025/10/01/day-1-of-30-days-of-solidity-challenge-build-your-first-click-counter-smart-contract-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.