This is a starter template for building a decentralized application (dApp) using React for the frontend and Hardhat for smart contract development and deployment. It includes a simple smart contract to trade ETH tokens between accounts and a basic React interface to interact with it. Inspired from the official Hardhat Boilerplate from the Hardhat Tutorial. But that template was outdated, with the last commit being almost 2 years ago.
Before you begin, ensure you have the following installed on your machine:
Follow these steps to set up and run the project:
Run the following command in the root directory to install all required dependencies:
npm install
Hardhat provides a local Ethereum network for development and testing. Start the Hardhat node using:
npx hardhat node
# or
npm run network # alias for npx hardhat node
This will create multiple test accounts with pre-funded Ether.
Once the Hardhat node is running, compile and deploy the smart contract:
npx hardhat clean
npx hardhat compile
npx hardhat run scripts/deploy.js --network localhost
This will deploy the contract on the local Hardhat network and output the contract address. Also, the contract Address & ABI will be saved in the client/src/contracts
directory.
Create a .env
file in the server
directory according to .env.example
. Make sure you have MongoDB installed and running on your machine. Setup MongoDB
Navigate to the backend, server
directory and start the Express server:
cd server
npm install
npm run server
The backend should now be running at http://localhost:3001.
Navigate to the frontend, client
directory and start the React application:
cd client
npm install
npm run dev
The frontend should now be running at http://localhost:5173. Open this link in your browser.
To interact with the deployed smart contract, configure MetaMask:
- Open MetaMask and click on the network dropdown.
- Select Custom RPC and enter the following details:
- Network Name: Localhost
- RPC URL:
http://127.0.0.1:8545
- Chain ID:
31337
- Import an account from the Hardhat node using its private key (displayed when running
npx hardhat node
).
If you encounter a nonce error when trying to interact with the contract, reset your MetaMask account:
- Open MetaMask.
- Go to Settings → Advanced.
- Click Clear activity and nonce data.
- Restart the Hardhat node and redeploy the contract.
This issue occurs when the transaction count gets out of sync after restarting the Hardhat node.
After restarting the Hardhat node, you may need to reset the DB hash data. As the previous hash data from the local chain will be invalid. You can simply delete the txhashes
collection from the DB.
- React – Frontend framework for building the UI.
- React Router – Library for handling routing in React applications.
- DaisyUI – Tailwind CSS components for styling.
- Express – Backend framework for building the API.
- MongoDB – NoSQL database for storing transaction data.
- Mongoose – ODM library for MongoDB.
- Hardhat – Ethereum development environment for smart contracts.
- Solidity – Programming language for writing smart contracts.
- Ethers.js – Library for interacting with the Ethereum blockchain.
- MetaMask – Wallet and browser extension for managing Ethereum accounts.
The official Hardhat Boilerplate from the Hardhat Tutorial by Nomic Foundation.