Metadata-Version: 2.5
Name: godark
Version: 0.2.0
Summary: Python SDK for the GoDark DEX -- encrypted trading over WebSocket
Project-URL: Homepage, https://godarkdex.com
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: cryptography>=42.0
Requires-Dist: httpx>=0.27
Requires-Dist: protobuf>=5.0
Requires-Dist: pyhpke>=0.6.5
Requires-Dist: websockets>=13.0
Provides-Extra: dev
Requires-Dist: grpcio-tools>=1.60; extra == 'dev'
Requires-Dist: pip-audit>=2.7.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.8.0; extra == 'dev'
Description-Content-Type: text/markdown

# GoDark Python SDK

Encrypted Python client for the GoDark DEX.

## WebSocket endpoints

The SDK speaks WebSocket on the canonical ``/ws/v1`` path. Pass the host
origin as ``base_url``; the client appends ``/ws/v1`` automatically. Legacy ``/ws``
bases are upgraded transparently. Set the host via ``base_url``,
``GODARK_EDGE_URL``, or ``GDX_EDGE_URL``; either ``<host>`` or ``<host>/ws/v1``
resolve to the same endpoint.

| Environment | Canonical URL | HPKE pin |
|-------------|---------------|-----------|
| Testnet (default) | ``wss://api.godark-dex.com/ws/v1`` | baked in (Testnet sequencer) |
| Devnet | ``wss://api.devnet.godark-dex.com/ws/v1`` | baked in (Devnet sequencer; distinct from Testnet) |
| Localnet | ``ws://127.0.0.1:4000/ws/v1`` | set via constructor / env |

```python
from godark import Environment, GodarkClient

async with GodarkClient(
    environment=Environment.TESTNET,  # default; sets URL + HPKE pin
    api_key_id="gdk_...",
    api_secret="...",
    passphrase="...",
) as client:
    ...
```

Public mainnet is not currently exposed; testnet is the live network for SDK
users today.

## Order confirmation

WebSocket order placement has two explicit confirmation modes:

- ``confirmation="book"`` (default) waits for the fast ack and then an ``OPEN``,
  ``REJECTED``, ``FILLED``, ``PARTIALLY_FILLED``, or ``CANCELLED`` order update.
  A rejection raises ``OrderError`` with the error code and server-provided
  ``reject_text``/``msg``.
- ``confirmation="ack"`` returns immediately after the fast ack. Use this for
  latency-sensitive clients that consume order updates independently.

```python
# Default: confirmed by an order update.
ack = await client.place_order("BTC-USDC-PERP", "BUY", "LIMIT", quantity=0.1, price=67_500)

# Fast acknowledgement only.
ack = await client.place_order(
    "BTC-USDC-PERP",
    "BUY",
    "LIMIT",
    quantity=0.1,
    price=67_500,
    confirmation="ack",
)
```

Configure the post-ack book wait with ``place_order_terminal_timeout`` on
``GodarkClient``. A disconnect fails outstanding book confirmations rather than
carrying them across reconnect.

Encrypted WebSocket trading uses **HPKE** (``HPKE Base (X25519 + AES-GCM)``)
after login. Preference order for the sequencer pin:
``hpke_static_public_key_hex=...`` → ``GDX_HPKE_STATIC_PUBLIC_KEY``
(aliases) → baked-in pin from ``environment=...``. Legacy ECDH
``session.setup`` is no longer used; encrypted REST order flow is unsupported
(use the WebSocket client).

## Quickstart

**Published package (clients):** ``pip install godark`` — no ``gdx`` or ``gdx-proto`` checkout.

**Standalone clone:** committed bindings in ``src/godark/_generated`` are enough to test.

```bash
git clone <repo-url> gdx-python-sdk
cd gdx-python-sdk
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest -m "not live"
```

**From the `gdx` meta-repo:** clone `gdx` with `--recurse-submodules`. This SDK
lives at `gdx-sdk/gdx-python-sdk/`. Proto codegen walks up to the umbrella
`gdx-proto` submodule (or uses `GDX_PROTO_ROOT`).

**Before examples or live tests:** copy ``.env.example`` to ``.env`` at the SDK root and add your credentials (see **Examples** for what each key does). Examples load that file automatically; live tests merge it into the process environment when keys are unset. Offline ``pytest -m "not live"`` does **not** require ``.env``.

``hatch run …`` targets (below) reuse whichever interpreter Hatch is pointed at — using a Hatch-managed env works too; plain ``venv`` + ``pip`` is the simplest default.

