-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
774 lines (633 loc) · 26.6 KB
/
app.py
File metadata and controls
774 lines (633 loc) · 26.6 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#!/usr/bin/env python3
"""
Polymarket AI Agent - Web Dashboard
Flask application for monitoring and controlling the agent
"""
from flask import Flask, render_template, jsonify, request, Response
from functools import wraps
from datetime import datetime
import os
import logging
import threading
import time
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging with in-memory buffer
class LogBuffer(logging.Handler):
"""Store recent logs in memory for dashboard display"""
def __init__(self, max_lines=100):
super().__init__()
self.logs = []
self.max_lines = max_lines
def emit(self, record):
msg = self.format(record)
self.logs.append(msg)
if len(self.logs) > self.max_lines:
self.logs = self.logs[-self.max_lines:]
def get_logs(self):
return self.logs.copy()
def clear(self):
self.logs = []
log_buffer = LogBuffer()
log_buffer.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
logger.addHandler(log_buffer)
# Initialize Flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv("FLASK_SECRET_KEY", "default-dev-key")
# Basic Auth
ADMIN_USER = os.getenv("ADMIN_USER", "admin")
ADMIN_PASS = os.getenv("ADMIN_PASS", "polymarket2024")
def check_auth(username, password):
return username == ADMIN_USER and password == ADMIN_PASS
def authenticate():
return Response(
'Login required', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'}
)
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
# Import modules (after app creation to avoid circular imports)
from src.market.polymarket import fetch_active_markets
from src.analysis.ai_analyzer import get_analyzer
from src.trading.wallet import get_wallet
from src.trading.executor import get_executor
# Initialize BlockRun client and trading
analyzer = get_analyzer()
wallet = get_wallet()
executor = get_executor()
def _truncate(addr):
return f"{addr[:6]}...{addr[-4:]}" if addr and len(addr) >= 12 else addr
if analyzer:
logger.info(f"BlockRun client initialized. Wallet: {_truncate(analyzer.wallet_address)}")
else:
logger.warning("BlockRun not configured. Set BASE_CHAIN_WALLET_KEY in .env")
if wallet:
logger.info(f"Trading wallet initialized: {_truncate(wallet.address)}")
else:
logger.warning("Trading wallet not configured. Set POLYGON_WALLET_PRIVATE_KEY in .env")
if executor:
logger.info("Trade executor initialized")
else:
logger.warning("Trade executor not available")
# ============ Agent State ============
class AgentState:
def __init__(self):
self.running = False
self.thread = None
self.last_run = None
self.cycle_count = 0
self.decisions = []
self.trades = []
self.error = None
self.auto_trade = False # Auto-trading disabled by default
self._load_persistent_data()
def _load_persistent_data(self):
"""
Load persisted data from Google Cloud Storage (if enabled)
Falls back to local /tmp storage if GCS is disabled
"""
try:
from src.storage import get_storage
storage = get_storage()
# If GCS is disabled, use local /tmp storage
if storage is None:
logger.info("📂 Using local /tmp storage (USE_GCS_STORAGE=false)")
self._load_from_tmp()
return
# Load from GCS
self.trades = storage.load_orders()
logger.info(f"📦 Loaded {len(self.trades)} orders from GCS")
self.decisions = storage.load_decisions()
logger.info(f"🧠 Loaded {len(self.decisions)} decisions from GCS")
except Exception as e:
logger.error(f"Failed to load from GCS: {e}")
logger.warning("Continuing with empty state")
self.trades = []
self.decisions = []
def _load_from_tmp(self):
"""Load data from local /tmp directory (fallback when GCS disabled)"""
import os
import json
tmp_orders = "/tmp/polymarket_orders.json"
tmp_decisions = "/tmp/polymarket_decisions.json"
# Load orders
try:
if os.path.exists(tmp_orders):
with open(tmp_orders, 'r') as f:
data = json.load(f)
self.trades = data.get('orders', [])
logger.info(f"📦 Loaded {len(self.trades)} orders from /tmp")
else:
self.trades = []
except Exception as e:
logger.error(f"Failed to load orders from /tmp: {e}")
self.trades = []
# Load decisions
try:
if os.path.exists(tmp_decisions):
with open(tmp_decisions, 'r') as f:
data = json.load(f)
self.decisions = data.get('decisions', [])
logger.info(f"🧠 Loaded {len(self.decisions)} decisions from /tmp")
else:
self.decisions = []
except Exception as e:
logger.error(f"Failed to load decisions from /tmp: {e}")
self.decisions = []
def save_persistent_data(self):
"""
Save all data to Google Cloud Storage (if enabled)
Falls back to local /tmp storage if GCS is disabled
"""
try:
from src.storage import get_storage
storage = get_storage()
# If GCS is disabled, use local /tmp storage
if storage is None:
self._save_to_tmp()
return
# Save to GCS
storage.save_orders(self.trades)
storage.save_decisions(self.decisions)
logger.info(f"💾 Saved {len(self.trades)} orders and {len(self.decisions)} decisions to GCS")
except Exception as e:
logger.error(f"Failed to save to GCS: {e}")
def _save_to_tmp(self):
"""Save data to local /tmp directory (fallback when GCS disabled)"""
import os
import json
from datetime import datetime
tmp_orders = "/tmp/polymarket_orders.json"
tmp_decisions = "/tmp/polymarket_decisions.json"
# Save orders
try:
orders_data = {
'orders': self.trades,
'updated_at': datetime.now().isoformat(),
'total_orders': len(self.trades)
}
with open(tmp_orders, 'w') as f:
json.dump(orders_data, f, indent=2)
except Exception as e:
logger.error(f"Failed to save orders to /tmp: {e}")
# Save decisions
try:
decisions_data = {
'decisions': self.decisions,
'updated_at': datetime.now().isoformat(),
'total_decisions': len(self.decisions)
}
with open(tmp_decisions, 'w') as f:
json.dump(decisions_data, f, indent=2)
except Exception as e:
logger.error(f"Failed to save decisions to /tmp: {e}")
logger.info(f"💾 Saved {len(self.trades)} orders and {len(self.decisions)} decisions to /tmp")
# Backward compatibility aliases
def save_persistent_trades(self):
"""Alias for backward compatibility"""
self.save_persistent_data()
state = AgentState()
def run_agent_cycle():
"""Run one cycle of market analysis with multi-model consensus"""
try:
state.cycle_count += 1
state.last_run = datetime.now()
state.error = None
markets = fetch_active_markets(limit=20)
if not markets:
logger.warning("No markets fetched from Polymarket API")
return
if not analyzer:
state.error = "AI analyzer not configured"
return
num_to_analyze = min(10, len(markets))
logger.info(f"Fetched {len(markets)} markets, analyzing {num_to_analyze} with 3-model consensus...")
# Analyze markets using multi-model consensus
for i, market in enumerate(markets[:num_to_analyze]):
logger.info(f"Analyzing market {i+1}/{num_to_analyze}...")
try:
question = market.get("question", "")
yes_odds = market.get("yes_odds", 0.5)
# Use condition_id for trade API (more reliable)
market_id = market.get("condition_id", market.get("id", ""))
# Get whale/smart money data from real Polymarket API
whale_data = None
try:
import asyncio
from src.signals.trades import get_smart_money_summary
whale_data = asyncio.get_event_loop().run_until_complete(
get_smart_money_summary(market_id)
)
if whale_data and whale_data.get("has_smart_money_activity"):
logger.info(f"Whale signal: {whale_data.get('consensus')} ({whale_data.get('confidence', 0)*100:.0f}% confidence)")
except Exception as e:
logger.debug(f"Whale data unavailable: {e}")
# Use 3-model consensus analysis
analysis = analyzer.consensus_analysis(
question=question,
current_odds=yes_odds,
whale_data=whale_data
)
action = analysis.get("recommendation", "SKIP")
edge = analysis.get("avg_edge", 0)
consensus = analysis.get("consensus", "MIXED")
# Build reasoning from model votes
model_results = analysis.get("model_results", [])
reasoning_parts = []
for r in model_results:
reasoning_parts.append(f"{r['model']}: {r['probability']*100:.0f}%")
reasoning = f"[{consensus}] " + " | ".join(reasoning_parts)
# Add whale info if available
if whale_data and whale_data.get("has_smart_money_activity"):
reasoning += f" | Whales: {whale_data.get('consensus', 'N/A')}"
# Prepare whale info for decision
whale_info = None
if whale_data and whale_data.get("has_smart_money_activity"):
whale_info = {
"consensus": whale_data.get("consensus"),
"confidence": whale_data.get("confidence", 0),
"pattern": whale_data.get("pattern"),
"direction": whale_data.get("smart_money_direction"),
"volume": whale_data.get("total_large_volume", 0),
"traders": whale_data.get("large_traders_count", 0),
}
# Get token IDs for trading
token_ids = market.get("token_ids", [])
yes_token = token_ids[0] if len(token_ids) > 0 else None
no_token = token_ids[1] if len(token_ids) > 1 else None
logger.info(f"Token IDs: {token_ids[:2] if token_ids else 'NONE'}")
decision = {
"timestamp": datetime.now().isoformat(),
"market": question[:60],
"action": action,
"confidence": analysis.get("avg_confidence", 0),
"reasoning": reasoning[:200],
"edge": edge,
"ai_prob": analysis.get("avg_probability", 0),
"market_prob": analysis.get("market_probability", 0),
"consensus": consensus,
"yes_votes": analysis.get("yes_votes", 0),
"no_votes": analysis.get("no_votes", 0),
"models_used": analysis.get("models_used", 0),
"whale_data": whale_info,
"trade_result": None,
}
# Auto-trade if enabled (only when action is BET YES or BET NO)
if state.auto_trade and executor and action.startswith("BET"):
try:
# Determine which token to trade
token_id = yes_token if "YES" in action else no_token
if token_id:
confidence = analysis.get("avg_confidence", 0)
bankroll = wallet.get_usdc_balance() if wallet else 100
trade_result = executor.execute_signal(
token_id=token_id,
action=action,
edge=edge,
confidence=confidence,
consensus=consensus,
bankroll=bankroll
)
decision["trade_result"] = trade_result
if trade_result.get("status") in ["success", "submitted"]:
status_msg = "SUBMITTED" if trade_result.get("status") == "submitted" else "EXECUTED"
logger.info(f"TRADE {status_msg}: {action} ${trade_result.get('size', 0):.2f}")
logger.info(f" Order ID: {trade_result.get('order_id', 'Unknown')}")
logger.info(f" ⚠️ Check Polymarket.com to see if order fills")
state.trades.append({
"timestamp": datetime.now().isoformat(),
"market": question[:40],
"action": action,
"size": trade_result.get("size", 0),
"order_id": trade_result.get("order_id"),
"status": trade_result.get("status", "submitted"),
"message": trade_result.get("message", "")
})
# Persist to disk so it survives restarts
state.save_persistent_trades()
else:
logger.info(f"Trade skipped: {trade_result.get('reason', 'unknown')}")
else:
logger.warning("No token ID available for trading")
except Exception as e:
logger.error(f"Trade execution error: {e}")
decision["trade_result"] = {"status": "error", "reason": str(e)}
state.decisions.append(decision)
# Also save market analysis for future assessment
market_record = {
"timestamp": datetime.now().isoformat(),
"question": question,
"market_id": market_id,
"yes_odds": yes_odds,
"volume": float(market.get("volume", 0) or 0),
"liquidity": float(market.get("liquidity", 0) or 0),
"ai_analysis": analysis,
"decision": action,
"edge": edge
}
# Save market analysis to GCS for assessment (if GCS enabled)
try:
from src.storage import get_storage
storage = get_storage()
if storage is not None: # Only save to GCS if enabled
storage.add_market_analysis(market_record)
except Exception as e:
logger.debug(f"Failed to save market analysis: {e}")
# Keep only last 30
if len(state.decisions) > 30:
state.decisions = state.decisions[-30:]
logger.info(f"Consensus: {consensus} | {action} for {question[:30]}... (edge: {edge*100:.1f}%)")
except Exception as e:
logger.error(f"Analysis error: {e}")
# Save decisions to GCS at end of cycle
try:
state.save_persistent_data()
except Exception as e:
logger.error(f"Failed to save cycle data to GCS: {e}")
except Exception as e:
state.error = str(e)
logger.error(f"Cycle error: {e}")
def agent_loop():
"""Background agent loop - continuous market analysis"""
while state.running:
run_agent_cycle()
# Wait 6 hours before next cycle (for continuous demo running)
logger.info("Cycle complete. Next cycle in 6 hours...")
time.sleep(6 * 60 * 60) # 6 hours between cycles
def start_agent():
"""Start the agent background thread"""
if state.running:
return False
state.running = True
state.thread = threading.Thread(target=agent_loop, daemon=True)
state.thread.start()
logger.info("Agent started")
return True
def stop_agent():
"""Stop the agent"""
state.running = False
logger.info("Agent stopped")
return True
def get_dashboard_data():
"""Get data for dashboard display"""
# Fetch markets
markets = fetch_active_markets(limit=10)
# Format for display - keep raw numeric values for template
formatted_markets = []
for m in markets[:8]:
# Handle volume as string or number
vol = m.get('volume', 0)
try:
vol = float(vol) if vol else 0
except (ValueError, TypeError):
vol = 0
# Get odds as floats
yes_odds = m.get('yes_odds', 0)
no_odds = m.get('no_odds', 0)
try:
yes_odds = float(yes_odds) if yes_odds else 0
except (ValueError, TypeError):
yes_odds = 0
try:
no_odds = float(no_odds) if no_odds else 0
except (ValueError, TypeError):
no_odds = 0
formatted_markets.append({
"question": m.get("question", "Unknown"),
"description": m.get("description", "")[:200],
"end_date": m.get("end_date", "Unknown"),
"volume": vol,
"yes_odds": yes_odds,
"no_odds": no_odds,
})
# Calculate mock totals (in production, use real portfolio data)
def safe_float(v):
try:
return float(v) if v else 0
except (ValueError, TypeError):
return 0
total_bet = sum(safe_float(m.get("volume", 0)) for m in markets[:5]) / 1000
expected_profit = total_bet * 0.15 # Mock 15% expected return
roi = 15.0
return {
'markets': formatted_markets,
'tomorrow_display': datetime.now().strftime('%B %d, %Y'),
'total_bet_amount': total_bet,
'total_expected_profit': expected_profit,
'roi_percentage': roi,
'current_year': datetime.now().year
}
def truncate_address(address: str) -> str:
"""Truncate wallet address for display (0x1234...5678)"""
if not address or len(address) < 12:
return address
return f"{address[:6]}...{address[-4:]}"
@app.route('/')
def home():
"""Dashboard home page - public for demo"""
data = get_dashboard_data()
return render_template('index.html', **data)
@app.route('/api/markets')
def api_markets():
"""API endpoint to get market data"""
markets = fetch_active_markets(limit=20)
return jsonify({
"count": len(markets),
"markets": markets
})
@app.route('/api/status')
def api_status():
"""API endpoint to get agent status"""
status = {
"ai_configured": analyzer is not None,
"wallet_configured": wallet is not None,
"timestamp": datetime.now().isoformat()
}
if analyzer:
status["ai_wallet"] = truncate_address(analyzer.wallet_address)
if wallet:
status["trading_wallet"] = truncate_address(wallet.address)
status["usdc_balance"] = wallet.get_usdc_balance()
status["approved"] = wallet.check_approval()
return jsonify(status)
@app.route('/api/analyze')
def api_analyze():
"""API endpoint to run AI analysis"""
if not analyzer:
return jsonify({"error": "AI analyzer not configured"}), 503
markets = fetch_active_markets(limit=10)
if not markets:
return jsonify({"error": "No markets available"}), 404
analysis = analyzer.analyze_markets(markets)
return jsonify({
"markets_analyzed": len(markets),
"analysis": analysis
})
# ============ Agent Control Endpoints ============
@app.route('/api/agent/start', methods=['POST'])
@requires_auth
def api_start():
"""Start the agent"""
if start_agent():
return jsonify({"status": "started"})
return jsonify({"status": "already_running"})
@app.route('/api/agent/stop', methods=['POST'])
@requires_auth
def api_stop():
"""Stop the agent"""
stop_agent()
return jsonify({"status": "stopped"})
@app.route('/api/agent/status')
def api_agent_status():
"""Get detailed agent status"""
return jsonify({
"running": state.running,
"auto_trade": state.auto_trade,
"last_run": state.last_run.isoformat() if state.last_run else None,
"cycle_count": state.cycle_count,
"error": state.error,
"decisions_count": len(state.decisions),
"trades_count": len(state.trades),
})
@app.route('/api/agent/decisions')
def api_decisions():
"""Get recent decisions"""
return jsonify({"decisions": state.decisions[-20:]})
@app.route('/api/agent/run-once', methods=['POST'])
@requires_auth
def api_run_once():
"""Run one analysis cycle manually"""
if state.running:
return jsonify({"error": "Agent is running, stop it first"}), 400
run_agent_cycle()
return jsonify({"status": "ok", "cycle": state.cycle_count})
@app.route('/api/agent/auto-trade', methods=['POST'])
@requires_auth
def api_toggle_auto_trade():
"""Toggle auto-trading"""
data = request.get_json() or {}
if "enabled" in data:
state.auto_trade = bool(data["enabled"])
else:
state.auto_trade = not state.auto_trade
logger.info(f"Auto-trade {'ENABLED' if state.auto_trade else 'DISABLED'}")
return jsonify({"auto_trade": state.auto_trade})
@app.route('/api/agent/trades')
def api_trades():
"""Get recent trades"""
return jsonify({"trades": state.trades[-20:]})
@app.route('/api/positions')
def api_positions():
"""
Get comprehensive trading data:
1. Open orders from Polymarket CLOB (live orderbook)
2. Filled positions from Polymarket (actual holdings)
3. Order history from persistent storage (all sessions)
"""
if not executor:
return jsonify({"error": "Executor not configured"}), 503
try:
# Fetch live data from Polymarket API
logger.info("📊 Fetching live data from Polymarket CLOB...")
open_orders = executor.get_open_orders()
positions = executor.get_positions()
logger.info(f" Live orders: {len(open_orders)}, Positions: {len(positions)}")
# Get tracked order history from persistent storage (all sessions)
order_history = []
for trade in state.trades:
if trade.get("order_id"): # Any order we've tracked
order_history.append({
"order_id": trade["order_id"],
"market": trade.get("market", "Unknown"),
"action": trade.get("action", "Unknown"),
"size": trade.get("size", 0),
"status": trade.get("status", "unknown"),
"timestamp": trade.get("timestamp"),
"message": trade.get("message", "")
})
logger.info(f" Order history: {len(order_history)} tracked orders")
return jsonify({
"open_orders": open_orders, # Live from Polymarket
"positions": positions, # Live from Polymarket
"order_history": order_history, # From persistent storage
"total_orders": len(open_orders),
"total_positions": len(positions),
"total_tracked": len(order_history)
})
except Exception as e:
logger.error(f"❌ Failed to fetch positions: {e}")
import traceback
logger.error(traceback.format_exc())
return jsonify({
"error": str(e),
"open_orders": [],
"positions": [],
"order_history": []
}), 500
@app.route('/api/add_order', methods=['POST'])
def api_add_order():
"""Manually add an order to track (for orders placed before persistence was added)"""
try:
data = request.json
order = {
"timestamp": data.get("timestamp", datetime.now().isoformat()),
"market": data.get("market", "Unknown"),
"action": data.get("action", "Unknown"),
"size": float(data.get("size", 0)),
"order_id": data.get("order_id"),
"status": data.get("status", "submitted"),
"message": data.get("message", "Manually added order")
}
state.trades.append(order)
state.save_persistent_trades()
logger.info(f"📝 Manually added order: {order['order_id'][:20]}... for {order['market']}")
return jsonify({"status": "success", "order": order})
except Exception as e:
logger.error(f"Failed to add order: {e}")
return jsonify({"status": "error", "error": str(e)}), 400
@app.route('/api/logs')
def api_logs():
"""Get recent logs"""
return jsonify({"logs": log_buffer.get_logs()})
@app.route('/api/logs/clear', methods=['POST'])
@requires_auth
def api_clear_logs():
"""Clear logs"""
log_buffer.clear()
return jsonify({"status": "cleared"})
@app.route('/setup')
def setup():
"""Setup page"""
missing_vars = []
if not os.getenv("BLOCKRUN_WALLET_KEY"):
missing_vars.append("BLOCKRUN_WALLET_KEY")
if not os.getenv("POLYGON_WALLET_PRIVATE_KEY"):
missing_vars.append("POLYGON_WALLET_PRIVATE_KEY")
return render_template('setup.html', missing_vars=missing_vars, current_year=datetime.now().year)
# Create templates directory if needed
if not os.path.exists('templates'):
os.makedirs('templates')
logger.info("Created templates directory")
if __name__ == '__main__':
print("=" * 60)
print("POLYMARKET AI AGENT - WEB DASHBOARD")
print("=" * 60)
if not analyzer:
print("WARNING: BlockRun not configured (AI features disabled)")
if not wallet:
print("WARNING: Trading wallet not configured")
print("\nStarting Flask app...")
print("Visit http://127.0.0.1:5001 in your browser")
app.run(debug=True, port=5001)