CounterContact
ð§® Counter Smart Contract â Project Info The Counter Smart Contract is a simple yet powerful Solidity project that demonstrates the fundamentals of blockchain programming.
ãããª
説æ
# ð§® 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 ---