CappedCrowdsale.sol 851 B

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