Lightweight, Modern Multicall3 Typescript Library

Overview

pilum • tests publish License Package Version

Lightweight, Modern Multicall3 Typescript Library published on npm.

Multicall3 flexibly aggregates results from multiple smart contract function calls. By allowing the caller to specify an additional parameter per call (a boolean for if the call should be required to succeed), the call results can be aggregated gracefully on a per-call basis.

Since calls are bundled as a single JSON-RPC request to the Multicall3 contract, this dramatically reduces the load on RPC servers, especially important for remote hosts like Infura and Alchemy. Additionally, packing calls in one request causes the calls to be executed in the same block, with the block number returned so potential responses from outdated RPC nodes may be ignored.

By default, the deployed Multicall3 contract is used. This can be overridden, see Custom-Multicall-Contracts. But, Multicall3 is highly recommendeded (more details below).

Multicall aggregators and executors are statically overloaded to allow for zero-config execution. Meaning, you can execute calls using Multicall with zero overhead except your calls. Although this is not recommended due to its rpc-unreliability, we demonstrate it here.

To further explore using pilum, please check out our Usage docs or dive right into our examples.

Installation

npm install pilum
# OR
yarn add pilum

Usage

Importing in ES6 Javascript / TypeScript:

import { Multicall } from 'pilum';

Since pilum makes an RPC call to a Multicall contract, we need to use a provider. It's highly recommended that you specify a provider, rather than rely on ethers's default provider (which is what pilum uses under the hood for the zero config execution).

Recommended Usage (using a custom provider)

import { ethers } from 'ethers';
import { Multicall, ContractCall } from 'pilum';

// Create a custom provider
const provider = new ethers.providers.JsonRpcProvider(
  "https://mainnet.infura.io/v3/<YOUR-INFURA-API-KEY>" // Example RPC URL
);

// Craft the Multicall Instance
const multicall = new Multicall({
  provider: provider
});

// Define our calls
const calls: ContractCall[] = [
  {
    reference: 'blockNumCall',
    address: '0xcA11bde05977b3631167028862bE2a173976CA11',
    abi: [ { name: 'getBlockNumber', type: 'function', stateMutability: 'view', inputs: [], outputs: [ { name: 'blockNumber', type: 'uint256' }] } ],
    method: 'getBlockNumber',
    params: [],
    value: 0,
  }
];

// Call the Multicall associated functions directly
const { results } = await Multicall.call(calls);

// Print the call result
console.log(results);

Zero Config Execution

Not Recommended (due to RPC unreliability).

import { Multicall, ContractCall } from 'pilum';

// Define our calls
const calls: ContractCall[] = [
  {
    reference: 'blockNumCall',
    address: '0xcA11bde05977b3631167028862bE2a173976CA11',
    abi: [ { name: 'getBlockNumber', type: 'function', stateMutability: 'view', inputs: [], outputs: [ { name: 'blockNumber', type: 'uint256' }] } ],
    method: 'getBlockNumber',
    params: [],
    value: 0,
  }
];

// Call the Multicall associated functions directly with zero overhead
const { results } = await Multicall.call(calls);

// Print the call result
console.log(results);

Multicall Options

Let's say you instantiated a multicall instance in your code, but later on want to multicall on a different network or to a different multicall address, without instantiating a new multicall instance.

pilum provides a flexible way to specify these parameters when executing a call. This is done by adding an optional options parameter to both the static (zero-config) and class-level call method.

To specify these parameters, execute a call with an additional object as the last parameter.

const { results } = await Multicall.call(calls, {
  provider: provider,
  abi: <YOUR_MULTICALL_ABI_OBJECT>,
  address: '<YOUR-MULTICALL-ADDRESS>',
  network: 5, // Any network id supported by ethers, here 5 is the Ethereum Goerli Testnet
});

Custom Multicall Contracts

By default, the deployed Multicall3 contract is used. This can be overridden by specifying the address parameter in the constructor like so:

const multicall = new Multicall({
  address: '0xcA11bde05977b3631167028862bE2a173976CA11',
});

