I've just tried to make a transaction in the past in block number 15981826 using Hardhat forking:
networks: {
hardhat: {
forking: {
url: "https://mainnet.infura.io/v3/",
blockNumber: 15981826,
allowUnlimitedContractSize: true,
gas: "auto",
gasLimit: 21000,
},
In my main file I'm using this provider
const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/");
const tokenBase = new ethers.Contract(importToken1, abi, provider);
const [deployer]= await ethers.getSigners()
//Standart contract deploying with helpers
const SwapExamples = await ethers.getContractFactory("FlashLoanRecipient")
swapExamples = await SwapExamples.deploy()
await swapExamples.deployed()
And after deployment I'm checking blocknumber:
block = await ethers.provider.getBlockNumber()
console.log(block) //result : 15981828 (+2 blocks)
block = await provider.getBlockNumber()
console.log(block) //result : 15982414 (current block)
I'm laucnhing my file with command :
node test/swap
, but even with
npx hardhat run test/swap.js
result is the same.
Seems like I don't fully understand the difference between data from ethers.provider and ethers.providers.JsonRpcProvider.
ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/") - is a real time info from blockchain and ethers.provider.someMethod - is an information from Hardhat?
And my last "test" was without any blocknumber input:
block = await ethers.provider.getBlockNumber()
console.log(block) //result : 15982499(-4 blocks from current)
block = await provider.getBlockNumber()
console.log(block) //result : 15982503(current block)
So I have 4 questions:
- Why do I always have +2 blocks difference from what I have in config?
- Why there is a delay in 4 blocks between provider and ethers.provider?
- When I'm using forking mainnet in Hardhat without any blocknumber parameter, is it a real time situation in the mainnet or it's also a couple of blocks delay?
- How can I get to exact block that I need?
Thanks.