A repo for boilerplate code for testing, deploying, and shipping chainlink solidity code.
A repo for boilerplate code for testing, deploying, and shipping chainlink solidity code.
[!IMPORTANT] This branch contains the Hardhat 2 starter kit. For Hardhat 3, see the hardhat-3 branch. This branch uses Solidity Remapping and the hardhat-preprocessor plugin to resolve imports correctly. If you have errors when compiling dependencies please consult the Remapping section of this README.
Please be attention that Chainlink Any API is removed from this starter kit because Chainlink Any API is currently replaced by Chainlink Functions. Please find the starter kit for Chainlink Functions here
Implementation of the following 3 Chainlink features using the Hardhat development environment:
For Chainlink Functions please go to these starter kits: Hardhat | Foundry (coming soon)
For Chainlink CCIP (Cross Chain Interoperability Prototocol) please go to these starter kits: Hardhat | Foundry
It's recommended that you've gone through the hardhat getting started documentation before proceeding here.
git --version and see a response like git version x.x.xnode --version and get an output like: vx.x.xnpmyarn --version And get an output like: x.x.xIf you're familiar with
npxandnpminstead ofyarn, you can usenpxfor execution andnpmfor installing dependencies.
After installing all the requirements, run the following:
git clone https://github.com/smartcontractkit/hardhat-starter-kit/
cd hardhat-starter-kit
Then:
npm install
The recommendation is to use npm 7 or later. If you are using an older version of npm, you'll also need to install all the packages used by the toolbox.
npm install --save-dev @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan chai ethers hardhat-gas-reporter solidity-coverage @typechain/hardhat typechain @typechain/ethers-v5 @ethersproject/abi @ethersproject/providers
That's also the case if you are using yarn.
yarn add --dev @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan chai ethers hardhat-gas-reporter solidity-coverage @typechain/hardhat typechain @typechain/ethers-v5 @ethersproject/abi @ethersproject/providers
npx hardhat test
or
npm run test
or
yarn test
To use typescript, run:
git checkout typescript
npm install
If you run npx hardhat --help you'll get an output of all the tasks you can run.
npm run deploy
This will deploy your contracts to a local network. Additionally, if on a local network, it will deploy mock Chainlink contracts for you to interact with. If you'd like to interact with your deployed contracts, skip down to Interacting with Deployed Contracts.
One of the best ways to test and interact with smart contracts is by using a local network. To run a local network with all your contracts in it, execute the following command:
npx hardhat node
You'll get a local blockchain, private keys, contracts deployed (from the deployment folder scripts), and an endpoint to potentially add to an EVM wallet.
In your hardhat.config.js you'll see section like:
module.exports = {
defaultNetwork: "hardhat",
networks: {
This section of the file is where you define which networks you want to interact with. You can read more about that whole file in the hardhat documentation.
To interact with a live or test network, you'll need:
Let's look at an example of setting these up using the Sepolia testnet.
First, we will need to set environment variables. We can do so by setting them in our .env file (create it if it's not there). You can also read more about environment variables from the linked twilio blog. You'll find a sample of what this file will look like in .env.example
IMPORTANT: MAKE SURE YOU'D DON'T EXPOSE THE KEYS YOU PUT IN THIS
.envFILE. By that, I mean don't push them to a public repo, and please try to keep them keys you use in development not associated with any real funds.
SEPOLIA_RPC_URL environment variable.You can get one for free from Alchemy, Infura, or Moralis. This is your connection to the blockchain.
PRIVATE_KEY environment variable.This is your private key from your wallet, ie MetaMask. This is needed for deploying contracts to public networks. You can optionally set your MNEMONIC environment variable instead with some changes to the hardhat.config.js.
⚠️ WARNING ⚠️
When developing, it's best practice to use a Metamask that isn't associated with any real money. A good way to do this is to make a new browser profile (on Chrome, Brave, Firefox, etc) and install Metamask on that browser, and never send this wallet money.
Don't commit and push any changes to .env files that may contain sensitive information, such as a private key! If this information reaches a public GitHub repository, someone can use it to check if you have any Mainnet funds in that wallet address, and steal them!
.env example:
SEPOLIA_RPC_URL='https://sepolia.infura.io/v3/asdfadsfafdadf'
PRIVATE_KEY='abcdef'
bash example:
export SEPOLIA_RPC_URL='https://sepolia.infura.io/v3/asdfadsfafdadf'
export PRIVATE_KEY='abcdef'
You can also use a
MNEMONICinstead of aPRIVATE_KEYenvironment variable by uncommenting the section in thehardhat.config.js, and commenting out thePRIVATE_KEYline. However this is not recommended.
For other networks like mainnet and polygon, you can use different environment variables for your RPC URL and your private key. See the hardhat.config.js to learn more.
Head over to the Chainlink faucets and get some ETH and LINK. Please follow the chainlink documentation if unfamiliar.
Head over to VRF Subscription Page and create the new subscription. Save your subscription ID and put it in helper-hardhat-config.js file as subscriptionId
You should now be all setup! You can run any command and just pass the --network sepolia now!
To deploy contracts:
npm run deploy --network sepolia
To run staging testnet tests
npm run test-staging
If you would like to run tests on a forked network, follow these steps:
MAINNET_RPC_URL environment variable that connects to the mainnet.FORKING_BLOCK_NUMBER environment variable. If ignored, it will use the latest block each time which can lead to test inconsistency.enabled flag to true/false to enable/disable forking feature forking: {
url: MAINNET_RPC_URL,
blockNumber: FORKING_BLOCK_NUMBER,
enabled: false,
}
Tests are located in the test directory, and are split between unit tests and staging/testnet tests. Unit tests should only be run on local environments, and staging tests should only run on live environments.
To run unit tests:
npx hardhat test
or
npm run test
or
yarn test
To run staging tests on Sepolia network:
npx hardhat test --network sepolia
or
npm run test-staging
Since all tests are written in a way to be independent from each other, you can save time by running them in parallel. Make sure that AUTO_FUND=false inside .env file. There are some limitations with parallel testing, read more about them here
To run tests in parallel:
npx hardhat test --parallel
or
npm run test --parallel
After deploying your contracts, the deployment output will give you the contract addresses as they are deployed. You can then use these contract addresses in conjunction with Hardhat tasks to perform operations on each contract.
The Price Feeds consumer contract has one task, to read the latest price of a specified price feed contract
npx hardhat read-price-feed --contract insert-contract-address-here --network network
The VRFConsumer contract has two tasks, one to request a random number, and one to read the result of the random number request. As explained in the developer documentation, there are two methods:
No open issues yet, or sync has not completed.