Address.sol 7.7 KB

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