tradecipher-blockchain-back.../tradecipher_blockchain_back.../adapters.py

83 lines
2.5 KiB
Python

from __future__ import annotations
"""Tiny adapters registry for two-venue MVP.
This module is deliberately small; it provides a stub registry that could be
extended to map LocalTrade -> canonical representation and to register
simple venue adapters. It demonstrates the intended extension point.
"""
import json
ADAPTER_REGISTRY = {}
def register_adapter(name: str, adapter: object) -> None:
ADAPTER_REGISTRY[name] = adapter
def get_adapter(name: str):
return ADAPTER_REGISTRY.get(name)
# Lightweight, two-venue MVP adapters (stubbed for bootstrap).
# These adapters translate between a venue-specific message format and
# the canonical LocalTrade/SharedSignals/PlanDelta representation used
# by the TradeCipher core. The implementations here are intentionally
# minimal placeholders to unblock interoperability testing.
class VenueAAdapter:
name = "VenueA"
@staticmethod
def to_canonical(obj: object) -> dict:
# Placeholder translation: assume obj is already a dict-like
# representation compatible with canonical fields.
if hasattr(obj, "to_json"):
return json.loads(obj.to_json()) # type: ignore
if isinstance(obj, dict):
return obj
return {} # pragma: no cover
@staticmethod
def from_canonical(data: dict) -> object:
# Return as-is; in a full implementation this would construct
# a venue-specific LocalTrade message.
return data
class VenueBAdapter:
name = "VenueB"
@staticmethod
def to_canonical(obj: object) -> dict:
if hasattr(obj, "to_json"):
return json.loads(obj.to_json()) # type: ignore
if isinstance(obj, dict):
return obj
return {}
@staticmethod
def from_canonical(data: dict) -> object:
return data
def register_default_adapters() -> None:
"""Register default, stub adapters for VenueA and VenueB.
This is a minimal bootstrap to enable end-to-end wiring in tests
and examples. Real adapters would implement complete mapping logic.
"""
register_adapter(VenueAAdapter.name, VenueAAdapter)
register_adapter(VenueBAdapter.name, VenueBAdapter)
# Autoload default adapters on import, so downstream code can rely on them
# being present without extra setup. This keeps the module side-effect-free
# for testing environments that import adapters without needing a runner.
try:
register_default_adapters()
except Exception:
# If anything goes wrong in bootstrap, fail softly to not break local tests
pass