Changing rate manually in a crowdsale

So, I have written a crowdsale and configured it to change rate after every purchase. I just want to confirm that my way on changing rate is correct here.

This is what I am doing.

 uint256 public currentRate = 0;
    constructor(
        uint256 initialRate,
        address payable wallet, //This is where ETH/funds go to
        IERC20 token,
        address tokenWallet //this wallet holds the tokens($BOOKS)//
    )
        AllowanceCrowdsale(tokenWallet)
        Crowdsale(initialRate, wallet, token)
        public
        {
            currentRate = initialRate;
        }

    function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
        // over riding the post purchase method to update the rate
        uint256 updatedBy = 1000000000000000; // 0.001 ethers in weis
        currentRate = currentRate.add(updatedBy);
    }
1 Like

Hi @PradhumnaPancholi,

I assume you are only showing a cut down version of your contract and that you have overriden rate to return the current rate, and _getTokenAmount to use your changing rate.

It appears ok, though for anyone reading this I always recommend appropriate testing and auditing.

2 Likes

Yes, I am showing cut down version

function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {

        return weiAmount.mul(currentRate);

    }

    // @DEV: overriding the rate method to return updated rate instead of initialRate //

    // here, we are returning "currentRate" as that's the value that gets updated after each purchase//

    function rate() public view returns (uint256) {

        return currentRate;
    } 

My question is mainly in regards to changing the price. To avoid any confusion, “price” means how much ETH for 1 Token(18 decimal). And rate is what we have in Openzeppelin crowdsale. So, I want to increase the price by 0.001 ETH of each token after every purchase. My question is if this is the correct way to do that?

1 Like

Hi @PradhumnaPancholi,

Sorry for the delay in responding I was on vacation for the holidays.

If you are changing the price after each purchase (rather than after a certain amount of tokens sold or Ether raised), you may want to consider what happens with frequent small purchases and what incentives/disincentives this creates.

1 Like

Got it, thanks for the help.

And sorry for my delayed response. I took kind of a break recently.

1 Like

I need help urgently with setting up my rate. I want to make 1ETH = 0.1TKN.

But I can't seem to use 0.1 as the rate, I keep getting error.