Address.sol 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.2 <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. // solhint-disable-next-line no-inline-assembly
  30. assembly { size := extcodesize(account) }
  31. return size > 0;
  32. }
  33. /**
  34. * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
  35. * `recipient`, forwarding all available gas and reverting on errors.
  36. *
  37. * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
  38. * of certain opcodes, possibly making contracts go over the 2300 gas limit
  39. * imposed by `transfer`, making them unable to receive funds via
  40. * `transfer`. {sendValue} removes this limitation.
  41. *
  42. * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
  43. *
  44. * IMPORTANT: because control is transferred to `recipient`, care must be
  45. * taken to not create reentrancy vulnerabilities. Consider using
  46. * {ReentrancyGuard} or the
  47. * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
  48. */
  49. function sendValue(address payable recipient, uint256 amount) internal {
  50. require(address(this).balance >= amount, "Address: insufficient balance");
  51. // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
  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(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
  83. return functionCallWithValue(target, data, 0, errorMessage);
  84. }
  85. /**
  86. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  87. * but also transferring `value` wei to `target`.
  88. *
  89. * Requirements:
  90. *
  91. * - the calling contract must have an ETH balance of at least `value`.
  92. * - the called Solidity function must be `payable`.
  93. *
  94. * _Available since v3.1._
  95. */
  96. function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
  97. return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
  98. }
  99. /**
  100. * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
  101. * with `errorMessage` as a fallback revert reason when `target` reverts.
  102. *
  103. * _Available since v3.1._
  104. */
  105. function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
  106. require(address(this).balance >= value, "Address: insufficient balance for call");
  107. require(isContract(target), "Address: call to non-contract");
  108. // solhint-disable-next-line avoid-low-level-calls
  109. (bool success, bytes memory returndata) = target.call{ value: value }(data);
  110. return _verifyCallResult(success, returndata, errorMessage);
  111. }
  112. /**
  113. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  114. * but performing a static call.
  115. *
  116. * _Available since v3.3._
  117. */
  118. function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
  119. return functionStaticCall(target, data, "Address: low-level static call failed");
  120. }
  121. /**
  122. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  123. * but performing a static call.
  124. *
  125. * _Available since v3.3._
  126. */
  127. function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
  128. require(isContract(target), "Address: static call to non-contract");
  129. // solhint-disable-next-line avoid-low-level-calls
  130. (bool success, bytes memory returndata) = target.staticcall(data);
  131. return _verifyCallResult(success, returndata, errorMessage);
  132. }
  133. /**
  134. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  135. * but performing a delegate call.
  136. *
  137. * _Available since v3.3._
  138. */
  139. function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
  140. return functionDelegateCall(target, data, "Address: low-level delegate call failed");
  141. }
  142. /**
  143. * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
  144. * but performing a delegate call.
  145. *
  146. * _Available since v3.3._
  147. */
  148. function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
  149. require(isContract(target), "Address: delegate call to non-contract");
  150. // solhint-disable-next-line avoid-low-level-calls
  151. (bool success, bytes memory returndata) = target.delegatecall(data);
  152. return _verifyCallResult(success, returndata, errorMessage);
  153. }
  154. function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
  155. if (success) {
  156. return returndata;
  157. } else {
  158. // Look for revert reason and bubble it up if present
  159. if (returndata.length > 0) {
  160. // The easiest way to bubble the revert reason is using memory via assembly
  161. // solhint-disable-next-line no-inline-assembly
  162. assembly {
  163. let returndata_size := mload(returndata)
  164. revert(add(32, returndata), returndata_size)
  165. }
  166. } else {
  167. revert(errorMessage);
  168. }
  169. }
  170. }
  171. }