Address.sol 7.7 KB

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