Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
350 lines (309 loc) · 10.1 KB
/
Copy pathmain.js
File metadata and controls
350 lines (309 loc) · 10.1 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
const BASE_API_URL =
"https://gamma-api.polymarket.com/markets?active=true&closed=false&limit=300&order=volume&ascending=false";
const REFRESH_MS = 5 * 60 * 1000;
const STATE = {
raw: [],
filtered: [],
chart: null,
autoRefreshTimer: null,
loading: false,
};
function proxyCandidates(url) {
const encoded = encodeURIComponent(url);
return [
url,
// corsproxy.io 现要求 ?url=<encoded> 查询参数形式(旧的路径式已弃用)
`https://corsproxy.io/?url=${encoded}`,
// allorigins 稳定兜底,raw 端点直接透传原始响应体
`https://api.allorigins.win/raw?url=${encoded}`,
// isomorphic-git 代理(路径式,保留为最后兜底)
`https://cors.isomorphic-git.org/${url}`,
];
}
function withTimeout(promiseFactory, timeoutMs) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error("请求超时")), timeoutMs);
promiseFactory()
.then((result) => {
clearTimeout(timeout);
resolve(result);
})
.catch((error) => {
clearTimeout(timeout);
reject(error);
});
});
}
async function fetchJson(url) {
const response = await withTimeout(
() =>
fetch(url, {
headers: { Accept: "application/json" },
cache: "no-store",
}),
10000
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.json();
}
async function fetchMarketsRaw() {
let lastError = new Error("未知错误");
const urls = proxyCandidates(BASE_API_URL);
for (const url of urls) {
try {
const payload = await fetchJson(url);
const markets = Array.isArray(payload) ? payload : payload.data || [];
if (!Array.isArray(markets) || markets.length === 0) {
throw new Error("返回数据为空");
}
return markets;
} catch (error) {
lastError = error;
}
}
throw lastError;
}
function safeNumber(value, fallback = 0) {
const parsed = Number.parseFloat(value);
return Number.isFinite(parsed) ? parsed : fallback;
}
function parseOutcomePrices(market) {
let prices = [];
if (Array.isArray(market.outcomePrices)) {
prices = market.outcomePrices.map((value) => safeNumber(value, 0));
} else if (typeof market.outcomePrices === "string" && market.outcomePrices.trim()) {
try {
const parsed = JSON.parse(market.outcomePrices);
if (Array.isArray(parsed)) {
prices = parsed.map((value) => safeNumber(value, 0));
}
} catch (_error) {
prices = [];
}
}
if (prices.length < 2 && Array.isArray(market.outcomes)) {
prices = market.outcomes.map((item) => safeNumber(item.price, 0));
}
const yes = Math.min(1, Math.max(0, safeNumber(prices[0], 0)));
let no = safeNumber(prices[1], 1 - yes);
no = Math.min(1, Math.max(0, no));
return { yes, no };
}
function computeHeatIndex(yes, volume, liquidity) {
const participation = Math.log10(volume + 1) * 30;
const conviction = Math.abs(yes - 0.5) * 100;
const depth = Math.log10(liquidity + 1) * 8;
return participation + conviction + depth;
}
function normalizeMarket(market) {
const question = String(market.question || "").trim();
if (!question) {
return null;
}
const volume = safeNumber(market.volumeNum ?? market.volume, 0);
if (volume <= 0) {
return null;
}
const liquidity = safeNumber(market.liquidityNum ?? market.liquidity, 0);
const { yes, no } = parseOutcomePrices(market);
const heat = computeHeatIndex(yes, volume, liquidity);
return {
id: String(market.id ?? market.conditionId ?? question),
question,
slug: String(market.slug || ""),
endDate: String(market.endDate || market.endDateIso || ""),
yes,
no,
volume,
liquidity,
heat,
};
}
function updateStatus(text, isError = false) {
const node = document.getElementById("data-status");
node.textContent = text;
node.style.color = isError ? "var(--danger)" : "var(--muted)";
}
function formatMoney(value) {
return new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(value);
}
function sortMarkets(markets, sortBy) {
const sorted = [...markets];
if (sortBy === "volume") {
sorted.sort((a, b) => b.volume - a.volume);
} else if (sortBy === "yesHigh") {
sorted.sort((a, b) => b.yes - a.yes);
} else if (sortBy === "yesLow") {
sorted.sort((a, b) => a.yes - b.yes);
} else {
sorted.sort((a, b) => b.heat - a.heat);
}
return sorted;
}
function applyFilters() {
const minVolume = safeNumber(document.getElementById("min-volume").value, 0);
const limit = Number.parseInt(document.getElementById("limit-select").value, 10) || 20;
const sortBy = document.getElementById("sort-select").value;
const filtered = STATE.raw.filter((market) => market.volume >= minVolume);
STATE.filtered = sortMarkets(filtered, sortBy).slice(0, limit);
}
function renderTable() {
const tbody = document.querySelector("#market-table tbody");
tbody.innerHTML = "";
if (STATE.filtered.length === 0) {
const row = document.createElement("tr");
row.innerHTML = `<td colspan="7">暂无符合条件的数据,请降低筛选门槛后重试。</td>`;
tbody.appendChild(row);
return;
}
STATE.filtered.forEach((market, index) => {
const url = market.slug
? `https://polymarket.com/event/${market.slug}`
: "https://polymarket.com/";
const row = document.createElement("tr");
row.innerHTML = `
<td class="mono">${index + 1}</td>
<td><a class="market-link" href="${url}" target="_blank" rel="noopener noreferrer">${market.question}</a></td>
<td class="mono yes">${(market.yes * 100).toFixed(1)}%</td>
<td class="mono no">${(market.no * 100).toFixed(1)}%</td>
<td class="mono">${formatMoney(market.volume)}</td>
<td class="mono">${formatMoney(market.liquidity)}</td>
<td class="mono">${market.heat.toFixed(1)}</td>
`;
tbody.appendChild(row);
});
}
function renderSummary() {
const count = STATE.filtered.length;
const totalVolume = STATE.filtered.reduce((sum, market) => sum + market.volume, 0);
const avgYes = count > 0 ? STATE.filtered.reduce((sum, market) => sum + market.yes, 0) / count : 0;
const bullish = STATE.filtered.filter((market) => market.yes > 0.5).length;
document.getElementById("stat-count").textContent = String(count);
document.getElementById("stat-volume").textContent = formatMoney(totalVolume);
document.getElementById("stat-yes").textContent = `${(avgYes * 100).toFixed(1)}%`;
const riskTone = avgYes >= 0.55 ? "风险偏好较强" : avgYes <= 0.45 ? "风险偏好偏谨慎" : "风险偏好中性";
document.getElementById("summary-text").textContent =
`当前筛选下共有 ${count} 个重点市场,合计成交量 ${formatMoney(totalVolume)},` +
`平均 Yes 概率 ${(avgYes * 100).toFixed(1)}%,其中 ${bullish} 个市场 Yes 概率超过 50%,整体 ${riskTone}。`;
}
function renderChart() {
const canvas = document.getElementById("trendChart");
if (STATE.chart) {
STATE.chart.destroy();
}
const labels = STATE.filtered.map((market) => {
if (market.question.length <= 30) {
return market.question;
}
return `${market.question.slice(0, 30)}...`;
});
const data = STATE.filtered.map((market) => Number.parseFloat(market.heat.toFixed(2)));
STATE.chart = new Chart(canvas, {
type: "bar",
data: {
labels,
datasets: [
{
label: "热度指数",
data,
backgroundColor: "rgba(42, 245, 152, 0.35)",
borderColor: "rgba(36, 212, 216, 0.9)",
borderWidth: 1.2,
},
],
},
options: {
maintainAspectRatio: false,
plugins: {
legend: { display: false },
},
scales: {
x: {
ticks: { color: "#a9c4cf", maxRotation: 0, autoSkip: true },
grid: { color: "rgba(42, 74, 91, 0.35)" },
},
y: {
ticks: { color: "#a9c4cf" },
grid: { color: "rgba(42, 74, 91, 0.35)" },
},
},
},
});
}
function updateLastUpdated() {
const now = new Date();
const formatted = new Intl.DateTimeFormat("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(now);
document.getElementById("last-updated").textContent = `最后更新:${formatted}`;
}
function renderAll() {
applyFilters();
renderSummary();
renderTable();
renderChart();
updateLastUpdated();
}
async function loadData() {
if (STATE.loading) {
return;
}
STATE.loading = true;
updateStatus("正在加载 Polymarket 数据…");
try {
const rawMarkets = await fetchMarketsRaw();
STATE.raw = rawMarkets.map(normalizeMarket).filter(Boolean);
renderAll();
updateStatus(`加载完成:${STATE.raw.length} 条市场记录`);
} catch (error) {
updateStatus(`加载失败:${error.message}`, true);
if (STATE.raw.length > 0) {
renderAll();
}
} finally {
STATE.loading = false;
}
}
function configureAutoRefresh(enabled) {
if (STATE.autoRefreshTimer) {
clearInterval(STATE.autoRefreshTimer);
STATE.autoRefreshTimer = null;
}
if (enabled) {
STATE.autoRefreshTimer = setInterval(loadData, REFRESH_MS);
}
}
function bindEvents() {
document.getElementById("sort-select").addEventListener("change", renderAll);
document.getElementById("limit-select").addEventListener("change", renderAll);
const minVolume = document.getElementById("min-volume");
const minVolumeValue = document.getElementById("min-volume-value");
const updateMinVolumeLabel = () => {
minVolumeValue.textContent = formatMoney(safeNumber(minVolume.value, 0));
};
minVolume.addEventListener("input", () => {
updateMinVolumeLabel();
renderAll();
});
updateMinVolumeLabel();
document.getElementById("refresh-btn").addEventListener("click", loadData);
const autoRefresh = document.getElementById("auto-refresh-toggle");
autoRefresh.addEventListener("change", () => {
configureAutoRefresh(autoRefresh.checked);
});
}
async function init() {
bindEvents();
configureAutoRefresh(document.getElementById("auto-refresh-toggle").checked);
await loadData();
}
window.__POLY_APP_INIT__ = init;
init();