Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@indexed-finance/multicall

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@indexed-finance/multicall

Solidity contract and typescript library for static multi-calls

  • 1.0.7
  • npm
  • Socket score

Version published
Weekly downloads
1.6K
increased by36.07%
Maintainers
2
Weekly downloads
 
Created
Source

@indexed-finance/multicall

Solidity contract and typescript library for static multi-calls.

The MultiCall.sol contract executes multiple calls inside its constructor and returns the result as an ABI encoded bytes array. When the initialization code is sent to the 0 address as an eth_call operation, the code is not deployed and the returndata that would have been deployed if it was sent with eth_sendTransaction is returned. This enables simple multi-call functionality for on-chain queries without needing to deploy an aggregator contract.

The class defines a generic function multiCall that takes an ethers Interface object or a JSON ABI array as inputs along with the address, function names and arguments to call. The class then decodes the returndata and returns the results as an array.

There are also three more specific contracts in this library that can simplify common use-cases while using less bytecode. These are:

  • Querying the balance of a single address for many ERC20 tokens (and ether)
  • Querying the balance of a single account and the allowance that account has provided to a specific address for many ERC20 tokens
  • Querying the reserves of many Uniswap pairs

Install

npm install --save @indexed-finance/multicall

Examples

General Usage

If we have this example contract:

contract TokenMap {
  struct TokenData {
    uint256 balance;
    uint8 decimals;
  }

  mapping(address => TokenData) internal _tokenDatas;

  function getTokenData(address token) external view returns (TokenData memory) {
    return _tokenDatas[token];
  }
}

We can execute a multi-call with this javascript code:

const MultiCall = require('@indexed-finance/multicall');
const { abi } = require('./artifacts/TokenMap.json');

async function getMultiCallResults(provider, tokenMapAddress, tokens) {
  const multi = new MultiCall(provider);
  const inputs = [];
  for (let token of tokens) {
    inputs.push({ target: tokenMapAddress, function: 'getTokenData', args: [token] });
  }
  const tokenDatas = await multi.multiCall(abi, inputs);
  return tokenDatas;
}
// Result: Array<{ balance: BigNumber, decimals: number }>

Querying Token Balances

// Check the balance of the null address for dai and weth
const multi = new MultiCall(provider);
const tokens = [
  '0x6b175474e89094c44da98b954eedeac495271d0f', // dai
  '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // weth
  '0x0000000000000000000000000000000000000000' // eth
];
const account = '0x0000000000000000000000000000000000000000';
const balances = await multi.getBalances(tokens, account);

const daiBalance = balances['0x6b175474e89094c44da98b954eedeac495271d0f'];
const wethBalance = balances['0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'];
const ethBalance = balances['0x0000000000000000000000000000000000000000'];

Querying Token Balances and Allowances

// Check the balance of an account for dai and weth
// and the allowance the account has given to the Uniswap router

const multi = new MultiCall(provider);

const tokens = [
  '0x6b175474e89094c44da98b954eedeac495271d0f', // dai
  '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' // weth
]

const owner = '0x0000000000000000000000000000000000000000'
const spender = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D' // uniswap router

const balancesAndAllowances = await multi.getBalancesAndAllowances(tokens, owner, spender)

const {
  balance: daiBalance,
  allowance: daiAllowance
} = balancesAndAllowances['0x6b175474e89094c44da98b954eedeac495271d0f']

const {
  balance: wethBalance,
  allowance: wethAllowance
} = balancesAndAllowances['0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2']

Querying Uniswap Pair Reserves

// Check the reserves for Uniswap pairs

const multi = new MultiCall(provider);

const pairs = [
  '0xa478c2975ab1ea89e8196811f51a7b7ade33eb11', // dai-weth
  '0x004375dff511095cc5a197a54140a24efef3a416' // wbtc-usdc
]

const reserves = await multi.getReserves(pairs);

const {
  reserve0: daiWethR0,
  reserve1: daiWethR1,
  blockTimestampLast: daiWethTimestamp
} = reserves['0xa478c2975ab1ea89e8196811f51a7b7ade33eb11'];

const {
  reserve0: wbtcUsdcR0,
  reserve1: wbtcUsdcR1,
  blockTimestampLast: wbtcUsdcTimestamp
} = reserves['0x004375dff511095cc5a197a54140a24efef3a416'];

API

MultiCall

To create a MultiCall object, give an ethers or web3 provider in the constructor, e.g. new MultiCall(provider)

MultiCall.multiCall

The MultiCall class has a multiCall function which takes an array of CallInput objects.

Each CallInput has:

  • interface - ABI array or ethers Interface object
  • target - address of the contract to call
  • function - name of the function to call, or function signature if the function is overloaded
  • args - array of parameters to send in the call

Optionally, the interface can be provided as the first parameter and left out of the call input objects if all the calls target the same contract.

The multi-call will then bundle all of these calls into the constructor arguments for the MultiCall contract, which will call each of the functions and return an array with each call's returndata.

Strict mode

If a call reverts, the default behavior for the contract is to return an empty bytes for that call. However, you may provide a strict boolean field as an input parameter to multiCall which will instead revert the entire call.

Parameters

multiCall has two function signatures:

multiCall(_interface: Interface | JsonFragment[], inputs: CallInput[], strict?: boolean): Promise<any[]>;

multiCall(inputs: CallInput[], strict?: boolean): Promise<any[]>;

Response

The result will be a promise which resolves to an array with the decoded return data from each call. The data will be decoded by the ethers interface using whatever return types are defined in the ABI. If strict: true is not given, the result of any call that reverted will be null.

MultiCall.getReserves

The MultiCall class has a getReserves function which can query the reserves for many Uniswap pairs.

Parameters

The input parameter is an array of addresses of Uniswap pairs.

Response

The response from this function is a promise that resolves to an object with the Uniswap pair addresses as keys and a UniswapPairReserves type as values.

// response type
Promise<{
  [pair: string]: {
    reserve0: BigNumber,
    reserve1: BigNumber,
    blockTimestamp: number
  }
}>

MultiCall.getBalances

The MultiCall class has a getBalances function which can query the balance of a single account for many tokens or ether.

Parameters

This function takes in an array of token addresses followed by the address of the account to query balances for. If the null address (0x0000000000000000000000000000000000000000) is provided as an input token, the balance returned will be the ether balance of account.

getBalances(tokens: string[], account: string);

Response

The response from this function is a promise that resolves to an object with the token addresses as keys and BigNumber as values.

// response type
Promise<{
  [token: string]: BigNumber
}>

MultiCall.getBalancesAndAllowances

The MultiCall class has a getBalancesAndAllowances function which can query the balance of a single account for many tokens and the allowance the account has provided to a specific address for those tokens.

Parameters

This function takes in an array of token addresses, the address of the account whose allowances and balances are being queried, and the address of the spender for the allowance queries.

getBalancesAndAllowances(tokens: string[], owner: string, spender: string)

Response

The response from this function is a promise that resolves to an object with the token addresses as keys and a TokenBalanceAndAllowance type as values.

// response type
Promise<{
  [token: string]: {
    balance: BigNumber; // balance of `owner`
    allowance: BigNumber; // amount `owner` has approved `spender` to spend
  }
}>

Keywords

FAQs

Package last updated on 23 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc