-
Notifications
You must be signed in to change notification settings - Fork 3
Mainnet Forking Config
Linux
sudo apt update
sudo apt install curl git
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs
MacOS
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash
nvm install 12
nvm use 12
nvm alias default 12
npm install npm --global # Upgrade npm to the latest version
Windows
Download and run these:
- Git's installer for Windows(opens new window)
- node-v12.XX.XX-x64.msi from here(opens new window)#
Open a new terminal and run these commands:
mkdir hardhat-tutorial
cd hardhat-tutorial
npm init --yes
npm install --save-dev hardhat
In the same directory where you installed Hardhat run:
npx hardhat
Choose “Create an empty hardhat.config.js”
For this tutorial we are going to use the Ethers.js and Waffle plugins. They'll allow you to interact with Ethereum and to test your contracts. We'll explain how they're used later on. To install them, in your project directory run: npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
hardhat.config.js will look like
require("@nomiclabs/hardhat-waffle");
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.7.3",
};
To use this feature you need to connect to an archive node. We recommend using Alchemy
The easiest way to try this feature is to start a node from the command line:
npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key>
You can also configure Hardhat Network to always do this:
networks: {
hardhat: {
forking: {
url: "https://eth-mainnet.alchemyapi.io/v2/<key>",
blockNumber: <blockNumber>,
}
}
}
(Note that you'll need to replace the component of the URL with your personal Alchemy API key.)
Note: it’s recommended to include the blockNumber, which helps to accelerate the speed of the tests. However, putting a wrong blockNumber will result a wrong value, therefore, you might need to look for the exact blockNumber when doing the impersonation.
More about the mainnet forking with harhat (https://hardhat.org/hardhat-network/guides/mainnet-forking.html)
- Import the contract ABI
const BUSDABI = require("./BUSD-ABI.json");
NB: You would need to find the exact ABI of the contract and save it in the .json format, then import it into the test file as the above-mentioned code.
- Initialize the contract’s address
const busdContract = "0xA8D394fE7380b8cE6145d5f85E6aC22d4E91ACDe"
NB: This is the live address of the contract
- Impersonate the owner of the contract
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: ["0x6d26AA4356cEF4A641b387218fE1eF7887e3C630"],
});
whiteBUSD = await ethers.getSigner("0x6d26AA4356cEF4A641b387218fE1eF7887e3C630");
- Impersonate a live contract
BUSD = new ethers.Contract(busdContract, BUSDABI, whiteBUSD)
The BUSD
is now becoming the contract variable, you can get all the characteristics of the contract such as: BUSD.address, BUSD.owner(), etc