RefundEscrow.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.0;
  3. import "./ConditionalEscrow.sol";
  4. /**
  5. * @title RefundEscrow
  6. * @dev Escrow that holds funds for a beneficiary, deposited from multiple
  7. * parties.
  8. * @dev Intended usage: See {Escrow}. Same usage guidelines apply here.
  9. * @dev The owner account (that is, the contract that instantiates this
  10. * contract) may deposit, close the deposit period, and allow for either
  11. * withdrawal by the beneficiary, or refunds to the depositors. All interactions
  12. * with `RefundEscrow` will be made through the owner contract.
  13. */
  14. contract RefundEscrow is ConditionalEscrow {
  15. enum State { Active, Refunding, Closed }
  16. event RefundsClosed();
  17. event RefundsEnabled();
  18. State private _state;
  19. address payable private _beneficiary;
  20. /**
  21. * @dev Constructor.
  22. * @param beneficiary_ The beneficiary of the deposits.
  23. */
  24. constructor (address payable beneficiary_) {
  25. require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
  26. _beneficiary = beneficiary_;
  27. _state = State.Active;
  28. }
  29. /**
  30. * @return The current state of the escrow.
  31. */
  32. function state() public view returns (State) {
  33. return _state;
  34. }
  35. /**
  36. * @return The beneficiary of the escrow.
  37. */
  38. function beneficiary() public view returns (address) {
  39. return _beneficiary;
  40. }
  41. /**
  42. * @dev Stores funds that may later be refunded.
  43. * @param refundee The address funds will be sent to if a refund occurs.
  44. */
  45. function deposit(address refundee) public payable virtual override {
  46. require(_state == State.Active, "RefundEscrow: can only deposit while active");
  47. super.deposit(refundee);
  48. }
  49. /**
  50. * @dev Allows for the beneficiary to withdraw their funds, rejecting
  51. * further deposits.
  52. */
  53. function close() public onlyOwner virtual {
  54. require(_state == State.Active, "RefundEscrow: can only close while active");
  55. _state = State.Closed;
  56. emit RefundsClosed();
  57. }
  58. /**
  59. * @dev Allows for refunds to take place, rejecting further deposits.
  60. */
  61. function enableRefunds() public onlyOwner virtual {
  62. require(_state == State.Active, "RefundEscrow: can only enable refunds while active");
  63. _state = State.Refunding;
  64. emit RefundsEnabled();
  65. }
  66. /**
  67. * @dev Withdraws the beneficiary's funds.
  68. */
  69. function beneficiaryWithdraw() public virtual {
  70. require(_state == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
  71. _beneficiary.transfer(address(this).balance);
  72. }
  73. /**
  74. * @dev Returns whether refundees can withdraw their deposits (be refunded). The overridden function receives a
  75. * 'payee' argument, but we ignore it here since the condition is global, not per-payee.
  76. */
  77. function withdrawalAllowed(address) public view override returns (bool) {
  78. return _state == State.Refunding;
  79. }
  80. }