Add Multicall module (#2608)
This commit is contained in:
@ -99,3 +99,41 @@ Want to check if an address is a contract? Use xref:api:utils.adoc#Address[`Addr
|
||||
|
||||
Want to keep track of some numbers that increment by 1 every time you want another one? Check out xref:api:utils.adoc#Counters[`Counters`]. This is useful for lots of things, like creating incremental identifiers, as shown on the xref:erc721.adoc[ERC721 guide].
|
||||
|
||||
=== Multicall
|
||||
|
||||
The `Multicall` abstract contract comes with a `multicall` function that bundles together multiple calls in a single external call. With it, external accounts may perform atomic operations comprising several function calls. This is not only useful for EOAs to make multiple calls in a single transaction, it's also a way to revert a previous call if a later one fails.
|
||||
|
||||
Consider this dummy contract:
|
||||
|
||||
[source,solidity]
|
||||
----
|
||||
// contracts/Box.sol
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "@openzeppelin/contracts/utils/Multicall.sol";
|
||||
|
||||
contract Box is Multicall {
|
||||
function foo() public {
|
||||
...
|
||||
}
|
||||
|
||||
function bar() public {
|
||||
...
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
This is how to call the `multicall` function using Truffle, allowing `foo` and `bar` to be called in a single transaction:
|
||||
[source,javascript]
|
||||
----
|
||||
// scripts/foobar.js
|
||||
|
||||
const Box = artifacts.require('Box');
|
||||
const instance = await Box.new();
|
||||
|
||||
await instance.multicall([
|
||||
instance.contract.methods.foo().encodeABI(),
|
||||
instance.contract.methods.bar().encodeABI()
|
||||
]);
|
||||
----
|
||||
|
||||
Reference in New Issue
Block a user