8

What exactly is multicall and the benefits of it, compared to Single call? What is multicall contract and the purpose of it, I can't find any proper definition of it.

Some source said that multicall merges all call into a single RPC call, I don't understand what are the benefits of doing so.

2

3 Answers 3

8

In some cases, you might need to gather synchronized data from the blockchain. A good example is querying token prices from Uniswap. You don't want to have the price for one token from block x and a price for another token at block y.

Multicall aggregates all queries to different contracts in one call, so it guarantees your data is retrieved from the same block.

It's useful to understand how it's done. This explains, in principle, how it works under the hood: https://blockheroes.dev/same-block-data-querying/

2

Please read MultiCall README.

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.

0

when you have a contract that have two functions in both of them we have block.timestamp , and when you call one of them and after that call another one, the time is different, now the reason is clear but please see this video https://www.youtube.com/watch?v=PDR054Cy8qM and read the example below

An example of contract that aggregates multiple queries using a for loop and staticcall.

 // SPDX-License-Identifier: MIT
  pragma solidity ^0.8.10;
contract MultiCall {
function multiCall(address[] calldata targets, bytes[] calldata data)
    external
    view
    returns (bytes[] memory)
{
    require(targets.length == data.length, "target length != data length");

    bytes[] memory results = new bytes[](data.length);

    for (uint i; i < targets.length; i++) {
        (bool success, bytes memory result) = 
   targets[i].staticcall(data[i]);
        require(success, "call failed");
        results[i] = result;
    }

    return results;
 }
}

Contract to test MultiCall

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract TestMultiCall {
function test(uint _i) external pure returns (uint) {
    return _i;
}

function getData(uint _i) external pure returns (bytes memory) {
    return abi.encodeWithSelector(this.test.selector, _i);
  }
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.