## Examples

The workspace ``.env.example`` is the **full** template (all optional keys documented in that file). Smaller **examples-only** checkouts use a minimal ``.env.example`` (typically credentials and optional ``GODARK_EDGE_URL``). If you have not already done so from Quickstart:

```bash
cp .env.example .env   # fill in GODARK_API_KEY_ID, GODARK_API_SECRET, GODARK_PASSPHRASE, …
PYTHONPATH=src python examples/full_trader_example.py
```

**Market-maker distribution:** runnable MM examples and wheels-only tarballs live in the separate [gdx-python-sdk-examples](https://github.com/gq-godark/gdx-python-sdk-examples) repo. That repo vendors this SDK under `sdk/` and mirrors `examples/` under `examples/upstream/` via `scripts/refresh_sdk.sh` and `scripts/refresh_examples.sh`.

Beyond the minimal fields, the workspace ``.env.example`` documents optional overrides such as:

| Key | Purpose |
|-----|---------|
| ``GODARK_API_KEY_ID``, ``GODARK_API_SECRET``, ``GODARK_PASSPHRASE`` | API credentials (passphrase is user-chosen at key creation) |
| ``GODARK_EDGE_URL``, ``GODARK_REST_URL`` | Optional endpoint overrides |
| ``GDX_*`` for the same fields | Supported as legacy aliases if ``GODARK_*`` is unset |

## Layout

- `src/godark/`   — package source
- `tests/`        — unit + mock-integration tests (live tests gated by `live` marker + ``GDX_LIVE_EDGE``)
- `examples/`    — runnable examples (see **Examples**)
- `scripts/`      — `proto_gen.sh` / `resolve-proto.sh`
- `shared/`       — vendored symbol map (canonical copy lives in gdx-proto repo)
- `src/godark/_generated/` — committed protobuf bindings

## Testing

Offline (default — no network):

```bash
pytest -m "not live"
# or
hatch run pytest -m "not live"
```

Live tests hit a real edge and only run when ``GDX_LIVE_EDGE=1``. Use the same
SDK root ``.env`` as **Examples** for credentials and URL overrides when you do
not set them in the shell.

```bash
GDX_LIVE_EDGE=1 pytest tests/test_live_integration.py tests/test_rest_live_integration.py -v

# REST only:
GDX_LIVE_EDGE=1 pytest tests/test_rest_live_integration.py -v

# WS only:
GDX_LIVE_EDGE=1 pytest tests/test_live_integration.py -v
```

Variables for **live tests** (conventional names across GoDark client SDKs):

| Var | Default | Notes |
|---|---|---|
| ``GDX_LIVE_EDGE`` | ``0`` (skip) | Set to ``1`` to run live tests |
| ``GODARK_API_KEY_ID`` + ``GODARK_API_SECRET`` + ``GODARK_PASSPHRASE`` (legacy ``GDX_*``) | falls back to legacy ``test-key-1`` | Production credentials |
| ``GODARK_REST_URL`` (legacy ``GDX_REST_URL``) | ``https://api.godark-dex.com`` | REST live tests only |
| ``GODARK_EDGE_URL`` (legacy ``GDX_EDGE_URL``) | ``wss://api.godark-dex.com`` | WS live tests only |
| ``GDX_HPKE_STATIC_PUBLIC_KEY`` | — | Sequencer HPKE static public key (64 hex chars) |
| ``GDX_USE_DOCS_WIRE`` | ``1`` (modern envelope) | Set ``0|false|no|off`` for legacy localnet edges (WS only) |
| ``GODARK_USER_UUID`` (legacy ``GDX_USER_UUID``) | test fixture ``00000000-0000-4000-8000-000000000001`` | Optional client UUID override |

Note: at the time of writing, the public testnet's sequencer is degraded;
live trading-flow tests may fail with ``POST /api/v1/session/setup HTTP 502``
(REST; encrypted REST trading is unsupported under HPKE) or
``SessionError`` / HPKE setup timeout (WS). The
``test_bad_api_key_rejected`` cases pass regardless and are the SDK-side
control confirming the failures are server-side.

## Development

```bash
hatch run ruff check .
hatch run ruff format --check .
```

## Regenerating protobuf bindings

Needs `gdx-proto` (meta-repo walk-up, `./gdx-proto` clone, or `GDX_PROTO_ROOT`):

```bash
bash scripts/proto_gen.sh
```
