# Using Openzeppelin's crowdsale contract as a base, how can I set the rate to be a decimal number?

For example if rate = 10 then for every wei the buyer gets 10 "token units" which are each 1*10^18 of a full token (as the default decimal for the erc20 token is 18).

If I want the users in a presale to get 15% more tokens, how can I set the rate to a number equivalent to 11.5 so that each wei receives 11.5 "token units"?

I am struggling to see how I can implement this.

Thank you.

default decimal for the erc20 token is 18

Not exactly. You can decide the decimals of your token. You are not bound to keep this 18. You can choose any number. Ether has 18 decimals. So this means the smallest value that can exist 1 wei. You can never trade less than 1 wei.

Depending on your requirement, you can decide decimals of your token. For eg, you can keep your decimals to be 8.

So, if you define rate=10, this means 1 wei = 1,000,000,000 tokens and increasing to 15%, 1 wei = 1.150,000,000

Now even if you use 18 decimal places. When you set rate=10, then if someone transfer 10 wei to you, you need to transfer 10000000000000000000 (10*10^18) token and if you want to pay 15% more, when someone sends you 10 wei, transfer 11500000000000000000 (11.5^10**18) tokens

• Its not 10 units, lets assume 15wei then? – Jitendra Kumar. Balla Feb 14 '18 at 11:31
• You mean the rate being 15 or rate is 10 and user send 15 wei? – Prashant Prabhakar Singh Feb 14 '18 at 11:35
• In case of rate being 10 and user sends 15 wei, then tokens transferred will be 15*10^18. And with 15% discount , tokens that needs to be sent will be 1725*10^16. – Prashant Prabhakar Singh Feb 14 '18 at 11:38
• Lets assume user pay for 15wei and rate is 1.15,15+(15*0.15) = 17.25 units. – Jitendra Kumar. Balla Feb 14 '18 at 11:38
• I'll update solution. soon – Jitendra Kumar. Balla Feb 14 '18 at 11:39

You can do using two ways:

1. One increases decimal places 18 to 20. As we know Eth can't hold less than wei. Because 1 wei is 115 units in your case.

So final statement would be 1 wei = (100+(100*0.15))115 units

Lets do some math now

 1 eth = 18 decimals  //In this case 1 wei is 1 uint of your token. i.e 1eth every thing works fine as per requirement.


Now Lets assume

  1 wei = 115 that means we need to hold fractional points now lets multiply with 100.

1eth  = 18 decimal + 2 decimals = 20 decimals


Now lets calculate 15+(15*0.15) = 15*115 = 1725 value in balances map, exchange will get decimal value form contract 20, then divide account[balance]/10^20 = 17.25 e^18.

Conclusion: multiply wei value with 115 and make sure decimals are 20. Then your problem solved.

• OP doesn't need to upgrade decimal places until he wants to have a rate of 1 wei = 1 or more token (including decimals like 1.15 ). But if he wants to have less than 1 token per wei, the he needs to increase decimal value. With 20 decimals OP can have 1 wei = 0.01 toke or more. Similarly for 21 decimals places we can have 1 wei = 0.001 token and so on – Prashant Prabhakar Singh Feb 14 '18 at 12:19

You have to use "SafeMath.sol" library SafeMath Library on github OpenZeppelin

And Use it in your contract as follows:

import "./SafeMath.sol";

contract CrowdSaleContract{
using SafeMath for uint;
uint baseTokenAmount = 10;

tokenTrnsfer(