Last active
March 21, 2022 08:24
-
-
Save a2468834/71c59d580c1da21337350cdfc47e515b to your computer and use it in GitHub Desktop.
Query WETH9 contract using Hardhat mainnet forking between a range of blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Please read mainnet forking tutorial at the LunDAO | |
// Constants | |
const EXIT_SUCCESS = 0; | |
const EXIT_FAILURE = 1; | |
const weth9_address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; | |
const contract_ABI = require("./contract-abi.json"); | |
const start_block = 14379900; // The starting block number you want | |
const end_block = 14379910; // The ending block number you want | |
async function method1() { | |
console.log("Method 1"); | |
var config = { method: "hardhat_reset", | |
params: [{ | |
forking: { | |
jsonRpcUrl: process.env.Mainnet, | |
blockNumber: 0}}] | |
}; | |
// Prepare the ethers.js Provider object | |
var provider = await hre.ethers.provider; // The default forking block number is in 'hardhat.config.js' or the current highest block | |
// Prepare the ethers.js Contract object | |
var WETH9 = new hre.ethers.Contract(weth9_address, contract_ABI, provider); | |
for(var block_i = start_block; block_i < (end_block+1); block_i++) { | |
config.params[0].forking.blockNumber = block_i; | |
await hre.network.provider.request(config); // Reset the mainnet forking from another block number | |
WETH9 = WETH9.connect(provider); | |
console.log("----------------------------------------"); | |
console.log(`Block number: ${await provider.getBlockNumber()}`); | |
console.log(`TotalSupply: ${ethers.utils.formatEther(await WETH9.totalSupply())}`); | |
} | |
} | |
async function method2() { | |
console.log("Method 2"); | |
var provider = await hre.ethers.provider; | |
var WETH9 = new hre.ethers.Contract(weth9_address, contract_ABI, provider); | |
for(var block_i = start_block; block_i < (end_block+1); block_i++) { | |
var overrides = {blockTag: block_i}; | |
console.log("----------------------------------------"); | |
console.log(`Block number: ${block_i}`); | |
console.log(`TotalSupply: ${ethers.utils.formatEther(await WETH9.totalSupply(overrides))}`); | |
} | |
} | |
async function main() { | |
// Delete one of the following comments | |
//await method1(); | |
//await method2(); | |
} | |
main() | |
.then(() => process.exit(EXIT_SUCCESS)) | |
.catch((error) => { | |
console.error(error); | |
process.exit(EXIT_FAILURE); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment