CappedCrowdsale.sol 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. pragma solidity ^0.4.24;
  2. import "../../math/SafeMath.sol";
  3. import "../Crowdsale.sol";
  4. /**
  5. * @title CappedCrowdsale
  6. * @dev Crowdsale with a limit for total contributions.
  7. */
  8. contract CappedCrowdsale is Crowdsale {
  9. using SafeMath for uint256;
  10. uint256 private cap_;
  11. /**
  12. * @dev Constructor, takes maximum amount of wei accepted in the crowdsale.
  13. * @param _cap Max amount of wei to be contributed
  14. */
  15. constructor(uint256 _cap) public {
  16. require(_cap > 0);
  17. cap_ = _cap;
  18. }
  19. /**
  20. * @return the cap of the crowdsale.
  21. */
  22. function cap() public view returns(uint256) {
  23. return cap_;
  24. }
  25. /**
  26. * @dev Checks whether the cap has been reached.
  27. * @return Whether the cap was reached
  28. */
  29. function capReached() public view returns (bool) {
  30. return weiRaised() >= cap_;
  31. }
  32. /**
  33. * @dev Extend parent behavior requiring purchase to respect the funding cap.
  34. * @param _beneficiary Token purchaser
  35. * @param _weiAmount Amount of wei contributed
  36. */
  37. function _preValidatePurchase(
  38. address _beneficiary,
  39. uint256 _weiAmount
  40. )
  41. internal
  42. {
  43. super._preValidatePurchase(_beneficiary, _weiAmount);
  44. require(weiRaised().add(_weiAmount) <= cap_);
  45. }
  46. }