0

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.

1 Answer 1

0

I'd do something like this. I don't think there's "a correct way" to achieve this.

function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
    uint256 rate;
    if (weiAmount <= 13*10**18) { 
        rate = 66666;  
    } else if(weiAmount > 13*10**18 && weiAmount <= 25*10**18) {
        rate = 100000; 
    } else {
        rate = 400000;
    }
 
    return weiAmount.mul(rate);
}
1
  • 1
    Thanks so much for this answer, I went ahead with something very similar and its working perfectly.
    – BigMadcode
    Mar 24, 2022 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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