npm package: @mrtruongleo/cab-multicall
Description: Multicall allows multiple smart contract constant function calls to be grouped into a single call and the results aggregated into a single result
Latest version: 2.23.3

# cab-multicall

Forked from ethereum-multicall.
cab-multicall is a lightweight library for interacting with the [`Multicall3`] smart contract.

Multicall allows multiple smart contract constant function calls to be grouped into a single call and the results aggregated into a single result. This reduces the number of separate JSON RPC requests that need to be sent over the network if using a remote node like Infura, and provides the guarantee that all values returned are from the same block. The latest block number is also returned along with the aggregated results.

cab-multicall is fully written in typescript so has full compile time support. The motivation of this package was to expose a super simple and easy to understand interface for you to take the full benefits of the multicalls. Also to not being opinionated on how you use it, you can use it with web3, ethers or even pass in a custom nodeUrl and we do it for you. This package takes care of the decoding for you but at the same time if you dont want it to you can turn that part off.

## Supports

The below networks are supported by default, and custom networks can be supported by providing your own instance of a deployed Multicall contract.
`
| Chain | Chain ID |
Mainnet = 1,
Kovan = 42,
Rinkeby = 4,
Görli = 5,
Ropsten = 3,
Sepolia = 11155111,
Holesky = 17000,
StoryIliadTestnet = 1513,
XterioChain = 112358,
XterioTestnet = 1637450,
Optimism = 10,
OptimismKovan = 69,
OptimismGörli = 420,
OptimismSepolia = 11155420,
Arbitrum = 42161,
ArbitrumNova = 42170,
ArbitrumGörli = 421613,
ArbitrumSepolia = 421614,
ArbitrumRinkeby = 421611,
StylusTestnet = 23011913,
Polygon = 137,
Mumbai = 80001,
Amoy = 80002,
PolygonzkEVM = 1101,
PolygonzkEVMTestnet = 1442,
CardonazkEVMTestnet = 2442,
GnosisChainxDai = 100,
ChiadoGnosisChainTestnet = 10200,
Avalanche = 43114,
AvalancheFuji = 43113,
FantomTestnet = 4002,
FantomOpera = 250,
FantomSonic = 64240,
BNBSmartChain = 56,
BNBSmartChainTestnet = 97,
opBNBTestnet = 5611,
opBNBMainnet = 204,
Moonbeam = 1284,
Moonriver = 1285,
MoonbaseAlphaTestnet = 1287,
Palm = 11297108109,
PalmTestnet = 11297108099,
Harmony = 1666600000,
Cronos = 25,
CronosTestnet = 338,
ReyaCronos = 89346162,
ReyaNetwork = 1729,
Fuse = 122,
FlareMainnet = 14,
SongbirdCanaryNetwork = 19,
CostonTestnet = 16,
Coston2Testnet = 114,
Boba = 288,
Aurora = 1313161554,
Astar = 592,
AstarzKyotoTestnet = 6038361,
AstarzkEVM = 3776,
OKC = 66,
HecoChain = 128,
MetisAndromeda = 1088,
MetisGoerli = 599,
MetisSepolia = 59902,
OverProtocol = 54176,
OverProtocolDolphinTestnet = 541764,
RSK = 30,
RSKTestnet = 31,
Evmos = 9001,
EvmosTestnet = 9000,
Thundercore = 108,
ThundercoreTestnet = 18,
Oasis = 42262,
OasisSapphire = 23294,
Celo = 42220,
CeloAlfajoresTestnet = 44787,
Godwoken = 71402,
GodwokenTestnet = 71401,
Klaytn = 8217,
KlaytnTestnetBaobab = 1001,
Milkomeda = 2001,
KCC = 321,
Velas = 106,
Telos = 40,
StepNetwork = 1234,
Canto = 7700,
CantoTestnet = 7701,
Iotex = 4689,
Bitgert = 32520,
Kava = 2222,
MantleSepoliaTestnet = 5003,
MantleTestnet = 5001,
Mantle = 5000,
ShardeumSphinx = 8082,
BaseTestnetGoerli = 84531,
BaseTestnetSepolia = 84532,
Base = 8453,
KromaTestnetSepolia = 2358,
Kroma = 255,
DeFiChainEVMMainnet = 1130,
DeFiChainEVMTestnet = 1131,
DefiOracleMetaMainnet = 138,
DFKChainTest = 335,
DFKChain = 53935,
NeonEVMDevNet = 245022926,
LineaSepoliaTestnet = 59141,
LineaGoerliTestnet = 59140,
LineaMainnet = 59144,
Hashbit = 11119,
Syscoin = 57,
SyscoinRolluxMainnet = 570,
SyscoinTannebaumTestnet = 5700,
SyscoinTannebaumRollux = 57000,
Pulsechainv4Testnet = 943,
PulsechainMainnet = 369,
ZoraGoerliTestnet = 999,
Zora = 7777777,
ZoraSepoliaTestnet = 999999999,
DarwiniaCrabNetwork = 44,
DarwiniaNetwork = 46,
ChainVerseMainnet = 5555,
ScrollAlphaTestnet = 534353,
ScrollSepoliaTestnet = 534351,
Scroll = 534352,
Xodex = 2415,
EOSEVMTestnet = 15557,
EOSEVM = 17777,
Crossbell = 3737,
Dogechain = 2000,
MEVerseChainTestnet = 4759,
MEVerseChainMainnet = 7518,
SKALECalypsoTestnet = 974399131,
SKALEEuropaTestnet = 1444673419,
SKALENebulaTestnet = 37084624,
SKALETitanTestnet = 1020352220,
SKALECalypsoHub = 1564830818,
SKALEEuropaLiquidityHub = 2046399126,
SKALENebulaGamingHub = 1482601649,
SKALETitanAIHub = 1350216234,
RoninSaigonTestnet = 2021,
RoninMainnet = 2020,
QitmeerTestnet = 8131,
QitmeerQNGMainnet = 813,
QTestnet = 35443,
QDevnet = 35442,
QMainnet = 35441,
NeonMainnet = 245022934,
LUKSOTestnet = 4201,
LUKSOMainnet = 42,
EdgewareEdgeEVM = 2021,
MeterTestnet = 83,
Meter = 82,
SepoliaPGNPublicGoodsNetworkTestnet = 58008,
PGNPublicGoodsNetwork = 424,
ShimmerEVM = 148,
HighburyEVM = 710,
ArtheraTestnet = 10243,
ArtheraMainnet = 10242,
MantaPacificMainnet = 169,
JolnirTaikoTestnet = 167007,
KatlaTaikoA6Testnet = 167008,
FilecoinMainnet = 314,
FilecoinCalibrationTestnet = 314159,
Fusion = 32659,
FusionTestnet = 46688,
XaiTestnet = 47279324479,
JFINChain = 3501,
JFINChainTestnet = 3502,
ChilizChain = 88888,
ChilizSpicyTestnet = 88882,
CORE = 1116,
CoreTestnet = 1115,
EthereumClassic = 61,
FrameTestnet = 68840142,
EtherlinkMainnet = 42793,
EtherlinkTestnet = 128123,
ZetaChainAthens3Testnet = 7001,
ZetaChain = 7000,
X1Testnet = 195,
LumiterraLayer3 = 94168,
BitTorrentChainMainnet = 199,
BTTChainTestnet = 1029,
CallistoMainnet = 820,
AreonNetworkTestnet = 462,
AreonNetworkMainnet = 463,
zkFairMainnet = 42766,
ModeMainnet = 34443,
BlastSepolia = 168587773,
Blast = 81457,
Xai = 660279,
DOSChain = 7979,
DOSChainTestnet = 3939,
Tron = 728126428,
zkSyncEra = 324,
zkSyncEraGoerliTestnet = 280,
zkSyncEraSepoliaTestnet = 300,
PlayFiAlbireoTestnet = 1612127,
AbstractTestnet = 11124,
FraxtalMainnet = 252,
FraxtalHoleskyTestnet = 2522,
OmaxMainnet = 311,
SyndicateFrameChain = 5101,
DelaSepolia = 9393,
NeoXTestnet = 12227330,
SankoMainnet = 1996,
SankoTestnet = 1992,
BerachainTestnetArtio = 80085,
Shibarium = 109,
ImmutablezkEVMMainnet = 13371,
ImmutablezkEVMTestnet = 13473,
RSS3VSLMainnet = 12553,
RSS3VSLSepoliaTestnet = 2331,
MorphSepoliaTestnet = 2710,
JIBCHAINL1 = 8899,
HaqqMainnet = 11235,
ZircuitSepoliaTestnet = 48899,
reAl = 111188,
MerlinTestnet = 686868,
IOTAEVM = 8822,
Planq = 7070,
CyberTestnet = 111557560,
UnitZeroMainnet = 88811,
UnitZeroStagenet = 88819,
UnitZeroTestnet = 88817,
SeiEVMDevnet = 713715,
SeiEVMMainnet = 1329,
HeklaTaikoA7Testnet = 167009,
TaikoMainnet = 167000,
CyberMainnet = 7560,
DreyerXMainnet = 23451,
SaharaTestnet = 313313,
BOXChain = 42299,
OXChain = 6699,
ConfluxEspace = 1030,
BEVMTestnet = 11503,
AuraMainnet = 6322,
SuperpositionTestnet = 98985,
XLayerMainnet = 196,
Nahmii3Mainnet = 4061,
Nahmii3Testnet = 4062,
PlumeTestnet = 161221135,
PlumeTestnetv2 = 98864,
PlumeMainnet = 98865,
AlgenL1 = 8911,
BitlayerMainnet = 200901,
LiskMainnet = 1135,
GravityAlphaMainnet = 1625,
Yominet = 5264468217,
Bob = 60808,
Superseed = 53302,
Rupaya = 499,
FluenceTestnet = 52164803,
FluenceStage = 123420000220,
Fluence = 9999999,
CampTestnetV2 = 325000,
OntologyTestnet = 5851,
OntologyMainnet = 58,
VictionTestnet = 89,
VictionMainnet = 88,
WorldChain = 480,
FlowMainnet = 747,
FlowTestnet = 545,
ConfluxCoreSpaceMainnet = 1029,
Superposition = 55244,
StarchainTestnet = 1570,
StarchainMainnet = 1578,
ApeChainMainnet = 33139,
WEMIX30Mainnet = 1111,
AlephZeroEVMMainnet = 41455,
EDUChainTestnet = 656476,
FormTestnet = 132902,
sapphire = 23294,
shibarium = 109,
etherlite = 111,
starknet = 300,
starknetTestnet = 301,

## Installation

### npm:

```js
$ npm install @mrtruongleo/cab-multicall
```

## Usage

### Overloaded methods

As the [official docs mentions here](https://docs.ethers.io/v3/api-contract.html#prototype):

> Due to signature overloading, multiple functions can have the same name. The first function specified in the ABI will be bound to its name. To access overloaded functions, use the full typed signature of the functions (e.g. contract["foobar(address,uint256)"]).

So, when creating the contract call context, under the calls array property we should have that in mind and use the method signature rather than the method name. E.g.

```js
const contractCallContext: ContractCallContext = {
  reference: 'upV2Controller',
  contractAddress: '0x19891DdF6F393C02E484D7a942d4BF8C0dB1d001',
  abi: [
    {
      inputs: [],
      name: 'getVirtualPrice',
      outputs: [
        {
          internalType: 'uint256',
          name: '',
          type: 'uint256',
        },
      ],
      stateMutability: 'view',
      type: 'function',
    },
    {
      inputs: [
        {
          internalType: 'uint256',
          name: 'sentValue',
          type: 'uint256',
        },
      ],
      name: 'getVirtualPrice',
      outputs: [
        {
          internalType: 'uint256',
          name: '',
          type: 'uint256',
        },
      ],
      stateMutability: 'view',
      type: 'function',
    },
  ],
  calls: [
    {
      reference: 'getVirtualPriceWithInput',
      methodName: 'getVirtualPrice(uint256)',
      methodParameters: ['0xFFFFFFFFFFFFF'],
    },
    {
      reference: 'getVirtualPriceWithoutInput',
      methodName: 'getVirtualPrice()',
      methodParameters: [],
    },
  ],
};
```

### Import examples:

### JavaScript (ES3)

```js
var ethereumMulticall = require('cab-multicall');
```

### JavaScript (ES5 or ES6)

```js
const ethereumMulticall = require('cab-multicall');
```

### JavaScript (ES6) / TypeScript

```js
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';
```

### ethers usage example

```ts
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';
import { ethers } from 'ethers';

