Skip to main content
edited title
Link

How What's a good way to override _getTokenAmount() inimplement multiple rates into Crowdsale contract?

Source Link

How to override _getTokenAmount() in Crowdsale contract

i am trying to deploy an allowance crowdsale contract, I have it working just fine. But I have multiple rates depending on the amount of ETH that is sent to the contract. I have deployed 3 different contracts for each rate but that seems super inefficient.

My rates are:

If weiAmount < 1310**18 rate is 66666

If weiAmount > 131018 && < 25*1018 rate is 100000

If weiAmount => 26*10**18 rate is 400000

I know I need to override this function:

/**
     * @dev Override to extend the way in which ether is converted to tokens.
     * @param weiAmount Value in wei to be converted into tokens
     * @return Number of tokens that can be purchased with the specified _weiAmount
     */
    function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
        return weiAmount.mul(_rate);
    }

Now logically I think that setting rate at 400000 once and then dividing it by 4 or 6 depending on wieAmount is the best approach.

Qustion #1. is there a better way?

Question #2. How can I write this in Solidity without breaking any furniture?

🔢 Code to reproduce

Here is my Contract:

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";
import "@openzeppelin/contracts/crowdsale/emission/AllowanceCrowdsale.sol";


  contract RedBasicPresubs is Crowdsale, AllowanceCrowdsale {
    constructor(
        uint256 rate,
        address payable wallet,
        IERC20 token,
        address tokenWallet  // <- new argument
    )
        AllowanceCrowdsale(tokenWallet)  // <- used here
        Crowdsale(rate, wallet, token)
        public
    
{
    
}
    

 

This is how I am thinking of doing it:

/**
     * @dev Override to extend the way in which ether is converted to tokens.
     * @param weiAmount Value in wei to be converted into tokens
     * @return Number of tokens that can be purchased with the specified _weiAmount
     */
     function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
      super._getTokenAmount(uint256 weiAmount);
          if (weiAmount < 13*10**18) rate = rate;  else if(weiAmount > 13*10**18 && weiAmount < 25*10**18) rate = rate.div(4); else(rate = rate.div(6));
     
        return weiAmount.mul(_rate);
    }

This is producing several errors, mainly Expected '(' but got identifier

Maybe I have been looking at it for too long now, can anyone guide me on the correct way to achieve this?

Thanks in advance.