hackquest logo

CounterContact

๐Ÿงฎ Counter Smart Contract โ€” Project Info The Counter Smart Contract is a simple yet powerful Solidity project that demonstrates the fundamentals of blockchain programming.

๋น„๋””์˜ค

๊ธฐ์ˆ  ์Šคํƒ

React
Web3
Java
Node
Ethers
Solidity
Next
Vue

์„ค๋ช…

# ๐Ÿงฎ Counter Smart Contract

A simple **Solidity smart contract** that demonstrates how to store and
modify a number on-chain using basic **state variables**, **functions**,
and **transactions**.

------------------------------------------------------------------------

## ๐Ÿš€ Overview

This smart contract allows anyone to **increment**, **decrement**, or
**read** a counter stored on the blockchain.

It's a perfect beginner project for learning how Solidity handles
**state**, **public variables**, and **gas-based function calls**.

------------------------------------------------------------------------

## ๐Ÿ“œ Smart Contract Code

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

/**
 * @title Counter
 * @dev A simple smart contract that allows incrementing and decrementing a count.
 */
contract Counter {
    /**
     * @dev The state variable 'count' is stored on the blockchain.
     * The 'public' keyword automatically creates a getter function for it.
     */
    uint256 public count;

    /**
     * @dev Increases the value of 'count' by 1.
     * This is a state-changing function and requires a transaction (and gas).
     */
    function increment() public {
        count += 1;
    }

    /**
     * @dev Decreases the value of 'count' by 1.
     * This will revert the transaction if 'count' is 0, preventing an underflow error,
     * because 'uint256' cannot be negative.
     */
    function decrement() public {
        require(count > 0, "Count cannot go below zero");
        count -= 1;
    }

    /**
     * @dev A view function to get the current value of the count.
     * Calling this function is free as it does not modify the state.
     * Note: A public getter is already created for 'count', but this is
     * included for explicit demonstration.
     */
    function get() public view returns (uint256) {
        return count;
    }
}
```

------------------------------------------------------------------------

## ๐Ÿง  Key Concepts Demonstrated

-   State variables and blockchain storage
-   Public visibility and automatic getters
-   Gas cost for state-changing functions
-   Safe increment/decrement operations
-   View functions (no gas cost for reads)

------------------------------------------------------------------------

## ๐Ÿช™ Deployment Info

**Contact Address:** `0x4f8a0FF83CBD6D02566EaE08DEa7e7d9De556cA6`

------------------------------------------------------------------------

## ๐Ÿ’ก How to Use

1.  Deploy the contract on Remix IDE (https://remix.ethereum.org)
2.  Click **Deploy** and open the deployed contract tab.
3.  Use:
    -   `increment()` โ†’ adds 1 to the count.
    -   `decrement()` โ†’ subtracts 1 (will fail if count is 0).
    -   `get()` โ†’ returns the current count.

------------------------------------------------------------------------

## ๐ŸŒ Author & Links

๐Ÿ‘ค **Developer:** Anindha Biswas\
๐Ÿ”— **LinkedIn:** [Connect with
me](https://www.linkedin.com/in/anindhabiswas)\
๐Ÿ“ซ **Contact Address:** `0x4f8a0FF83CBD6D02566EaE08DEa7e7d9De556cA6`

------------------------------------------------------------------------

โญ If you found this project useful, give it a like or share it on
LinkedIn!

ํ•ด์ปคํ†ค ์ง„ํ–‰ ์ƒํ™ฉ

Perfect ๐Ÿ‘Œ Hereโ€™s a **step-by-step process** for how to **create, test, and deploy** your Solidity **Counter Smart Contract**, formatted clearly so you can post it or include it in your README or LinkedIn project write-up. --- ## โš™๏ธ Steps to Create the Counter Smart Contract ### ๐Ÿชถ **1. Set Up Your Environment** You can use **Remix IDE** โ€” a free, browser-based Solidity editor. * Visit ๐Ÿ‘‰ [https://remix.ethereum.org](https://remix.ethereum.org) * No installation required. * Make sure your browser has **MetaMask** extension installed if you want to deploy to a live or test network. --- ### ๐Ÿงฑ **2. Create a New Solidity File** 1. In Remix, go to the **File Explorer** (left sidebar). 2. Click **New File** โ†’ name it `Counter.sol`. 3. Paste this code: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; contract Counter { uint256 public count; function increment() public { count += 1; } function decrement() public { require(count > 0, "Count cannot go below zero"); count -= 1; } function get() public view returns (uint256) { return count; } } ``` --- ### ๐Ÿงฉ **3. Compile the Contract** 1. Click the **Solidity Compiler** tab (on the left). 2. Select the compiler version **0.8.20** (or compatible). 3. Click **Compile Counter.sol**. โœ… You should see a green check mark if it compiles successfully. --- ### ๐Ÿš€ **4. Deploy the Contract** 1. Go to the **Deploy & Run Transactions** tab (the Ethereum icon). 2. Under **Environment**, choose: * `Remix VM (London)` for local testing, or * `Injected Provider - MetaMask` to deploy on a testnet like **Sepolia** or **Polygon Amoy**. 3. Click **Deploy**. 4. After deployment, youโ€™ll see your contract instance under **Deployed Contracts**. --- ### ๐Ÿ” **5. Interact with the Contract** In the deployed contract panel: * Click `increment()` โ†’ increases count by 1. * Click `decrement()` โ†’ decreases count by 1 (will revert if 0). * Click `get()` โ†’ returns the current value of count. ๐Ÿ’ก Each transaction (except `get()`) will cost **gas**, since it changes blockchain state. --- ### ๐ŸŒ **6. Optional โ€” View on Blockchain Explorer** If deployed on a testnet: * Copy your contract address (e.g., `0x4f8a0FF83CBD6D02566EaE08DEa7e7d9De556cA6`). * Paste it into [Etherscan Testnet](https://sepolia.etherscan.io/) or [Polygonscan](https://amoy.polygonscan.com/) to view details. --- ### ๐Ÿง  **7. (Optional) Extend the Contract** You can expand it later by adding: * `reset()` function to set the count to 0. * `onlyOwner` restriction (using OpenZeppelinโ€™s `Ownable`). * Event logging for each increment/decrement. --- ### โœ… **Youโ€™ve Deployed Your First Smart Contract!** You now understand: * How to use state variables * How functions interact with blockchain data * The difference between **view** and **transactional** functions * How to deploy to a test network ---
ํŒ€ ๋ฆฌ๋”
AAnindha Biswas
ํ”„๋กœ์ ํŠธ ๋งํฌ
๋ถ€๋ฌธ
SocialFiDeFiNFT