RefundableCrowdsale.sol 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * @param _beneficiary Whose refund will be claimed.
  28. */
  29. function claimRefund(address _beneficiary) public {
  30. require(isFinalized);
  31. require(!goalReached());
  32. escrow_.withdraw(_beneficiary);
  33. }
  34. /**
  35. * @dev Checks whether funding goal was reached.
  36. * @return Whether funding goal was reached
  37. */
  38. function goalReached() public view returns (bool) {
  39. return weiRaised >= goal;
  40. }
  41. /**
  42. * @dev escrow finalization task, called when owner calls finalize()
  43. */
  44. function _finalization() internal {
  45. if (goalReached()) {
  46. escrow_.close();
  47. escrow_.beneficiaryWithdraw();
  48. } else {
  49. escrow_.enableRefunds();
  50. }
  51. super._finalization();
  52. }
  53. /**
  54. * @dev Overrides Crowdsale fund forwarding, sending funds to escrow.
  55. */
  56. function _forwardFunds() internal {
  57. escrow_.deposit.value(msg.value)(msg.sender);
  58. }
  59. }