CappedCrowdsale.sol 892 B

12345678910111213141516171819202122232425262728293031323334
  1. pragma solidity ^0.4.11;
  2. import '../math/SafeMath.sol';
  3. import './Crowdsale.sol';
  4. /**
  5. * @title CappedCrowdsale
  6. * @dev Extension of Crowsdale with a max amount of funds raised
  7. */
  8. contract CappedCrowdsale is Crowdsale {
  9. using SafeMath for uint256;
  10. uint256 public cap;
  11. function CappedCrowdsale(uint256 _cap) {
  12. require(_cap > 0);
  13. cap = _cap;
  14. }
  15. // overriding Crowdsale#validPurchase to add extra cap logic
  16. // @return true if investors can buy at the moment
  17. function validPurchase() internal constant returns (bool) {
  18. bool withinCap = weiRaised.add(msg.value) <= cap;
  19. return super.validPurchase() && withinCap;
  20. }
  21. // overriding Crowdsale#hasEnded to add cap logic
  22. // @return true if crowdsale event has ended
  23. function hasEnded() public constant returns (bool) {
  24. bool capReached = weiRaised >= cap;
  25. return super.hasEnded() || capReached;
  26. }
  27. }