Multicall

Multicall simply aggregates multiple contract calls into a single JSON RPC request.

Overview

In order to extract a large amount of data in an efficient way we have used Uniswap Multicall Contract for all JSON RPC requests in adapters.

We have coded a JS wrapper over the contracts and placed it in the abi module of our adapter SDK @spockanalytics/base. You can use it for contract calls in your project adapter.

Multicall.singleContractMultipleData

Execute multiple inputs for a single contract.

Usage

abi.Multicall.singleContractMultipleData<Erc20>({
    address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
    chain: 1,
    contractInterface: Erc20_factory.createInterface(),
    fragment: 'balanceOf',
    callInput: ['0xF04a5cC80B1E94C69B48f5ee68a08CD2F09A7c3E','0x2F0b23f53734252Bda2277357e97e1517d6B042A']
});

Multicall.multipleContractMultipleData

Execute multiple inputs for multiple contracts.

Usage

abi.Multicall.multipleContractMultipleData<Erc20>({
    address: ['0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2','0x37c997b35c619c21323f3518b9357914e8b99525'],
    chain: 1,
    contractInterface: Erc20_factory.createInterface(),
    fragment: 'balanceOf',
    callInput: ['0xF04a5cC80B1E94C69B48f5ee68a08CD2F09A7c3E','0x2F0b23f53734252Bda2277357e97e1517d6B042A']
})

Multicall.multipleContractSingleData

Execute single input for multiple contracts.

Usage

abi.Multicall.multipleContractSingleData<Erc20>({
    address: ['0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2','0x37c997b35c619c21323f3518b9357914e8b99525'],
    chain: 1,
    contractInterface: Erc20_factory.createInterface(),
    fragment: 'balanceOf',
    callInput: ['0xF04a5cC80B1E94C69B48f5ee68a08CD2F09A7c3E']
})

Multicall.execute

Execute multiple calls in one request.

Usage

const WETH = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
const contractInterface = Erc20_factory.createInterface();

const calls = [
  new abi.Call<Erc20>({address:WETH, contractInterface, fragment:"name"}),
  new abi.Call<Erc20>({address:WETH, contractInterface, fragment:"symbol"}),
  new abi.Call<Erc20>({address:WETH, contractInterface, fragment:"decimals"})
];

abi.Multicall.execute({calls, chain:1});

Call

Execute

Call Params

import { BigNumber } from '@ethersproject/bignumber';

type MethodArg = string | number | BigNumber;
type OptionalMethodInputs =
  | Array<MethodArg | MethodArg[] | undefined>
  | undefined;

Call Result

The return type of each method.

//each method return an array of CallResult promise
// Promise<Array<CallResult>>

interface CallResult {
  call: {
    address: string;
    input: OptionalMethodInputs;
  };
  output: any;
  success: boolean;
}

Last updated