DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for 115 ethereum hardhat : frontend project
565.ee
565.ee

Posted on

115 ethereum hardhat : frontend project

β€’ introduce

β€’ Frontend app

β€’ How to use it

β€’ send token

β€’ hardhat Tutorials , hardhat 教程

β€’ Contact 联系方式

β€’ introduce

If you want to get started with your dApp quickly or see what this whole project looks like with a frontend, you can use our boilerplate repo.

β€’ What's included

  • The Solidity contract we used in this tutorial
  • Tests for the entire functionality of the contract
  • A minimal React front-end to interact with the contract using ethers.js

β€’ Solidity contract & tests

In the root of the repo you'll find the Hardhat project we put together through this tutorial with the Token contract. To refresh your memory on what it implements:

  • There is a fixed total supply of tokens that can't be changed.
  • The entire supply is assigned to the address that deploys the contract.
  • Anyone can receive tokens.
  • Anyone with at least one token can transfer tokens.
  • The token is non-divisible. You can transfer 1, 2, 3 or 37 tokens but not 2.5.

β€’ Frontend app

In frontend you'll find a simple app that allows the user to do two things:

  • Check the connected wallet's balance
  • Send tokens to an address

It's a separate npm project and it was created using create-react-app, so this means that it uses webpack and babel.

β€’ Frontend file architecture

  • src/ contains all the code
    • src/components contains the react components
    • Dapp.js is the only file with business logic. This is where you'd replace the code with your own if you were to use this as boilerplate
    • Every other component just renders HTML, no logic.
    • src/contracts has the ABI and address of the contract and these are automatically generated by the deployment script

β€’ How to use it

First clone the repository, and then prepare for the contract deployment:

cd hardhat-boilerplate
npm install
npx hardhat node
Enter fullscreen mode Exit fullscreen mode

Here we just install the npm project's dependencies, and by running npx hardhat node we spin up an instance of Hardhat Network that you can connect to using MetaMask. In a different terminal in the same directory, run:

npx hardhat --network localhost run scripts/deploy.js
Enter fullscreen mode Exit fullscreen mode

This will deploy the contract to Hardhat Network. After this completes, start the react web app:

cd frontend
npm install
npm run start
Enter fullscreen mode Exit fullscreen mode

Then open http://127.0.0.1:3000/ in your browser and you should see this:

Set your network in MetaMask to 127.0.0.1:8545.

Now click the button in the web app. You should then see this:
image

β€’ send token

What's happening here is that the frontend code to show the current wallet's balance is detecting that the balance is 0, so you wouldn't be able to try the transfer functionality. By running:

npx hardhat --network localhost faucet <your address>
Enter fullscreen mode Exit fullscreen mode

You'll run a custom Hardhat task we included that uses the balance of the deploying account to send 100 MHT and 1 ETH to your address. This will allow you to send tokens to another address.

You can check out the code for the task in /tasks/faucet.js, which is required from hardhat.config.js.

$ npx hardhat --network localhost faucet 0x0987a41e73e69f60c5071ce3c8f7e730f9a60f90
Transferred 1 ETH and 100 tokens to 0x0987a41e73e69f60c5071ce3c8f7e730f9a60f90
Enter fullscreen mode Exit fullscreen mode

In the terminal where you ran npx hardhat node you should also see:

eth_sendTransaction
  Contract call:       Token#transfer
  Transaction:         0x460526d98b86f7886cd0f218d6618c96d27de7c745462ff8141973253e89b7d4
  From:                0xc783df8a850f42e7f7e57013759c285caa701eb6
  To:                  0x7c2c195cd6d34b8f845992d380aadb2730bb9c6f
  Value:               0 ETH
  Gas used:            37098 of 185490
  Block #8:            0x6b6cd29029b31f30158bfbd12faf2c4ac4263068fd12b6130f5655e70d1bc257

  console.log:
    Transferring from 0xc783df8a850f42e7f7e57013759c285caa701eb6 to 0x0987a41e73e69f60c5071ce3c8f7e730f9a60f90 100 tokens
Enter fullscreen mode Exit fullscreen mode

Showing the console.log output from the transfer() function in our contract, and this is what the web app will look like after you run the faucet task:

Try playing around with it and reading the code. It's full of comments explaining what's going on and clearly indicating what code is Ethereum boilerplate and what's actually dApp logic. This should make the repository easy to reuse for your project.

β€’ hardhat Tutorials , hardhat 教程

CN δΈ­ζ–‡ Github hardhat 教程 : github.com/565ee/hardhat_CN

CN δΈ­ζ–‡ CSDN hardhat 教程 : blog.csdn.net/wx468116118

EN θ‹±ζ–‡ Github hardhat Tutorials : github.com/565ee/hardhat_EN

β€’ Contact 联系方式

Homepage : 565.ee

GitHub : github.com/565ee

Email : 565.eee@gmail.com

Facebook : facebook.com/565.ee

Twitter : twitter.com/565_eee

Telegram : t.me/ee_565

Latest comments (0)

🌚 Life is too short to browse without dark mode