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
---