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. }