import os, requests, json, time
from websockets.sync.client import connect
DATA = "https://api.predexon.com"
TRADE = "https://trade.predexon.com"
WSS = "wss://wss.predexon.com/v1"
KEY = os.environ["PREDEXON_API_KEY"]
H = {"x-api-key": KEY}
ACCOUNT_ID = "your-account-id"
def boot_state():
# 1. Account info — gives us per-venue wallets
acct = requests.get(f"{TRADE}/api/accounts/{ACCOUNT_ID}", headers=H).json()
venues = {v: info for v, info in acct["venues"].items() if info["status"] == "active"}
# 2. Aggregated positions across every enabled venue
positions = requests.get(
f"{TRADE}/api/accounts/{ACCOUNT_ID}/positions",
headers=H,
params={"aggregated": "true"},
).json()["positions"]
# 3. Cash balance per venue + USD-equivalent total
balance = requests.get(
f"{TRADE}/api/accounts/{ACCOUNT_ID}/balance",
headers=H,
params={"aggregated": "true"},
).json()
# 4. Polymarket P&L history (other venues TBD)
poly_wallet = venues.get("polymarket", {}).get("address")
pnl = {}
if poly_wallet:
pnl = requests.get(
f"{DATA}/v2/polymarket/wallet/pnl/{poly_wallet}",
headers=H,
).json()
return {
"venues": venues,
"positions": positions,
"balance": balance,
"pnl": pnl,
"loaded_at": time.time(),
}
state = boot_state()
print(f"Loaded {len(state['positions'])} positions across {len(state['venues'])} venues")