Address.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. pragma solidity ^0.6.0;
  2. /**
  3. * @dev Collection of functions related to the address type
  4. */
  5. library Address {
  6. /**
  7. * @dev Returns true if `account` is a contract.
  8. *
  9. * [IMPORTANT]
  10. * ====
  11. * It is unsafe to assume that an address for which this function returns
  12. * false is an externally-owned account (EOA) and not a contract.
  13. *
  14. * Among others, `isContract` will return false for the following
  15. * types of addresses:
  16. *
  17. * - an externally-owned account
  18. * - a contract in construction
  19. * - an address where a contract will be created
  20. * - an address where a contract lived, but was destroyed
  21. * ====
  22. */
  23. function isContract(address account) internal view returns (bool) {
  24. // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
  25. // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
  26. // for accounts without code, i.e. `keccak256('')`
  27. bytes32 codehash;
  28. bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
  29. // solhint-disable-next-line no-inline-assembly
  30. assembly { codehash := extcodehash(account) }
  31. return (codehash != accountHash && codehash != 0x0);
  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. * _Available since v2.4.0._
  50. */
  51. function sendValue(address payable recipient, uint256 amount) internal {
  52. require(address(this).balance >= amount, "Address: insufficient balance");
  53. // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
  54. (bool success, ) = recipient.call.value(amount)("");
  55. require(success, "Address: unable to send value, recipient may have reverted");
  56. }
  57. }