Address.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.2;
  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. // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
  26. // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
  27. // for accounts without code, i.e. `keccak256('')`
  28. bytes32 codehash;
  29. bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
  30. // solhint-disable-next-line no-inline-assembly
  31. assembly { codehash := extcodehash(account) }
  32. return (codehash != accountHash && codehash != 0x0);
  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. // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
  53. (bool success, ) = recipient.call{ value: amount }("");
  54. require(success, "Address: unable to send value, recipient may have reverted");
  55. }
  56. /**
  57. * @dev Performs a Solidity function call using a low level `call`. A
  58. * plain`call` is an unsafe replacement for a function call: use this
  59. * function instead.
  60. *
  61. * If `target` reverts with a revert reason, it is bubbled up by this
  62. * function (like regular Solidity function calls).
  63. *
  64. * Returns the raw returned data. To convert to the expected return value,
  65. * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
  66. *
  67. * Requirements:
  68. *
  69. * - `target` must be a contract.
  70. * - calling `target` with `data` must not revert.
  71. *
  72. * _Available since v3.1._
  73. */
  74. function functionCall(address target, bytes memory data) internal returns (bytes memory) {
  75. return functionCall(target, data, "Address: low-level call failed");
  76. }
  77. /**
  78. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
  79. * `errorMessage` as a fallback revert reason when `target` reverts.
  80. *
  81. * _Available since v3.1._
  82. */
  83. function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
  84. return _functionCallWithValue(target, data, 0, errorMessage);
  85. }
  86. /**
  87. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
  88. * but also transferring `value` wei to `target`.
  89. *
  90. * Requirements:
  91. *
  92. * - the calling contract must have an ETH balance of at least `value`.
  93. * - the called Solidity function must be `payable`.
  94. *
  95. * _Available since v3.1._
  96. */
  97. function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
  98. return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
  99. }
  100. /**
  101. * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
  102. * with `errorMessage` as a fallback revert reason when `target` reverts.
  103. *
  104. * _Available since v3.1._
  105. */
  106. function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
  107. require(address(this).balance >= value, "Address: insufficient balance for call");
  108. return _functionCallWithValue(target, data, value, errorMessage);
  109. }
  110. function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) {
  111. require(isContract(target), "Address: call to non-contract");
  112. // solhint-disable-next-line avoid-low-level-calls
  113. (bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
  114. if (success) {
  115. return returndata;
  116. } else {
  117. // Look for revert reason and bubble it up if present
  118. if (returndata.length > 0) {
  119. // The easiest way to bubble the revert reason is using memory via assembly
  120. // solhint-disable-next-line no-inline-assembly
  121. assembly {
  122. let returndata_size := mload(returndata)
  123. revert(add(32, returndata), returndata_size)
  124. }
  125. } else {
  126. revert(errorMessage);
  127. }
  128. }
  129. }
  130. }