GithubHelp home page GithubHelp logo

utils-blockchain's Introduction

Blockchain Utils

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

The TypeScript utility package designed for addressing multicall problems in dApp development. Built upon the foundation of the 1inch multicall library.

Install

Node

npm install utils-blockchain

Yarn

yarn add utils-blockchain

Multicall contract addresses

โš ๏ธ The MultiCall contract was not audited yet, so use at your OWN RISK! Syndika does not provide any guarantees or assurances!!!

Mainnet

Name Address
Ethereum 0x8d035edd8e09c3283463dade67cc0d49d6868063
BSC 0x804708de7af615085203fa2b18eae59c5738e2a9
Polygon 0xe38D748a07a6e3f9911Ec53ed434af2aB65716bc

Testnet

Name Address
Sepolia 0x069C02e7fF41Fd3d3f0c3fa80Ce0cB3b368aCc31
Goerli 0x20B136D01e87C99F2866f71C2d6Cc027E724AAF7
Mumbai 0xe38D748a07a6e3f9911Ec53ed434af2aB65716bc

Motivation

The purpose of this utility library is to efficiently execute multiple view calls simultaneously. It accomplishes this by taking a list of requests, dividing them into manageable chunks, and making batched calls to the provider. By reducing the number of requests and optimizing the request-response flow, the library significantly speeds up the process.

For instance, when comparing the time taken for 50 balanceOf requests, the difference in request time between a naive approach and the batch approach is substantial.

$ npm run benchmark

> [email protected] benchmark
> ts-node benchmark/multicall.benchmark.ts

Time spent to make 50 requests using Multicall: 219 ms
----------------------------------------------------------
Time spent to make 50 requests using Promise.all(): 1620 ms
Multicall saved 1401 ms
----------------------------------------------------------
Time spent to make 50 requests using separate calls: 9753 ms
Multicall saved 9534 ms

You can play with various parameters by modifying the benchmark script from benchmark/multicall.banchmark.ts directory and observing the difference.

The default multicall parameters are set as follows:

  • maxChunkSize: 500
  • retriesLimit: 3
  • blockNumber: 'latest'
  • gasBuffer: 100000

For better understanding of how requests are packed and all trade-offs made when splitting in batches, please refer to 1inch algorithm visualisation.

Library Usage

Initializing multicall service

import { Multicall } from 'utils-blockchain';

const providerURL = 'https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY';
const multicallService = await Multicall.init(providerURL);

Request batch generation

import { Contract } from 'ethers';

const contract = new Contract(
  '0xdAC17F958D2ee523a2206206994597C13D831ec7', // address of the requested contract
  ABI, // abi of the requested contract
  new JsonRpcProvider(providerURL) // JSON-RPC or Web3 provider
);
const functionName = 'balanceOf';
const targetTokenOwners = [
  '0x1212121212121212121212121212121212121212',
  '0x3434343434343434343434343434343434343434',
  '0xabcabcabcabcabcabcabcabcabcabcabcabcabca',
];
const batch = await Promise.all(
  targetTokenOwners.map(address => {
    return multicall.getRequestObject(contract, functionName, [address]);
  })
);

The batch array is formatted in the following way:

[
  {
    to: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    data: '0x70a082310000000000000000000000001212121212121212121212121212121212121212',
    gas: 146999999
  },
  {
    to: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    data: '0x70a082310000000000000000000000003434343434343434343434343434343434343434',
    gas: 146999999
  },
  {
    to: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    data: '0x70a08231000000000000000000000000abcabcabcabcabcabcabcabcabcabcabcabcabca',
    gas: 146999999
  }
]

Multicall request execution

const res = await multicallService.multicall(batch);

The response array denotes the balances of the users specified in the request batch.

[
  '0x000000000000000000000000000000000000000000000000000000000002528f',
  '0x0000000000000000000000000000000000000000000000000000000000000000',
  '0x0000000000000000000000000000000000000000000000000000000000000000'
]

utils-blockchain's People

Contributors

maxcojocari avatar

Stargazers

 avatar NiceArti avatar Mihail avatar Alex Cryp avatar

Watchers

Alex Cryp avatar Radu Nita 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.