forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLowLevelCall.sol
More file actions
99 lines (88 loc) · 4.11 KB
/
LowLevelCall.sol
File metadata and controls
99 lines (88 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Errors} from "./Errors.sol";
/**
* @dev Library of low level call functions that implement different calling strategies to deal with the return data.
*/
library LowLevelCall {
/// === CALL ===
/// @dev Performs a Solidity function call using a low level `call` and ignoring the return data.
function callRaw(address target, uint256 value, bytes memory data) internal returns (bool success) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0, 0)
}
}
/// @dev Performs a Solidity function call using a low level `call` and returns the first 32 bytes of the result
/// in the scratch space of memory. Useful for functions that return a single-word value.
///
/// WARNING: Do not assume that the result is zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function callReturnScratchBytes32(
address target,
uint256 value,
bytes memory data
) internal returns (bool success, bytes32 result) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0, 0x20)
result := mload(0)
}
}
/// @dev Performs a Solidity function call using a low level `call` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple of single-word values values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function callReturnScratchBytes32Pair(
address target,
uint256 value,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0, 0x40)
result1 := mload(0)
result2 := mload(0x20)
}
}
/// === STATICCALL ===
/// @dev Performs a Solidity function call using a low level `staticcall` and ignoring the return data.
function staticcallRaw(address target, bytes memory data) internal view returns (bool success) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0, 0)
}
}
/// @dev Performs a Solidity function call using a low level `staticcall` and returns the first 32 bytes of the result
/// in the scratch space of memory. Useful for functions that return a single-word value.
///
/// WARNING: Do not assume that the result is zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function staticcallReturnScratchBytes32(
address target,
bytes memory data
) internal view returns (bool success, bytes32 result) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0, 0x20)
result := mload(0)
}
}
/// @dev Performs a Solidity function call using a low level `staticcall` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple of single-word values values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function staticcallReturnScratchBytes32Pair(
address target,
bytes memory data
) internal view returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0, 0x40)
result1 := mload(0)
result2 := mload(0x20)
}
}
/// @dev Returns the size of the return data buffer.
function returnDataSize() internal pure returns (uint256 size) {
assembly ("memory-safe") {
size := returndatasize()
}
}
}