How to change rate/price of a token in Crowdsale?

So, I am trying to write a sale contract which increases the price or modifies the rate of a crowdsale after each purchase(when buyTokens is called). I was going to implement it with a state variable the adds (+1) each time when buyTokens is called. And writing a custom buy method. But, I just realized that there is no method to change rate. Or is it and I miissed it? Can someone help me on this?

2 Likes

Yeah, if you want to change the rate, you need to add it by yourself.

1 Like

Yes, but I am currently struggling to get the rate in my contract. How am I suppose to access it and modify it? It’s a private variable

1 Like

Maybe you can have a look at this doc: https://docs.openzeppelin.com/contracts/2.x/crowdsales#crowdsale-rate

2 Likes

Hi @PradhumnaPancholi,

There is an IncreasingPriceCrowdsale in OpenZeppelin Contracts 2.x that you could use for inspiration.

I put together an example of manually setting a crowdsale rate using this. :warning: This code has not been tested or audited. Please appropriately test and audit before using in production.
Setting crowdsale rate manually

1 Like

Hello, I was looking at setting the token price equal to, say, $1 per minted token. I know I can calculate how many wei a dollar is and set that price at deployment, but I was hoping that there is a way to check this each time the token is minted.

Or, could I simply have the rate pegged to a different token (instead of eth), so that it would only mint when it receives USDC? Such that the rate could still be "1" but it only responds to USDC or FRAX or something along those lines. Or does it just utilize the chain's base token: if deployed on Polygon, a rate of 1 would be 1 TOK for 1 matic? (I would think that it would be in the chain's base coin, since that is the gwei you're paying your gas in)

For example, I deploy when 1 eth = $3000, I could code in that you'd get 3000TOK for 1 eth. But then the price could climb to $4000 which would still only issue 3000 TOK.

Thank you!

Why is the rate() function revert?

   function rate() public view returns (uint256) {
        revert("IncreasingPriceCrowdsale: rate() called");
    }

Wouldn't it be useful if it returned the current rate value knowing that it precisely changes over time?

You will have to connect your smart contract to an oracle that returns the price of the token you need and thus update it from time to time.

https://docs.chain.link/docs/get-the-latest-price/

Thanks for the link to that! Is there instead a way to tell it to accept only a different token, like USDC?

Also, does the wei rate calculate in comparison to the chain's base token, like MATIC on Polygon?

I cannot answer your questions because although I know this service I have never used it. Maybe another reader can help you.

[image]