Skip to content

Commit

Permalink
Fix decimal issue
Browse files Browse the repository at this point in the history
  • Loading branch information
philogicae committed Feb 13, 2025
1 parent aca81f0 commit d310a69
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/aleph/sdk/connectors/superfluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def can_start_flow(self, flow: Decimal, block=True) -> bool:
if not valid and block:
raise InsufficientFundsError(
token_type=TokenType.ALEPH,
required_funds=float(MIN_FLOW_4H),
required_funds=float(from_wei_token(MIN_FLOW_4H)),
available_funds=float(from_wei_token(balance)),
)
return valid
Expand Down
6 changes: 4 additions & 2 deletions src/aleph/sdk/evm_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from decimal import ROUND_CEILING, Decimal
from decimal import ROUND_CEILING, Context, Decimal
from enum import Enum
from typing import List, Optional, Union

Expand Down Expand Up @@ -39,7 +39,9 @@ def to_wei_token(amount: Decimal) -> Decimal:

def ether_rounding(amount: Decimal) -> Decimal:
"""Rounds the given value to 18 decimals."""
return amount.quantize(Decimal(1) / Decimal(10**18), rounding=ROUND_CEILING)
return amount.quantize(
Decimal(1) / Decimal(10**18), rounding=ROUND_CEILING, context=Context(prec=18)
)


def get_chain_id(chain: Union[Chain, str, None]) -> Optional[int]:
Expand Down
16 changes: 11 additions & 5 deletions src/aleph/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import subprocess
from datetime import date, datetime, time
from decimal import Decimal
from decimal import Context, Decimal, InvalidOperation
from enum import Enum
from pathlib import Path
from shutil import make_archive
Expand Down Expand Up @@ -414,18 +414,24 @@ def safe_getattr(obj, attr, default=None):


def displayable_amount(
amount: Union[str, int, float, Decimal], decimals: Optional[int] = None
amount: Union[str, int, float, Decimal], decimals: int = 18
) -> str:
"""Returns the amount as a string without unnecessary decimals."""

str_amount = ""
try:
dec_amount = Decimal(amount)
if decimals:
dec_amount = dec_amount.quantize(Decimal(1) / Decimal(10**decimals))
dec_amount = dec_amount.quantize(
Decimal(1) / Decimal(10**decimals), context=Context(prec=18)
)
str_amount = str(format(dec_amount.normalize(), "f"))
except ValueError as e:
raise ValueError(f"Invalid amount: {amount}") from e
except ValueError:
logger.error(f"Invalid amount to display: {amount}")
exit(1)
except InvalidOperation:
logger.error(f"Invalid operation on amount to display: {amount}")
exit(1)
return str_amount


Expand Down

0 comments on commit d310a69

Please sign in to comment.