Address.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }