RefundEscrow.sol 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. pragma solidity ^0.4.24;
  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 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 onlyOwner {
  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 onlyOwner {
  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. }