JSPM

  • Created
  • Published
  • Downloads 351
  • Score
    100M100P100Q91244F
  • License MIT

JavaScript library for interacting with the multicall smart contract.

Package Exports

  • @makerdao/multicall

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@makerdao/multicall) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Multicall.js

npm version

Installation

yarn add @makerdao/multicall

or

npm install @makerdao/multicall

Multicall.js is a lightweight JavaScript library for interacting with the multicall smart contract.

Multicall allows multiple view calls to be grouped into a single call and the results aggregated into a single result from the multicall contract. 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 the assurance that all values returned are from the same block. (The latest block number is returned along with the aggregated results).

Currently supported data types are: booleans, integers, addresses, fixed-size byte arrays (e.g. bytes32)

Summary

  • Get the return value from different smart contract function calls in a single call
  • Assurance that all values are from the same block number / block height
  • Compare the returned block number against the previous call's block number to know if it's possible for any returned values to be different (i.e. for polling)

Usage

import MultiCall from "@makerdao/multicall";

// "kovan" or "mainnet" presets
// optionally pass in a specific block you'd like to query (defaults to "latest")
const multicall = new MultiCall('kovan', { block: 9158211 });


const { blockNumber, mkrBalance, priceOracleAddress } = await multicall.aggregate([
    {
        to: '0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD',
        method: 'balanceOf(address)',
        args:  [['0x72776bb917751225d24c07d0663b3780b2ada67c', 'address']],
        returns: [['mkrBalance', 'uint256']]
    },
    {
        to: '0xa71937147b55Deb8a530C7229C442Fd3F31b7db2',
        method: 'pip()',
        returns: [['priceFeed', 'address']]
    },

    // ... etc, many more value reads are possible w/ this request

]); // all of these values are fetched within a single call


 -- or --

multicall.aggregate([
    {
        to: '0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD',
        method: 'balanceOf(address)',
        args:  [['0x72776bb917751225d24c07d0663b3780b2ada67c', 'address']],
        returns: [['mkrBalance', 'uint256']]
    },
    {
        to: '0xa71937147b55Deb8a530C7229C442Fd3F31b7db2',
        method: 'pip()',
        returns: [['priceFeed', 'address']]
    },

    // ... etc, many more value reads are possible w/ this request

]).then(({ blockNumber, mkrBalance, priceOracleAddress }) => {

    // all of these values are fetched within a single call

});

Examples

First, clone this Repo

git clone https://github.com/makerdao/multicall.js

Then use npm or yarn to install the dependencies:

yarn

Finally run the example from the examples folder:

yarn examples

Test

To run tests use:

yarn test