Dynamic Crowdsale Rate

Hello! I am brand new to smart contracts, ERC20, and Crowdsales (a normie trying to get into crypto). I want to conduct a Crowdsale where 1 TKN would be worth one dollar (USD) in Ether. I saw the OpenZeppelin post about Crowdsales describing how this could be possible with the following math:

  • assume 1 ETH == $400
  • therefore, 10^18 wei = $400
  • therefore, 1 USD is 10^18 / 400 , or 2.5 * 10^15 wei
  • we have a decimals of 18, so we’ll use 10 ^ 18 TKNbits instead of 1 TKN
  • therefore, if the participant sends the crowdsale 2.5 * 10^15 wei we should give them 10 ^ 18 TKNbits
  • therefore the rate is 2.5 * 10^15 wei === 10^18 TKNbits , or 1 wei = 400 TKNbits
  • therefore, our rate is 400

However, what happens if the value of ETH changes from $400 throughout the time period of Crowdsale. How could I code my rate so that if the value of ETH fluctuates throughout the Crowdsale, that I would still be receiving 1 USD for each TKN? The reason I would like to know this is because I want to do a Crowdsale to help raise funds, and use those funds to buy a physical collectible, and have those who bought the TKN to become the owners of the collectible.

I was also wondering what kind of fees are associated with conducting a Crowdsale?

Thanks a lot, looking forward to being an active member here :cowboy_hat_face:

I am not sure why you want to use eth rather than a stable coin like USDC, but it is ok. I think you need an oracle to get the price of ETH, maybe you can use ChainLink.

Hey Skyge, thanks for the help. How would I go about using a stable coin like USDC instead of ETH?

I think this is almost same as the eth, maybe you can have a try like this:

USDC.transferFrom(msg.sender, RECIPIENT_ADDRESS, AMOUNT);

You can add require for this, or use safeTransferFrom, it is up to you.

But you should notice that: the decimals of USDC is 6, I am not sure what is your token, so maybe you should do some changes for this, BTW DAI is 18 decimals.

1 Like

Thanks a bunch Skyge, I'll give that a shot!

You could also get the value of ETH vs USDT/USDC through through the Uniswap router.

1 Like

Hi djrthree, just checked out what the uniswap router is. Seems like a really good idea. Just wondering if you think this would be quite costly for gas fees?

It is a non-state-changing function. It should be free.

Sounds good, thanks djrthree!