Minimal, audited-style example of a production-ready Counter smart contract + vanilla frontend. Fork it, learn from it, and build on it.
If this project helps you learn Web3 or speeds up a hackathon prototype:
👉 Please Star the repo (it really helps!)
👉 Consider Forking to extend it
👉 Follow the author for more templates & patterns
👉 Support via sponsorship (see below) 💖
- Clean, modern Solidity ^0.8.x contract (
Counter) with events - Gas-optimized increment/decrement using
uncheckedblocks where safe - Backward compatibility layer (
incr,decr,display) for older tutorials
npm run build:solArtifacts land in artifacts/ (Hardhat JSON + ABI + bytecode).
If you previously used the legacy solc script, you can delete the old build/ folder.
Start a local Hardhat node in a separate terminal: npm run dev (defaults to http://localhost:8545).
npm run deployCopy the deployed address printed in the console, then in the browser devtools run:
localStorage.setItem('counterAddress', '0xYOUR_DEPLOYED_ADDRESS');Reload counter.html.
Serve the static files (any simple server works):
npx serve .Open http://localhost:3000/counter.html (or whichever port serve prints). MetaMask will prompt for account access.
Run the included Hardhat test suite:
npm testIt validates:
- Initial count = 0
- Increment -> 1
- Decrement -> 0
- Revert on decrement at zero
Core public interface:
function getCount() external view returns (uint256);
function increment() external;
function decrement() external; // reverts if already zeroEvents:
event Incremented(uint256 newValue);
event Decremented(uint256 newValue);Legacy compatibility (still callable): incr(), decr(), display().
- Add ownership (e.g. OpenZeppelin Ownable) restricting decrement
- Add batch increment function
- Expose events via a small Graph / subgraph
- Wrap UI with React / Next.js
- Convert to ERC20-like supply model for learning
Open an issue to discuss ideas—collaboration welcome.
- Fork
- Create a feature branch:
git checkout -b feat/your-idea - Commit changes:
git commit -m 'feat: add your idea' - Push:
git push origin feat/your-idea - Open a Pull Request
Please run npm test before submitting.
If you find this helpful, you can:
- Star & share the repo
- Follow for more templates
- Sponsor coffee / development time (add your preferred platform link here)
Your support accelerates more open-source learning repos.
ethers.js: Contract interactionhardhat: Development environmentsolc(via Hardhat): Compilation
MIT — free to use. See LICENSE (add file if missing).
Made with ❤️ for the Web3 community. If you build something cool with this, drop a link in Issues.