CappedCrowdsale.sol 892 B

1234567891011121314151617181920212223242526272829303132333435
  1. pragma solidity ^0.4.18;
  2. import "../math/SafeMath.sol";
  3. import "./Crowdsale.sol";
  4. /**
  5. * @title CappedCrowdsale
  6. * @dev Extension of Crowdsale 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) public {
  12. require(_cap > 0);
  13. cap = _cap;
  14. }
  15. // overriding Crowdsale#hasEnded to add cap logic
  16. // @return true if crowdsale event has ended
  17. function hasEnded() public view returns (bool) {
  18. bool capReached = weiRaised >= cap;
  19. return super.hasEnded() || capReached;
  20. }
  21. // overriding Crowdsale#validPurchase to add extra cap logic
  22. // @return true if investors can buy at the moment
  23. function validPurchase() internal view returns (bool) {
  24. bool withinCap = weiRaised.add(msg.value) <= cap;
  25. return super.validPurchase() && withinCap;
  26. }
  27. }