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

multicall-provider

v0.1.23

Published

Save in your infura bills, aggreate multiple transactions in a single rpc call, with no extra effort

Downloads

51

Readme

multicall-provider

Save in your infura bills, aggreate multiple transactions in a single rpc call, with no extra effort

Inspired by 0xsequence/multicall, It works by wrapping an ethers provider and overrides the call method to aggregate supported transactions into a single multicall3 call

npm install multicall-provider

yarn add multicall-provider

pnpm add multicall-provider

Usage

  • It implements a buffer with a configurable 50ms delay and aggregates all operations received within that window
  • Calls targeting different block heights (blockTags) are aggregated based on the blockTag
  • Transactions including from, value or gasPrice skip aggregation and are forwarded to the underlying provider
const provider = multicallProvider(new providers.JsonRpcProvider(...), {
  batchSize: 25, // max amount of transactions per multicall call
  timeWindow: 0, // time in ms to wait for new transactions before sending (0 still gets all from current event loop which is probably enough on most cases)
  multicall3: '' // multicall3 contract address, only the aggregate3 method is used
})

keep in mind it works as long as there are no await's between calls

// calls won't be aggregated
const daiDecimals = await dai.decimals()
const daiBalance = await dai.balanceOf('0x507f0daa42b215273b8a063b092ff3b6d27767af')

// will be aggreated into a single rpc call
const [daiDecimals, daiBalance] = await Promise.all([
  dai.decimals(),
  dai.balanceOf('0x507f0daa42b215273b8a063b092ff3b6d27767af'),
])

// this way works too
const daiDecimals = dai.decimals()
const daiBalance = dai.balanceOf('0x507f0daa42b215273b8a063b092ff3b6d27767af')

const balance = await balancePromise
const supply = await supplyPromise

Usage with Wagmi

A util to wrap your wagmi provider with multicall is under multicall-provider/wagmi as follows

import { configureChains, createClient, mainnet, WagmiConfig } from 'wagmi'
import { publicProvider } from '@wagmi/core/providers/public'
import { MetaMaskConnector } from '@wagmi/core/connectors/metaMask'

import { multicallProvider } from 'multicall-provider/wagmi'

const { chains, provider } = configureChains([mainnet], [publicProvider()])

const client = createClient({
  provider: multicallProvider(provider),
  connectors: [new MetaMaskConnector({ chains })],
})

All useContractReads will be aggregated, but useContractWrites won't

Why

Imagine you're building an app like

const App = () => {
  return (
    <>
      <UserBalance token="dai" />
      <UserBalance token="usdc" />
      <UserBalance token="usdt" />
    </>
  )
}

Inside each <UserBalance/> you have a useContractRead to fetch the balance of the token.

This app would start by making 3 different rpc calls, you may see how this grows depending on your app.

You could useContractReads on the parent and pass the balances down.

but what if you also need to use the Dai balance in another place way down the tree?
I mean the closer to the component using the data the better

wrapping your wagmi provider with multicallProvider, you don't need to worry about that, need to use the Dai balance down the tree? wagmi will have it cached for you already, because of the first useContractRead, gg

Potential Issues

batchSize: eth_call has a timeout restriction at node level, if it fails with a node error, consider lowering your batch size, 50 should be fine tho