RefundEscrow.sol 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. pragma solidity ^0.4.24;
  2. import "./ConditionalEscrow.sol";
  3. import "../ownership/Secondary.sol";
  4. /**
  5. * @title RefundEscrow
  6. * @dev Escrow that holds funds for a beneficiary, deposited from multiple parties.
  7. * The primary account may close the deposit period, and allow for either withdrawal
  8. * by the beneficiary, or refunds to the depositors.
  9. */
  10. contract RefundEscrow is Secondary, ConditionalEscrow {
  11. enum State { Active, Refunding, Closed }
  12. event Closed();
  13. event RefundsEnabled();
  14. State private _state;
  15. address private _beneficiary;
  16. /**
  17. * @dev Constructor.
  18. * @param beneficiary The beneficiary of the deposits.
  19. */
  20. constructor(address beneficiary) public {
  21. require(beneficiary != address(0));
  22. _beneficiary = beneficiary;
  23. _state = State.Active;
  24. }
  25. /**
  26. * @return the current state of the escrow.
  27. */
  28. function state() public view returns (State) {
  29. return _state;
  30. }
  31. /**
  32. * @return the beneficiary of the escrow.
  33. */
  34. function beneficiary() public view returns (address) {
  35. return _beneficiary;
  36. }
  37. /**
  38. * @dev Stores funds that may later be refunded.
  39. * @param refundee The address funds will be sent to if a refund occurs.
  40. */
  41. function deposit(address refundee) public payable {
  42. require(_state == State.Active);
  43. super.deposit(refundee);
  44. }
  45. /**
  46. * @dev Allows for the beneficiary to withdraw their funds, rejecting
  47. * further deposits.
  48. */
  49. function close() public onlyPrimary {
  50. require(_state == State.Active);
  51. _state = State.Closed;
  52. emit Closed();
  53. }
  54. /**
  55. * @dev Allows for refunds to take place, rejecting further deposits.
  56. */
  57. function enableRefunds() public onlyPrimary {
  58. require(_state == State.Active);
  59. _state = State.Refunding;
  60. emit RefundsEnabled();
  61. }
  62. /**
  63. * @dev Withdraws the beneficiary's funds.
  64. */
  65. function beneficiaryWithdraw() public {
  66. require(_state == State.Closed);
  67. _beneficiary.transfer(address(this).balance);
  68. }
  69. /**
  70. * @dev Returns whether refundees can withdraw their deposits (be refunded).
  71. */
  72. function withdrawalAllowed(address payee) public view returns (bool) {
  73. return _state == State.Refunding;
  74. }
  75. }