0

I just learnt a lot about solidity and decided to put it into practice. Something I truly don't understand is how I can bypass the limitations of this programming language.

Assume the following scenario:

We have some sort of ICO where users exchange their altcoins for our own coin, for instance CoinA.

Today CoinA = $1

Tomorrow we set CoinA = $1.5

After-tomorrow we set CoinA = $1.75

Let's assume for now that altcoins have a stable value:

Altcoin1 = $0.222

Altcoin2 = $0.7

The problem I have is that solidity doesn't allow us to use decimals. Answers that I found say to use all of the 18 decimals that Ethereum offers however I'm truly confused about that. Nobody shows a way to implement in a practical scenario.

Consider the following code:

pragma solidity ^0.4.24;
contract Ballot {
    uint256 icotokenPrice;
    mapping(string => uint256) tokensPrice;

    function assignICOPrice(uint256 _tokenPrice) public returns(bool){
        icotokenPrice = _tokenPrice;
        return true;
    }

    function createTokenPrice(string symbol_, uint256 price_) public returns(bool){
     tokensPrice[symbol_] = icotokenPrice / price_;
     return true;
    }

   function returnPrice(string symbol_) public view returns(uint256){
     return(tokensPrice[symbol_]);
 }

}

Brief description

assignICOPrice function allows us to manually assign "CoinA" from $1 to $1.5, to $1.75. The problem we have is that we can't do anything after . since uint256 are integers. How do we solve this problem?

createTokenPrice allows us to define the AltcoinPrice. For example, for Altcoin1 = $0.222 and for Altcoin2 = $0.7

Any proper solution to bypass this issue? I also tried to read EtherDelta's smart contract but it doesn't seem to have anything like that.

0

The typical solution is the one you alluded to. Use a big multiplier.

E.g. instead of storing 1, 1.5, and 1.75 store 1000, 1500, and 1750. More typical in smart contracts, because ether natively uses 18 decimal points, is to multiply by 10^18. So store 1 * 10^18, 1.5 * 10^18, and 1.75 * 10^18.

You won't typically see such a multiplier anywhere in the smart contract because the multiplication usually happens at the UI layer, likely JavaScript in the browser. Similarly, you'll want to divide anything you read from the smart contract before displaying it to a user.

0

A decimal symbol is a notation for human interpretation.

Ethereum itself uses 18 decimal places of precision but is, in fact, represented by very large integers. 18 decimals of precision is an emerging standard for token contracts as well. The number of decimal digits is signaled to the UI with a read-only variable so they will know where to place the dot in the human-readable UI.

All operations use the integer representation (smallest possible units) so contracts don't have to be concerned with fractional units. This is why you don't see evidence of it in the code.

E.g., 1.75 at 18 decimal places is 1,750,000,000,000,000,000 or 1.75 * 10 ** 18. This computation is done client-side so contracts send/receive very large integers.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.