When is it necessary to use .super and or Override on crowdsale?

Hi Team,
I am working on a crowdsale contract, I have deployed it to testnet and it seems to be working just fine.

But I am Overriding this function in order to limit the minimum investment on the contract...

 /**
     * @dev Overrides parent behavior by checking message value minimum.
     */

    function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
        require(beneficiary != address(0), "Crowdsale: beneficiary is the zero address");
        require(weiAmount != 0, "Crowdsale: weiAmount is 0");
        require(weiAmount < 13*10**18, "Crowdsale: Amount above Subscription maximum");
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
    }

The OZ contract says the following if Overriding.

/**
     * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met.
     * Use `super` in contracts that inherit from Crowdsale to extend their validations.
     * Example from CappedCrowdsale.sol's _preValidatePurchase method:
     *     super._preValidatePurchase(beneficiary, weiAmount);
     *     require(weiRaised().add(weiAmount) <= cap);
     * @param beneficiary Address performing the token purchase
     * @param weiAmount Value in wei involved in the purchase
     */
    function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
        require(beneficiary != address(0), "Crowdsale: beneficiary is the zero address");
        require(weiAmount != 0, "Crowdsale: weiAmount is 0");
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
    }

Have I done this correctly or do I need to implement .super????

:1234: Code to reproduce

Same as above

/**
     * @dev Overrides parent behavior by checking message value minimum.
     */

    function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
        require(beneficiary != address(0), "Crowdsale: beneficiary is the zero address");
        require(weiAmount != 0, "Crowdsale: weiAmount is 0");
        require(weiAmount < 13*10**18, "Crowdsale: Amount above Subscription maximum");
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
    }

:computer: Environment

Truffle VS Code solidity 0.5.0

We no longer support the Crowdsale contracts so I don't remember about this particular function. Generally you should always call the super version of a function, especially for "hook" kind of functions which this "preValidate" seems to be.

1 Like

Thanks for the reply, I tried to call super on it and I immediately got a syntax error "Expected identifier but got '('. "

Or maybe this is the correct approach?

 /**
     * @dev Overrides parent behavior by checking message value minimum.
     */

     function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
         super._preValidatePurchase(beneficiary, weiAmount);
        require(beneficiary != address(0), "Crowdsale: beneficiary is the zero address");
        require(weiAmount != 0, "Crowdsale: weiAmount is 0");
        require(weiAmount < 13*10**18, "Crowdsale: Amount above Subscription maximum");
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
    }

Yes, that snippet looks right!

1 Like

Thanks so much.

Idiotically I have multiple rates on this project, and in order to avoid errors I am deploying multiple contracts for each rate. Now that seems very inefficient and pricey so I am trying to find a different approach.
Please take a look at this and let me know what you think at your earliest convenience.