Address.sol 8.1 KB

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