26/06/2023, 22:25 How to Fork Mainnet for Testing
AUDIT CAMP FARM LIQUID STAK
How to Fork Mainnet
for Testing
Author: MixBytes team
Intro
Example of usage mainnet forking in hardhat
Conclusion
Additional readings and tutorials
Intro
One of the most exciting features of DeFi is interoperability which allows constructing protocols using deployed smart contracts as base blocks for
your protocol (e.g. Yearn Strategies). As a matter of fact, interoperability is one of the reasons why DeFi protocols are referred to as legos. Just like
Lego blocks, you have to find the right way to fit two DeFi protocols together for specific use cases.
Developers have 2 options for testing smart contracts which integrate with at least one already existing protocol: creating a mock which implements
all necessary functions of already deployed smart contract or using mainnet forking for tests. Mocks can mask very dangerous problems because
usually they copy only the need function from real contract which can lead to incorrect work of a mock in some cases. That's the reason why we
strongly recommend using mocks only if it is crucial, in all other cases you can use mainnet forking.
Example of usage mainnet forking in hardhat
In this article we will discuss how to set your hardhat project for mainnet forking. First of all, you must have an Infura or Alchemy API key to be able
to use a RPC node to fork the state of the specific block. After getting an API key from one of the RPC providers you need to change your config file
like this:
const CHAIN_IDS = {
hardhat: 31337, // chain ID for hardhat testing
};
module.exports = {
networks: {
hardhat: {
chainId: CHAIN_IDS.hardhat,
forking: {
https://mixbytes.io/blog/how-fork-mainnet-testing 1/3
26/06/2023, 22:25 How to Fork Mainnet for Testing
// Using Alchemy
url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_KEY}`, // url to RPC node, ${ALCHEMY_KEY} CAMP
AUDIT - must be your APIFARM
key LIQUID STAK
// Using Infura
// url: `https://mainnet.infura.io/v3/${INFURA_KEY}`, // ${INFURA_KEY} - must be your API key
blockNumber: 12821000, // a specific block number with which you want to work
},
},
... // you can also add more necessary information to your config
}
}
// Function which allows to convert any address to the signer which can sign transactions in a test
const impersonateAddress = async (address) => {
const hre = require('hardhat');
await hre.network.provider.request({
method: 'hardhat_impersonateAccount',
params: [address],
});
const signer = await ethers.provider.getSigner(address);
signer.address = signer._address;
return signer;
};
// Function to increase time in mainnet fork
async function increaseTime(value) {
if (!ethers.BigNumber.isBigNumber(value)) {
value = ethers.BigNumber.from(value);
}
await ethers.provider.send('evm_increaseTime', [value.toNumber()]);
await ethers.provider.send('evm_mine');
}
// Construction to get any contract as an object by its interface and address in blockchain
// It is necessary to note that you must add an interface to your project
const WETH = await ethers.getContractAt('IWETH', wethAddress);
https://mixbytes.io/blog/how-fork-mainnet-testing 2/3
26/06/2023, 22:25 How to Fork Mainnet for Testing
AUDIT CAMP FARM LIQUID STAK
Other posts
RESEARCH JUN 20, 2023 DEV TIPS MAY 11, 2023 RESEARCH APR 27, 2023
One more problem with ERC777 zkSNARKS, Circom, and Go Practice (Part 2) BendDAO Protocol Overview
We’ve discovered an intriguing bug that could The second article highlighting practical aspects of In this article, learn about BendDAO, an NFT lending
potentially serve as an attack vector for some DeFi zk-SNARKs usage with the review of zk-related protocol offering instant liquidity for blue-chip
projects. This bug is specifically associated with projects and SNARK-friendly algorithms. NFTs.
the well-known ERC777 token standard.
One more problem with zkSNARKS, Circom, and BendDAO Protocol Dealing with Utilization in
ERC777 Go Practice (Part 2) Overview Lending Protocols
See all
© 2023 MixBytes() [email protected] Privacy policy Terms of Service Mediakit
https://mixbytes.io/blog/how-fork-mainnet-testing 3/3