Address.sol 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. *
  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://consensys.net/diligence/blog/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(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
  107. return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
  108. }
  109. /**
  110. * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
  111. * with `errorMessage` as a fallback revert reason when `target` reverts.
  112. *
  113. * _Available since v3.1._
  114. */
  115. function functionCallWithValue(
  116. address target,
  117. bytes memory data,
  118. uint256 value,
  119. string memory errorMessage
  120. ) internal returns (bytes memory) {
  121. require(address(this).balance >= value, "Address: insufficient balance for call");
  122. (bool success, bytes memory returndata) = target.call{value: value}(data);
  123. return verifyCallResultFromTarget(target, 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. (bool success, bytes memory returndata) = target.staticcall(data);
  146. return verifyCallResultFromTarget(target, success, returndata, errorMessage);
  147. }
  148. /**
  149. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  150. * but performing a delegate call.
  151. *
  152. * _Available since v3.4._
  153. */
  154. function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
  155. return functionDelegateCall(target, data, "Address: low-level delegate call failed");
  156. }
  157. /**
  158. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  159. * but performing a delegate call.
  160. *
  161. * _Available since v3.4._
  162. */
  163. function functionDelegateCall(
  164. address target,
  165. bytes memory data,
  166. string memory errorMessage
  167. ) internal returns (bytes memory) {
  168. (bool success, bytes memory returndata) = target.delegatecall(data);
  169. return verifyCallResultFromTarget(target, success, returndata, errorMessage);
  170. }
  171. /**
  172. * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
  173. * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
  174. *
  175. * _Available since v4.8._
  176. */
  177. function verifyCallResultFromTarget(
  178. address target,
  179. bool success,
  180. bytes memory returndata,
  181. string memory errorMessage
  182. ) internal view returns (bytes memory) {
  183. if (success) {
  184. if (returndata.length == 0) {
  185. // only check isContract if the call was successful and the return data is empty
  186. // otherwise we already know that it was a contract
  187. require(isContract(target), "Address: call to non-contract");
  188. }
  189. return returndata;
  190. } else {
  191. _revert(returndata, errorMessage);
  192. }
  193. }
  194. /**
  195. * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
  196. * revert reason or using the provided one.
  197. *
  198. * _Available since v4.3._
  199. */
  200. function verifyCallResult(
  201. bool success,
  202. bytes memory returndata,
  203. string memory errorMessage
  204. ) internal pure returns (bytes memory) {
  205. if (success) {
  206. return returndata;
  207. } else {
  208. _revert(returndata, errorMessage);
  209. }
  210. }
  211. function _revert(bytes memory returndata, string memory errorMessage) private pure {
  212. // Look for revert reason and bubble it up if present
  213. if (returndata.length > 0) {
  214. // The easiest way to bubble the revert reason is using memory via assembly
  215. /// @solidity memory-safe-assembly
  216. assembly {
  217. let returndata_size := mload(returndata)
  218. revert(add(32, returndata), returndata_size)
  219. }
  220. } else {
  221. revert(errorMessage);
  222. }
  223. }
  224. }