1. Home
  2. /
  3. Hardhat
  4. /
  5. Listen to Events

Listen to Events - Hardhat Typing CST Test

Loading…

Listen to Events — Hardhat Code

Listen to events emitted by a contract using Hardhat provider.

async function main() {
	const Counter = await ethers.getContractFactory('Counter')
	const counter = await Counter.attach('0xYourContractAddress')
	counter.on('Incremented', (newValue) => {
		console.log('Counter incremented to:', newValue)
	})
}

main().catch((error) => { console.error(error); process.exitCode = 1 })

Hardhat Language Guide

Hardhat is a JavaScript/TypeScript-based Ethereum development environment and framework. It enables developers to compile, deploy, test, and debug smart contracts efficiently on Ethereum and EVM-compatible networks.

Primary Use Cases

  • ▸Compiling and deploying Solidity smart contracts
  • ▸Running local Ethereum test networks
  • ▸Automated testing of smart contracts
  • ▸Debugging and logging transactions
  • ▸Scripted contract interactions and deployments

Notable Features

  • ▸Local development network (Hardhat Network)
  • ▸Solidity compilation and artifact management
  • ▸JavaScript/TypeScript-based scripting
  • ▸Plugin system for extended functionality
  • ▸Integration with ethers.js, web3.js, and Waffle

Origin & Creator

Hardhat was created by Nomic Labs in 2018 to simplify Ethereum smart contract development and provide a powerful local blockchain testing environment.

Industrial Note

Hardhat is preferred for Ethereum development workflows, CI/CD smart contract testing, automated deployment, and integration with frontends or other tools like ethers.js and web3.js.

Quick Explain

  • ▸Hardhat provides a local Ethereum network for development, enabling fast testing and debugging.
  • ▸Integrates with Solidity compiler to build smart contracts and generate artifacts.
  • ▸Supports scripts for deployment, contract interaction, and automated testing.
  • ▸Provides a flexible plugin system for tasks like ethers.js integration, gas reporting, and contract verification.
  • ▸Widely used in DeFi, NFT projects, and backend DApp development.

Core Features

  • ▸Tasks and scripts for custom operations
  • ▸Hardhat Network for instant blockchain simulation
  • ▸Console logging for smart contract debugging
  • ▸Testing framework integration (Mocha/Chai)
  • ▸Contract deployment and verification automation

Learning Path

  • ▸Learn JavaScript/TypeScript basics
  • ▸Understand Ethereum accounts, gas, and transactions
  • ▸Install Hardhat and configure project
  • ▸Practice writing deployment scripts and tests
  • ▸Integrate with frontend or backend apps

Practical Examples

  • ▸ERC20/ERC721 token deployment
  • ▸DeFi protocol contract testing
  • ▸NFT marketplace simulation
  • ▸Automated contract interaction scripts
  • ▸Transaction tracing and debugging

Comparisons

  • ▸Hardhat vs Truffle: Hardhat is faster and plugin-based, Truffle is monolithic
  • ▸Hardhat vs Brownie: Hardhat JS/TS-focused, Brownie Python-focused
  • ▸Hardhat vs Web3.py: Hardhat manages full dev workflow, Web3.py is library only
  • ▸Hardhat vs Foundry: Hardhat JS ecosystem, Foundry Rust/solidity-native
  • ▸Hardhat vs Remix: Remix is web-based IDE, Hardhat is local development framework

Strengths

  • ▸Fast local blockchain simulation
  • ▸Easy Solidity compilation and deployment
  • ▸Highly extensible with plugins
  • ▸Debugging and stack traces for contracts
  • ▸Integrates seamlessly with JS/TS frontends

Limitations

  • ▸Requires JavaScript/TypeScript knowledge
  • ▸Primarily Ethereum/EVM-focused
  • ▸Local network state resets on restart (ephemeral)
  • ▸Some plugins may have version compatibility issues
  • ▸Less Python integration compared to Web3.py

When NOT to Use

  • ▸Non-EVM blockchains
  • ▸Pure Python backend workflows
  • ▸Projects not using Node.js or JS/TS
  • ▸Simple scripts not needing local blockchain simulation
  • ▸Quick one-off testing without full framework

Cheat Sheet

  • ▸npx hardhat -> initialize or run tasks
  • ▸hardhat.config.js -> project configuration
  • ▸scripts/ -> deployment scripts
  • ▸contracts/ -> Solidity contracts
  • ▸test/ -> automated tests

FAQ

  • ▸Can Hardhat deploy to testnets?
  • ▸Yes - via scripts using Infura/Alchemy keys
  • ▸Does Hardhat support TypeScript?
  • ▸Yes - native TS support is available
  • ▸Can Hardhat fork mainnet?
  • ▸Yes - using Hardhat Network with mainnet fork
  • ▸Is Hardhat suitable for NFTs?
  • ▸Yes - can deploy and test ERC721 contracts
  • ▸Can Hardhat be used in CI/CD?
  • ▸Yes - scripts and tests can be fully automated

30-Day Skill Plan

  • ▸Week 1: JS/TS and Node.js fundamentals
  • ▸Week 2: Hardhat setup and compiling contracts
  • ▸Week 3: Writing scripts and tasks
  • ▸Week 4: Automated testing and debugging
  • ▸Week 5: Deploy full DApp and integrate frontend

Final Summary

  • ▸Hardhat is a comprehensive Ethereum development framework.
  • ▸Provides compilation, deployment, testing, and debugging tools.
  • ▸Node.js/JS/TS ecosystem integration with plugins.
  • ▸Local Hardhat Network enables fast testing and debugging.
  • ▸Ideal for DeFi, NFT, and DApp development on Ethereum.

Project Structure

  • ▸contracts/ - Solidity source code
  • ▸scripts/ - deployment and interaction scripts
  • ▸test/ - unit and integration tests
  • ▸hardhat.config.js - configuration
  • ▸artifacts/ - compiled contracts and ABIs

Monetization

  • ▸DeFi and NFT project deployments
  • ▸Backend automation scripts
  • ▸Subscription-based monitoring solutions
  • ▸Analytics dashboards
  • ▸Smart contract auditing tools

Productivity Tips

  • ▸Use Hardhat tasks to automate repetitive work
  • ▸Fork mainnet for realistic testing
  • ▸Combine plugins for gas and verification reporting
  • ▸Modularize scripts and tests
  • ▸Leverage console logging for debugging contracts

Basic Concepts

  • ▸Project - Hardhat workspace with contracts, scripts, and tests
  • ▸Task - CLI commands for custom operations
  • ▸Hardhat Network - local Ethereum simulation
  • ▸Artifact - compiled contract and ABI
  • ▸Plugin - extends Hardhat functionality

Official Docs

  • ▸https://hardhat.org/getting-started/
  • ▸https://github.com/NomicFoundation/hardhat

More Hardhat Typing Exercises

Hardhat Minimal Smart Contract DeploymentHardhat Call Contract FunctionHardhat Get AccountsHardhat Estimate GasHardhat Deploy with ParametersHardhat Read Contract VariableHardhat Transfer EtherHardhat Deploy Multiple ContractsHardhat Upgradeable Contract Deployment

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher