MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / getCryptoFiatPrice

Method getCryptoFiatPrice

rate/rate.ts:418–459  ·  view source on GitHub ↗
(crypto: string, fiat: string)

Source from the content-addressed store, hash-verified

416
417 for (const bridge of bridges) {
418 try {
419 console.log(`[RatePlugin] 尝试通过 ${bridge} 桥接: ${crypto} -> ${fiat}`);
420
421 // 获取加密货币对稳定币价格
422 const cryptoPrice = await this.fetchBinancePrice(`${crypto}${bridge}`);
423 console.log(`[RatePlugin] ${crypto}${bridge} 价格: ${cryptoPrice}`);
424
425 // 如果目标就是稳定币
426 if (fiat.toUpperCase() === bridge) {
427 return { price: cryptoPrice, lastUpdated: new Date() };
428 }
429
430 // 获取稳定币对法币汇率 (1 USDT = 1 USD)
431 let bridgeForFiat = bridge.toLowerCase();
432 if (bridge === 'USDT' || bridge === 'BUSD' || bridge === 'USDC') {
433 bridgeForFiat = 'usd';
434 }
435
436 console.log(`[RatePlugin] 获取 ${bridgeForFiat} 到 ${fiat} 的汇率`);
437 const fiatRates = await this.fetchFiatRates(bridgeForFiat);
438 const fiatRate = fiatRates[fiat.toLowerCase()];
439
440 if (fiatRate) {
441 const finalPrice = cryptoPrice * fiatRate;
442 console.log(`[RatePlugin] 最终价格: ${crypto} = ${finalPrice} ${fiat}`);
443 return { price: finalPrice, lastUpdated: new Date() };
444 } else {
445 lastError = `无法获取 ${bridgeForFiat} 到 ${fiat} 的汇率`;
446 }
447 } catch (error: any) {
448 lastError = `${bridge} 桥接失败: ${error.message}`;
449 console.warn(`[RatePlugin] ${lastError}`);
450 }
451 }
452
453 throw new Error(`无法获取 ${crypto} 对 ${fiat} 的价格。最后错误: ${lastError}`);
454 }
455
456 // 动态判断是否为法币(优先使用网络列表,失败则回退本地列表)
457 private async isFiat(query: string): Promise<boolean> {
458 const now = Date.now();
459 // 强制刷新法币列表以确保 TRY 被正确识别
460 if (!this.vsFiats || now - this.vsFiatsTs > 6 * 60 * 60 * 1000 || query.toLowerCase() === 'try') {
461 // 1) CoinGecko vs_currencies
462 try {

Callers 1

getUniversalPriceMethod · 0.95

Calls 3

fetchBinancePriceMethod · 0.95
fetchFiatRatesMethod · 0.95
warnMethod · 0.80

Tested by

no test coverage detected