-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathapi_client.py
More file actions
411 lines (369 loc) · 14.8 KB
/
Copy pathapi_client.py
File metadata and controls
411 lines (369 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Polymarket API client and cross-platform price feeds
import asyncio
import json
import time
import logging
import hmac
import hashlib
import base64
from typing import Any, Dict, List, Optional, Tuple
from datetime import datetime, timezone
import aiohttp
import websockets
from config import (
CLOB_API_URL, GAMMA_API_URL, WS_URL,
POLYMARKET_API_KEY, POLYMARKET_SECRET, POLYMARKET_PASSPHRASE,
BINANCE_API_KEY, BINANCE_SECRET, COINGECKO_API_URL,
CRYPTO_ASSETS, MARKET_SEARCH_KEYWORDS,
ORDER_TYPE, SLIPPAGE_TOLERANCE, DRY_RUN, CHAIN_ID,
)
logger = logging.getLogger("polymarket_arb")
class PolymarketClient:
"""Polymarket CLOB API client."""
def __init__(self):
self.base_url = CLOB_API_URL
self.gamma_url = GAMMA_API_URL
self.api_key = POLYMARKET_API_KEY
self.secret = POLYMARKET_SECRET
self.passphrase = POLYMARKET_PASSPHRASE
self.session: Optional[aiohttp.ClientSession] = None
self._rate_limit_remaining = 100
self._last_request_time = 0
self._min_request_interval = 0.6 # ~100 req/min
async def _ensure_session(self):
if self.session is None or self.session.closed:
self.session = aiohttp.ClientSession(
headers=self._auth_headers(),
timeout=aiohttp.ClientTimeout(total=10)
)
def _auth_headers(self) -> Dict[str, str]:
timestamp = str(int(time.time()))
message = timestamp + "GET" + "/auth"
if self.secret:
signature = hmac.new(
base64.b64decode(self.secret),
message.encode("utf-8"),
hashlib.sha256
).hexdigest()
else:
signature = ""
return {
"POLY-API-KEY": self.api_key,
"POLY-SIGNATURE": signature,
"POLY-TIMESTAMP": timestamp,
"POLY-PASSPHRASE": self.passphrase,
"Content-Type": "application/json",
}
async def _rate_limit(self):
"""Enforce rate limiting."""
now = time.time()
elapsed = now - self._last_request_time
if elapsed < self._min_request_interval:
await asyncio.sleep(self._min_request_interval - elapsed)
self._last_request_time = time.time()
async def _get(self, endpoint: str, params: Optional[Dict] = None) -> Any:
await self._ensure_session()
await self._rate_limit()
url = f"{self.base_url}{endpoint}"
try:
async with self.session.get(url, params=params) as resp:
if resp.status == 200:
return await resp.json()
elif resp.status == 429:
logger.warning("Rate limited, backing off 60s...")
await asyncio.sleep(60)
return await self._get(endpoint, params)
else:
text = await resp.text()
logger.error(f"API error {resp.status}: {text}")
return None
except Exception as e:
logger.error(f"Request failed: {e}")
return None
async def _post(self, endpoint: str, data: Dict) -> Any:
await self._ensure_session()
await self._rate_limit()
url = f"{self.base_url}{endpoint}"
try:
async with self.session.post(url, json=data) as resp:
if resp.status in (200, 201):
return await resp.json()
else:
text = await resp.text()
logger.error(f"POST error {resp.status}: {text}")
return None
except Exception as e:
logger.error(f"POST failed: {e}")
return None
# Market discovery
async def get_markets(self, next_cursor: str = "") -> Dict:
"""Fetch all active markets from the CLOB API."""
params = {"active": "true"}
if next_cursor:
params["next_cursor"] = next_cursor
return await self._get("/markets", params)
async def search_gamma_markets(self, query: str) -> List[Dict]:
"""Search for markets via the Gamma API (richer metadata)."""
await self._ensure_session()
url = f"{self.gamma_url}/markets"
params = {"closed": "false", "limit": 50}
try:
async with self.session.get(url, params=params) as resp:
if resp.status == 200:
markets = await resp.json()
# Filter by search query
return [
m for m in markets
if query.lower() in m.get("question", "").lower()
or query.lower() in m.get("description", "").lower()
or query.lower() in str(m.get("tags", [])).lower()
]
return []
except Exception as e:
logger.error(f"Gamma search failed: {e}")
return []
async def discover_crypto_markets(self) -> Dict[str, List[Dict]]:
"""Discover all active crypto prediction markets for our target assets."""
crypto_markets = {asset: [] for asset in CRYPTO_ASSETS}
for asset, keywords in MARKET_SEARCH_KEYWORDS.items():
for keyword in keywords:
markets = await self.search_gamma_markets(keyword)
for m in markets:
if m not in crypto_markets[asset]:
crypto_markets[asset].append(m)
await asyncio.sleep(0.5) # Gentle rate limiting
total = sum(len(v) for v in crypto_markets.values())
logger.info(f"Discovered {total} crypto markets: " +
", ".join(f"{k}={len(v)}" for k, v in crypto_markets.items()))
return crypto_markets
# Order book
async def get_order_book(self, token_id: str) -> Optional[Dict]:
"""Get the full order book for a token."""
return await self._get(f"/book", {"token_id": token_id})
async def get_midpoint(self, token_id: str) -> Optional[float]:
"""Get the midpoint price for a token."""
data = await self._get(f"/midpoint", {"token_id": token_id})
if data and "mid" in data:
return float(data["mid"])
return None
async def get_price(self, token_id: str) -> Optional[Dict]:
"""Get best bid/ask for a token."""
data = await self._get(f"/price", {"token_id": token_id})
return data
async def get_spread(self, token_id: str) -> Optional[Dict]:
"""Get the bid-ask spread for a token."""
return await self._get(f"/spread", {"token_id": token_id})
async def get_prices_for_market(self, condition_id: str) -> Dict[str, Dict]:
"""Get YES and NO prices for a binary market."""
market = await self._get(f"/markets/{condition_id}")
if not market:
return {}
tokens = market.get("tokens", [])
result = {}
for token in tokens:
outcome = token.get("outcome", "").upper()
token_id = token.get("token_id", "")
price_data = await self.get_price(token_id)
if price_data:
result[outcome] = {
"token_id": token_id,
"price": float(price_data.get("price", 0)),
"bid": float(price_data.get("bid", 0)),
"ask": float(price_data.get("ask", 0)),
}
return result
# Trading
async def place_order(
self,
token_id: str,
side: str, # "BUY" or "SELL"
price: float,
size: float,
order_type: str = ORDER_TYPE,
) -> Optional[Dict]:
"""Place an order on the CLOB."""
if DRY_RUN:
logger.info(f"[DRY RUN] Order: {side} {size:.2f} @ ${price:.4f} "
f"token={token_id[:16]}... type={order_type}")
return {
"id": f"dry_run_{int(time.time()*1000)}",
"status": "SIMULATED",
"side": side,
"price": price,
"size": size,
"token_id": token_id,
}
order_data = {
"tokenID": token_id,
"price": price,
"size": size,
"side": side,
"type": order_type,
"feeRateBps": 0,
}
return await self._post("/order", order_data)
async def place_market_order(
self,
token_id: str,
side: str,
amount_usd: float,
) -> Optional[Dict]:
"""Place a market order (FOK at best available price)."""
book = await self.get_order_book(token_id)
if not book:
return None
if side == "BUY":
asks = book.get("asks", [])
if not asks:
return None
best_ask = float(asks[0]["price"])
price = best_ask * (1 + SLIPPAGE_TOLERANCE / 100)
size = amount_usd / best_ask
else:
bids = book.get("bids", [])
if not bids:
return None
best_bid = float(bids[0]["price"])
price = best_bid * (1 - SLIPPAGE_TOLERANCE / 100)
size = amount_usd / best_bid
return await self.place_order(token_id, side, price, size, "FOK")
async def cancel_order(self, order_id: str) -> Optional[Dict]:
"""Cancel an active order."""
if DRY_RUN:
logger.info(f"[DRY RUN] Cancel order: {order_id}")
return {"status": "CANCELLED"}
return await self._post(f"/cancel", {"orderID": order_id})
async def get_open_orders(self) -> List[Dict]:
"""Get all open orders."""
data = await self._get("/orders/active")
return data if data else []
async def get_balance(self) -> float:
"""Get USDC balance (simplified)."""
# In production, query the Polygon USDC contract directly
data = await self._get("/balances")
if data and "balance" in data:
return float(data["balance"])
return 0.0
# WebSocket
async def subscribe_orderbook(
self,
token_ids: List[str],
callback,
):
"""Subscribe to real-time order book updates via WebSocket."""
while True:
try:
async with websockets.connect(WS_URL) as ws:
subscribe_msg = {
"type": "subscribe",
"channel": "book",
"assets": token_ids,
}
await ws.send(json.dumps(subscribe_msg))
logger.info(f"WebSocket subscribed to {len(token_ids)} tokens")
async for message in ws:
data = json.loads(message)
await callback(data)
except (websockets.exceptions.ConnectionClosed, Exception) as e:
logger.warning(f"WebSocket disconnected: {e}. Reconnecting in 5s...")
await asyncio.sleep(5)
async def close(self):
if self.session and not self.session.closed:
await self.session.close()
class CrossPlatformFeeds:
"""Fetch prices from Binance and CoinGecko."""
COINGECKO_IDS = {
"BTC": "bitcoin",
"ETH": "ethereum",
"XRP": "ripple",
"SOL": "solana",
}
BINANCE_SYMBOLS = {
"BTC": "BTCUSDT",
"ETH": "ETHUSDT",
"XRP": "XRPUSDT",
"SOL": "SOLUSDT",
}
def __init__(self):
self.session: Optional[aiohttp.ClientSession] = None
self._price_cache: Dict[str, Dict] = {} # {asset: {price, timestamp, source}}
async def _ensure_session(self):
if self.session is None or self.session.closed:
self.session = aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=10)
)
async def get_binance_price(self, asset: str) -> Optional[float]:
"""Get current spot price from Binance."""
symbol = self.BINANCE_SYMBOLS.get(asset)
if not symbol:
return None
await self._ensure_session()
try:
url = f"https://api.binance.com/api/v3/ticker/price?symbol={symbol}"
async with self.session.get(url) as resp:
if resp.status == 200:
data = await resp.json()
price = float(data["price"])
self._price_cache[asset] = {
"price": price,
"timestamp": time.time(),
"source": "binance",
}
return price
except Exception as e:
logger.error(f"Binance price fetch failed for {asset}: {e}")
return None
async def get_coingecko_price(self, asset: str) -> Optional[float]:
"""Get current spot price from CoinGecko."""
cg_id = self.COINGECKO_IDS.get(asset)
if not cg_id:
return None
await self._ensure_session()
try:
url = f"{COINGECKO_API_URL}/simple/price?ids={cg_id}&vs_currencies=usd"
async with self.session.get(url) as resp:
if resp.status == 200:
data = await resp.json()
price = data[cg_id]["usd"]
self._price_cache[asset] = {
"price": price,
"timestamp": time.time(),
"source": "coingecko",
}
return float(price)
except Exception as e:
logger.error(f"CoinGecko price fetch failed for {asset}: {e}")
return None
async def get_all_prices(self) -> Dict[str, Dict]:
"""Fetch prices for all crypto assets from multiple sources."""
results = {}
tasks = []
for asset in CRYPTO_ASSETS:
tasks.append(self._fetch_asset_price(asset))
fetched = await asyncio.gather(*tasks, return_exceptions=True)
for asset, data in zip(CRYPTO_ASSETS, fetched):
if isinstance(data, dict):
results[asset] = data
return results
async def _fetch_asset_price(self, asset: str) -> Dict:
"""Fetch price from Binance first, fallback to CoinGecko."""
price = await self.get_binance_price(asset)
source = "binance"
if price is None:
price = await self.get_coingecko_price(asset)
source = "coingecko"
return {
"asset": asset,
"price": price,
"source": source,
"timestamp": time.time(),
}
def get_cached_price(self, asset: str, max_age_sec: int = 30) -> Optional[float]:
"""Get cached price if fresh enough."""
cached = self._price_cache.get(asset)
if cached and (time.time() - cached["timestamp"]) < max_age_sec:
return cached["price"]
return None
async def close(self):
if self.session and not self.session.closed:
await self.session.close()