CappedCrowdsale.sol 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 public 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. * @dev Checks whether the cap has been reached.
  21. * @return Whether the cap was reached
  22. */
  23. function capReached() public view returns (bool) {
  24. return weiRaised >= cap;
  25. }
  26. /**
  27. * @dev Extend parent behavior requiring purchase to respect the funding cap.
  28. * @param _beneficiary Token purchaser
  29. * @param _weiAmount Amount of wei contributed
  30. */
  31. function _preValidatePurchase(
  32. address _beneficiary,
  33. uint256 _weiAmount
  34. )
  35. internal
  36. {
  37. super._preValidatePurchase(_beneficiary, _weiAmount);
  38. require(weiRaised.add(_weiAmount) <= cap);
  39. }
  40. }