Hardhat Template with TypeScript support. For the main deployment plugin, we use hardhat-deploy instead of the standard deployment script, because it provides many utilities, such as saving all information about contracts and automatically verifying them on the block scanner, and so on read here. Also template has plugin for manipulation time on blockchain extensions/time.ts. And two extends for standart classes String and BigNumber.
After yarn deploy --network <network> all inforamtion abount contract saves in file contracts.json. You can modify path in package.json
Put all your secret inforamtion inside .secrets.ts, see example .secrets.ts.example
For testing we dont use hardhat-deploy fixtures because for now solidity-coverage does not support them
$ yarn
$ yarn compileyarn compile- compiles all smartcontracts and generates typechainyarn deploy <netowrk>- executes all deploy scripts with providednetwork(reuse old deployments)yarn test- runs all testsyarn coverage- runs coverage of testsyarn verify <network>- verifies contract onnetwork
contracts- for smartcontractsdeploy- for deploy scriptsdeployments- for all deploymentsextensionsscripts- setup scripts for init smartcontractstest- for teststypechain- generated interfaces for contractsutils- different utils
Extension for hardhat hre. Provides base methods evm time manipulation
evm_snapshot- snapshot the state of the blockchain at the current block. Takes no parameters.evm_revert- revert the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot id to revert to. If no snapshot id is passed it will revert to the latest snapshot.evm_increaseTime- jump forward in time. Takes one parameter, which is the amount of time to increase in seconds.evm_mine- Force a block to be mined. Takes no parameters. Mines a block independent of whether or not mining is started or stopped.
import { time } from 'hardhat'
async function example() {
const snapId = await time.snapshot() // saves snpashot id of node state
await time.increaseTime(1000) // increase time on 1000 seconds
await time.mine() // mines actual block
await time.revert(snapId) // reverts state of node to snapId
}Extends ethers.js BigNumber class with two useful functions
BigNumber.prototype.formatString(decimals: number, precision: number): string- formatsBigNumberto human readable string of token amount with providedprecisionBigNumber.prototype.formatNumber(decimals: number, precision: number): number- formatsBigNumberto human readable number of token amount with providedprecision
import { BigNumber, ethers } from 'ethers'
function example() {
const tokens = ethers.constants.WeiPerEther.mul(1111).div(1000) // 1.111 ether
const formattedInString = tokens.formatString(/* decimals */ 18, /* precision */ 2) // '1.11'
const formattedInNumber = tokens.fromatNumber((/* decimals */ 18, /* precision */ 2) // 1.111
}Extends class String with useful function for blockchain
String.prototype.toBigNumber(decimals = 0): BigNumber- converts string to BigNumber withdecimalsString.prototype.cutZeros(): string- cuts all zeros after commaString.prototype.shortAddress(start = 6, end = start - 2): string- converts long address to short variantString.prototype.validNumber(...)- validates number input. By default, any positive, non-zero number consisting only of0-9and maybe.dot character
String.prototype.validNumber(
num: string,
params = {} as {
/** Forbid float. Default: false */
nofloat?: boolean
/** Allow zero value. Default: false */
allowzero?: boolean
/** Allow negative value. Default: false */
allownegative?: boolean
})function example() {
const address = '0x5FbDB2315678afecb367f032d93F642f64180aa3'
address.shortAddress() // '0x5FbD...0aa3'
// ...
}