MultiCall
使用 for 循环和 聚合多个查询的合约示例staticcall。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
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;
}
}
合同测试MultiCall
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
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);
}
}