RefundEscrow.sol 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.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. using Address for address payable;
  16. enum State { Active, Refunding, Closed }
  17. event RefundsClosed();
  18. event RefundsEnabled();
  19. State private _state;
  20. address payable private _beneficiary;
  21. /**
  22. * @dev Constructor.
  23. * @param beneficiary_ The beneficiary of the deposits.
  24. */
  25. constructor (address payable beneficiary_) {
  26. require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
  27. _beneficiary = beneficiary_;
  28. _state = State.Active;
  29. }
  30. /**
  31. * @return The current state of the escrow.
  32. */
  33. function state() public view virtual returns (State) {
  34. return _state;
  35. }
  36. /**
  37. * @return The beneficiary of the escrow.
  38. */
  39. function beneficiary() public view virtual returns (address payable) {
  40. return _beneficiary;
  41. }
  42. /**
  43. * @dev Stores funds that may later be refunded.
  44. * @param refundee The address funds will be sent to if a refund occurs.
  45. */
  46. function deposit(address refundee) public payable virtual override {
  47. require(state() == State.Active, "RefundEscrow: can only deposit while active");
  48. super.deposit(refundee);
  49. }
  50. /**
  51. * @dev Allows for the beneficiary to withdraw their funds, rejecting
  52. * further deposits.
  53. */
  54. function close() public virtual onlyOwner {
  55. require(state() == State.Active, "RefundEscrow: can only close while active");
  56. _state = State.Closed;
  57. emit RefundsClosed();
  58. }
  59. /**
  60. * @dev Allows for refunds to take place, rejecting further deposits.
  61. */
  62. function enableRefunds() public onlyOwner virtual {
  63. require(state() == State.Active, "RefundEscrow: can only enable refunds while active");
  64. _state = State.Refunding;
  65. emit RefundsEnabled();
  66. }
  67. /**
  68. * @dev Withdraws the beneficiary's funds.
  69. */
  70. function beneficiaryWithdraw() public virtual {
  71. require(state() == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
  72. beneficiary().sendValue(address(this).balance);
  73. }
  74. /**
  75. * @dev Returns whether refundees can withdraw their deposits (be refunded). The overridden function receives a
  76. * 'payee' argument, but we ignore it here since the condition is global, not per-payee.
  77. */
  78. function withdrawalAllowed(address) public view override returns (bool) {
  79. return state() == State.Refunding;
  80. }
  81. }