npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utils-blockchain

v1.0.1

Published

The TypeScript utility package designed for solving different problems in dApp development.

Downloads

8

Readme

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

:warning: 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'
]