stellar / soroban-examples

Example Soroban Contracts
Apache License 2.0
57 stars 60 forks source link

PR: decimal value shall not be greater than 18 #309

Closed unboxedtype closed 3 months ago

unboxedtype commented 3 months ago

What

initialize() shall panic in case the supplied decimal value is greater than 18.

Why

The Soroban token has a balance type of i128, which allows for up to 38 digits (base 10), including the decimal part. However, the current token implementation allows decimal values up to u8::MAX. This is an overly loose input validation, since providing a value of >=39 would result in a unusable token, as 10^39 is not representable in i128 and will lead to overflows. Also, having this value slightly below 38 could may cause sporadic overflows in a protocol that rely on this token, due to the limited size of the integer part of the number.

The recommended upper bound for decimal value is 18. With this, you would still have ~20 digits available for the integer part, which should be sufficient for most use-cases. Also, 18 decimals is a standard value for Ethereum tokens, so using the same value leads to a greater compatibility between platforms.

unboxedtype commented 3 months ago

I fixed the corresponding test in test.rs to reflect the latest changes.

unboxedtype commented 3 months ago

There was a failing cargo fmt target that I didn't know existed. I fixed the formatting issue.