Interacting with Ethereum MainNet using Ganache and QuikNode

Ethereum Mainnet in your local environment

Gaurav Agrawal
QuickNode
2 min readSep 17, 2019

--

When developing dApps locally, the process to interact with protocols like Compound and MakerDAO can be a tedious one.

We need to fetch Smart Contracts of these protocols from Etherscan, and then initialize them with data.

Usually, we look for (or develop) scripts to set up our Truffle environment (check this script to set up Uniswap exchange locally), which is an error-prone process.

However, as protocols become more complex, these migrations scripts are becoming complex too.

Some of these protocols contain up to 50 Smart Contracts! Like this MakerDao migration script, which migrates around 40 Smart Contracts.

But now, using Ganache and QuikNode, we can have a virtual fork of Ethereum MainNet, for quick and convenient use locally!

ganache-cli --fork {url quiknode http provider}@{block number}

Here, block number is optional; if you don’t specify a block, it will get just the latest block’s info.

Note: this will not download Ethereum’s MainNet data, just make the call directly to your node when required.

Remember, you still need to set up the interface with external contracts.

Let’s see an example of this:

Github gist to get DAI token balance

Get DAI Token Balance for a Given Address from MainNet

Above is a simple script for getting the DAI token balance for a given wallet address. In this case, we simply added DAI token ABI and DAI’s MainNet Smart Contract address.

When you run this script, it will give us the desired result:

We are getting DAI balance for a given address from the Ethereum MainNet, and also getting the latest block number and block details.

As you can see we can get actual MainNet data in our local environment now. Similarly, we can use testnets with Ganache as well.

Let us know in the comment section below if you find this useful!

About QuikNode

QuikNode.io is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers & companies, helping scale dApps and providing high-performance Ethereum nodes. We’re working on something interesting from the past few months and will be launching soon, so subscribe our newsletter for more updates!! 😃

--

--