import signal
import sys
from dome_api_sdk import DomeClient
# Initialize client with auto-reconnect
dome = DomeClient({
"api_key": "YOUR_API_KEY",
"websocket": {
"auto_reconnect": True,
"reconnect_interval": 5,
"max_reconnect_attempts": 10
}
})
# Track subscriptions
subscriptions = {}
# Connection handler
def on_connect():
print("✅ Connected to Dome API WebSocket")
# Subscribe to orders
try:
sub = dome.polymarket.websocket.subscribe({
"platform": "polymarket",
"version": 1,
"type": "orders",
"filters": {
"users": [
"0x6031b6eed1c97e853c6e0f03ad3ce3529351f96d",
"0x7c3db723f1d4d8cb9c550095203b686cb11e5c6b"
]
}
})
subscriptions[sub.subscription_id] = {
"platform": "polymarket",
"version": 1,
"type": "orders",
"filters": {
"users": [
"0x6031b6eed1c97e853c6e0f03ad3ce3529351f96d",
"0x7c3db723f1d4d8cb9c550095203b686cb11e5c6b"
]
}
}
print(f"📡 Subscribed: {sub.subscription_id}")
except Exception as error:
print(f"❌ Subscription failed: {error}")
def on_error(error):
print(f"❌ WebSocket error: {error}")
def on_close():
print("🔌 Disconnected from WebSocket")
def on_reconnect():
print("🔄 Reconnected, restoring subscriptions...")
# Restore all subscriptions
for sub_id, config in subscriptions.items():
try:
sub = dome.polymarket.websocket.subscribe(config)
print(f"📡 Restored subscription: {sub.subscription_id}")
except Exception as error:
print(f"❌ Failed to restore subscription: {error}")
# Register handlers
dome.polymarket.websocket.on_connect(on_connect)
dome.polymarket.websocket.on_error(on_error)
dome.polymarket.websocket.on_close(on_close)
dome.polymarket.websocket.on_reconnect(on_reconnect)
# Handle order events
def handle_order_event(event):
order = event.data
print("\n📦 New Order Event:")
print(f" Subscription: {event.subscription_id}")
print(f" User: {order.user}")
print(f" Side: {order.side}")
print(f" Shares: {order.shares_normalized}")
print(f" Price: {order.price}")
print(f" Market: {order.market_slug}")
print(f" Title: {order.title}")
from datetime import datetime
print(f" Timestamp: {datetime.fromtimestamp(order.timestamp).isoformat()}")
dome.polymarket.websocket.on_event(handle_order_event)
# Graceful shutdown
def signal_handler(sig, frame):
print("\n🛑 Shutting down...")
# Unsubscribe from all
for sub_id in list(subscriptions.keys()):
try:
dome.polymarket.websocket.unsubscribe(sub_id)
print(f"✅ Unsubscribed: {sub_id}")
except Exception as error:
print(f"❌ Unsubscribe failed: {error}")
# Disconnect
dome.polymarket.websocket.disconnect()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Keep the script running
try:
import time
while True:
time.sleep(1)
except KeyboardInterrupt:
signal_handler(None, None)