Git Product home page Git Product logo

web3-multicall's Introduction

web3-mulitcall

npm version

A library to do multiple calls via a single eth_call using web3.

Installation

  • via yarn
yarn add @dopex-io/web3-multicall
  • via npm
npm i @dopex-io/web3-multicall

Usage

Contract Reference

Default Supported Networks

Constructing

  • With chainId

    import Multicall from '@dopex-io/web3-multicall';
    
    import erc20Abi from './abi/erc20.json';
    
    async function main() {
      const web3 = new Web3(provider /* Your Web3 provider here */);
    
      const multicall = new Multicall({
        chainId: 1,
        provider: 'Your Web3 provider here',
      });
    
      ...
    }
    
    main()
  • With custom Multicall address

    import Multicall from '@dopex-io/web3-multicall';
    
    import erc20Abi from './abi/erc20.json';
    
    async function main() {
      const web3 = new Web3(provider /* Your Web3 provider here */);
    
      const multicall = new Multicall({
        multicallAddress: "The address of the deployed multicall contract",
        provider: 'Your Web3 provider here',
      });
    
      ...
    }
    
    main();

Aggregating

const dpxContract = new web3.eth.Contract(dpxAddress, erc20Abi);

const balances = await multicall.aggregate([
  dpxContract.methods.balanceOf('Address 1'),
  dpxContract.methods.balanceOf('Address 2'),
  multicall.getEthBalance('Address 3'),
]);

console.log('DPX balance of Address 1', balances[0]);
console.log('DPX balance of Address 2', balances[1]);
console.log('ETH balance of Address 3', balances[2]);

Helper Functions

  • getEthBalance Gets the ETH balance of an address

    const ethBalance = multicall.getEthBalance('address');
  • getBlockHash Gets the block hash

    const blockHash = multicall.getBlockHash(blockNumber);
  • getLastBlockHash Gets the last blocks hash

    const lastBlockHash = multicall.getLastBlockHash();
  • getCurrentBlockTimestamp Gets the current block timestamp

    const currentBlockTimestamp = multicall.getCurrentBlockTimestamp();
  • getCurrentBlockDifficulty Gets the current block difficulty

    const currentBlockDifficulty = multicall.getCurrentBlockDifficulty();
  • getCurrentBlockGasLimit Gets the current block gas limit

    const currentBlockGasLimit = multicall.getCurrentBlockGasLimit();
  • getCurrentBlockCoinbase Gets the current block coinbase

    const currentBlockCoinbase = multicall.getCurrentBlockCoinbase();

License

This project is licensed under the MIT License - Copyright (c) 2021 Dopex

web3-multicall's People

Contributors

witherblock avatar skydev0h 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.