Skip to content
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

Open
thegostep opened this issue Dec 26, 2020 · 5 comments
Open

Simulate transactions at the beginning of a block #1134

thegostep opened this issue Dec 26, 2020 · 5 comments
Labels
blocked-reason:needs-edr status:blocked Blocked by other issues or external reasons type:feature Feature request

Comments

@thegostep
Copy link

Cross posting a ticket that I think would be useful to have on hardhat fork

flashbots/mev-geth#9

@fvictorio fvictorio changed the title simulation rpc Simulate transactions at the beginning of a block Jan 4, 2021
@fvictorio fvictorio added the type:feature Feature request label Jan 4, 2021
@alcuadrado
Copy link
Member

This is really interesting @thegostep, thanks for sharing.

Do you see this being useful for forked networks exclusively, or also for local ones?

@thegostep
Copy link
Author

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.

@alcuadrado
Copy link
Member

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?

@thegostep
Copy link
Author

woah that's right! nice.

only thing missing is ability to send more than one tx per block

@thegostep
Copy link
Author

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)
  })
})

@fvictorio fvictorio added status:blocked Blocked by other issues or external reasons blocked-reason:needs-edr labels Dec 30, 2022
@fvictorio fvictorio moved this to Blocked in Hardhat Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked-reason:needs-edr status:blocked Blocked by other issues or external reasons type:feature Feature request
Projects
Status: Blocked
Development

No branches or pull requests

3 participants