Skip to content

jquesnelle/hardhat-typescript-example

Repository files navigation

hardhat-typescript-example

This repository contains a fully functional example of using TypeScript with Hardhat, including several extensions I've found useful. Neccessary configuration variables are stored in a .env

Deploying a contract

Deploy a contract and perform initalization via a resumable script with hardhat-deploy.

npx hardhat deploy

To customize the deployment, see the script in deploy/. Deployments are saved to deployments/

Verifying contact source on Etherscan

Contract source code can be verified on Etherscan if an API key is provided.

npx hardhat verify <contract address>

Publishing ABIs

Publish contract ABIs with hardhat-etherscan so other applications can easily integrate with your contracts.

npx hardhat export-abi

Contract ABIs will be exported to abis/ which are suitable for committing.

Testing a contract

Contracts can be easily tested with a mix of mocha, chai, and waffle -- see test/. When using Visual Studio Code, individual tests can be run or debugged from the GUI by installing Mocha Test Explorer.

If desired a gas usage report can be generated by setting REPORT_GAS=true, and quoted in USD if a CoinMarketCap API key is provided.

npx hardhat test
  Sample
    tests
      √ math (49ms)

·------------------------|---------------------------|--------------|-----------------------------·
|  Solc version: 0.8.11  ·  Optimizer enabled: true  ·  Runs: 9999  ·  Block limit: 30000000 gas  │
·························|···························|··············|······························
|  Methods               ·               106 gwei/gas               ·       3176.74 usd/eth       │
··············|··········|·············|·············|··············|···············|··············
|  Contract   ·  Method  ·  Min        ·  Max        ·  Avg         ·     calls     ·  usd (avg)  │
··············|··········|·············|·············|··············|···············|··············
|  Sample     ·  math    ·          -  ·          -  ·       43810  ·            1  ·      14.75  │
··············|··········|·············|·············|··············|···············|··············
|  Deployments           ·                                          ·  % of limit   ·             │
·························|·············|·············|··············|···············|··············
|  Sample                ·          -  ·          -  ·      111169  ·        0.4 %  ·      37.43  │
·------------------------|-------------|-------------|--------------|---------------|-------------·

  1 passing (725ms)

Interacting with a contract

Deployed contracts can be interacted with programatically -- see scripts/.

npx hardhat run ./scripts/interact.ts

About

Example of using TypeScript with Hardhat

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published