# Secure unit conversion in Solidity

Let's say you want mint a token TOK that is pegged to the USD. Then following must hold:
(1) `TOKUSD = USDTOK = 1`
(2) `USDETH = 1/ETHUSD`
(3) `TOKETH = 1/ETHTOK`
The formula of the exchange rate is `TOKETH = TOKUSD * USDETH = USDETH`. If ETH rises against the USD your token price goes down.
`if TOKETH = USDETH = 10, 10 TOK * ETHTOK = 10 TOK / TOKETH = 1 ETH = 10 USD`
`if TOKETH = USDETH = 20, 10 TOK / 20 = 0.5 ETH = 10 USD`
`if TOKETH = USDETH = 5, 10 TOK / 5 = 2 ETH = 10 USD`

Since Solidity has no float point regime, a division will always result in integers. Tracking the price of USDETH correctly seems not feasable.

How can one accurately code the conversion rates, without loss?

• Two possible remedies seem possible: The use of smallest units of accounting `1 Wei = 0.1*10^18 ETH` so that a division will always be a integer division. For purposes of exchange rate this does not solve the problem super well, because what if ETH rises large? `1 USD becomes 0.0001 ETH. 0.0001 = USDETH = TOKETH` The precision must be lower ** TL;DR fexible conversion rates need fixed point arithmetic, the solution must be that the resolution (precision) of the decimal places numbers of a minted token must be lower than that of Ethereum ** – Roland Kofler Aug 12 '16 at 9:21
• Insight the conversion rate must me a rational number expresst in the ratio of two integers 2:3 for example – Roland Kofler Aug 12 '16 at 13:43