Address.sol 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. pragma solidity ^0.5.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. * This test is non-exhaustive, and there may be false-negatives: during the
  10. * execution of a contract's constructor, its address will be reported as
  11. * not containing a contract.
  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. function isContract(address account) internal view returns (bool) {
  17. // This method relies in extcodesize, which returns 0 for contracts in
  18. // construction, since the code is only stored at the end of the
  19. // constructor execution.
  20. // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
  21. // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
  22. // for accounts without code, i.e. `keccak256('')`
  23. bytes32 codehash;
  24. bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
  25. // solhint-disable-next-line no-inline-assembly
  26. assembly { codehash := extcodehash(account) }
  27. return (codehash != 0x0 && codehash != accountHash);
  28. }
  29. /**
  30. * @dev Converts an `address` into `address payable`. Note that this is
  31. * simply a type cast: the actual underlying value is not changed.
  32. */
  33. function toPayable(address account) internal pure returns (address payable) {
  34. return address(uint160(account));
  35. }
  36. }