Integrating Hardhat with frontend frameworks such as React allows developers to build decentralized applications (dApps) that interact with smart contracts on the Ethereum blockchain. This guide will walk you through the process of setting up a React application that uses Hardhat to deploy and interact with smart contracts.
Prerequisites
- Basic knowledge of React and JavaScript.
- Node.js and npm installed on your machine.
- A Hardhat project set up with a smart contract deployed.
Setting Up the Hardhat Project
- If you haven't already, create a new Hardhat project:
mkdir my-dapp
cd my-dapp
npm init --yes
npm install --save-dev hardhat
npx hardhat
- Follow the prompts to create a basic Hardhat project. Create a sample smart contract in the
contracts
directory, such asGreeter.sol
:
pragma solidity ^0.7.0;
contract Greeter {
string greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}
Deploying the Smart Contract
- Create a deployment script in the
scripts
directory nameddeploy.js
:
async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
console.log("Greeter deployed to:", greeter.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
- Run the deployment script to deploy your contract:
npx hardhat run scripts/deploy.js --network rinkeby
Setting Up the React Application
- In the root directory of your Hardhat project, create a new React application:
npx create-react-app frontend
- Navigate to the
frontend
directory:
cd frontend
- Install ethers.js to interact with the Ethereum blockchain:
npm install ethers
Connecting React to Hardhat
- In the
frontend/src
directory, create a new file namedGreeter.js
:
import React, { useEffect, useState } from 'react';
import { ethers } from 'ethers';
import GreeterArtifact from '../artifacts/contracts/Greeter.sol/Greeter.json';
const greeterAddress = "your_deployed_contract_address"; // Replace with your deployed contract address
function Greeter() {
const [greeting, setGreeting] = useState("");
const [newGreeting, setNewGreeting] = useState("");
useEffect(() => {
const fetchGreeting = async () => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const greeterContract = new ethers.Contract(greeterAddress, GreeterArtifact.abi, provider);
const currentGreeting = await greeterContract.greet();
setGreeting(currentGreeting);
};
fetchGreeting();
}, []);
const updateGreeting = async () => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const greeterContract = new ethers.Contract(greeterAddress, GreeterArtifact.abi, signer);
const tx = await greeterContract.setGreeting(newGreeting);
await tx.wait();
setGreeting(newGreeting);
};
return (
<div>
<h1>Current Greeting: {greeting}</h1>
<input
type="text"
value={newGreeting}
onChange={(e) => setNewGreeting(e.target.value)}
placeholder="Set a new greeting"
/>
<button onClick={updateGreeting}>Update Greeting</ button>
</div>
);
}
export default Greeter;
Integrating the Greeter Component
- Open the
frontend/src/App.js
file and import theGreeter
component:
import React from 'react';
import Greeter from './Greeter';
function App() {
return (
<div className="App">
<Greeter />
</div>
);
}
export default App;
Running the Application
- Make sure your Hardhat local network is running:
npx hardhat node
- In a new terminal, deploy your contract again if you haven't done so:
npx hardhat run scripts/deploy.js --network localhost
- Now, start your React application:
npm start
Conclusion
By following these steps, you can successfully integrate Hardhat with a React frontend, allowing you to build interactive decentralized applications that can communicate with your smart contracts on the Ethereum blockchain. This setup provides a solid foundation for developing more complex dApps.