Address.sol 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. pragma solidity ^0.5.5;
  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. * IMPORTANT: It is unsafe to assume that an address for which this
  14. * function returns false is an externally-owned account (EOA) and not a
  15. * contract.
  16. */
  17. function isContract(address account) internal view returns (bool) {
  18. // This method relies in extcodesize, which returns 0 for contracts in
  19. // construction, since the code is only stored at the end of the
  20. // constructor execution.
  21. // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
  22. // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
  23. // for accounts without code, i.e. `keccak256('')`
  24. bytes32 codehash;
  25. bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
  26. // solhint-disable-next-line no-inline-assembly
  27. assembly { codehash := extcodehash(account) }
  28. return (codehash != 0x0 && codehash != accountHash);
  29. }
  30. /**
  31. * @dev Converts an `address` into `address payable`. Note that this is
  32. * simply a type cast: the actual underlying value is not changed.
  33. *
  34. * NOTE: This is a feature of the next version of OpenZeppelin Contracts.
  35. * @dev Get it via `npm install @openzeppelin/contracts@next`.
  36. *
  37. * _Available since v2.4.0._
  38. */
  39. function toPayable(address account) internal pure returns (address payable) {
  40. return address(uint160(account));
  41. }
  42. /**
  43. * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
  44. * `recipient`, forwarding all available gas and reverting on errors.
  45. *
  46. * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
  47. * of certain opcodes, possibly making contracts go over the 2300 gas limit
  48. * imposed by `transfer`, making them unable to receive funds via
  49. * `transfer`. {sendValue} removes this limitation.
  50. *
  51. * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
  52. *
  53. * IMPORTANT: because control is transferred to `recipient`, care must be
  54. * taken to not create reentrancy vulnerabilities. Consider using
  55. * {ReentrancyGuard} or the
  56. * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
  57. *
  58. * _Available since v2.4.0._
  59. */
  60. function sendValue(address payable recipient, uint256 amount) internal {
  61. require(address(this).balance >= amount, "Address: insufficient balance");
  62. // solhint-disable-next-line avoid-call-value
  63. (bool success, ) = recipient.call.value(amount)("");
  64. require(success, "Address: unable to send value, recipient may have reverted");
  65. }
  66. }