-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simulate transactions at the beginning of a block #1134
Comments
This is really interesting @thegostep, thanks for sharing. Do you see this being useful for forked networks exclusively, or also for local ones? |
This is mostly useful for asking "what if" questions at a specific point in chain history. I don't think that is as useful with a local chain since chain history is usually ephemeral / easy to replicate. That being said, maybe some people are doing more complex things with local chains than I am. |
I may be missing something, but isn't this equivalent as forking from block N and send a tx/call to simulate a tx in the block N+1? |
woah that's right! nice. only thing missing is ability to send more than one tx per block |
Here is a simple test written to take advantage of this: import { expect } from 'chai'
import { Contract } from 'ethers'
import { ethers, network } from 'hardhat'
import { ERC20s } from '../src/constants'
const resetFork = async (blockNumber: number) =>
network.provider.request({
method: 'hardhat_reset',
params: [
{
forking: {
jsonRpcUrl: process.env.ETHEREUM_ARCHIVE_URL,
blockNumber,
},
},
],
})
describe('Epoch', function () {
let contract: Contract
before(async () => {
// fork at target block
await resetFork(11592051)
// deploy execution contract
contract = await (await ethers.getContractFactory('Epoch')).deploy()
await contract.deployed()
console.log(await ethers.provider.getBlockNumber())
})
it('should execute transaction at top of block', async function () {
const signer = (await ethers.getSigners())[0]
const preBal = await signer.getBalance()
// try execution
const tx = await contract.advance(
'0x6A2E6510B2BBF8C9AD7bC817D0Dc711711E8d747',
[ERC20s.ZAI, ERC20s.DAI, ERC20s.WETH9],
1,
{ gasPrice: 0 },
)
await tx.wait()
// assert increase balance
const postBal = await signer.getBalance()
console.log(`Profit`, ethers.utils.formatEther(postBal.sub(preBal)))
expect(postBal).to.be.gt(preBal)
})
}) |
Cross posting a ticket that I think would be useful to have on hardhat fork
flashbots/mev-geth#9
The text was updated successfully, but these errors were encountered: