Always fork mainnet on latest block

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?

1 Like

Hi @jmarrr,

Sorry I haven’t done this before, so not sure how you would do this with ganache-cli. I didn’t see anything obvious in https://github.com/trufflesuite/ganache-cli

I think the following may work with hardhat:

// hardhat.config.js
require("@nomiclabs/hardhat-ethers");
require('@openzeppelin/hardhat-upgrades');

const { alchemyApiKey, mnemonic } = require('./secrets.json');

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.7.3",
  networks: {
    hardhat: {
      forking: {
       url: `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`
      }
    }
  }
};