Lesson 17 of 21
In Progress

Integrating with a Front-End Framework

Integrating a front-end framework with your Hardhat project can bring a number of benefits, including the ability to build out more complex and interactive user interfaces for your decentralized applications (DApps). In this chapter, we will cover the steps for integrating a front-end framework, such as React, into your Hardhat project.

Setting Up the Project

Before we can start integrating a front-end framework into our Hardhat project, we need to set up the project itself. First, create a new directory for your project and navigate into it. Then, run the following command to initialize a new Hardhat project:

hardhat init

This will create a hardhat.config.js file in your project directory, which you can use to configure your Hardhat project. You should also see a contracts/ directory, which is where you will store your Solidity contracts.

Next, we need to install the front-end framework we will be using. In this example, we will use React, so run the following command to install it:

npm install react react-dom

Integrating the Front-End Framework

Now that we have set up our project and installed the front-end framework, we can start integrating it. The first step is to create a src/ directory in your project, which will store the source code for your front-end application.

In the src/ directory, create a new file called index.html, which will serve as the entry point for your application. This file should include the following basic HTML structure:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>My DApp</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

Next, create a new file called index.js in the src/ directory, which will serve as the entry point for your front-end application. This file should import the necessary dependencies and render the root component of your application:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";

ReactDOM.render(<App />, document.getElementById("root"));

Finally, create a new file called App.js in the src/ directory, which will contain the root component of your application. This file should import the front-end framework and define the root component:

import React from "react";

const App = () => {
  return <div>Hello, World!</div>;
};

export default App;

At this point, your front-end application should be set up and ready to go. You can test it by running the following command:

This will start a development server and open the application in your default web browser. If everything is set up correctly, you should see the message “Hello, World!” displayed on the page.

Conclusion

In this article, we learned how to integrate our blockchain project with a front-end framework. By using Hardhat’s built-in Web3 provider and configuring the front-end framework to use it, we can easily connect our DApp to the blockchain and start building user interfaces that interact with our smart contracts. With this knowledge, you can now take your blockchain development skills to the next level by building complete, user-friendly DApps that run on the blockchain.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Integrate a Hardhat project with a front-end framework of your choice (e.g. React, Angular, Vue).

  • Install the necessary dependencies for the chosen front-end framework.
  • Set up the front-end framework’s build process to include the compiled contract artifacts produced by Hardhat.
  • Use the artifacts to interact with the contracts in your front-end code.

Create a form in your front-end that allows a user to input values and submit a transaction to a contract using the web3 library.

  • Import the web3 library and the compiled contract artifact into your front-end code.
  • Create a form with input fields for the necessary values.
  • Use the web3 library to create a transaction object with the input values and the contract method you want to call.
  • Send the transaction using web3.eth.sendTransaction().
  • Optionally, you can use the returned transaction hash to track the status of the transaction.

Display contract data on the front-end by calling a contract’s public method and rendering the returned value.

  • Import the web3 library and the compiled contract artifact into your front-end code.
  • Call the contract’s public method using web3.eth.call().
  • Use the returned value to update the front-end display.

Set up event listeners to listen for events emitted by your contract and update the front-end display accordingly.

  • Import the web3 library and the compiled contract artifact into your front-end code.
  • Use web3.eth.subscribe() to listen for events emitted by the contract.
  • When an event is received, use the event data to update the front-end display.

Use the Hardhat-Web3 provider to run front-end integration tests.

  • In your test suite, import the Hardhat-Web3 provider.
  • Use the provider to create a web3 instance with the Hardhat network configured.
  • Use this instance to call contract methods and assert on the returned values as you would in your production front-end code.