RefundableCrowdsale.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 private 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. * @return minimum amount of funds to be raised in wei.
  27. */
  28. function goal() public view returns(uint256) {
  29. return goal_;
  30. }
  31. /**
  32. * @dev Investors can claim refunds here if crowdsale is unsuccessful
  33. * @param _beneficiary Whose refund will be claimed.
  34. */
  35. function claimRefund(address _beneficiary) public {
  36. require(finalized());
  37. require(!goalReached());
  38. escrow_.withdraw(_beneficiary);
  39. }
  40. /**
  41. * @dev Checks whether funding goal was reached.
  42. * @return Whether funding goal was reached
  43. */
  44. function goalReached() public view returns (bool) {
  45. return weiRaised() >= goal_;
  46. }
  47. /**
  48. * @dev escrow finalization task, called when finalize() is called
  49. */
  50. function _finalization() internal {
  51. if (goalReached()) {
  52. escrow_.close();
  53. escrow_.beneficiaryWithdraw();
  54. } else {
  55. escrow_.enableRefunds();
  56. }
  57. super._finalization();
  58. }
  59. /**
  60. * @dev Overrides Crowdsale fund forwarding, sending funds to escrow.
  61. */
  62. function _forwardFunds() internal {
  63. escrow_.deposit.value(msg.value)(msg.sender);
  64. }
  65. }