let provider = ethers.getDefaultProvider();

// you can use any ethers provider context here this example is
// just shows passing in a default provider, ethers hold providers in
// other context like wallet, signer etc all can be passed in as well.
const multicall = new Multicall({ ethersProvider: provider, tryAggregate: true });

const contractCallContext: ContractCallContext[] = [
    {
        reference: 'testContract',
        contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
        abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
        calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
    },
    {
        reference: 'testContract2',
        contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
        abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256', name: "path", "type": "address[]" }] } ],
        calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
    }
];

const results: ContractCallResults = await multicall.call(contractCallContext);
console.log(results);

// results:
{
  results: {
      testContract: {
          originalContractCallContext:  {
            reference: 'testContract',
            contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
            abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
            calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: BigNumber }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      },
      testContract2: {
          originalContractCallContext:  {
            reference: 'testContract2',
            contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
            abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256[]' ] } ],
            calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: [BigNumber, BigNumber, BigNumber] }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      }
  },
  blockNumber: 10994677
}
```

### web3 usage example

```ts
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';
import Web3 from 'web3';

const web3 = new Web3('https://some.local-or-remote.node:8546');

const multicall = new Multicall({ web3Instance: web3, tryAggregate: true });

const contractCallContext: ContractCallContext[] = [
    {
        reference: 'testContract',
        contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
        abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
        calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
    },
    {
        reference: 'testContract2',
        contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
        abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256', name: "path", "type": "address[]" }] } ],
        calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
    }
];

