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) internal {
  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. view
  43. {
  44. super._preValidatePurchase(beneficiary, weiAmount);
  45. require(weiRaised().add(weiAmount) <= _cap);
  46. }
  47. }