How do I keep the fork mainnet use the latest block using web3? I need to fork mainnet to latest block before I send the transaction What originally i do is just run the ganache fork command which only have the state the block the time i ran it and use

// at some conditions
const localWeb3 = new Web3( // connect to forked net
    new Web3.providers.WebsocketProvider('ws://localhost:8546') // port of forked mainnet ganache
// do the transaction

What I tried

// at some conditions
const localWeb3 = new Web3( // connect to forked net
    ganache.provider( {fork: "myalchemyprovider"} ) 
// do the transaction

or with
    ganache.provider( {fork: web3.currentProvider} ) // no good since i dont have archive mode of eth node 

what is the correct way to do this?

  • did you find a solution for that to your problem ? I am interested as well.
    – user71607
    Commented Apr 29, 2021 at 11:04
  • I do no think there is a solution, because the second fork, by definition, overrides any state changes you would do yourself. What is your use case? Commented Apr 29, 2021 at 17:16
  • @MikkoOhtamaa one use case is that states frequently change for the contract you are looking at. So if you were dynamically forming transactions yourself, they can fail and this will be expensive to try on mainnet, they will also not produce the same results as your testnet if your fork has the contract in a different state
    – CQM
    Commented Aug 18, 2021 at 3:05


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.