const results: ContractCallResults = await multicall.call(contractCallContext);
console.log(results);

// results:
{
  results: {
      testContract: {
          originalContractCallContext:  {
            reference: 'testContract',
            contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
            abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
            calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: BigNumber }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      },
      testContract2: {
          originalContractCallContext:  {
            reference: 'testContract2',
            contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
            abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256[]' ] } ],
            calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: [BigNumber, BigNumber, BigNumber] }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      }
  },
  blockNumber: 10994677
}
```

### specify call block number

The multicall instance call method has an optional second argument of type [ContractCallOptions](src/models/contract-call-options.ts).

One of the options is the blockNumber, so you can choose the height where you want the data from.

It is compatible with both ethers and web3 providers.

```ts
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';
import Web3 from 'web3';

const web3 = new Web3('https://some.local-or-remote.node:8546');

const multicall = new Multicall({ web3Instance: web3, tryAggregate: true });

const contractCallContext: ContractCallContext[] = [
    {
        reference: 'testContract',
        contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
        abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
        calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
    }
];

const results: ContractCallResults = await multicall.call(contractCallContext,{
    blockNumber: '14571050'
});
console.log(results);

// results: it will have the same block as requested
{
  results: {
      testContract: {
          originalContractCallContext:  {
            reference: 'testContract',
            contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
            abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
            calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: BigNumber }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      },
  },
  blockNumber: 14571050
}
```

### passing extra context to the call

If you want to store any context or state so you don't need to look back over arrays once you got the result back. it can be stored in `context` within `ContractCallContext`.

```ts
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';
import { ethers } from 'ethers';

