Stage 04.3 - repositories, balance snapshots and environment mode fix
This commit is contained in:
56
app/src/trading/accounts/service.py
Normal file
56
app/src/trading/accounts/service.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from src.integrations.exchange.models import BalanceSummary
|
||||
from src.integrations.exchange.service import ExchangeService
|
||||
from src.storage.repositories.balance_snapshots import BalanceSnapshotRepository
|
||||
from src.trading.journal.service import JournalService
|
||||
|
||||
|
||||
class AccountsService:
|
||||
def __init__(self) -> None:
|
||||
self.exchange_service = ExchangeService()
|
||||
self.snapshot_repository = BalanceSnapshotRepository()
|
||||
self.journal = JournalService()
|
||||
|
||||
def get_live_balance_summary(self) -> list[BalanceSummary]:
|
||||
balances = self.exchange_service.get_balance_summary()
|
||||
self._save_snapshot(balances)
|
||||
return balances
|
||||
|
||||
def _save_snapshot(self, balances: list[BalanceSummary]) -> None:
|
||||
payload = {
|
||||
"assets": [
|
||||
{
|
||||
"currency": item.currency,
|
||||
"available": item.available,
|
||||
"locked": item.locked,
|
||||
"source": item.source,
|
||||
}
|
||||
for item in balances
|
||||
]
|
||||
}
|
||||
|
||||
try:
|
||||
self.snapshot_repository.add_snapshot(
|
||||
source="portfolio_screen",
|
||||
payload=payload,
|
||||
)
|
||||
except Exception as exc:
|
||||
try:
|
||||
self.journal.log_warning(
|
||||
"balance_snapshot_error",
|
||||
f"Не удалось сохранить snapshot баланса: {exc}",
|
||||
{"assets_count": len(balances)},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
|
||||
try:
|
||||
self.journal.log_info(
|
||||
"balance_snapshot_saved",
|
||||
f"Snapshot баланса сохранён. Активов: {len(balances)}",
|
||||
{"assets_count": len(balances)},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
Reference in New Issue
Block a user