CappedCrowdsale.sol 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. pragma solidity ^0.5.0;
  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, "CappedCrowdsale: cap is 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(address beneficiary, uint256 weiAmount) internal view {
  38. super._preValidatePurchase(beneficiary, weiAmount);
  39. require(weiRaised().add(weiAmount) <= _cap, "CappedCrowdsale: cap exceeded");
  40. }
  41. }