Skip to content

Commit d310a69

Browse files
committed
Fix decimal issue
1 parent aca81f0 commit d310a69

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/aleph/sdk/connectors/superfluid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def can_start_flow(self, flow: Decimal, block=True) -> bool:
5858
if not valid and block:
5959
raise InsufficientFundsError(
6060
token_type=TokenType.ALEPH,
61-
required_funds=float(MIN_FLOW_4H),
61+
required_funds=float(from_wei_token(MIN_FLOW_4H)),
6262
available_funds=float(from_wei_token(balance)),
6363
)
6464
return valid

src/aleph/sdk/evm_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from decimal import ROUND_CEILING, Decimal
1+
from decimal import ROUND_CEILING, Context, Decimal
22
from enum import Enum
33
from typing import List, Optional, Union
44

@@ -39,7 +39,9 @@ def to_wei_token(amount: Decimal) -> Decimal:
3939

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

4446

4547
def get_chain_id(chain: Union[Chain, str, None]) -> Optional[int]:

src/aleph/sdk/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import subprocess
1010
from datetime import date, datetime, time
11-
from decimal import Decimal
11+
from decimal import Context, Decimal, InvalidOperation
1212
from enum import Enum
1313
from pathlib import Path
1414
from shutil import make_archive
@@ -414,18 +414,24 @@ def safe_getattr(obj, attr, default=None):
414414

415415

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

421421
str_amount = ""
422422
try:
423423
dec_amount = Decimal(amount)
424424
if decimals:
425-
dec_amount = dec_amount.quantize(Decimal(1) / Decimal(10**decimals))
425+
dec_amount = dec_amount.quantize(
426+
Decimal(1) / Decimal(10**decimals), context=Context(prec=18)
427+
)
426428
str_amount = str(format(dec_amount.normalize(), "f"))
427-
except ValueError as e:
428-
raise ValueError(f"Invalid amount: {amount}") from e
429+
except ValueError:
430+
logger.error(f"Invalid amount to display: {amount}")
431+
exit(1)
432+
except InvalidOperation:
433+
logger.error(f"Invalid operation on amount to display: {amount}")
434+
exit(1)
429435
return str_amount
430436

431437

0 commit comments

Comments
 (0)