RefundableCrowdsale.sol 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. pragma solidity ^0.4.24;
  2. import "../../math/SafeMath.sol";
  3. import "./FinalizableCrowdsale.sol";
  4. import "../../payment/RefundEscrow.sol";
  5. /**
  6. * @title RefundableCrowdsale
  7. * @dev Extension of Crowdsale contract that adds a funding goal, and
  8. * the possibility of users getting a refund if goal is not met.
  9. */
  10. contract RefundableCrowdsale is FinalizableCrowdsale {
  11. using SafeMath for uint256;
  12. // minimum amount of funds to be raised in weis
  13. uint256 public goal;
  14. // refund escrow used to hold funds while crowdsale is running
  15. RefundEscrow private escrow;
  16. /**
  17. * @dev Constructor, creates RefundEscrow.
  18. * @param _goal Funding goal
  19. */
  20. constructor(uint256 _goal) public {
  21. require(_goal > 0);
  22. escrow = new RefundEscrow(wallet);
  23. goal = _goal;
  24. }
  25. /**
  26. * @dev Investors can claim refunds here if crowdsale is unsuccessful
  27. */
  28. function claimRefund() public {
  29. require(isFinalized);
  30. require(!goalReached());
  31. escrow.withdraw(msg.sender);
  32. }
  33. /**
  34. * @dev Checks whether funding goal was reached.
  35. * @return Whether funding goal was reached
  36. */
  37. function goalReached() public view returns (bool) {
  38. return weiRaised >= goal;
  39. }
  40. /**
  41. * @dev escrow finalization task, called when owner calls finalize()
  42. */
  43. function finalization() internal {
  44. if (goalReached()) {
  45. escrow.close();
  46. escrow.beneficiaryWithdraw();
  47. } else {
  48. escrow.enableRefunds();
  49. }
  50. super.finalization();
  51. }
  52. /**
  53. * @dev Overrides Crowdsale fund forwarding, sending funds to escrow.
  54. */
  55. function _forwardFunds() internal {
  56. escrow.deposit.value(msg.value)(msg.sender);
  57. }
  58. }