preflight book check: all wallets + active-market fallback

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
jaxperro
2026-07-09 00:31:39 -04:00
parent 82b0acd5f1
commit 4adf80aee4
+26 -15
View File
@@ -87,22 +87,33 @@ def main():
# a sharp's LATEST trade is often an in-play market that has already
# closed (404 = "no orderbook", which itself proves connectivity) —
# walk recent trades until one still has a live book
req = urllib.request.Request(
"https://data-api.polymarket.com/activity?user="
+ cfg["watchlist"][0] + "&type=TRADE&limit=15",
headers={"User-Agent": "Mozilla/5.0"})
trades = json.loads(urllib.request.urlopen(req, timeout=15, context=_SSL).read())
last = None
for t in trades:
try:
ob = client.get_order_book(t["asset"])
except Exception as e:
last = e
continue
bid = ob.bids[-1].price if ob.bids else ""
ask = ob.asks[-1].price if ob.asks else ""
return f"{(t.get('title') or '')[:40]}… bid {bid} / ask {ask}"
raise RuntimeError(f"no live book among the wallet's last {len(trades)} trades ({last})")
for w in cfg["watchlist"]:
req = urllib.request.Request(
"https://data-api.polymarket.com/activity?user=" + w
+ "&type=TRADE&limit=10",
headers={"User-Agent": "Mozilla/5.0"})
for t in json.loads(urllib.request.urlopen(req, timeout=15, context=_SSL).read()):
try:
ob = client.get_order_book(t["asset"])
except Exception as e:
last = e
continue
bid = ob.bids[-1].price if ob.bids else ""
ask = ob.asks[-1].price if ob.asks else ""
return f"{(t.get('title') or '')[:40]}… bid {bid} / ask {ask}"
# every followed wallet is fully in-play right now — prove book access
# against any active market instead
req = urllib.request.Request(
"https://gamma-api.polymarket.com/markets?active=true&closed=false"
"&limit=1&order=volume24hr&ascending=false",
headers={"User-Agent": "Mozilla/5.0"})
gm = json.loads(urllib.request.urlopen(req, timeout=15, context=_SSL).read())[0]
tok = json.loads(gm["clobTokenIds"])[0]
ob = client.get_order_book(tok)
bid = ob.bids[-1].price if ob.bids else ""
ask = ob.asks[-1].price if ob.asks else ""
return f"[fallback: {(gm.get('question') or '')[:36]}…] bid {bid} / ask {ask}"
check("order book fetch (market access)", book)
def gas():