Skip to main content

Fork Ethereum with Ganache

Ganache is a local development blockchain that simulates the Ethereum network, allowing developers to build and test dapps before releasing them to production.

The Ganache integration with Infura gives you access to Ethereum archive data, allowing you to fork the Ethereum network and replay historical transactions.

Install Ganache

To install Ganache, run:

npm install ganache --global

Run Ganache on archive mode

Create a fork of Ethereum Mainnet, running Ganache on full archive mode using the following command:

ganache --fork

You can now send requests to this fork in a new terminal window. For example, use eth_getBalance to fetch the balance of address 0xe5Fb31A5CaEE6a96de393bdBF89FBe65fe125Bb3 at block 1:

curl -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0", "id": 1, "method": "eth_getBalance", "params": ["0xe5Fb31A5CaEE6a96de393bdBF89FBe65fe125Bb3", "0x1"] }' http://localhost:8545

Specify a network to fork

By default, the --fork option creates a fork of Ethereum Mainnet. Alternatively, specify which network to fork using the option. The options are mainnet, goerli, görli, and sepolia.

ganache <network name>

Specify a provider URL

By default, Ganache uses Infura as the blockchain provider. Alternatively, specify the provider URL using the --fork.url option.

Specify your Infura URL, for example wss://{API-KEY}, or the URL of an Ethereum client, for example http://localhost:1337. You can specify the block to fork from using the @ sign, for example http://localhost:1337@8675309.

You can specify Basic Authentication credentials in the URL, for example wss:// If you need to use an Infura API key secret, specify wss://:{API-KEY-SECRET}{API-KEY}.

ganache --fork.url <URL>

Other command line options

Additionally, use the following command line options when forking Ethereum with Ganache:

  • --fork.blockNumber - Block number to fork from. The default is the latest block number.
  • --fork.preLatestConfirmations - Number of blocks before the provider's latest block to fork from, when the fork.blockNumber is set to latest (default). The default is 5.
  • --fork.requestsPerSecond - Restrict the number of requests per second sent to the fork. The default is 0, which means no limit is applied.
  • --fork.disableCache - Disables caching of all forking requests. The default is false.
  • --fork.deleteCache - Deletes the persistent cache before starting. The default is false.
  • --fork.username - Username to use for Basic Authentication.
  • --fork.password - Password to use for Basic Authentication.
  • --fork.jwt - Encoded JSON Web Token (JWT) to use for Bearer Authentication.
  • --fork.headers - Headers to send to the fork on each request. Headers set using this option override headers set by the following options:
    • --fork.userAgent - User-Agent header to send to the fork on each request. Sent as Api-User-Agent when used in the browser.
    • --fork.origin - Origin header to send to the fork on each request. Ignored in the browser.