- Contracts can have a cap to restrict the amount that can be raised through the crowdsale. Create a variable to store the cap restriction:
uint256 public cap;
- The value is assigned through the constructor. Add the initialization to the constructor along with the rate, wallet, and token:
constructor(uint256 _cap, ....) public { // Other validations and assignment require(_cap > 0); cap = _cap;}
- During the token purchase operation, add an additional condition to verify the cap:
function buyTokens(address _investor) public payable { require(weiRaised.add(_weiAmount) <= cap); //..}
- Create a read-only function that can be used to check whether the cap has been reached. It returns true or false:
function capReached() public ...