let provider = ethers.getDefaultProvider();

// you can use any ethers provider context here this example is
// just shows passing in a default provider, ethers hold providers in
// other context like wallet, signer etc all can be passed in as well.
const multicall = new Multicall({ ethersProvider: provider, tryAggregate: true });

// this is showing you having the same context for all `ContractCallContext` but you can also make this have
// different context for each `ContractCallContext`, as `ContractCallContext<TContext>` takes generic `TContext`.
const contractCallContext: ContractCallContext<{extraContext: string, foo4: boolean}>[] = [
    {
        reference: 'testContract',
        contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
        abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
        calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }],
        // pass it in here!
        context: {
          extraContext: 'extraContext',
          foo4: true
        }
    },
    {
        reference: 'testContract2',
        contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
        abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256', name: "path", "type": "address[]" }] } ],
        calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }],
         // pass it in here!
        context: {
          extraContext: 'extraContext2',
          foo4: false
        }
    }
];

const results: ContractCallResults = await multicall.call(contractCallContext);
console.log(results);

// results:
{
  results: {
      testContract: {
          originalContractCallContext:  {
            reference: 'testContract',
            contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
            abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
            calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }],
            context: {
                extraContext: 'extraContext',
                foo4: true
            }
          },
          callsReturnContext: [{
              returnValues: [{ amounts: BigNumber }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      },
      testContract2: {
          originalContractCallContext:  {
            reference: 'testContract2',
            contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
            abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256[]' ] } ],
            calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }],
            context: {
                extraContext: 'extraContext2',
                foo4: false
            }
          },
          callsReturnContext: [{
              returnValues: [{ amounts: [BigNumber, BigNumber, BigNumber] }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      }
  },
  blockNumber: 10994677
}
```

### try aggregate

By default if you dont turn `tryAggregate` to true if 1 `eth_call` fails in your multicall the whole result will throw. If you turn `tryAggregate` to true it means if 1 of your `eth_call` methods fail it still return you the rest of the results. It will still be in the same order as you expect but you have a `success` boolean to check if it passed or failed. Keep in mind that if using a custom multicall contract deployment, Multicall version 1's will not work. Use a Multicall2 deployment (contract can be found [here](https://github.com/makerdao/multicall/blob/master/src/Multicall2.sol)).

```ts
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';

const multicall = new Multicall({ nodeUrl: 'https://some.local-or-remote.node:8546', tryAggregate: true });

const contractCallContext: ContractCallContext[] = [
    {
        reference: 'testContract',
        contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
        abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] }, { name: 'foo_fail', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
        calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }, { reference: 'fooCall_fail', methodName: 'foo_fail', methodParameters: [42] }]
    },
    {
        reference: 'testContract2',
        contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
        abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256', name: "path", "type": "address[]" }] } ],
        calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
    }
];

