"""Exchange amount/price precision helpers for hedge and local trading.""" from __future__ import annotations from typing import Any, Optional def _decimals_from_precision_value(value: Any) -> Optional[int]: if value in (None, ""): return None try: p = float(value) except (TypeError, ValueError): return None if p >= 1 and abs(p - round(p)) < 1e-9 and p <= 12: return int(round(p)) if 0 < p < 1: s = f"{p:.12f}".rstrip("0") if "." in s: return min(12, len(s.split(".", 1)[1])) return None def _decimals_from_ccxt_str(text: str) -> int: s = str(text or "").strip() if not s or "." not in s: return 0 frac = s.split(".", 1)[1] if not frac: return 0 return min(12, len(frac.rstrip("0") or frac)) def amount_decimals_from_exchange(exchange: Any, exchange_symbol: str) -> int: try: return _decimals_from_ccxt_str(exchange.amount_to_precision(exchange_symbol, 1.23456789)) except Exception: market = exchange.market(exchange_symbol) prec = (market.get("precision") or {}).get("amount") d = _decimals_from_precision_value(prec) return d if d is not None else 4