RefundableCrowdsale.sol 2.6 KB

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