const results: ContractCallResults = await multicall.call(contractCallContext);
console.log(results);

// results:
{
  results: {
      testContract: {
          originalContractCallContext:  {
            reference: 'testContract',
            contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
            abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] }, { name: 'foo_fail', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
             calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }, { reference: 'fooCall_fail', methodName: 'foo_fail', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: BigNumber }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          },
          {
              returnValues: [],
              decoded: false,
              reference: 'fooCall_fail',
              methodName: 'foo_fail',
              methodParameters: [42],
              success: false
          }]
      },
      testContract2: {
          originalContractCallContext:  {
            reference: 'testContract2',
            contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
            abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256[]' ] } ],
            calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: [BigNumber, BigNumber, BigNumber] }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      }
  },
  blockNumber: 10994677
}
```

### custom jsonrpc provider usage example

```ts
import {
  Multicall,
  ContractCallResults,
  ContractCallContext,
} from 'cab-multicall';

const multicall = new Multicall({ nodeUrl: 'https://some.local-or-remote.node:8546', tryAggregate: true });

const contractCallContext: ContractCallContext[] = [
    {
        reference: 'testContract',
        contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
        abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
        calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
    },
    {
        reference: 'testContract2',
        contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
        abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256', name: "path", "type": "address[]" }] } ],
        calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
    }
];

const results: ContractCallResults = await multicall.call(contractCallContext);
console.log(results);

// results:
{
  results: {
      testContract: {
          originalContractCallContext:  {
            reference: 'testContract',
            contractAddress: '0x6795b15f3b16Cf8fB3E56499bbC07F6261e9b0C3',
            abi: [ { name: 'foo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256' }] } ],
            calls: [{ reference: 'fooCall', methodName: 'foo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: BigNumber }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      },
      testContract2: {
          originalContractCallContext:  {
            reference: 'testContract2',
            contractAddress: '0x66BF8e2E890eA0392e158e77C6381b34E0771318',
            abi: [ { name: 'fooTwo', type: 'function', inputs: [ { name: 'example', type: 'uint256' } ], outputs: [ { name: 'amounts', type: 'uint256[]' ] } ],
            calls: [{ reference: 'fooTwoCall', methodName: 'fooTwo', methodParameters: [42] }]
          },
          callsReturnContext: [{
              returnValues: [{ amounts: [BigNumber, BigNumber, BigNumber] }],
              decoded: true,
              reference: 'fooCall',
              methodName: 'foo',
              methodParameters: [42],
              success: true
          }]
      }
  },
  blockNumber: 10994677
}
```

### Multicall contracts

by default it looks at your network from the provider you passed in and makes the contract address to the known multicall contract addresses `0xcA11bde05977b3631167028862bE2a173976CA11` this is deployed on every single network but `etherlite` which uses `0x21681750D7ddCB8d1240eD47338dC984f94AF2aC`.

````ts

If you wanted this to point at a different multicall contract address just pass that in the options when creating the multicall instance, example:

```ts
const multicall = new Multicall({
  multicallCustomContractAddress: '0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696',
  // your rest of your config depending on the provider your using.
});
````

## Issues

Please raise any issues in the below link.

https://github.com/joshstevens19/cab-multicall/issues

## Thanks And Support

This package is brought to you by [Josh Stevens](https://github.com/joshstevens19). My aim is to be able to keep creating these awesome packages to help the Ethereum space grow with easier-to-use tools to allow the learning curve to get involved with blockchain development easier and making Ethereum ecosystem better. If you want to help with that vision and allow me to invest more time into creating cool packages or if this package has saved you a lot of development time donations are welcome, every little helps. By donating, you are supporting me to be able to maintain existing packages, extend existing packages (as Ethereum matures), and allowing me to build more packages for Ethereum due to being able to invest more time into it. Thanks, everyone!

## Direct donations

Direct donations any token accepted - Eth address > `0x699c2daD091ffcF18f3cd9E8495929CA3a64dFe1`

## Github sponsors

[sponsor me](https://github.com/sponsors/joshstevens19) via github using fiat money