Address.sol 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.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 functionCallWithValue(target, data, 0, "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. (bool success, bytes memory returndata) = target.call{value: value}(data);
  127. return verifyCallResultFromTarget(target, success, returndata, errorMessage);
  128. }
  129. /**
  130. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  131. * but performing a static call.
  132. *
  133. * _Available since v3.3._
  134. */
  135. function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
  136. return functionStaticCall(target, data, "Address: low-level static call failed");
  137. }
  138. /**
  139. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  140. * but performing a static call.
  141. *
  142. * _Available since v3.3._
  143. */
  144. function functionStaticCall(
  145. address target,
  146. bytes memory data,
  147. string memory errorMessage
  148. ) internal view returns (bytes memory) {
  149. (bool success, bytes memory returndata) = target.staticcall(data);
  150. return verifyCallResultFromTarget(target, success, returndata, errorMessage);
  151. }
  152. /**
  153. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  154. * but performing a delegate call.
  155. *
  156. * _Available since v3.4._
  157. */
  158. function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
  159. return functionDelegateCall(target, data, "Address: low-level delegate call failed");
  160. }
  161. /**
  162. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  163. * but performing a delegate call.
  164. *
  165. * _Available since v3.4._
  166. */
  167. function functionDelegateCall(
  168. address target,
  169. bytes memory data,
  170. string memory errorMessage
  171. ) internal returns (bytes memory) {
  172. (bool success, bytes memory returndata) = target.delegatecall(data);
  173. return verifyCallResultFromTarget(target, success, returndata, errorMessage);
  174. }
  175. /**
  176. * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
  177. * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
  178. *
  179. * _Available since v4.8._
  180. */
  181. function verifyCallResultFromTarget(
  182. address target,
  183. bool success,
  184. bytes memory returndata,
  185. string memory errorMessage
  186. ) internal view returns (bytes memory) {
  187. if (success) {
  188. if (returndata.length == 0) {
  189. // only check isContract if the call was successful and the return data is empty
  190. // otherwise we already know that it was a contract
  191. require(isContract(target), "Address: call to non-contract");
  192. }
  193. return returndata;
  194. } else {
  195. _revert(returndata, errorMessage);
  196. }
  197. }
  198. /**
  199. * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
  200. * revert reason or using the provided one.
  201. *
  202. * _Available since v4.3._
  203. */
  204. function verifyCallResult(
  205. bool success,
  206. bytes memory returndata,
  207. string memory errorMessage
  208. ) internal pure returns (bytes memory) {
  209. if (success) {
  210. return returndata;
  211. } else {
  212. _revert(returndata, errorMessage);
  213. }
  214. }
  215. function _revert(bytes memory returndata, string memory errorMessage) private pure {
  216. // Look for revert reason and bubble it up if present
  217. if (returndata.length > 0) {
  218. // The easiest way to bubble the revert reason is using memory via assembly
  219. /// @solidity memory-safe-assembly
  220. assembly {
  221. let returndata_size := mload(returndata)
  222. revert(add(32, returndata), returndata_size)
  223. }
  224. } else {
  225. revert(errorMessage);
  226. }
  227. }
  228. }