-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintensive_arbitrage_scanner.js
More file actions
293 lines (250 loc) · 13.3 KB
/
Copy pathintensive_arbitrage_scanner.js
File metadata and controls
293 lines (250 loc) · 13.3 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
#!/usr/bin/env node
const https = require('https');
const fs = require('fs');
// WALLET CREDENTIALS
const WALLET_ADDRESS = '0x4365F3339e8Aef1EdD95916DBF57949012E8B6f2';
const WALLET_BALANCE = '78.43 POL (~$43.14)';
// API Configuration
const POLYMARKET_API = 'https://gamma-api.polymarket.com';
const BACKUP_APIS = [
'https://gamma-api.polymarket.com',
'https://api.polymarket.com',
'https://polymarket-api.xyz' // If exists
];
async function fetchJSON(url, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
return await new Promise((resolve, reject) => {
const options = {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; ArbitrageBot/2.0)',
'Accept': 'application/json',
'Cache-Control': 'no-cache'
},
timeout: 15000
};
https.get(url, options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
if (res.statusCode === 200) {
resolve(JSON.parse(data));
} else {
reject(new Error(`HTTP ${res.statusCode}`));
}
} catch (e) {
reject(e);
}
});
}).on('error', reject).on('timeout', () => reject(new Error('Timeout')));
});
} catch (error) {
if (i === retries - 1) throw error;
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)));
}
}
}
async function intensiveArbitrageScan() {
const timestamp = new Date();
const scanId = timestamp.toISOString().slice(0,19).replace('T', '_').replace(/:/g, '-');
console.log('🚨 INTENSIVE ARBITRAGE SCAN #' + scanId);
console.log('💰 Wallet:', WALLET_ADDRESS);
console.log('💵 Funds:', WALLET_BALANCE);
console.log('⏰ Scan time:', timestamp.toISOString());
console.log('🎯 Target: ALL profitable opportunities >50% APY');
console.log('=' .repeat(70));
const results = {
scanId: scanId,
timestamp: timestamp.toISOString(),
opportunities: [],
apiErrors: [],
totalMarkets: 0,
highValueOpportunities: []
};
try {
let marketsData = null;
let apiUsed = null;
// Try multiple API endpoints
for (const baseUrl of BACKUP_APIS) {
const endpoints = [
`${baseUrl}/markets?active=true&limit=50&closed=false`,
`${baseUrl}/markets?limit=50`,
`${baseUrl}/events?active=true&limit=30`
];
for (const url of endpoints) {
try {
console.log(`🌐 Trying: ${url.slice(-30)}...`);
const data = await fetchJSON(url);
if (data && (data.data || data.length > 0)) {
marketsData = data.data || data;
apiUsed = url;
console.log(`✅ Connected! Found ${marketsData.length} markets`);
break;
}
} catch (e) {
results.apiErrors.push(`${url}: ${e.message}`);
console.log(`❌ ${url.slice(-30)}: ${e.message}`);
}
}
if (marketsData) break;
}
if (!marketsData) {
console.log('🚨 ALL APIs FAILED - Using backup strategy');
console.log('📝 Manual check: EventArb.com + Polymarket.com');
results.error = 'All APIs unavailable';
return results;
}
results.totalMarkets = marketsData.length;
console.log(`\\n🔍 Deep-scanning ${marketsData.length} markets for arbitrage...`);
const now = Date.now();
const HOUR = 60 * 60 * 1000;
const DAY = 24 * HOUR;
let processedMarkets = 0;
for (const market of marketsData) {
try {
processedMarkets++;
// Progress indicator for long scans
if (processedMarkets % 10 === 0) {
console.log(`📊 Processed ${processedMarkets}/${marketsData.length} markets...`);
}
// Check market timing
let endTime = null;
if (market.end_date_iso) {
endTime = new Date(market.end_date_iso).getTime();
} else if (market.endDate) {
endTime = new Date(market.endDate).getTime();
}
let timeLeft = endTime ? (endTime - now) : (7 * DAY);
let hoursLeft = Math.max(1, Math.round(timeLeft / HOUR));
// Scan markets ending within 30 days (broader net)
if (timeLeft > 0 && timeLeft < 30 * DAY) {
if (market.tokens && market.tokens.length > 0) {
for (const token of market.tokens) {
try {
// Get live market data
let probability = null;
let volume24h = market.volume_24hr || market.volume || 0;
let hasLiveData = false;
// Try to get live book/pricing
try {
const bookUrl = `${POLYMARKET_API}/book?token_id=${token.token_id}`;
const book = await fetchJSON(bookUrl, 1); // Single retry for speed
if (book && book.bids && book.asks && book.bids.length > 0 && book.asks.length > 0) {
const bestBid = parseFloat(book.bids[0].price);
const bestAsk = parseFloat(book.asks[0].price);
probability = (bestBid + bestAsk) / 2;
hasLiveData = true;
}
} catch (bookError) {
// Use fallback pricing
if (token.price) {
probability = parseFloat(token.price);
}
}
// AGGRESSIVE FILTERING: Catch ALL profitable opportunities
if (probability !== null && probability > 0.75) { // Lowered from 0.85 to catch more
const potential = (1.0 - probability) / probability;
const annualized = potential * (365 * 24 / Math.max(hoursLeft, 1));
// Include if annualized > 25% (very aggressive)
if (annualized > 0.25) {
const opportunity = {
scanId: scanId,
market: market.question || market.title || 'Unknown Market',
outcome: token.outcome || 'Yes',
probability: (probability * 100).toFixed(1),
price: probability.toFixed(4),
potentialReturn: (potential * 100).toFixed(2),
annualizedReturn: (annualized * 100).toFixed(0),
hoursLeft: hoursLeft,
daysLeft: Math.round(hoursLeft / 24 * 10) / 10,
volume24h: volume24h,
tokenId: token.token_id,
hasLiveData: hasLiveData,
marketId: market.id || 'unknown',
urgency: hoursLeft < 48 ? 'HIGH' : hoursLeft < 168 ? 'MEDIUM' : 'LOW'
};
results.opportunities.push(opportunity);
// Flag high-value opportunities
if (annualized > 1.0 && volume24h > 1000) { // >100% APY + good volume
results.highValueOpportunities.push(opportunity);
}
}
}
} catch (tokenError) {
// Skip individual token errors silently
}
}
}
}
// Rate limiting - very brief delay
if (processedMarkets % 20 === 0) {
await new Promise(resolve => setTimeout(resolve, 200));
}
} catch (marketError) {
// Skip individual market errors silently
}
}
// Sort opportunities by annualized return
results.opportunities.sort((a, b) => parseFloat(b.annualizedReturn) - parseFloat(a.annualizedReturn));
results.highValueOpportunities.sort((a, b) => parseFloat(b.annualizedReturn) - parseFloat(a.annualizedReturn));
console.log(`\\n🎯 SCAN RESULTS:`);
console.log(`📊 Markets analyzed: ${results.totalMarkets}`);
console.log(`💰 Total opportunities: ${results.opportunities.length}`);
console.log(`🚀 High-value opportunities: ${results.highValueOpportunities.length}`);
if (results.highValueOpportunities.length > 0) {
console.log('\\n🚨 🚨 HIGH-VALUE OPPORTUNITIES DETECTED! 🚨 🚨');
console.log('=' .repeat(70));
results.highValueOpportunities.slice(0, 5).forEach((opp, i) => {
const urgencyIcon = opp.urgency === 'HIGH' ? '🔥' : opp.urgency === 'MEDIUM' ? '⚡' : '💡';
const liveIcon = opp.hasLiveData ? '📊' : '📈';
console.log(`${i + 1}. ${urgencyIcon} ${liveIcon} ${opp.market.slice(0, 50)}...`);
console.log(` 💰 ${opp.outcome}: $${opp.price} (${opp.probability}% probability)`);
console.log(` 📈 Return: ${opp.potentialReturn}% in ${opp.daysLeft} days`);
console.log(` 🚀 Annualized: ${opp.annualizedReturn}% APY`);
console.log(` 📊 Volume: $${opp.volume24h.toLocaleString()} | Urgency: ${opp.urgency}`);
console.log(` 🔗 Token: ${opp.tokenId}`);
console.log('');
});
console.log('🔥 = <48h to resolve | ⚡ = <7d to resolve | 💡 = >7d to resolve');
console.log('📊 = Live data | 📈 = Estimated data');
}
if (results.opportunities.length > results.highValueOpportunities.length) {
console.log(`\\n💡 Additional ${results.opportunities.length - results.highValueOpportunities.length} lower-tier opportunities available`);
}
if (results.opportunities.length === 0) {
console.log('\\n😐 No profitable opportunities found in this scan');
console.log('💡 This is normal - opportunities are cyclical');
}
// Save results to file for tracking
const logFile = `arbitrage_scans/scan_${scanId}.json`;
try {
if (!fs.existsSync('arbitrage_scans')) {
fs.mkdirSync('arbitrage_scans');
}
fs.writeFileSync(logFile, JSON.stringify(results, null, 2));
console.log(`\\n💾 Results saved to: ${logFile}`);
} catch (e) {
console.log(`⚠️ Could not save results: ${e.message}`);
}
console.log('\\n⏰ Next intensive scan in 5 minutes');
return results;
} catch (error) {
console.error('❌ Intensive scan error:', error.message);
results.error = error.message;
return results;
}
}
// Execute scan
if (require.main === module) {
intensiveArbitrageScan()
.then(results => {
const summary = `Scan complete: ${results.opportunities?.length || 0} total, ${results.highValueOpportunities?.length || 0} high-value`;
console.log(`\\n✅ ${summary}`);
if (results.highValueOpportunities?.length > 0) {
console.log('🚨 ALERT: High-value opportunities detected!');
console.log('💰 Ready to execute trades immediately!');
}
})
.catch(console.error);
}