But, Multicall3 is highly recommendeded. It's ABI is backwards compatible with Multicall and Multicall2, but it's cheaper to use (so you can fit more calls into a single request), and it adds an aggregate3 method so you can specify whether calls are allowed to fail on a per-call basis. Additionally, it's deployed on every network at the same address.

Alternate Networks

By default, pilum uses Ethereum mainnet. You can specify a different network by passing a network parameter (the chain(network) id) to the constructor like so:

const multicall = new Multicall({
  network: 4,
});

Multicall, Multicall2, and Multicall3 deployments are supported across all networks listed in the Multicall3 repository.

Examples

There are numerous examples demonstrating how to use pilum's Multicall in the examples directory.

Development

Any and all pull requests are welcome and highly appreciated!

Repository Blueprint

examples
├─ uniquery — Minimal Queries for Uniswap pool statistics
├─ compquery — Multicalled Compound Protocol Math Queries
src
├─ abis/Multicall Contract ABIs
├─ models/Multicall Class Argument and Response Types
├─ networks/Network Configurations and their respective Multicall Contract Deployment Addresses
├─ index.ts"Package Re-exports"
├─ Multicall.ts"The Multicall Contract"
tests
└─ ...

License

AGPL-3.0-only

Acknowledgements

Disclaimer

This library is provided as is. No guarantee, representation or warranty is being made, express or implied, as to the safety or correctness of the user interface or library code. None of the content present here has been audited and as such there can be no assurance it will work as intended, and users may experience delays, failures, errors, omissions, loss of transmitted information or loss of funds. The creators are not liable for any of the foregoing. Users should proceed with caution and use at their own risk.

You might also like...

Create HTML from CSS! A modern javascript library you'd expect Facebook to invent.

Create HTML from CSS! A modern javascript library you'd expect Facebook to invent.

Create HTML from CSS! A modern javascript library you'd expect Facebook to invent. Slowly build modern websites without ever leaving your CSS, with HeadwindHTML

Dec 30, 2022

A modern lazy loading library for images.

Layzr.js A modern lazy loading library for images. Demo Page Getting Started Follow these steps: Install Setup Images Instantiate Review Options Revie

Dec 25, 2022

A tiny, lightning fast jQuery-like library for modern browsers.

Sprint.js Sprint is a high-performance, 5KB (gzipped) DOM library for modern browsers. Sprint notably shines on bandwidth and resource constrained dev

Jan 3, 2023

Zepto.js is a minimalist JavaScript library for modern browsers, with a jQuery-compatible API

Zepto.js – a minimalist JavaScript library Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you u

Dec 31, 2022

Flexible, powerful and modern library for creating the running horizontal blocks effect, also known as ticker or the «marquee effect».

Flexible, powerful and modern library for creating the running horizontal blocks effect, also known as ticker or the «marquee effect».

Cuberto Reeller Flexible, powerful and modern library for creating the running horizontal blocks effect, also known as ticker or the «marquee effect».

Jan 4, 2023

Simple modern JavaScript ES6 library that fetches JSON data into an HTML table which displays nicely within a Bootstrap 4 Card.

Simple modern JavaScript ES6 library that fetches JSON data into an HTML table which displays nicely within a Bootstrap 4 Card. Uses simplenotsimpler/modern-table library.

Feb 17, 2022

Node.js library for the Modern Treasury API.

ModernTreasury Node API Library The ModernTreasury Node library provides convenient access to the ModernTreasury REST API from applications written in

Nov 4, 2022

A modern JavaScript library for vanilla spreadsheets

Spreadsheet.js Landing page: https://spread-sheet.herokuapp.com/index.html Documentation: https://spread-sheet.herokuapp.com/documentation.html Gettin

Feb 23, 2022

A customizable lightweight Alert Library with Material UI and awesome features.

A customizable lightweight Alert Library with Material UI and awesome features.

SoloAlert A customizable lightweight Alert Library with Material UI and awesome features. view soloalert on npm : https://www.npmjs.com/soloalert Inst

