You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 18, 2023. It is now read-only.
Multicall.js is a lightweight JavaScript library for interacting with the multicall smart contract.
Multicall allows multiple smart contract constant function calls to be grouped into a single call and the results aggregated into a single result. This reduces the number of separate JSON RPC requests that need to be sent over the network if using a remote node like Infura, and provides the guarantee that all values returned are from the same block. The latest block number is also returned along with the aggregated results.
Summary
Get the return value(s) of multiple smart contract function calls in a single call
Guarantee that all values are from the same block
Use watchers to poll for multiple blockchain state variables/functions
Get updates when a watcher detects state has changed
Results from out of sync nodes are automatically ignored
Get new block updates
Installation
yarn add @makerdao/multicall
Usage
import{createWatcher}from'@makerdao/multicall';// Contract addresses used in this exampleconstMKR_TOKEN='0xaaf64bfcc32d0f15873a02163e7e500671a4ffcd';constMKR_WHALE='0xdb33dfd3d61308c33c63209845dad3e6bfb2c674';constMKR_FISH='0x2dfcedcb401557354d0cf174876ab17bfd6f4efd';// Preset can be 'mainnet', 'kovan', 'rinkeby', 'goerli' or 'xdai'constconfig={preset: 'kovan'};// Create watcherconstwatcher=createWatcher([{target: MKR_TOKEN,call: ['balanceOf(address)(uint256)',MKR_WHALE],returns: [['BALANCE_OF_MKR_WHALE',val=>val/10**18]]}],config);// Subscribe to state updateswatcher.subscribe(update=>{console.log(`Update: ${update.type} = ${update.value}`);});// Subscribe to batched state updateswatcher.batch().subscribe(updates=>{// Handle batched updates here// Updates are returned as { type, value } objects, e.g:// { type: 'BALANCE_OF_MKR_WHALE', value: 70000 }});// Subscribe to new block number updateswatcher.onNewBlock(blockNumber=>{console.log('New block:',blockNumber);});// Start the watcher pollingwatcher.start();
// The JSON RPC URL and multicall contract address can also be specified in the config:constconfig={rpcUrl: 'https://kovan.infura.io',multicallAddress: '0xc49ab4d7de648a97592ed3d18720e00356b4a806'};
// Update the watcher calls using tap()constfetchWaiter=watcher.tap(calls=>[// Pass back existing calls...
...calls,// ...plus new calls{target: MKR_TOKEN,call: ['balanceOf(address)(uint256)',MKR_FISH],returns: [['BALANCE_OF_MKR_FISH',val=>val/10**18]]}]);// This promise resolves when the first fetch completesfetchWaiter.then(()=>{console.log('Initial fetch completed');});
// Recreate the watcher with new calls and config (allowing the network to be changed)constconfig={preset: 'mainnet'};watcher.recreate([{target: MKR_TOKEN,call: ['balanceOf(address)(uint256)',MKR_WHALE],returns: [['BALANCE_OF_MKR_WHALE',val=>val/10**18]]}],config);
Helper Functions
Special variables and functions (e.g. addr.balance, block.blockhash, block.timestamp) can be accessed by calling their corresponding helper function.
To call these helper functions simply omit the target property (and it will default to multicall's contract address).