Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. Get Solidity stack traces & console.log.
This project consists in a simple token contract that can be deployed to the Polygon Mumbai network.
- Populate .env file
ALCHEMY_API_KEY
WALLET_PRIVATE_KEY
POLYGONSCAN_API_KEY
- Run
npm install
to install dependencies - Run
npm run compile
to compile the contracts - (optional) Run
npm run test
to check that all tests work properly - Run
npm run deploy:mumbai
to deploy the contract to the Polygon Mumbai network- or
npm run deploy:localhost
to deploy the contract to the local Hardhat network
- or
- (optional) Run
npm run verify
to verify the contract on Etherscan (Polygon Mumbai) - Run
npm run transfer
to transfer some tokens to the address specified in scripts/transfer
Follow this guide to recreate the repo step-by-step.
- Assuming you are in a new (and clean) folder, the first thing to do is initializing a new npm project. You can do that using
the CLI
npm init -y
. This command will create a package.json file with a simple setup and configuration that you will be able to edit later. - Let's also install and initialize our hardhat environment. To install and use the hardhat CLI locally,
run
npm install --save hardhat
. Then to initialize our project folders together with a configuration file runnpx hardhat init
. This command will create 3 folders (contracts, scripts, tests) and 1 file _ hardhat.config.ts_- The contract folder will contain an example Solidity contract with which you can start experiment
- The test folder will contain a Typescript file to test the same contract (you can run
npx hardhat test
to try it out) - The scripts folder will contain a deploy.ts Typescript file that allows you to deploy the contract either on a local or remote (and live) network
- Before playing with the contract, for the sake of this tutorial I would like to use this Token.sol contract, which
is a simple Solidity contract that implements a token. You can get
the Solidity source code here.
Some important notes about it:
- In the constructor, the deployer of the contract will receive the total supply available (1000000 in our example, but feel free to edit it)
- The transfer function can be used to transfer tokens from one address to another
- The balanceOf function can be used to know the balance of a specific address
- Now it's time to write some tests for our contract before deploying it making sure that everything works
properly.
What are we going to test:
- The deploy works properly and sets the right owner
- The deploy works properly and assigns the total supply of tokens to the owner
- The transfer function correctly changes token balances
- The transfer function emits the Transfer events properly
- Time to deploy! Let's modify the existing deploy script to deploy our Token contract. Then we can use this
command
npx hardhat run scripts/deploy.ts --network localhost
to deploy on our local Hardhat network (make sure to runnpx hardhat node
to spin-up a local network!). - Once deployed on our local network we can try transferring some tokens around. In the script folder, we will
create a transfer.ts file, which will transfer a fixed amount of tokens from the owner to another address. Once
done, to run it type
npx hardhat run scripts/transfer.ts --network localhost
. - Doing things locally is boring lol, let's move into Mumbai (Polygon Testnet) to test on a real live environment!
- First thing, we need to edit our hardhat.config.ts specifying a network RPC url and an account to use. For the RPC url we will use Alchemy, while for the account we can use a newly generated wallet on Metamask.
- Then type
npx hardhat run scripts/deploy.ts --network mumbai
, and boom, our contract is live on Mumbai. You can see it on Polygon Mumbai explorer, just insert the contract address. - Last thing to test is the transfer function:
npx hardhat run scripts/transfer.ts --network mumbai
. Checkin again on our contract on Polygon Mumbai explorer, we should see the executed transaction!
- (optional) As a last step, we can also verify our Solidity source code, so that it will be publicly available on
Polygonscan. Let's write a verify.ts file in the scripts folder. Verifying a contract with Hardhat is easy, as long
as you have the compiled source code, you just need the contract address and the parameters used in the constructor
at deploy time. Once done, type
npx hardhat run scripts/verify.ts --network mumbai
. Checking again on Polygonscan, we should see a new tab "Contract ☑️".