RefundEscrowUpgradeable.sol 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/escrow/RefundEscrow.sol)
  3. pragma solidity ^0.8.0;
  4. import "./ConditionalEscrowUpgradeable.sol";
  5. import "../../proxy/utils/Initializable.sol";
  6. /**
  7. * @title RefundEscrow
  8. * @dev Escrow that holds funds for a beneficiary, deposited from multiple
  9. * parties.
  10. * @dev Intended usage: See {Escrow}. Same usage guidelines apply here.
  11. * @dev The owner account (that is, the contract that instantiates this
  12. * contract) may deposit, close the deposit period, and allow for either
  13. * withdrawal by the beneficiary, or refunds to the depositors. All interactions
  14. * with `RefundEscrow` will be made through the owner contract.
  15. */
  16. contract RefundEscrowUpgradeable is Initializable, ConditionalEscrowUpgradeable {
  17. using AddressUpgradeable for address payable;
  18. enum State {
  19. Active,
  20. Refunding,
  21. Closed
  22. }
  23. event RefundsClosed();
  24. event RefundsEnabled();
  25. State private _state;
  26. address payable private _beneficiary;
  27. /**
  28. * @dev Constructor.
  29. * @param beneficiary_ The beneficiary of the deposits.
  30. */
  31. function __RefundEscrow_init(address payable beneficiary_) internal onlyInitializing {
  32. __Ownable_init_unchained();
  33. __RefundEscrow_init_unchained(beneficiary_);
  34. }
  35. function __RefundEscrow_init_unchained(address payable beneficiary_) internal onlyInitializing {
  36. require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
  37. _beneficiary = beneficiary_;
  38. _state = State.Active;
  39. }
  40. /**
  41. * @return The current state of the escrow.
  42. */
  43. function state() public view virtual returns (State) {
  44. return _state;
  45. }
  46. /**
  47. * @return The beneficiary of the escrow.
  48. */
  49. function beneficiary() public view virtual returns (address payable) {
  50. return _beneficiary;
  51. }
  52. /**
  53. * @dev Stores funds that may later be refunded.
  54. * @param refundee The address funds will be sent to if a refund occurs.
  55. */
  56. function deposit(address refundee) public payable virtual override {
  57. require(state() == State.Active, "RefundEscrow: can only deposit while active");
  58. super.deposit(refundee);
  59. }
  60. /**
  61. * @dev Allows for the beneficiary to withdraw their funds, rejecting
  62. * further deposits.
  63. */
  64. function close() public virtual onlyOwner {
  65. require(state() == State.Active, "RefundEscrow: can only close while active");
  66. _state = State.Closed;
  67. emit RefundsClosed();
  68. }
  69. /**
  70. * @dev Allows for refunds to take place, rejecting further deposits.
  71. */
  72. function enableRefunds() public virtual onlyOwner {
  73. require(state() == State.Active, "RefundEscrow: can only enable refunds while active");
  74. _state = State.Refunding;
  75. emit RefundsEnabled();
  76. }
  77. /**
  78. * @dev Withdraws the beneficiary's funds.
  79. */
  80. function beneficiaryWithdraw() public virtual {
  81. require(state() == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
  82. beneficiary().sendValue(address(this).balance);
  83. }
  84. /**
  85. * @dev Returns whether refundees can withdraw their deposits (be refunded). The overridden function receives a
  86. * 'payee' argument, but we ignore it here since the condition is global, not per-payee.
  87. */
  88. function withdrawalAllowed(address) public view override returns (bool) {
  89. return state() == State.Refunding;
  90. }
  91. /**
  92. * This empty reserved space is put in place to allow future versions to add new
  93. * variables without shifting down storage in the inheritance chain.
  94. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  95. */
  96. uint256[49] private __gap;
  97. }