-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-handler.js
More file actions
480 lines (414 loc) · 17.3 KB
/
Copy pathapi-handler.js
File metadata and controls
480 lines (414 loc) · 17.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
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
// API Handler - Abstraction layer for different exchange APIs
class APIHandler {
/**
* Fetch price data from the specified API
* @param {Object} apiConfig - API configuration object
* @param {string} symbol - Trading pair symbol (e.g., BTCUSDT)
* @returns {Promise<Object>} Price data object
*/
static async fetchPrice(apiConfig, symbol) {
if (!apiConfig || !apiConfig.enabled) {
throw new Error('API not configured or not enabled');
}
switch (apiConfig.type) {
case 'binance':
return await this.fetchBinance(apiConfig, symbol);
case 'binanceFutures':
return await this.fetchBinanceFutures(apiConfig, symbol);
case 'coingecko':
return await this.fetchCoinGecko(apiConfig, symbol);
case 'coinmarketcap':
return await this.fetchCoinMarketCap(apiConfig, symbol);
case 'ashare':
return await this.fetchAShare(apiConfig, symbol);
case 'custom':
return await this.fetchCustom(apiConfig, symbol);
default:
throw new Error(`Unknown API type: ${apiConfig.type}`);
}
}
/**
* Fetch price from Binance API
*/
static async fetchBinance(config, symbol) {
const headers = {};
if (config.apiKey) {
headers['X-MBX-APIKEY'] = config.apiKey;
}
// Fetch 24hr ticker
const tickerUrl = `${config.baseUrl}/ticker/24hr?symbol=${symbol}`;
const tickerResponse = await fetch(tickerUrl, { headers });
if (!tickerResponse.ok) {
throw new Error(`Binance API error: ${tickerResponse.statusText}`);
}
const tickerData = await tickerResponse.json();
const currentPrice = parseFloat(tickerData.lastPrice);
const priceChangePercent = parseFloat(tickerData.priceChangePercent);
// Fetch K-line data
let history = [];
try {
const klineUrl = `${config.baseUrl}/klines?symbol=${symbol}&interval=1h&limit=24`;
const klineResponse = await fetch(klineUrl);
if (klineResponse.ok) {
const klineData = await klineResponse.json();
history = klineData.map(k => parseFloat(k[4])); // Close price
}
} catch (e) {
console.error('Error fetching Binance klines:', e);
}
return {
currentPrice,
priceChangePercent,
history
};
}
/**
* Fetch price from Binance Futures API
*/
static async fetchBinanceFutures(config, symbol) {
const headers = {};
if (config.apiKey) {
headers['X-MBX-APIKEY'] = config.apiKey;
}
// Fetch 24hr ticker from Futures API
const tickerUrl = `${config.baseUrl}/ticker/24hr?symbol=${symbol}`;
const tickerResponse = await fetch(tickerUrl, { headers });
if (!tickerResponse.ok) {
throw new Error(`Binance Futures API error: ${tickerResponse.statusText}`);
}
const tickerData = await tickerResponse.json();
const currentPrice = parseFloat(tickerData.lastPrice);
const priceChangePercent = parseFloat(tickerData.priceChangePercent);
// Fetch K-line data from Futures API
let history = [];
try {
const klineUrl = `${config.baseUrl}/klines?symbol=${symbol}&interval=1h&limit=24`;
const klineResponse = await fetch(klineUrl);
if (klineResponse.ok) {
const klineData = await klineResponse.json();
history = klineData.map(k => parseFloat(k[4])); // Close price
}
} catch (e) {
console.error('Error fetching Binance Futures klines:', e);
}
return {
currentPrice,
priceChangePercent,
history
};
}
/**
* Fetch price from CoinGecko API
*/
static async fetchCoinGecko(config, symbol) {
// CoinGecko uses coin IDs, not trading pairs
// Convert BTCUSDT -> bitcoin, ETHUSDT -> ethereum, etc.
const coinMap = {
// Cryptocurrencies
'BTCUSDT': 'bitcoin',
'ETHUSDT': 'ethereum',
'SOLUSDT': 'solana',
'PAXGUSDT': 'pax-gold',
'BNBUSDT': 'binancecoin',
'DOGEUSDT': 'dogecoin',
'XRPUSDT': 'ripple',
'ADAUSDT': 'cardano',
'MATICUSDT': 'matic-network',
'DOTUSDT': 'polkadot',
'AVAXUSDT': 'avalanche-2',
'LINKUSDT': 'chainlink',
'UNIUSDT': 'uniswap',
'LTCUSDT': 'litecoin',
'ATOMUSDT': 'cosmos',
'HYPEUSDT': 'hyperliquid',
'HYPE': 'hyperliquid',
// RWA - xStock Tokenized Assets
'TSLAUSDT': 'tesla-xstock',
'TESLAAPPS': 'tesla-xstock',
'GOOGLUSDT': 'alphabet-xstock',
'GOOGLEUSDT': 'alphabet-xstock',
'NVDAUSDT': 'nvidia-xstock',
'NVIDIAUSDT': 'nvidia-xstock',
'AAPLUSDT': 'apple-xstock',
'APPLEUSDT': 'apple-xstock',
'MSFTUSDT': 'microsoft-xstock',
'MICROSOFTUSDT': 'microsoft-xstock',
'AMZNUSDT': 'amazon-xstock',
'AMAZONUSDT': 'amazon-xstock',
'METAUSDT': 'meta-xstock',
'NFLXUSDT': 'netflix-xstock',
'NETFLIXUSDT': 'netflix-xstock',
'COINUSDT': 'coinbase-xstock',
'COINBASEUSDT': 'coinbase-xstock',
// Fuzzy search aliases (stock tickers without USDT)
'TSLA': 'tesla-xstock',
'GOOGL': 'alphabet-xstock',
'GOOG': 'alphabet-xstock',
'NVDA': 'nvidia-xstock',
'AAPL': 'apple-xstock',
'MSFT': 'microsoft-xstock',
'AMZN': 'amazon-xstock',
'META': 'meta-xstock',
'NFLX': 'netflix-xstock',
'COIN': 'coinbase-xstock'
};
// Try direct mapping first, then with USDT suffix, then lowercase
let coinId = coinMap[symbol] ||
coinMap[symbol + 'USDT'] ||
coinMap[symbol.toUpperCase()];
// If still not found, try removing USDT and lowercase
if (!coinId) {
coinId = symbol.replace('USDT', '').toLowerCase();
}
const headers = {
'Accept': 'application/json'
};
if (config.apiKey) {
headers['x-cg-demo-api-key'] = config.apiKey;
}
// Fetch current price and 24h change
const priceUrl = `${config.baseUrl}/simple/price?ids=${coinId}&vs_currencies=usd&include_24hr_change=true`;
const priceResponse = await fetch(priceUrl, { headers });
if (!priceResponse.ok) {
throw new Error(`CoinGecko API error: ${priceResponse.statusText}`);
}
const priceData = await priceResponse.json();
if (!priceData[coinId]) {
throw new Error(`CoinGecko: Coin ${coinId} not found`);
}
const currentPrice = priceData[coinId].usd;
const priceChangePercent = priceData[coinId].usd_24h_change || 0;
// Fetch market chart for history (last 24 hours)
let history = [];
try {
const chartUrl = `${config.baseUrl}/coins/${coinId}/market_chart?vs_currency=usd&days=1&interval=hourly`;
console.log(`[CoinGecko] Fetching chart for ${coinId}: ${chartUrl}`);
const chartResponse = await fetch(chartUrl, { headers });
console.log(`[CoinGecko] Chart response status: ${chartResponse.status}`);
if (chartResponse.ok) {
const chartData = await chartResponse.json();
console.log(`[CoinGecko] Chart data received:`, chartData);
if (chartData.prices && chartData.prices.length > 0) {
history = chartData.prices.slice(-24).map(p => p[1]); // Get last 24 prices
console.log(`[CoinGecko] Extracted ${history.length} price points`);
} else {
console.warn(`[CoinGecko] No prices in chart data for ${coinId}`);
}
} else {
const errorText = await chartResponse.text();
console.error(`[CoinGecko] Chart fetch failed (${chartResponse.status}):`, errorText);
}
} catch (e) {
console.error(`[CoinGecko] Error fetching chart for ${coinId}:`, e);
}
return {
currentPrice,
priceChangePercent,
history
};
}
/**
* Fetch price from CoinMarketCap API
*/
static async fetchCoinMarketCap(config, symbol) {
// CoinMarketCap requires API key
if (!config.apiKey) {
throw new Error('CoinMarketCap requires an API key');
}
// Expanded symbol mapping for CMC
const symbolMap = {
// Cryptocurrencies
'BTCUSDT': 'BTC',
'ETHUSDT': 'ETH',
'SOLUSDT': 'SOL',
'PAXGUSDT': 'PAXG',
'BNBUSDT': 'BNB',
'DOGEUSDT': 'DOGE',
'XRPUSDT': 'XRP',
'ADAUSDT': 'ADA',
'MATICUSDT': 'MATIC',
'DOTUSDT': 'DOT',
'AVAXUSDT': 'AVAX',
'LINKUSDT': 'LINK',
'UNIUSDT': 'UNI',
'LTCUSDT': 'LTC',
'ATOMUSDT': 'ATOM',
// RWA - Tokenized Stocks
'TSLAUSDT': 'TSLA',
'GOOGLUSDT': 'GOOGL',
'NVDAUSDT': 'NVDA',
'AAPLUSDT': 'AAPL',
'MSFTUSDT': 'MSFT',
'AMZNUSDT': 'AMZN',
'METAUSDT': 'META',
'NFLXUSDT': 'NFLX',
'COINUSDT': 'COIN',
// RWA - ETFs
'QQQUSDT': 'QQQ',
'SPYUSDT': 'SPY',
'VTIUSDT': 'VTI',
'IWIUSDT': 'IWI'
};
const cmcSymbol = symbolMap[symbol] || symbol.replace('USDT', '');
const headers = {
'X-CMC_PRO_API_KEY': config.apiKey,
'Accept': 'application/json'
};
// Fetch latest quote
const quoteUrl = `${config.baseUrl}/cryptocurrency/quotes/latest?symbol=${cmcSymbol}&convert=USD`;
const quoteResponse = await fetch(quoteUrl, { headers });
if (!quoteResponse.ok) {
const errorText = await quoteResponse.text();
throw new Error(`CoinMarketCap API error (${quoteResponse.status}): ${errorText}`);
}
const quoteData = await quoteResponse.json();
// Check for API-level errors
if (quoteData.status && quoteData.status.error_code !== 0) {
throw new Error(`CoinMarketCap: ${quoteData.status.error_message}`);
}
if (!quoteData.data || !quoteData.data[cmcSymbol]) {
throw new Error(`CoinMarketCap: Symbol ${cmcSymbol} not found`);
}
const coinData = quoteData.data[cmcSymbol];
const currentPrice = coinData.quote.USD.price;
const priceChangePercent = coinData.quote.USD.percent_change_24h || 0;
// Note: CMC historical data requires premium plan
// For now, we'll return empty history
let history = [];
return {
currentPrice,
priceChangePercent,
history
};
}
/**
* Fetch price from Custom API
* Expected format: { price: number, change24h: number, history: number[] }
*/
static async fetchCustom(config, symbol) {
const headers = {
'Content-Type': 'application/json'
};
if (config.apiKey) {
headers['Authorization'] = `Bearer ${config.apiKey}`;
}
const url = `${config.baseUrl}?symbol=${symbol}`;
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`Custom API error: ${response.statusText}`);
}
const data = await response.json();
// Flexible parsing - try different common formats
const currentPrice = parseFloat(data.price || data.lastPrice || data.last || 0);
const priceChangePercent = parseFloat(data.change24h || data.priceChangePercent || data.changePercent || 0);
const history = data.history || data.kline || data.candles || [];
if (!currentPrice) {
throw new Error('Custom API: Could not parse price from response');
}
return {
currentPrice,
priceChangePercent,
history: Array.isArray(history) ? history : []
};
}
/**
* Auto-detect A-share market based on stock code
* @param {string} code - Stock code (e.g., 600519, 000001)
* @returns {string} Full code with market prefix (e.g., sh600519, sz000001)
*/
static detectAShareMarket(code) {
// Remove any existing prefix
const cleanCode = code.replace(/^(sh|sz|SH|SZ)/i, '');
// A股市场识别规则:
// 上海(sh): 6开头(主板), 688开头(科创板), 900开头(B股)
// 深圳(sz): 0开头(主板), 3开头(创业板), 200开头(B股)
if (cleanCode.startsWith('6') || cleanCode.startsWith('9')) {
return `sh${cleanCode}`;
} else if (cleanCode.startsWith('0') || cleanCode.startsWith('3') || cleanCode.startsWith('2')) {
return `sz${cleanCode}`;
} else {
// 指数代码: 000开头上证指数, 399开头深证指数
if (cleanCode.startsWith('000')) {
return `sh${cleanCode}`;
} else if (cleanCode.startsWith('399')) {
return `sz${cleanCode}`;
}
// Default to Shanghai
return `sh${cleanCode}`;
}
}
/**
* Fetch price from A-Share (China Stock Market) using Tencent API
*/
static async fetchAShare(config, symbol) {
// Auto-detect market and add prefix
const fullCode = this.detectAShareMarket(symbol);
console.log(`[A-Share] Fetching ${symbol} -> ${fullCode}`);
// Fetch real-time quote from Tencent
const quoteUrl = `https://qt.gtimg.cn/q=${fullCode}`;
const quoteResponse = await fetch(quoteUrl);
if (!quoteResponse.ok) {
throw new Error(`A-Share API error: ${quoteResponse.statusText}`);
}
// Tencent API returns GBK encoded text, need to decode properly
const arrayBuffer = await quoteResponse.arrayBuffer();
const decoder = new TextDecoder('gbk');
const quoteText = decoder.decode(arrayBuffer);
console.log(`[A-Share] Quote response:`, quoteText.substring(0, 200));
// Parse Tencent quote format
// Format: v_sh600519="1~贵州茅台~600519~1849.00~1857.88~1850.00~24936~..."
const match = quoteText.match(/v_[^=]+="([^"]+)"/);
if (!match || !match[1]) {
throw new Error(`A-Share: Invalid response for ${fullCode}`);
}
const parts = match[1].split('~');
if (parts.length < 45) {
throw new Error(`A-Share: Incomplete data for ${fullCode}`);
}
// Parse data fields
// [1] 股票名称, [3] 当前价, [4] 昨收, [5] 今开, [6] 成交量(手)
// [31] 最高, [32] 最低, [33] 涨跌额, [34] 涨跌幅
const stockName = parts[1];
const currentPrice = parseFloat(parts[3]);
const prevClose = parseFloat(parts[4]);
const priceChangePercent = parseFloat(parts[32]) ||
((currentPrice - prevClose) / prevClose * 100);
console.log(`[A-Share] ${stockName} (${fullCode}): ¥${currentPrice}, ${priceChangePercent.toFixed(2)}%`);
// Fetch K-line history data
let history = [];
try {
// Get daily K-line data (last 30 days)
const klineUrl = `https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param=${fullCode},day,,,30,qfq`;
console.log(`[A-Share] Fetching K-line: ${klineUrl}`);
const klineResponse = await fetch(klineUrl);
if (klineResponse.ok) {
const klineData = await klineResponse.json();
console.log(`[A-Share] K-line response:`, JSON.stringify(klineData).substring(0, 300));
// Parse K-line data
// Response format: { code: 0, data: { sh600519: { qfqday: [[date, open, close, high, low, volume], ...] } } }
if (klineData.code === 0 && klineData.data) {
const stockData = klineData.data[fullCode];
if (stockData && stockData.qfqday) {
// Extract close prices for sparkline
history = stockData.qfqday.map(k => parseFloat(k[2])); // k[2] is close price
console.log(`[A-Share] Extracted ${history.length} K-line points`);
} else if (stockData && stockData.day) {
// Fallback to non-adjusted data
history = stockData.day.map(k => parseFloat(k[2]));
console.log(`[A-Share] Extracted ${history.length} K-line points (non-adjusted)`);
}
}
}
} catch (e) {
console.error(`[A-Share] Error fetching K-line for ${fullCode}:`, e);
}
return {
currentPrice,
priceChangePercent,
history,
stockName // Extra field for displaying stock name
};
}
}
// Export for ES6 modules
export default APIHandler;