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