Address.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0) (utils/Address.sol)
  3. pragma solidity ^0.8.20;
  4. import {Errors} from "./Errors.sol";
  5. /**
  6. * @dev Collection of functions related to the address type
  7. */
  8. library Address {
  9. /**
  10. * @dev There's no code at `target` (it is not a contract).
  11. */
  12. error AddressEmptyCode(address target);
  13. /**
  14. * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
  15. * `recipient`, forwarding all available gas and reverting on errors.
  16. *
  17. * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
  18. * of certain opcodes, possibly making contracts go over the 2300 gas limit
  19. * imposed by `transfer`, making them unable to receive funds via
  20. * `transfer`. {sendValue} removes this limitation.
  21. *
  22. * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
  23. *
  24. * IMPORTANT: because control is transferred to `recipient`, care must be
  25. * taken to not create reentrancy vulnerabilities. Consider using
  26. * {ReentrancyGuard} or the
  27. * https://solidity.readthedocs.io/en/v0.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
  28. */
  29. function sendValue(address payable recipient, uint256 amount) internal {
  30. if (address(this).balance < amount) {
  31. revert Errors.InsufficientBalance(address(this).balance, amount);
  32. }
  33. (bool success, bytes memory returndata) = recipient.call{value: amount}("");
  34. if (!success) {
  35. _revert(returndata);
  36. }
  37. }
  38. /**
  39. * @dev Performs a Solidity function call using a low level `call`. A
  40. * plain `call` is an unsafe replacement for a function call: use this
  41. * function instead.
  42. *
  43. * If `target` reverts with a revert reason or custom error, it is bubbled
  44. * up by this function (like regular Solidity function calls). However, if
  45. * the call reverted with no returned reason, this function reverts with a
  46. * {Errors.FailedCall} error.
  47. *
  48. * Returns the raw returned data. To convert to the expected return value,
  49. * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
  50. *
  51. * Requirements:
  52. *
  53. * - `target` must be a contract.
  54. * - calling `target` with `data` must not revert.
  55. */
  56. function functionCall(address target, bytes memory data) internal returns (bytes memory) {
  57. return functionCallWithValue(target, data, 0);
  58. }
  59. /**
  60. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  61. * but also transferring `value` wei to `target`.
  62. *
  63. * Requirements:
  64. *
  65. * - the calling contract must have an ETH balance of at least `value`.
  66. * - the called Solidity function must be `payable`.
  67. */
  68. function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
  69. if (address(this).balance < value) {
  70. revert Errors.InsufficientBalance(address(this).balance, value);
  71. }
  72. (bool success, bytes memory returndata) = target.call{value: value}(data);
  73. return verifyCallResultFromTarget(target, success, returndata);
  74. }
  75. /**
  76. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  77. * but performing a static call.
  78. */
  79. function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
  80. (bool success, bytes memory returndata) = target.staticcall(data);
  81. return verifyCallResultFromTarget(target, success, returndata);
  82. }
  83. /**
  84. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  85. * but performing a delegate call.
  86. */
  87. function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
  88. (bool success, bytes memory returndata) = target.delegatecall(data);
  89. return verifyCallResultFromTarget(target, success, returndata);
  90. }
  91. /**
  92. * @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
  93. * was not a contract or bubbling up the revert reason (falling back to {Errors.FailedCall}) in case
  94. * of an unsuccessful call.
  95. */
  96. function verifyCallResultFromTarget(
  97. address target,
  98. bool success,
  99. bytes memory returndata
  100. ) internal view returns (bytes memory) {
  101. if (!success) {
  102. _revert(returndata);
  103. } else {
  104. // only check if target is a contract if the call was successful and the return data is empty
  105. // otherwise we already know that it was a contract
  106. if (returndata.length == 0 && target.code.length == 0) {
  107. revert AddressEmptyCode(target);
  108. }
  109. return returndata;
  110. }
  111. }
  112. /**
  113. * @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
  114. * revert reason or with a default {Errors.FailedCall} error.
  115. */
  116. function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
  117. if (!success) {
  118. _revert(returndata);
  119. } else {
  120. return returndata;
  121. }
  122. }
  123. /**
  124. * @dev Reverts with returndata if present. Otherwise reverts with {Errors.FailedCall}.
  125. */
  126. function _revert(bytes memory returndata) private pure {
  127. // Look for revert reason and bubble it up if present
  128. if (returndata.length > 0) {
  129. // The easiest way to bubble the revert reason is using memory via assembly
  130. assembly ("memory-safe") {
  131. revert(add(returndata, 0x20), mload(returndata))
  132. }
  133. } else {
  134. revert Errors.FailedCall();
  135. }
  136. }
  137. }