USDC Yield With Cover Built InQuicknode Earn now runs USDC yield with cover for protocol risk built in, underwritten by OpenCover. Six covered vaults on Base.
阅读公告使用 Ganache 与Ethereum MainNet 进行交互,以及Quicknode
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.

September 16, 2019 — 2 min read

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 Quicknode, we can have a virtual fork of Ethereum MainNet, for quick and convenient use locally!
ganache-cli --fork {url quicknode http provider}@{block number}
Here, 区块编号 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:
var http = require('http');
var Web3 = require('web3');
var provider = 'http://127.0.0.1:8545';
var web3Provider = new Web3.providers.HttpProvider(provider);
var web3 = new Web3(web3Provider);
var DAI_TOKEN_ADDRESS = "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359"
var Wallet_address = "0x2c82E14352e98c931852D9BB5f0760E36942CC3c"
var DAI_ABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"stop","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"burn","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"name_","type":"bytes32"}],"name":"setName","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"stopped","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"burn","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"push","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"move","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"start","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"},{"name":"guy","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"wad","type":"uint256"}],"name":"pull","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"symbol_","type":"bytes32"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"}]
let contract = new web3.eth.Contract(DAI_ABI, DAI_TOKEN_ADDRESS)
const foo = async () => {
try {
var balance = await contract.methods.balanceOf(Wallet_address).call();
console.log(balance);
} catch (err) {
console.log(err.message);
}
}
foo();
web3.eth.getBlockNumber().then((result) => {
web3.eth.getBlock(result).then((block) =>{
console.log(block);
})
console.log(result);
});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.
项目需要帮助或有疑问吗?请通过此表单、TwitterQuicknode 或在Discord 上联系我们!
Quicknode 成立于 2017 年,Quicknode 为开发者和企业Quicknode 机构级区块链基础设施。凭借 99.99% 的运行时间以及对 80 多条区块链的支持,各团队能够毫无妥协地构建和扩展链上应用。
将最新的工程见解、产品更新和 Web3 资讯直接发送至您的收件箱。
通过 SOC 2 II 类认证 · ISO 27001