First crypto project develped using Hardhat (instead of Truffle).
This project, and README file are following the excellent video by Julien on How to use Hardhat.
- Smart contract
- Tests
- Debug
- Deployment (in-memory, actual network)
- Frontend
npm init -y
- create new projectnpm i -D hardhat
- install hardhat as dev dependencynpx hardhat
- run and choose empty confignpm i -D ethers @nomiclabs/hardhat-ethers ethereum-waffle @nomiclabs/hardhat-waffle chai
-install plugins and test platforms- Add
require('@nomiclabs/hardhat-waffle');
at top of config file. No need to require the ethers plugin, since the waffle plugin requires it.
- Create a contracts folder and start working on Solidity contracts!
- Compile using
npx hardhat compile
- Create a test folder and put your test files in there
- Start the test file with
const {expect} = require('chai');
- The
ethers
object is injected automatically into the test. Use it to deploy the contract and get the addresses:Token = await ethers.getContractFactory('Token'); token = await Token.deploy(); [owner, addr1, addr2, _] = await ethers.getSigners();
- No need to do BigNumber conversions - hardhat does it for you
- Run tests with
npx hardhat test
- Add
import 'hardhat/console.sol';
at the top of your contract file - Use
console.log
statments, using substitution strings (read more here) - When you run your tests, you'll be able to see values in the console window
- Create a directory called
scripts
and adeploy.js
file in it - The ethers object is injected automatically into the test. Use it to deploy the contract:
Token = await ethers.getContractFactory('Token'); token = await Token.deploy();
- To deploy to Hardhat in-memory blockchain:
npx hardhat run scripts/deploy.js
- To deploy to other networks (ie Rinkeby):
- Get the Infura URL and private key of the account to use for deployment:
- Create a
.env
file - Add
INFURA_RINKBEY_URL=
to.env
with your Rinkeby Infura key (get free project) - Add
RINKEBEY_PRIVATEKEY=
to.env
with the private key of the account you intend to use - Add
.env
to.gitignore
to avoid saving file to repo - Run
npm i -D dotenv
- Add the line
require('dotenv').config();
to the top ofhardhat.config.js
- Create a
- Add the network to the config in
hardhat.config.js
:networks: { rinkeby: { url: process.env.INFURA_RINKBEY_URL, accounts: [`0x${process.env.RINKBEY_PRIVATE_KEY}`] } }
- Deploy:
npx hardhat run scripts/deploy.js --network rinkeby
- Check on Rinkeby Etherscan to verify your contract has been deployed to that address
- You can now repeat the same with mainnet, or other testnets, by adding keys to
.env
and networks tohardhat.config.js
- Get the Infura URL and private key of the account to use for deployment:
Unlike Truffle, we need to generate the abi file/s ourselves, to allow the frontend to interact with our contract.
- We'll first create a
frontend
folder and asrc
subfolder. - We'll add the following to our
deploy.js
:const fs = require('fs'); // ... rest of the deployemnt // after the .deploy() const data = { address: token.address, abi: JSON.parse(token.interface.format('json')) } fs.writeFileSync('frontend/src/Token.js', JSON.stringify(data));
- We'll then run a local blockchain:
npx hardhat node
- We'll deploy the contract to the local chain (in a separate console window!):
npx hardhat run scripts/deploy.js --network localhost
- Our contract is now deployed, and the abi file created. We can use the localhost chain to debug, and finally redeploy again to any chain we need.
- The
App.js
andethereum.js
files infrontend/src
show how to connect to, and use our contract. - You can use
create-react-app
to bootstrap the app, and addethers
andbootstrap
modules to have a workable app.