exchange.IO("multicall", ...)
Batch-reads the results of multiple contract calls in a single request through the Multicall3 contract. It is suitable for batch querying balances, liquidity pool states, quotes, and other data, and can effectively reduce the number of RPC requests.
exchange.IO(k, calls)
exchange.IO(k, calls, options)Examples
Read wallet balances and the Uniswap liquidity pool state simultaneously in a single request.
javascript
function main() {
var USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
var WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
var POOL = "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640" // Uniswap V3 USDC/WETH 0.05% pool
var owner = exchange.IO("address")
exchange.IO("abi", POOL, JSON.stringify([
{ type: "function", name: "slot0", stateMutability: "view", inputs: [], outputs: [
{ name: "sqrtPriceX96", type: "uint160" }, { name: "tick", type: "int24" },
{ name: "observationIndex", type: "uint16" }, { name: "observationCardinality",
type: "uint16" },
{ name: "observationCardinalityNext", type: "uint16" }, { name: "feeProtocol",
type: "uint8" },
{ name: "unlocked", type: "bool" }] },
{ type: "function", name: "liquidity", stateMutability: "view", inputs: [],
outputs: [{ type: "uint128" }] }
]))
// Each item is written the same way as exchange.IO("call", ...): [contract address, method,
// ...arguments], all read in a single request
var ret = exchange.IO("multicall", [
[USDC, "balanceOf", owner],
[WETH, "balanceOf", owner],
[POOL, "slot0"],
[POOL, "liquidity"]
])
Log("USDC balance:", ret[0], "WETH balance:", ret[1])
Log("Pool tick:", ret[2].tick, "Liquidity:", ret[3])
// By default, if any item fails, the entire result is null; when allowFailure is true, failed
// items are null
var ret2 = exchange.IO("multicall", [[USDC, "decimals"], [USDC, "transfer", POOL, 1]],
{ allowFailure: true })
Log("decimals:", ret2[0], "transfer (the caller is the Multicall3 contract, not the wallet):",
ret2[1])
}Returns
| Type | Description |
array | Returns an array of results in the same order as |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Function of the |
calls | array | Yes | Array of calls. Each item in the array has the format |
options | object | No | Contains optional settings: when |
See Also
Remarks
All calls are executed against the same block state, so the returned results are consistent with one another.
Currently only Ethereum (EVM) chains are supported; TRON is not supported yet.