All checks were successful
deploy / deploy (push) Successful in 22s
Proof: Persist portfolio value and PnL snapshots from the live inventory and reference-price path so operators can inspect trading performance from repo-controlled data. Assumptions: The last credited inventory snapshot before the first live command is the correct baseline for trade-driven PnL, and EURe remains explicit 1:1 with EUR. Still fake: The new portfolio metrics and watch output are implemented and tested locally but are not live until the updated app image is deployed to k3s.
138 lines
4.7 KiB
Python
138 lines
4.7 KiB
Python
from __future__ import annotations
|
|
|
|
import sys
|
|
import unittest
|
|
from decimal import Decimal
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
import watch_live_mm
|
|
|
|
|
|
ASSETS = {
|
|
"nep141:btc.omft.near": watch_live_mm.AssetMeta(
|
|
asset_id="nep141:btc.omft.near",
|
|
symbol="BTC",
|
|
decimals=8,
|
|
),
|
|
"nep141:eure": watch_live_mm.AssetMeta(
|
|
asset_id="nep141:eure",
|
|
symbol="EURe",
|
|
decimals=18,
|
|
),
|
|
}
|
|
|
|
|
|
class WatchLiveMmTests(unittest.TestCase):
|
|
def test_format_amount_humanizes_btc(self) -> None:
|
|
self.assertEqual(
|
|
watch_live_mm.format_amount("100000", ASSETS["nep141:btc.omft.near"]),
|
|
"0.001 BTC",
|
|
)
|
|
|
|
def test_format_amount_humanizes_eure(self) -> None:
|
|
self.assertEqual(
|
|
watch_live_mm.format_amount("122753877246000000000", ASSETS["nep141:eure"]),
|
|
"122.75387725 EURe",
|
|
)
|
|
|
|
def test_format_decimal_trims_trailing_zeroes(self) -> None:
|
|
self.assertEqual(watch_live_mm.format_decimal(Decimal("60.00000000")), "60")
|
|
|
|
def test_render_quote_row_formats_assets(self) -> None:
|
|
row = {
|
|
"ingested_at": "2026-04-02 14:00:00+00",
|
|
"quote_id": "quote-1",
|
|
"asset_in": "nep141:eure",
|
|
"asset_out": "nep141:btc.omft.near",
|
|
"request_kind": "exact_in",
|
|
"amount_in": "122753877246000000000",
|
|
"amount_out": "",
|
|
"min_deadline_ms": "15000",
|
|
}
|
|
rendered = watch_live_mm.render_quote_row(row, ASSETS)
|
|
self.assertIn("QUOTE", rendered)
|
|
self.assertIn("EURe->BTC", rendered)
|
|
self.assertIn("122.75387725 EURe", rendered)
|
|
|
|
def test_render_decision_row_formats_inventory(self) -> None:
|
|
row = {
|
|
"ingested_at": "2026-04-02 14:00:01+00",
|
|
"quote_id": "quote-2",
|
|
"direction": "eure_to_btc",
|
|
"decision": "rejected",
|
|
"decision_reason": "insufficient_inventory",
|
|
"gross_edge_pct": "2.000000",
|
|
"eure_notional": "16.246379",
|
|
"inventory_asset": "nep141:btc.omft.near",
|
|
"inventory_available": "100000",
|
|
"inventory_required": "1592145",
|
|
}
|
|
rendered = watch_live_mm.render_decision_row(row, ASSETS)
|
|
self.assertIn("DECISION", rendered)
|
|
self.assertIn("insufficient_inventory", rendered)
|
|
self.assertIn("0.001 BTC", rendered)
|
|
|
|
def test_select_new_rows_returns_oldest_first_and_marks_seen(self) -> None:
|
|
seen = {"existing"}
|
|
rows = [
|
|
{"event_id": "newer"},
|
|
{"event_id": "middle"},
|
|
{"event_id": "existing"},
|
|
]
|
|
fresh = watch_live_mm.select_new_rows(rows, seen)
|
|
self.assertEqual([row["event_id"] for row in fresh], ["middle", "newer"])
|
|
self.assertEqual(seen, {"existing", "middle", "newer"})
|
|
|
|
def test_current_state_fingerprint_includes_portfolio_metrics(self) -> None:
|
|
responses = {
|
|
"near-intents-ingest": {
|
|
"ingest": {
|
|
"last_matching_quote_at": "2026-04-02T20:17:44.623Z",
|
|
"published_count": 7,
|
|
}
|
|
},
|
|
"inventory-sync": {
|
|
"last_snapshot": {
|
|
"spendable": {
|
|
"nep141:btc.omft.near": "137014",
|
|
"nep141:eure": "38999978799978799978",
|
|
}
|
|
}
|
|
},
|
|
"history-writer": {
|
|
"latest_portfolio_metrics": {
|
|
"trade_pnl_eure": "0.391183707978799978",
|
|
"mark_to_market_pnl_eure": "0.497413887978799978",
|
|
}
|
|
},
|
|
"strategy-engine": {
|
|
"armed": False,
|
|
"latest_decision": {
|
|
"decision_reason": "actionable",
|
|
"quote_id": "quote-1",
|
|
},
|
|
},
|
|
"trade-executor": {
|
|
"armed": False,
|
|
"signer_registered": True,
|
|
},
|
|
}
|
|
|
|
with mock.patch.object(
|
|
watch_live_mm,
|
|
"fetch_service_state",
|
|
side_effect=lambda *, namespace, deployment: responses[deployment],
|
|
):
|
|
state = watch_live_mm.current_state_fingerprint(namespace="unrip", assets=ASSETS)
|
|
|
|
self.assertEqual(state["trade_pnl_eure"], "0.391183707978799978")
|
|
self.assertEqual(state["mark_to_market_pnl_eure"], "0.497413887978799978")
|
|
self.assertEqual(state["btc_spendable"], "0.00137014 BTC")
|
|
self.assertEqual(state["eure_spendable"], "38.9999788 EURe")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|