foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(`anvil`): support state caching for forked historical blocks

Open asafyish opened this issue 2 years ago • 9 comments

Component

Anvil

Describe the feature you would like

I am running anvil in fork mode (connected to archive node), like so:

anvil --fork-url "https://eth-mainnet.alchemyapi.io/v2/XXXXXXXX" --fork-block-number 14577209

Then I am trying to read historical blocks data, which works, but only caches block 14577209, even though I am reading from the other blocks. The same scenario does create cache in hardhat and ganache. Since anvil is so much faster in the initial reading, I prefer using it.

This is how I am reading historical data:

const ethers = require("ethers");

const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const daiAbi = ["function name() view returns (string)"];

const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545");
const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);

for (let blockTag = 14577100; blockTag < 14577209; blockTag++) {
  const name = await daiContract.name({
    blockTag,
  });
  console.log(name);
}

Running

forge cache ls

returns: -️ mainnet (2.1 kB) -️ Block 14577209 (2.1 kB)

Additional context

forge 0.2.0 (6ca977f 2022-05-29T02:10:50.584077Z)

asafyish avatar May 29 '22 09:05 asafyish