To fork the mainnet, open your terminal/cmd and copy-paste the following:
fork ethereum mainnet using ganache
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>
Replace ADD_YOUR_QUICKNODE_URL_HERE with the QuickNode HTTP URL we got earlier and run the command; you must see something similar to this.
It will fork the mainnet at the blockchain’s latest block, 12200647, in the above example. You can query the forked chain by pinging localhost:8545.
You can fork at a specific block in the blockchain by mentioning the block number along with ‘@’ after your node URL.
fork ethereum mainnet using ganache
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>@<block_number>
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>@<block_number>
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>@<block_number>
Let’s say we want to do some development on the xDai chain which resides on the Ethereum blockchain network and uses xDai for gas. We can search for Dai on etherscan, and by going to the
holder’s section, we can view the biggest Dai holders copy an address and run the following.
fork ethereum mainnet using ganache
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE> -u <address of token holder>
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE> -u <address of token holder>
$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE> -u <address of token holder>
Here ganache will fork the Ethereum blockchain and unlock (-u) the above account for the local ganache environment. Using ganache-cli we can impersonate a particular account address which is usually locked for use. We can also make transactions on the simulated blockchain from that account address.
Note: The tokens you get to use are not real tokens and are meant to be used for development and testing purposes.
Note: You’ll need a node with an archive add-on to access the blocks older than 64 blocks from the latest block.