RefundVault.sol 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. pragma solidity ^0.4.23;
  2. import "../../../math/SafeMath.sol";
  3. import "../../../ownership/Ownable.sol";
  4. /**
  5. * @title RefundVault
  6. * @dev This contract is used for storing funds while a crowdsale
  7. * is in progress. Supports refunding the money if crowdsale fails,
  8. * and forwarding it if crowdsale is successful.
  9. */
  10. contract RefundVault is Ownable {
  11. using SafeMath for uint256;
  12. enum State { Active, Refunding, Closed }
  13. mapping (address => uint256) public deposited;
  14. address public wallet;
  15. State public state;
  16. event Closed();
  17. event RefundsEnabled();
  18. event Refunded(address indexed beneficiary, uint256 weiAmount);
  19. /**
  20. * @param _wallet Vault address
  21. */
  22. constructor(address _wallet) public {
  23. require(_wallet != address(0));
  24. wallet = _wallet;
  25. state = State.Active;
  26. }
  27. /**
  28. * @param investor Investor address
  29. */
  30. function deposit(address investor) onlyOwner public payable {
  31. require(state == State.Active);
  32. deposited[investor] = deposited[investor].add(msg.value);
  33. }
  34. function close() onlyOwner public {
  35. require(state == State.Active);
  36. state = State.Closed;
  37. emit Closed();
  38. wallet.transfer(address(this).balance);
  39. }
  40. function enableRefunds() onlyOwner public {
  41. require(state == State.Active);
  42. state = State.Refunding;
  43. emit RefundsEnabled();
  44. }
  45. /**
  46. * @param investor Investor address
  47. */
  48. function refund(address investor) public {
  49. require(state == State.Refunding);
  50. uint256 depositedValue = deposited[investor];
  51. deposited[investor] = 0;
  52. investor.transfer(depositedValue);
  53. emit Refunded(investor, depositedValue);
  54. }
  55. }