When you’re developping Ethereum contracts that interact with other Dapps and protocols like DeFi it’s important to be able to test your smart contracts agains the blockchain as you can’t re-deploy every smart contracts that your smart contract depends on.
For this we’ll use Ganache from Truffle. Ganache is a program that lets you quickly fire up a personal Ethereum blockchain which you can use to run tests, execute commands, and inspect state while controlling how the chain operates.
To install use:
npm install -g ganache-cli
Then you’ll need to have a way to connect to the Ethereum blockchain. They are 2 solutions:
- Spin up your own node with your favorite client (Geth, Parity…). This solution will require you to store all the blockchain data on your computer which might be a little annoying
- Use an Ethereum provider like Infura or Cloudfare
If you just want to test we really encourage you to go with the second solution and use the Cloudfare gateway as there is no signup required.
Join the newsletter
Get a weekly summary of what is happening in the Ethereum developer space for free
To fork the blockchain using ganache and cloudfare just use, if you’re usng another provider please replace the -f parameter:
ganache-cli -f https://cloudflare-eth.com/ -m "clutch captain shoe salt awake harvest setup primary inmate ugly among become" -i 999 -u 0x9759A6Ac90977b93B58547b4A71c78317f391A28
-fForks the Main Ethereum network to your local machine for development and testing.
-mRuns Ganache with an Ethereum key set based on the mnemonic passed. The first 10 addresses have 100 test ETH in their balance on the local test net every time you boot Ganache. Do not use this mnemonic anywhere other than for your tests
-iSets an explicit network ID to avoid confusion and errors.
-uUnlocks an address so you can write to your localhost test blockchain without knowing that address’s private key if you are using JS to interact with the smart contracts.
Now that Ganache is running, here is how to interact with the forked blockchain using the different tools you may use:
In the Deploy and Run transaction tab change the environment to Web3 provider. You’ll be prompted for the host and port and should just go with the default option if you did not change this in Ganache.
Then you’ll be able to directly deploy smart contracts and interact as if you had unlimited Ether on the real main Ethereum blockchain.
On your Metamask window, at the top click on the dropdown to change the current network and select localhost 8545. You should then directly be able to use Metamaks and your already imported accounts on your forked blockchain to try your dapps.
Just use the running Ganache instance as a Web3 provider:
const web3 = new Web3('http://localhost:8545');
You can then use the unlocked account by directly reading it from your web3 instance like this:
const ganacheAccounts = await web3.eth.getAccounts(); const myWalletAddress = ganacheAccounts;
That’s all to setup your own version of the Ethereum mainnet and being able to test your ideas and your code while being able to interact with the Ethereum mainnet and all the protocols and dapps living on it.