RefundableCrowdsale.sol 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. pragma solidity ^0.5.0;
  2. import "../../math/SafeMath.sol";
  3. import "./FinalizableCrowdsale.sol";
  4. import "../../payment/escrow/RefundEscrow.sol";
  5. /**
  6. * @title RefundableCrowdsale
  7. * @dev Extension of `FinalizableCrowdsale` contract that adds a funding goal, and the possibility of users
  8. * getting a refund if goal is not met.
  9. *
  10. * Deprecated, use `RefundablePostDeliveryCrowdsale` instead. Note that if you allow tokens to be traded before the goal
  11. * is met, then an attack is possible in which the attacker purchases tokens from the crowdsale and when they sees that
  12. * the goal is unlikely to be met, they sell their tokens (possibly at a discount). The attacker will be refunded when
  13. * the crowdsale is finalized, and the users that purchased from them will be left with worthless tokens.
  14. */
  15. contract RefundableCrowdsale is FinalizableCrowdsale {
  16. using SafeMath for uint256;
  17. // minimum amount of funds to be raised in weis
  18. uint256 private _goal;
  19. // refund escrow used to hold funds while crowdsale is running
  20. RefundEscrow private _escrow;
  21. /**
  22. * @dev Constructor, creates RefundEscrow.
  23. * @param goal Funding goal
  24. */
  25. constructor (uint256 goal) public {
  26. require(goal > 0, "RefundableCrowdsale: goal is 0");
  27. _escrow = new RefundEscrow(wallet());
  28. _goal = goal;
  29. }
  30. /**
  31. * @return minimum amount of funds to be raised in wei.
  32. */
  33. function goal() public view returns (uint256) {
  34. return _goal;
  35. }
  36. /**
  37. * @dev Investors can claim refunds here if crowdsale is unsuccessful.
  38. * @param refundee Whose refund will be claimed.
  39. */
  40. function claimRefund(address payable refundee) public {
  41. require(finalized(), "RefundableCrowdsale: not finalized");
  42. require(!goalReached(), "RefundableCrowdsale: goal reached");
  43. _escrow.withdraw(refundee);
  44. }
  45. /**
  46. * @dev Checks whether funding goal was reached.
  47. * @return Whether funding goal was reached
  48. */
  49. function goalReached() public view returns (bool) {
  50. return weiRaised() >= _goal;
  51. }
  52. /**
  53. * @dev Escrow finalization task, called when finalize() is called.
  54. */
  55. function _finalization() internal {
  56. if (goalReached()) {
  57. _escrow.close();
  58. _escrow.beneficiaryWithdraw();
  59. } else {
  60. _escrow.enableRefunds();
  61. }
  62. super._finalization();
  63. }
  64. /**
  65. * @dev Overrides Crowdsale fund forwarding, sending funds to escrow.
  66. */
  67. function _forwardFunds() internal {
  68. _escrow.deposit.value(msg.value)(msg.sender);
  69. }
  70. }