How to use the multicall() function

💡
What is a multicall() function?

A multicall function in a smart contract is a mechanism that allows multiple function calls to be executed in a single transaction. It is designed to optimize efficiency and reduce the cost of interacting with the smart contract by batching multiple calls into a single operation.

 
💡
What is the benefit of using a multicall() function?
  • With a multicall function, you can combine several function calls into one transaction, reducing the number of transactions and optimizing gas usage.
  • The multicall function operates by receiving a list of function calls as input, where each call specifies the target contract, the function to be invoked, and any required parameters. The smart contract then executes each function call sequentially within the same transaction, allowing multiple operations to be performed atomically.
  • It helps reduce the number of blockchain interactions, conserves gas costs, and can enhance the overall performance of decentralized applications (DApps) built on the blockchain.
 
💡
How do we use a multicall() function via the SDK?

Using multicall from the Contract Explorer can be tricky, so you need to pass encoded bytes data of the function(s) you want to call. Alternatively, you can create a script using the SDK that uses multicall whichever other function you are looking to batch in with the transaction.

For example, if you want to encode data for some function, let's say mintTo(address to, uint256 amount) on your contract, here's how you'll do it in Javascript:

const myContract = // initialize contract instance here
 const encodedData = myContract.interface.encodeFunctionData("mintTo", [
         to,
         amount,
       ]);
  • This encodedData is the calldata for the function that you want to call.
  • Now, you can have multiple such function calls encoded. You need to put them all in a single array, and that will become the input for multicall()
  • You can find an example on how we use a multicall() function on this github repo provided below (reference):
    • public async to(args: TokenMintInput[]): Promise<TransactionResult> {
          const encoded: string[] = [];
          for (const arg of args) {
            encoded.push(
              this.contractWrapper.readContract.interface.encodeFunctionData(
                "mintTo",
                [arg.toAddress, await this.erc20.normalizeAmount(arg.amount)],
              ),
            );
          }
          return { receipt: await this.contractWrapper.multiCall(encoded) };
        }
 

 

Kindly reach out to us via our Discord if you have further questions regarding this. We will ensure that we provide you with the support that you require!

Did this answer your question?
😞
😐
🤩