Forking Ethereum Mainnet: Mint Your Own Dai – Ethereum Grid
There are a handful of reasons why you may want to fork Ethereum Mainnet to use on your local developer machine. Chief among them is the ability to test your smart contracts against Mainnet chain code and addresses. For example, if your smart contract interacts with external ERC20 tokens or contracts, like Dai or Compound, having a forked Mainnet chain to run your test suite against can be incredibly helpful in simplifying your workflow.
One tool, ganache-cli, can extend off of any Ethereum node endpoint, directing new requests to itself and letting anything older pass through to the underlying node. It even has a nifty feature to let you unlock any addresses or contracts so you can control them without needing authorization.
This is a compelling alternative to using a public testnet, like Rinkeby, or using your own self-mining developer environment. In these cases, you would have to deploy your own contracts for any existing artifacts you may be utilizing and swap the addresses in your contract.
Now, if you’re sufficiently convinced, let’s walk through forking Mainnet for development.
To get started, first install ganache-cli globally:
npm install -g ganache-cli
Once installed, the command we’re looking for is:
ganache-cli --fork NODE_URL
where NODE_URL is the node endpoint you would like to fork, typically http://localhost:8545. No node is running on that port yet, so let’s change that.
Running a node can be done using remote infrastructure or, ideally, by using your own machine.
Infura is great remote node service that gives you a free allowance of 100,000 requests per day. However, to access any chain data greater than 30 minutes old, you have to pay an additional $250 per month (at the time of writing). For most hobbyist developers, this price point is a deal breaker.
Grid is a desktop app that helps you manage and launch Ethereum nodes, tools, and services. For the rest of this tutorial, we’ll use Grid to easily manage a Geth developer instance and sync a Mainnet node for ganache-cli to fork off of. Grid will also remember our settings so it requires just one click to get our node back up and running to jump back into our testing environment.
If you don’t have Grid already, Windows, Mac, and Linux builds are available at grid.ethereum.org.
Configuring Grid and Geth
After opening Grid, click on Geth and you will be taken to its plugin page. Download the latest version, then click on the Settings tab. Ensure the Network is set to Main and the Sync Mode is set to Light. You will also want to ensure the HTTP RPC API is set to Enabled for All Origins (*) so ganache doesn’t have any difficulty accessing the endpoint.
Now you can toggle on Geth. You should see the network status first start looking for peers, then start syncing. After fully synchronized, we are ready to go.
Back to ganache-cli
Now we will start ganache-cli with:
ganache-cli --fork http://localhost:8545
You should see output similar to this:
Published at Tue, 05 Nov 2019 17:56:19 +0000
Swap your tokens below to get the best prices across all decentralized crypto exchanges.