Address.sol 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
  3. pragma solidity ^0.8.19;
  4. /**
  5. * @dev Collection of functions related to the address type
  6. */
  7. library Address {
  8. /**
  9. * @dev The ETH balance of the account is not enough to perform the operation.
  10. */
  11. error AddressInsufficientBalance(address account);
  12. /**
  13. * @dev There's no code at `target` (it is not a contract).
  14. */
  15. error AddressEmptyCode(address target);
  16. /**
  17. * @dev A call to an address target failed. The target may have reverted.
  18. */
  19. error FailedInnerCall();
  20. /**
  21. * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
  22. * `recipient`, forwarding all available gas and reverting on errors.
  23. *
  24. * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
  25. * of certain opcodes, possibly making contracts go over the 2300 gas limit
  26. * imposed by `transfer`, making them unable to receive funds via
  27. * `transfer`. {sendValue} removes this limitation.
  28. *
  29. * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
  30. *
  31. * IMPORTANT: because control is transferred to `recipient`, care must be
  32. * taken to not create reentrancy vulnerabilities. Consider using
  33. * {ReentrancyGuard} or the
  34. * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
  35. */
  36. function sendValue(address payable recipient, uint256 amount) internal {
  37. if (address(this).balance < amount) {
  38. revert AddressInsufficientBalance(address(this));
  39. }
  40. (bool success, ) = recipient.call{value: amount}("");
  41. if (!success) {
  42. revert FailedInnerCall();
  43. }
  44. }
  45. /**
  46. * @dev Performs a Solidity function call using a low level `call`. A
  47. * plain `call` is an unsafe replacement for a function call: use this
  48. * function instead.
  49. *
  50. * If `target` reverts with a revert reason, it is bubbled up by this
  51. * function (like regular Solidity function calls).
  52. *
  53. * Returns the raw returned data. To convert to the expected return value,
  54. * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
  55. *
  56. * Requirements:
  57. *
  58. * - `target` must be a contract.
  59. * - calling `target` with `data` must not revert.
  60. */
  61. function functionCall(address target, bytes memory data) internal returns (bytes memory) {
  62. return functionCallWithValue(target, data, 0, defaultRevert);
  63. }
  64. /**
  65. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with a
  66. * `customRevert` function as a fallback when `target` reverts.
  67. *
  68. * Requirements:
  69. *
  70. * - `customRevert` must be a reverting function.
  71. */
  72. function functionCall(
  73. address target,
  74. bytes memory data,
  75. function() internal view customRevert
  76. ) internal returns (bytes memory) {
  77. return functionCallWithValue(target, data, 0, customRevert);
  78. }
  79. /**
  80. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  81. * but also transferring `value` wei to `target`.
  82. *
  83. * Requirements:
  84. *
  85. * - the calling contract must have an ETH balance of at least `value`.
  86. * - the called Solidity function must be `payable`.
  87. */
  88. function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
  89. return functionCallWithValue(target, data, value, defaultRevert);
  90. }
  91. /**
  92. * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
  93. * with a `customRevert` function as a fallback revert reason when `target` reverts.
  94. *
  95. * Requirements:
  96. *
  97. * - `customRevert` must be a reverting function.
  98. */
  99. function functionCallWithValue(
  100. address target,
  101. bytes memory data,
  102. uint256 value,
  103. function() internal view customRevert
  104. ) internal returns (bytes memory) {
  105. if (address(this).balance < value) {
  106. revert AddressInsufficientBalance(address(this));
  107. }
  108. (bool success, bytes memory returndata) = target.call{value: value}(data);
  109. return verifyCallResultFromTarget(target, success, returndata, customRevert);
  110. }
  111. /**
  112. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  113. * but performing a static call.
  114. */
  115. function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
  116. return functionStaticCall(target, data, defaultRevert);
  117. }
  118. /**
  119. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  120. * but performing a static call.
  121. */
  122. function functionStaticCall(
  123. address target,
  124. bytes memory data,
  125. function() internal view customRevert
  126. ) internal view returns (bytes memory) {
  127. (bool success, bytes memory returndata) = target.staticcall(data);
  128. return verifyCallResultFromTarget(target, success, returndata, customRevert);
  129. }
  130. /**
  131. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  132. * but performing a delegate call.
  133. */
  134. function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
  135. return functionDelegateCall(target, data, defaultRevert);
  136. }
  137. /**
  138. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  139. * but performing a delegate call.
  140. */
  141. function functionDelegateCall(
  142. address target,
  143. bytes memory data,
  144. function() internal view customRevert
  145. ) internal returns (bytes memory) {
  146. (bool success, bytes memory returndata) = target.delegatecall(data);
  147. return verifyCallResultFromTarget(target, success, returndata, customRevert);
  148. }
  149. /**
  150. * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
  151. * the revert reason or using the provided `customRevert`) in case of unsuccessful call or if target was not a contract.
  152. */
  153. function verifyCallResultFromTarget(
  154. address target,
  155. bool success,
  156. bytes memory returndata,
  157. function() internal view customRevert
  158. ) internal view returns (bytes memory) {
  159. if (success) {
  160. if (returndata.length == 0) {
  161. // only check if target is a contract if the call was successful and the return data is empty
  162. // otherwise we already know that it was a contract
  163. if (target.code.length == 0) {
  164. revert AddressEmptyCode(target);
  165. }
  166. }
  167. return returndata;
  168. } else {
  169. _revert(returndata, customRevert);
  170. }
  171. }
  172. /**
  173. * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
  174. * revert reason or with a default revert error.
  175. */
  176. function verifyCallResult(bool success, bytes memory returndata) internal view returns (bytes memory) {
  177. return verifyCallResult(success, returndata, defaultRevert);
  178. }
  179. /**
  180. * @dev Same as {xref-Address-verifyCallResult-bool-bytes-}[`verifyCallResult`], but with a
  181. * `customRevert` function as a fallback when `success` is `false`.
  182. *
  183. * Requirements:
  184. *
  185. * - `customRevert` must be a reverting function.
  186. */
  187. function verifyCallResult(
  188. bool success,
  189. bytes memory returndata,
  190. function() internal view customRevert
  191. ) internal view returns (bytes memory) {
  192. if (success) {
  193. return returndata;
  194. } else {
  195. _revert(returndata, customRevert);
  196. }
  197. }
  198. /**
  199. * @dev Default reverting function when no `customRevert` is provided in a function call.
  200. */
  201. function defaultRevert() internal pure {
  202. revert FailedInnerCall();
  203. }
  204. function _revert(bytes memory returndata, function() internal view customRevert) private view {
  205. // Look for revert reason and bubble it up if present
  206. if (returndata.length > 0) {
  207. // The easiest way to bubble the revert reason is using memory via assembly
  208. /// @solidity memory-safe-assembly
  209. assembly {
  210. let returndata_size := mload(returndata)
  211. revert(add(32, returndata), returndata_size)
  212. }
  213. } else {
  214. customRevert();
  215. revert FailedInnerCall();
  216. }
  217. }
  218. }