"""Illustrative Liftx stream framing parser; no networking or trading actions. Not a production SDK: validate returned JSON with the public models and maintain link scope, generations, revisions, completeness, and bounded client state. """ import json import re import struct from decimal import Decimal, InvalidOperation class WireError(ValueError): pass class Reader: def __init__(self, data: bytes): self.data = data self.offset = 0 def number(self, fmt: str): size = struct.calcsize(fmt) if self.offset + size > len(self.data): raise WireError("truncated integer") value = struct.unpack_from(fmt, self.data, self.offset)[0] self.offset += size return value def string(self, maximum: int): end = self.data.find(b"\0", self.offset, self.offset + maximum + 1) if end < 0: raise WireError("unterminated or overlong string") try: value = self.data[self.offset:end].decode("utf-8", errors="strict") except UnicodeDecodeError as error: raise WireError("invalid UTF-8") from error self.offset = end + 1 return value def finish(self): if self.offset != len(self.data): raise WireError("trailing bytes") def unique_object(pairs): result = {} for key, value in pairs: if key in result: raise WireError("duplicate JSON field") result[key] = value return result def reject_constant(value): raise WireError("nonstandard JSON constant") def decode_frame(data: bytes, *, max_frame_bytes: int): """Decode one assembled binary WebSocket message under a caller-owned bound.""" if not isinstance(data, bytes) or not 7 <= len(data) <= max_frame_bytes: raise WireError("invalid frame size/type") message_type, length, version, channel = struct.unpack_from(" (len(payload) - 4) // 10: raise WireError("impossible balance row count") for _ in range(count): asset = reader.string(128) if not asset: raise WireError("empty asset") rows.append({"asset": asset, "free_atomic": str(reader.number(" (len(payload) - 3) // minimum: raise WireError("invalid ticker kind/count") if (kind == 1 and message_type != 3) or (kind == 2 and message_type != 1): raise WireError("invalid ticker kind/type") for _ in range(count): topic = reader.number(" 30: raise WireError("invalid price decimal") rows.append({"topic_id": topic, "price_atomic": atomic, "price_scale": scale}) result["payload"] = {"kind": kind, "rows": rows} reader.finish() return result