Address.sol 8.1 KB

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