CappedCrowdsale.sol 864 B

123456789101112131415161718192021222324252627282930313233
  1. pragma solidity ^0.4.11;
  2. import '../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. cap = _cap;
  13. }
  14. // overriding Crowdsale#purchaseValid to add extra cap logic
  15. // @return true if investors can buy at the moment
  16. function purchaseValid() internal constant returns (bool) {
  17. bool withinCap = weiRaised.add(msg.value) <= cap;
  18. return super.purchaseValid() && withinCap;
  19. }
  20. // overriding Crowdsale#hasEnded to add cap logic
  21. // @return true if crowdsale event has ended
  22. function hasEnded() public constant returns (bool) {
  23. bool capReached = weiRaised >= cap;
  24. return super.hasEnded() || capReached;
  25. }
  26. }