Adding Minimum and Maximum Purchased Failed Crowdsale

I am having trouble adding minimum and maximum purchase for my crowdsale. What am I doing wrong ? been stuck for 3 hours. Thanks for any help

   pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/crowdsale/Crowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/crowdsale/emission/AllowanceCrowdsale.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/crowdsale/validation/CappedCrowdsale.sol";

// contracts/SimpleCrowdsale.sol
// SPDX-License-Identifier: MIT


 

 
contract MyCrowdsale is Crowdsale , AllowanceCrowdsale,CappedCrowdsale {

uint256 public investorMinCap = 100000000000000000;

uint256 public investorHardCap =30000000000000000000;
    
   mapping(address => uint256) private _contributions;

   constructor (uint256 rate,uint256 cap, address payable wallet, IERC20 token, address tokenWallet)
    public
    Crowdsale(rate, wallet, token)
    AllowanceCrowdsale(tokenWallet)
    CappedCrowdsale(cap)
{
    // solhint-disable-previous-line no-empty-blocks
}


function _preValidatePurchase(
address _beneficiary,
uint256 _weiAmount
  )
internal view
  {
super._preValidatePurchase(_beneficiary, _weiAmount);
  
   
  require(_contributions[_beneficiary].add(_weiAmount) >= investorMinCap  && _contributions[_beneficiary].add(_weiAmount) <= investorHardCap);
   
    
}


function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
    super._updatePurchasingState(beneficiary, weiAmount);
    _contributions[beneficiary] = _contributions[beneficiary].add(weiAmount);
}


   

}

Hi @Hello,

Have a look at IndividuallyCappedCrowdsale how it checks each purchasers cap. You can do similar but with your hardcoded cap:

Also see the tests

1 Like

Thank you for the response sir.

I was able to solve the hard cap problem . I’m just wondering why the minimum cap won’t work .

  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal view
  {
    super._preValidatePurchase(beneficiary, weiAmount);
  
     require(_contributions[beneficiary].add(weiAmount) >= investorMinCap && _contributions[beneficiary].add(weiAmount) <= investorHardCap, "Beneficiary's cap exceeded");
    
        
    }
    
    
    function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
        super._updatePurchasingState(beneficiary, weiAmount);
        _contributions[beneficiary] = _contributions[beneficiary].add(weiAmount);
    }
1 Like

Hi @Hello,

I suggest putting the minimum in its own require as it will be easier to create a unit test for.

1 Like

Will try sir Thank you

2 Likes

Hello bro plz can you share me the code for your crowdesale, am having similar problem for the past 3 days?