#!/usr/bin/env python3 """ Reliable OKX credential loader. Reads directly from ~/.bashrc file, bypassing env var quoting issues. Usage: from okx_cred_loader import load_okx_creds, create_okx_exchange creds = load_okx_creds() exchange = create_okx_exchange(creds, default_type='swap') """ import re, os, ccxt def load_okx_creds(): """Load OKX credentials from ~/.bashrc, stripping quotes.""" creds = {} bashrc = os.path.expanduser('~/.bashrc') with open(bashrc) as f: for line in f: m = re.match(r'export\s+(OKX_\w+)=(.*)', line.strip()) if m: key, val = m.group(1), m.group(2).strip().strip('"').strip("'") creds[key] = val required = ['OKX_API_KEY', 'OKX_SECRET', 'OKX_PASSPHRASE'] missing = [k for k in required if k not in creds] if missing: raise ValueError(f"Missing OKX creds in ~/.bashrc: {missing}") return creds def create_okx_exchange(creds=None, default_type='swap'): """Create a configured ccxt.okx exchange instance.""" if creds is None: creds = load_okx_creds() return ccxt.okx({ 'apiKey': creds['OKX_API_KEY'], 'secret': creds['OKX_SECRET'], 'password': creds['OKX_PASSPHRASE'], 'proxies': { 'http': 'http://127.0.0.1:7890', 'https': 'http://127.0.0.1:7890', }, 'options': {'defaultType': default_type}, }) if __name__ == '__main__': creds = load_okx_creds() print(f"OKX creds loaded: KEY={creds['OKX_API_KEY'][:8]}...") exchange = create_okx_exchange(creds) balance = exchange.fetch_balance() usdt = float(balance.get('USDT', {}).get('free', 0)) print(f"USDT free: {usdt:.2f}")