Git Product home page Git Product logo

multicall.js's Introduction

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

multicall.js's People

Contributors

jparklev avatar michaelelliot avatar

Watchers

Arkadiusz Szaleniec avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.