RefundEscrow.sol 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. pragma solidity ^0.4.23;
  2. import "./ConditionalEscrow.sol";
  3. import "../ownership/Ownable.sol";
  4. /**
  5. * @title RefundEscrow
  6. * @dev Escrow that holds funds for a beneficiary, deposited from multiple parties.
  7. * The contract owner may close the deposit period, and allow for either withdrawal
  8. * by the beneficiary, or refunds to the depositors.
  9. */
  10. contract RefundEscrow is Ownable, ConditionalEscrow {
  11. enum State { Active, Refunding, Closed }
  12. event Closed();
  13. event RefundsEnabled();
  14. State public state;
  15. address public 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. * @dev Stores funds that may later be refunded.
  27. * @param _refundee The address funds will be sent to if a refund occurs.
  28. */
  29. function deposit(address _refundee) public payable {
  30. require(state == State.Active);
  31. super.deposit(_refundee);
  32. }
  33. /**
  34. * @dev Allows for the beneficiary to withdraw their funds, rejecting
  35. * further deposits.
  36. */
  37. function close() public onlyOwner {
  38. require(state == State.Active);
  39. state = State.Closed;
  40. emit Closed();
  41. }
  42. /**
  43. * @dev Allows for refunds to take place, rejecting further deposits.
  44. */
  45. function enableRefunds() public onlyOwner {
  46. require(state == State.Active);
  47. state = State.Refunding;
  48. emit RefundsEnabled();
  49. }
  50. /**
  51. * @dev Withdraws the beneficiary's funds.
  52. */
  53. function beneficiaryWithdraw() public {
  54. require(state == State.Closed);
  55. beneficiary.transfer(address(this).balance);
  56. }
  57. /**
  58. * @dev Returns whether refundees can withdraw their deposits (be refunded).
  59. */
  60. function withdrawalAllowed(address _payee) public view returns (bool) {
  61. return state == State.Refunding;
  62. }
  63. }