The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/solidity/contracts/libs/Call.sol

146 lines
3.4 KiB

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {TypeCasts} from "./TypeCasts.sol";
library CallLib {
struct StaticCall {
// supporting non EVM targets
bytes32 to;
bytes data;
}
struct Call {
// supporting non EVM targets
bytes32 to;
uint256 value;
bytes data;
}
struct StaticCallWithCallback {
StaticCall _call;
bytes callback;
}
function call(Call memory _call)
internal
returns (bytes memory returnData)
{
return
Address.functionCallWithValue(
TypeCasts.bytes32ToAddress(_call.to),
_call.data,
_call.value
);
}
function staticcall(StaticCall memory _call)
private
view
returns (bytes memory)
{
return
Address.functionStaticCall(
TypeCasts.bytes32ToAddress(_call.to),
_call.data
);
}
function staticcall(StaticCallWithCallback memory _call)
internal
view
returns (bytes memory callback)
{
return bytes.concat(_call.callback, staticcall(_call._call));
}
function multicall(Call[] memory calls) internal {
uint256 i = 0;
uint256 len = calls.length;
while (i < len) {
call(calls[i]);
unchecked {
++i;
}
}
}
function multistaticcall(StaticCallWithCallback[] memory _calls)
internal
view
returns (bytes[] memory)
{
uint256 i = 0;
uint256 len = _calls.length;
bytes[] memory callbacks = new bytes[](len);
while (i < len) {
callbacks[i] = staticcall(_calls[i]);
unchecked {
++i;
}
}
return callbacks;
}
function multicallto(address to, bytes[] memory calls) internal {
uint256 i = 0;
uint256 len = calls.length;
while (i < len) {
Address.functionCall(to, calls[i]);
unchecked {
++i;
}
}
}
function build(bytes32 to, bytes memory data)
internal
pure
returns (StaticCall memory)
{
return StaticCall(to, data);
}
function build(address to, bytes memory data)
internal
pure
returns (StaticCall memory)
{
return build(TypeCasts.addressToBytes32(to), data);
}
function build(
bytes32 to,
uint256 value,
bytes memory data
) internal pure returns (Call memory) {
return Call(to, value, data);
}
function build(
address to,
uint256 value,
bytes memory data
) internal pure returns (Call memory) {
return Call(TypeCasts.addressToBytes32(to), value, data);
}
function build(
bytes32 to,
bytes memory data,
bytes memory callback
) internal pure returns (StaticCallWithCallback memory) {
return StaticCallWithCallback(build(to, data), callback);
}
function build(
address to,
bytes memory data,
bytes memory callback
) internal pure returns (StaticCallWithCallback memory) {
return StaticCallWithCallback(build(to, data), callback);
}
}