Nov 30, 2022
Comments
  • [FEAT] Extensible Options

    [FEAT] Extensible Options

    Overview

    Zero-config, and stateful, calls should have extensible options that are checked gracefully to allow for specification of multicall address, network, multicall abi, etc, etc.

    enhancement 
    opened by refcell 0
  • [FIX] Better Return Type Encapsulation

    [FIX] Better Return Type Encapsulation

    Overview

    Return types defined in src/models/ are incohesive and poorly adapt to the varying Multicall aggregation method versions (aggregate, tryAggregate, aggregate3).

    Specification

    • [ ] Coalesce return types from the Multicall class aggregation methods into a standard type with optional values (eg blockNumber)
    invalid 
    opened by refcell 0
  • [FEAT] Call Caching

    [FEAT] Call Caching

    Specification

    An instance of the Multicall class should cache calls by block number and short circuit if the same call is made within the same block.

    Caching should be configurable by the client since there would be a tradeoff if the multicall executes in a different block height than the previous cache (ie we check block number, and it increments between then and multicall execution, we'd have to execute the multicall again with the previously omitted cached call, 2x'ing the delay in this worst-case scenario). NOTE: realistically this wouldn't happen since blocks take ~ 10-15 seconds on Ethereum and the time delay between block # query and multicall execution would be signifcantly shorter.

    enhancement 
    opened by refcell 0
  • [FEAT] Extend Calls to Specify Network

    [FEAT] Extend Calls to Specify Network

    Specification

    Network should be allowed to be specified on an individual call granularity. The Multicall class should be able to flexibly group calls by network and execute each of those bundles as individual multicalls, and return the unbundled results.

    enhancement 
    opened by refcell 0
Releases(v3.0.0)
A lightweight, modern and customizable JavaScript slider library.

NSlider NSlider is a lightweight (< 10 KB), modern and customizable JavaScript slider library. CDN Development https://cdn.jsdelivr.net/gh/fatihege/ns

Fatih 6 Jan 20, 2022
EggyJS is a Javascript micro Library for simple, lightweight toast popups focused on being dependency-less, lightweight, quick and efficient.

EggyJS EggyJS is a Javascript micro Library for simple, lightweight toast popups. The goal of this library was to create something that meets the foll

Sam 10 Jan 8, 2023
A lightweight, fully-featured, modular, typescript-compatible javascript library for Paymongo.

paymongo.js A lightweight, fully-featured, modular, typescript-compatible javascript library for PayMongo. Installation npm install paymongo.js # or y

Prince Carlo Juguilon 15 Nov 23, 2022
A fully type-safe and lightweight internationalization library for all your TypeScript and JavaScript projects.

?? typesafe-i18n A fully type-safe and lightweight internationalization library for all your TypeScript and JavaScript projects. Advantages ?? lightwe

Hofer Ivan 1.3k Jan 4, 2023
A lightweight Adobe Photoshop .psd/.psb file parser in typescript with zero-dependency for WebBrowser and NodeJS

@webtoon/psd A lightweight Adobe Photoshop .psd/.psb file parser in typescript with zero-dependency for WebBrowser and NodeJS Browser Support Chrome F

null 830 Jan 1, 2023
Simple, lightweight at-runtime type checking functions, with full TypeScript support

pheno Simple, lightweight at-runtime type checking functions, with full TypeScript support Features Full TypeScript integration: TypeScript understand

Lily Scott 127 Sep 5, 2022
📬 Lightweight Typescript-first framework built on top of Express

?? abstain Lightweight Typescript-first framework built on top of Express [WIP] ?? api // index.ts import { Application } from '@pinkcig/abstain'; imp

Faye Keller 5 May 26, 2022
A clean and lightweight TypeScript Next.js Starter powered by the tools (nearly) everybody use

This is a template for a Next.js project based on create-next-app Report Bug · Request Feature ⚡ Quickstart You can click on the button Use this templ

José Manuel Lucas 8 Nov 14, 2022
A lightweight "Falling Sand Game" written in TypeScript

TechSquid Falling Sand Game | TSFSG A lightweight "Falling Sand Game" written in TypeScript with Pixi.js. Click to spawn particles of sand. TO DO: col

Kyle T 8 Aug 15, 2022
An authorization library that supports access control models like ACL, RBAC, ABAC in modern JavaScript platforms

Casbin-Core ?? Looking for an open-source identity and access management solution like Okta, Auth0, Keycloak ? Learn more about: Casdoor News: still w

Casbin 6 Oct 20, 2022