-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
527 lines (460 loc) Β· 16.6 KB
/
server.js
File metadata and controls
527 lines (460 loc) Β· 16.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
const express = require('express');
const cors = require('cors');
const axios = require('axios');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors({
origin: process.env.FRONTEND_URL || '*',
methods: ['GET', 'POST'],
credentials: true
}));
app.use(express.json());
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
environment: process.env.NODE_ENV || 'development'
});
});
// ============================================
// POLYMARKET API PROXY ENDPOINTS
// ============================================
/**
* GET /api/polymarket/comments/:marketId
* Fetch comments for a specific Polymarket market
*/
app.get('/api/polymarket/comments/:marketId', async (req, res) => {
try {
let { marketId } = req.params;
const { limit = 100, offset = 0 } = req.query;
console.log(`Fetching comments for market: ${marketId}`);
// If marketId is a slug (not hex), fetch market details first to get condition ID
if (!marketId.startsWith('0x')) {
console.log(`Converting slug to condition ID: ${marketId}`);
try {
// Search for market by slug using query parameter
const marketResponse = await axios.get(`https://gamma-api.polymarket.com/markets`, {
params: {
slug: marketId
},
headers: {
'Accept': 'application/json',
'User-Agent': 'PolymarketAnalyzer/1.0'
},
timeout: 10000
});
console.log(`Market search response:`, marketResponse.data ? `Found ${marketResponse.data.length || 0} markets` : 'No data');
if (marketResponse.data && marketResponse.data.length > 0) {
const market = marketResponse.data[0];
marketId = market.conditionId;
console.log(`Resolved to condition ID: ${marketId}`);
} else {
throw new Error('Market not found - no results returned');
}
} catch (lookupError) {
console.error('Market lookup failed:', lookupError.message);
console.error('Full error:', lookupError.response?.data || lookupError);
return res.status(404).json({
success: false,
error: 'Market not found',
message: `Could not find market "${req.params.marketId}". Please check the market slug or try the Browse Markets button.`,
originalSlug: req.params.marketId,
suggestion: 'Use the Browse Markets button to see available markets with comments.'
});
}
}
// Polymarket Comments API endpoint
const response = await axios.get(`https://gamma-api.polymarket.com/comments`, {
params: {
_market: marketId,
_limit: limit,
_offset: offset
},
headers: {
'Accept': 'application/json',
'User-Agent': 'PolymarketAnalyzer/1.0'
},
timeout: 10000
});
console.log(`Successfully fetched ${response.data.length} comments`);
if (response.data.length === 0) {
return res.json({
success: false,
error: 'No comments found',
message: 'This market exists but has no comments yet. Try a more active market with discussions.',
count: 0,
marketId: marketId
});
}
res.json({
success: true,
data: response.data,
count: response.data.length,
marketId: marketId
});
} catch (error) {
console.error('Polymarket API Error:', error.message);
console.error('Error details:', {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
marketId: marketId
});
// Better error message for 422
if (error.response?.status === 422) {
return res.status(422).json({
success: false,
error: 'Cannot fetch comments',
message: 'This market may not have comments, or the market ID format is incorrect. Try browsing markets with the Browse button.',
marketId: marketId
});
}
res.status(error.response?.status || 500).json({
success: false,
error: 'Failed to fetch comments from Polymarket',
message: error.message,
details: error.response?.data || null
});
}
});
/**
* GET /api/polymarket/markets
* Fetch list of active markets
*/
app.get('/api/polymarket/markets', async (req, res) => {
try {
const { limit = 50, offset = 0 } = req.query;
console.log(`Fetching markets list (limit: ${limit})`);
const response = await axios.get('https://gamma-api.polymarket.com/markets', {
params: {
limit: 100, // Fetch more to filter from
offset: offset,
closed: false,
active: true
},
headers: {
'Accept': 'application/json',
'User-Agent': 'PolymarketAnalyzer/1.0'
},
timeout: 10000
});
console.log(`Fetched ${response.data.length} total markets`);
// Sort by volume (higher volume = more likely to have comments)
const sortedMarkets = response.data
.filter(market => {
// Filter for recent, active markets
const volume = parseFloat(market.volume || 0);
const isActive = market.active === true;
const isClosed = market.closed === true;
const endDate = market.endDate ? new Date(market.endDate) : null;
const isNotExpired = !endDate || endDate > new Date();
return isActive && !isClosed && isNotExpired && volume > 1000; // At least $1k volume
})
.sort((a, b) => {
// Sort by volume descending
return parseFloat(b.volume || 0) - parseFloat(a.volume || 0);
})
.slice(0, parseInt(limit));
console.log(`Filtered to ${sortedMarkets.length} active markets with volume`);
res.json({
success: true,
data: sortedMarkets,
count: sortedMarkets.length
});
} catch (error) {
console.error('Polymarket Markets API Error:', error.message);
res.status(error.response?.status || 500).json({
success: false,
error: 'Failed to fetch markets',
message: error.message
});
}
});
/**
* GET /api/polymarket/market/:marketId
* Fetch market metadata
*/
app.get('/api/polymarket/market/:marketId', async (req, res) => {
try {
const { marketId } = req.params;
console.log(`Fetching market data for: ${marketId}`);
const response = await axios.get(`https://gamma-api.polymarket.com/markets/${marketId}`, {
headers: {
'Accept': 'application/json',
'User-Agent': 'PolymarketAnalyzer/1.0'
},
timeout: 10000
});
res.json({
success: true,
data: response.data
});
} catch (error) {
console.error('Polymarket Market API Error:', error.message);
res.status(error.response?.status || 500).json({
success: false,
error: 'Failed to fetch market data',
message: error.message
});
}
});
// ============================================
// AI ANALYSIS ENDPOINTS
// ============================================
/**
* POST /api/analyze/comments
* Analyze comments using Claude AI (primary) with OpenAI fallback
*/
app.post('/api/analyze/comments', async (req, res) => {
try {
const { comments, options = {} } = req.body;
if (!comments || !Array.isArray(comments)) {
return res.status(400).json({
success: false,
error: 'Invalid request: comments array is required'
});
}
console.log(`Analyzing ${comments.length} comments with options:`, options);
// Try Claude first
let analysisResult;
let aiProvider = 'claude';
try {
analysisResult = await analyzeWithClaude(comments, options);
} catch (claudeError) {
console.warn('Claude API failed, falling back to OpenAI:', claudeError.message);
// Fallback to OpenAI
if (process.env.OPENAI_API_KEY) {
aiProvider = 'openai';
analysisResult = await analyzeWithOpenAI(comments, options);
} else {
throw new Error('Both Claude and OpenAI APIs unavailable');
}
}
res.json({
success: true,
data: analysisResult,
metadata: {
commentsAnalyzed: comments.length,
aiProvider: aiProvider,
timestamp: new Date().toISOString()
}
});
} catch (error) {
console.error('Analysis Error:', error.message);
res.status(500).json({
success: false,
error: 'Failed to analyze comments',
message: error.message
});
}
});
/**
* Analyze comments using Claude AI
*/
async function analyzeWithClaude(comments, options) {
if (!process.env.ANTHROPIC_API_KEY) {
throw new Error('ANTHROPIC_API_KEY not configured');
}
const prompt = buildAnalysisPrompt(comments, options);
const response = await axios.post(
'https://api.anthropic.com/v1/messages',
{
model: 'claude-3-5-sonnet-20241022',
max_tokens: 4096,
temperature: 0.7,
messages: [{
role: 'user',
content: prompt
}]
},
{
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01'
},
timeout: 60000
}
);
const content = response.data.content[0].text;
return parseAIResponse(content);
}
/**
* Analyze comments using OpenAI GPT-4
*/
async function analyzeWithOpenAI(comments, options) {
if (!process.env.OPENAI_API_KEY) {
throw new Error('OPENAI_API_KEY not configured');
}
const prompt = buildAnalysisPrompt(comments, options);
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-4-turbo-preview',
messages: [{
role: 'system',
content: 'You are an expert financial analyst specializing in prediction markets and sentiment analysis. Provide structured, actionable insights.'
}, {
role: 'user',
content: prompt
}],
temperature: 0.7,
max_tokens: 4096
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
},
timeout: 60000
}
);
const content = response.data.choices[0].message.content;
return parseAIResponse(content);
}
/**
* Build analysis prompt based on comments and options
*/
function buildAnalysisPrompt(comments, options) {
const {
analyzeSentiment = true,
detectDivergence = true,
findInsiders = true,
filterHolders = false
} = options;
// Filter holders if requested
const targetComments = filterHolders
? comments.filter(c => c.profile?.positions?.length > 0)
: comments;
const commentsText = targetComments.map((comment, idx) => {
const username = comment.profile?.pseudonym || comment.profile?.name || 'Anonymous';
const position = comment.profile?.positions?.[0]?.positionSize || '0';
const hasPosition = parseFloat(position) > 0;
return `[${idx + 1}] ${username} ${hasPosition ? `(Position: $${position})` : '(No position)'}: ${comment.body}`;
}).join('\n\n');
return `You are analyzing ${targetComments.length} comments from a Polymarket prediction market.
COMMENTS:
${commentsText}
ANALYSIS REQUIREMENTS:
${analyzeSentiment ? '- Perform sentiment analysis (bullish/bearish/neutral) for each comment' : ''}
${detectDivergence ? '- Detect divergence between large position holders and smaller traders' : ''}
${findInsiders ? '- Identify potential insider information or unique alpha signals' : ''}
- Assign an "alpha score" (1-10) to each comment based on information value
- Extract key insights and trading implications
OUTPUT FORMAT (MUST BE VALID JSON):
{
"summary": {
"totalComments": ${targetComments.length},
"holdersCount": <number>,
"overallSentiment": "<bullish|bearish|neutral>",
"sentimentDistribution": {
"bullish": <number>,
"bearish": <number>,
"neutral": <number>
}
},
"insights": [
"Key insight 1",
"Key insight 2",
...
],
"divergenceAlerts": [
{
"severity": "<high|medium|low>",
"description": "Alert description"
}
],
"topComments": [
{
"commentIndex": <number>,
"username": "<string>",
"sentiment": "<bullish|bearish|neutral>",
"alphaScore": <1-10>,
"positionSize": "<string>",
"reasoning": "Why this comment is valuable"
}
],
"insiderMentions": [
{
"comment": "Relevant excerpt",
"significance": "Why this might be insider info"
}
]
}
IMPORTANT: Respond ONLY with valid JSON. No markdown, no explanations outside the JSON.`;
}
/**
* Parse AI response and ensure valid JSON
*/
function parseAIResponse(content) {
try {
// Remove markdown code blocks if present
let cleaned = content.trim();
if (cleaned.startsWith('```json')) {
cleaned = cleaned.replace(/```json\n?/g, '').replace(/```\n?$/g, '');
} else if (cleaned.startsWith('```')) {
cleaned = cleaned.replace(/```\n?/g, '');
}
return JSON.parse(cleaned);
} catch (error) {
console.error('Failed to parse AI response:', error.message);
console.error('Raw content:', content);
// Return a fallback structure
return {
summary: {
totalComments: 0,
holdersCount: 0,
overallSentiment: 'neutral',
sentimentDistribution: { bullish: 0, bearish: 0, neutral: 0 }
},
insights: ['AI response could not be parsed. Please try again.'],
divergenceAlerts: [],
topComments: [],
insiderMentions: []
};
}
}
// ============================================
// ERROR HANDLING
// ============================================
// 404 handler
app.use((req, res) => {
res.status(404).json({
success: false,
error: 'Endpoint not found',
availableEndpoints: [
'GET /health',
'GET /api/polymarket/markets',
'GET /api/polymarket/comments/:marketId',
'GET /api/polymarket/market/:marketId',
'POST /api/analyze/comments'
]
});
});
// Global error handler
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
res.status(500).json({
success: false,
error: 'Internal server error',
message: err.message
});
});
// ============================================
// START SERVER
// ============================================
app.listen(PORT, () => {
console.log('='.repeat(50));
console.log(`π Polymarket Analyzer Backend Server`);
console.log(`π‘ Port: ${PORT}`);
console.log(`π Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`π Claude API: ${process.env.ANTHROPIC_API_KEY ? 'β
Configured' : 'β Missing'}`);
console.log(`π OpenAI API: ${process.env.OPENAI_API_KEY ? 'β
Configured' : 'β Missing'}`);
console.log('='.repeat(50));
console.log(`\n⨠Server ready at http://localhost:${PORT}`);
console.log(`π Health check: http://localhost:${PORT}/health\n`);
});
module.exports = app;