Skip to content

Use this as a template to start rapid smart contract development using the best of hardhat, truffle and typescript.

License

Notifications You must be signed in to change notification settings

Apegurus/hardhat-truffle-template

 
 

Repository files navigation

Hardhat Truffle Template

tests

Use this as a hardhat/truffle template to start rapid smart contract development using typescript and other mind blowing features.

Motivation

Truffle Framework and HardHat are both solidity smart contract development frameworks in which developers normally choose one over the other. Hardhat is generally the framework of choice for modern solidity development, but Truffle has a very helpful plugin, truffle-plugin-verify for quickly verifying contracts after deployment. While Hardhat has a tool for this, it still requires much more effort to verify complex projects after deployment.

Because truffle-plugin-verify uses the Truffle artifacts after deployment, we need to use truffle for deploying and verifying.

Hardhat fills in all of the other needed tools for rapid development including typescript support and broad spectrum of plugins which make testing and UI integration easier.

Truffle

  • Deploy Contracts
  • Verify Contracts

Hardhat

  • Testing
  • Gas Reporting
  • Generate Contract Types
  • Run Scripts
  • UI Integration Support

Configuration
The deploy.config.ts acts at the parent configuration file for the project.

  • Setup Solidity compiler options
  • Add networks
  • Add deployment variables which can be returned based on deployment network

The main configuration file above is processed inside the configuration files of Hardhat and Truffle.

Features

Typescript

Tests and Scripts: Use typescript to write smart contract tests and scripts.
Integration: Using Typechain and hardhat plugins, smart contract types are generated to integrate with the contracts through ethers.

Typechain

Use typechain to create typescript declaration files for typed contract interactions.

yarn compile || yarn compile:force

When contracts are compiled, typescript declarations are generated in typechain/.

Mocha

yarn test

Mocha: This repo uses mocha as the main testing framework

Use vscode-mocha-test-adapter to run individual unit tests.
Make sure to set the vscode option to "mochaExplorer.files" to "test/**/*.{j,t}s" more info

Chai: The mocha tests use the chai assertion library.

Coverage

Use solidity-coverage to generate a smart contract coverage report based on current test suite.

yarn coverage

Gas Reporter

Use hardhat-gas-reporter to generate a contract function gas/cost estimator.

yarn test:gas

Contracts

Compile Contracts

Contracts located in the contracts/ directory will be compiled using the solidity version in the solidity.config file.

Depending on the need, contracts can be compiled with Hardhat or Truffle:

Hardhat
yarn compile || yarn compile --force

Truffle
yarn compile:truffle || yarn compile:truffle --all

Deploy Contracts

After defining the network configuration as needed in solidity.config, you can deploy using one of the following commands:

# Deploy locally
yarn testrpc
yarn deploy:dev
# Deploy to specific network
yarn deploy:bsc
yarn deploy:bsc-testnet
yarn deploy:polygon
yarn deploy:polygon-testnet

Verify Contracts

Configuration Make sure to provide your network specific API key in a .env file. See .env.example for examples.

yarn verify:bsc
yarn verify:bsc-testnet
yarn verify:polygon
yarn verify:polygon-testnet

If new contracts are added, the verification scripts need to be updated in package.json

Development Blockchain

Use hardhat's built in blockchain node to run tests locally.

yarn testrpc

Code Formatting

Eslint, prettier and solhint are provided to provide TS and Solidity code linting/fixing.

yarn eslint
yarn eslint:fix
yarn prettier
yarn prettier:fix
yarn solhint
yarn solhint:fix
yarn lint # Run all linters
yarn lint:fix # Run all fixes

About

Use this as a template to start rapid smart contract development using the best of hardhat, truffle and typescript.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 68.5%
  • JavaScript 21.6%
  • Solidity 7.3%
  • Shell 2.6%