Fix early truncation

If we cast to int too early, we may lose precision (e.g. "0.1 ether")
pull/1377/head
Emilio López 2 years ago
parent d47dd66579
commit c09aa50c18
  1. 26
      slither/utils/arithmetic.py

@ -1,33 +1,33 @@
from slither.exceptions import SlitherException
from slither.utils.integer_conversion import convert_string_to_int
from slither.utils.integer_conversion import convert_string_to_fraction
# pylint: disable=too-many-branches
def convert_subdenomination(
value: str, sub: str
) -> int: # pylint: disable=too-many-return-statements
decimal_value = convert_string_to_int(value)
decimal_value = convert_string_to_fraction(value)
if sub == "wei":
return decimal_value
return int(decimal_value)
if sub == "gwei":
return decimal_value * int(1e9)
return int(decimal_value * int(1e9))
if sub == "szabo":
return decimal_value * int(1e12)
return int(decimal_value * int(1e12))
if sub == "finney":
return decimal_value * int(1e15)
return int(decimal_value * int(1e15))
if sub == "ether":
return decimal_value * int(1e18)
return int(decimal_value * int(1e18))
if sub == "seconds":
return decimal_value
return int(decimal_value)
if sub == "minutes":
return decimal_value * 60
return int(decimal_value * 60)
if sub == "hours":
return decimal_value * 60 * 60
return int(decimal_value * 60 * 60)
if sub == "days":
return decimal_value * 60 * 60 * 24
return int(decimal_value * 60 * 60 * 24)
if sub == "weeks":
return decimal_value * 60 * 60 * 24 * 7
return int(decimal_value * 60 * 60 * 24 * 7)
if sub == "years":
return decimal_value * 60 * 60 * 24 * 7 * 365
return int(decimal_value * 60 * 60 * 24 * 7 * 365)
raise SlitherException(f"Subdemonination conversion impossible {decimal_value} {sub}")

Loading…
Cancel
Save