This function from the openzeppelin crowdsale sample is marked view as seen here:

 // Override this method to have a way to add business logic to your crowdsale when buying

  function getTokenAmount(uint256 weiAmount) internal view 
returns(uint256) {
    return weiAmount.mul(rate);

If I want to add logic to alter the rate based on time (for a tiered ICO), is it safe to remove the view constraint and place that logic in this function?

I'm not sure if it had a view modifier for performance/cost reasons or if it is a security feature that could cause problems if I remove it.

I want to implement logic and I think it would be more suited to the function that calculates the token amount than anywhere else?

I'm very new to this though and appreciate any help or answers you may have.

Thank you.


view is used to indicate that a function does not modify the state.

Unless you edits will modify the state itself, which is not recommended for a getter function, it's actually just fine to keep it.

If your function edit will alter the state, I recommend just making a new function and for good practice, not using get as the beginning of the function name (which usually represents getters).

Either way, you go...it's relatively safe to remove.

More info here: http://solidity.readthedocs.io/en/develop/contracts.html#view-functions

| improve this answer | |

The keyword view means that it will not alter the behaviour of future interactions with any contract (it doesn't change the state), so to answer your question, you do not need to remove it and yes this is the right place to add logic that will calculate the token amount depending on your ICO tier constraints.

I'm guessing you want to give extra tokens depending if now is between certain date/time, so you would simply override that function in your crowdsale contract adding in it that logic, for example:

function getTokenAmount(uint256 weiAmount) internal view returns(uint256) {
    uint256 tokens = weiAmount.mul(rate);
    if(now <= 1519858800){ //March 1st, 2018
        tokens = tokens.add(25); //25 extra tokens
    return tokens;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.