RefundEscrowUpgradeable.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. __Context_init_unchained();
  33. __Ownable_init_unchained();
  34. __Escrow_init_unchained();
  35. __ConditionalEscrow_init_unchained();
  36. __RefundEscrow_init_unchained(beneficiary_);
  37. }
  38. function __RefundEscrow_init_unchained(address payable beneficiary_) internal onlyInitializing {
  39. require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
  40. _beneficiary = beneficiary_;
  41. _state = State.Active;
  42. }
  43. /**
  44. * @return The current state of the escrow.
  45. */
  46. function state() public view virtual returns (State) {
  47. return _state;
  48. }
  49. /**
  50. * @return The beneficiary of the escrow.
  51. */
  52. function beneficiary() public view virtual returns (address payable) {
  53. return _beneficiary;
  54. }
  55. /**
  56. * @dev Stores funds that may later be refunded.
  57. * @param refundee The address funds will be sent to if a refund occurs.
  58. */
  59. function deposit(address refundee) public payable virtual override {
  60. require(state() == State.Active, "RefundEscrow: can only deposit while active");
  61. super.deposit(refundee);
  62. }
  63. /**
  64. * @dev Allows for the beneficiary to withdraw their funds, rejecting
  65. * further deposits.
  66. */
  67. function close() public virtual onlyOwner {
  68. require(state() == State.Active, "RefundEscrow: can only close while active");
  69. _state = State.Closed;
  70. emit RefundsClosed();
  71. }
  72. /**
  73. * @dev Allows for refunds to take place, rejecting further deposits.
  74. */
  75. function enableRefunds() public virtual onlyOwner {
  76. require(state() == State.Active, "RefundEscrow: can only enable refunds while active");
  77. _state = State.Refunding;
  78. emit RefundsEnabled();
  79. }
  80. /**
  81. * @dev Withdraws the beneficiary's funds.
  82. */
  83. function beneficiaryWithdraw() public virtual {
  84. require(state() == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
  85. beneficiary().sendValue(address(this).balance);
  86. }
  87. /**
  88. * @dev Returns whether refundees can withdraw their deposits (be refunded). The overridden function receives a
  89. * 'payee' argument, but we ignore it here since the condition is global, not per-payee.
  90. */
  91. function withdrawalAllowed(address) public view override returns (bool) {
  92. return state() == State.Refunding;
  93. }
  94. uint256[49] private __gap;
  95. }