Address.sol 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.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. * Furthermore, `isContract` will also return true if the target contract within
  25. * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
  26. * which only has an effect at the end of a transaction.
  27. * ====
  28. *
  29. * [IMPORTANT]
  30. * ====
  31. * You shouldn't rely on `isContract` to protect against flash loan attacks!
  32. *
  33. * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
  34. * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
  35. * constructor.
  36. * ====
  37. */
  38. function isContract(address account) internal view returns (bool) {
  39. // This method relies on extcodesize/address.code.length, which returns 0
  40. // for contracts in construction, since the code is only stored at the end
  41. // of the constructor execution.
  42. return account.code.length > 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://consensys.net/diligence/blog/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.8.0/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 functionCallWithValue(target, data, 0, "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(address target, bytes memory data, uint256 value) 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. }