Skip to content

Commit

Permalink
Round down in log assets
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-stakewise committed Dec 9, 2024
1 parent 32e5428 commit e7e77c4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ database
.vscode
.coverage
.python-version
.history
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ ignore_names = [
"DATA_DIR", # settings
"contract_event", "get_from_block", "process_events", # event processor
"validators_root", # ApprovalRequest
"previous_version", "current_version", "genesis_validators_root", "fork_info", "voluntary_exit" # remote.py
"previous_version", "current_version", "genesis_validators_root", "fork_info", "voluntary_exit", # remote.py
"rounding" # decimal context
]
8 changes: 6 additions & 2 deletions src/common/startup_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
get_protocol_config,
)
from src.common.harvest import get_harvest_params
from src.common.utils import format_error, warning_verbose
from src.common.utils import format_error, round_down, warning_verbose
from src.common.wallet import hot_wallet
from src.config.networks import NETWORKS
from src.config.settings import settings
Expand Down Expand Up @@ -219,9 +219,13 @@ async def startup_checks() -> None:

harvest_params = await get_harvest_params()
withdrawable_assets = await get_withdrawable_assets(harvest_params)

# Note. We round down assets in the log message because of the case when assets
# is slightly less than required amount to register validator.
# Standard rounding will show that we have enough assets, but in fact we don't.
logger.info(
'Vault withdrawable assets: %s %s',
round(Web3.from_wei(withdrawable_assets, 'ether'), 2),
round_down(Web3.from_wei(withdrawable_assets, 'ether'), 2),
settings.network_config.VAULT_BALANCE_SYMBOL,
)

Expand Down
Empty file added src/common/tests/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions src/common/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from decimal import Decimal
from src.common.utils import round_down


def test_round_down():
assert round_down(100, 2) == Decimal('100.00')
assert round_down(Decimal('100.123'), 2) == Decimal('100.12')
assert round_down(Decimal('100.999'), 2) == Decimal('100.99')
10 changes: 10 additions & 0 deletions src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from collections import defaultdict
from datetime import datetime, timezone
from decimal import ROUND_FLOOR, Decimal, localcontext
from pathlib import Path
from typing import Any, Iterator

Expand Down Expand Up @@ -113,3 +114,12 @@ def add_fields(self, log_record, record, message_dict): # type: ignore
log_record['level'] = log_record['level'].upper()
else:
log_record['level'] = record.levelname


def round_down(d: int | Decimal, precision: int) -> Decimal:
if isinstance(d, int):
d = Decimal(d)

with localcontext() as ctx:
ctx.rounding = ROUND_FLOOR
return round(d, precision)

0 comments on commit e7e77c4

Please sign in to comment.