I am writing a contract in Ligo that manages FA2 fungible tokens with an interface that lets users easily create new tokens. I am building a very simple exchange where the users can create orders to swap fixed amounts of tokens they own with fixed amounts of other tokens. Other users can then fulfil the order and initialize the swap.
This works great when the amounts of tokens are fixed but what if I want the users to swap only a part of the order? For example, Alice creates an order to swap 9 tokens A for 14 tokens B. Bob wants to get some tokens A, but only 7, so he would have to give away (14 / 9) * 7 tokens B or 10.88 tokens B. However Ligo (and Michelson) doesn't work with floating-point numbers. So how do exchanges handle this kind of situation? I thought about rounding it up by using the quotient returned by EDIV
but I am not sure if rounding up 10.1 and 10.9 to 11 makes sense or even 0 to 1 in case of a remainder.
-
That’s the reason why many tokens use decimals (multiply every quantity by a huge number and work in this world). Division / rounding is then more reasonable.– FFFSep 29, 2020 at 9:19
1 Answer
To be more precise, since floating-point numbers are not available (because they are not deterministic, it depends on the hardware that runs the node), you have to use fixed point arithmetic