Solidity Template
Includes:
- Hardhat: compile and run the smart contracts on a local development network
- TypeChain: generate TypeScript types for smart contracts
- Ethers: renowned Ethereum library and wallet implementation
- Waffle: tooling for writing comprehensive smart contract tests
- Solhint: linter
- Prettier Plugin Solidity: code formatter
This is a GitHub template, which means you can reuse it as many times as you want. You can do that by clicking the "Use this template" button at the top of the page.
Set up your .env with the respective values:
cp -v .env.example .env
Alchemy requires the API URL and ETH Private Key, Infura requires the API KEY and Mnemonic
- ALCHEMY_API_URL
- ETH_PRIVATE_KEY
- MNEMONIC
- INFURA_API_KEY
- ETHERSCAN_API_KEY
- REPORT_GAS=true
Before running any command, make sure to install dependencies:
yarn
yarn lint:prettier
If you want to re-initialize Solhint's configuration file with all the default rules enabled:
yarn && solhint --init
Or replace the existing file with:
{
"extends": "solhint:default"
}
Lint all the files inside the contracts
directory:
solhint 'contracts/**/*.sol'
Or use the included:
yarn lint:sol
Or both Prettier and Solhint:
yarn lint
Compile the smart contracts with Hardhat:
yarn compile
Compile the smart contracts and generate TypeChain artifacts:
yarn build
Run the Mocha tests:
yarn test
(requires Mnemonic and Infura API key)
npx hardhat run --network rinkeby ./scripts/deploy.ts
(requires Alchemy API URL and Ethereum private key) The API URL is for the Ropsten network.
npx hardhat run scripts/deploy.ts --network alchemy
(requires Etherscan API key)
npx hardhat verify --network <network> <DEPLOYED_CONTRACT_ADDRESS> "Constructor argument 1"
For example:
npx hardhat verify --network ropsten 0x4ed4DDd7981e347b673f697DC821965A3EB64b9c
Returns:
Creating Typechain artifacts in directory typechain for target ethers-v5
Successfully generated Typechain artifacts!
Compiling 1 file with 0.6.12
Successfully submitted source code for contract
contracts/TestToken.sol:TestToken at 0x4ed4DDd7981e347b673f697DC821965A3EB64b9c
for verification on Etherscan. Waiting for verification result...
Successfully verified contract TestToken on Etherscan.
https://ropsten.etherscan.io/address/0x4ed4DDd7981e347b673f697DC821965A3EB64b9c#code
0x4ed4DDd7981e347b673f697DC821965A3EB64b9c
To verify your various test, hardhat, and Alchemy networks are configured properly, npx hardhat networks
will return such details.
Network settings =>
Hardhat Runtime Environment =>
[
'config',
'hardhatArguments',
'tasks',
'run',
'artifacts',
'network',
'_extenders',
'ethers',
'waffle',
'upgrades'
]
Alchemy =>
{
accounts: [
'0xREDACTED_PRIVATE_KEY'
],
gas: 'auto',
gasPrice: 'auto',
gasMultiplier: 1,
httpHeaders: {},
timeout: 20000,
url: 'https://eth-ropsten.alchemyapi.io/v2/REDACTED_ALCHEMY_API_KEY'
}
Ropsten =>
{
accounts: {
initialIndex: 0,
count: 10,
path: "m/44'/60'/0'/0",
mnemonic: 'REDACTED_MNEMONIC_WORDS'
},
gas: 'auto',
gasPrice: 'auto',
gasMultiplier: 1,
httpHeaders: {},
timeout: 20000,
chainId: 3,
url: 'https://ropsten.infura.io/v3/REDACTED_INFURA_API_KEY'
}
- Gas reporter (hardhat-gas-reporter)
- Etherscan (hardhat-etherscan)
- Open Zeppelin (@openzeppelin/hardhat-upgrades)
There are several token examples. The first includes an ICO contract, the Interview Token and ChuckNorris Token are both derived from OpenZeppelin. The ChuckNorris Token includes a deployment script where the Token description is updated with Chuck Norris jokes fetched from an API.
npx prettier --write 'contracts/**/*.sol'
To disable all validations in the line following a comment:
// solhint-disable-next-line
bytes32 public constant MINTER_PANTS = keccak256("MINTER_PANTS");
Current line:
bytes32 public constant MINTER_PANTS = keccak256("MINTER_PANTS"); // solhint-disable-line
Block of code:
/* solhint-disable */
contract Forwarder {
address public destinationAddress;
function Forwarder() public {
destinationAddress = msg.sender;
}
function() payable public {
destinationAddress.transfer(msg.value);
}
function flush() public {
destinationAddress.transfer(this.balance);
}
}
/* solhint-enable */
1The Sale contract was updated tp ^0.8.0 from HunterLong's ^0.4.21 Solidity contract.