RefundEscrow.sol 2.1 KB

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