|
| 1 | +// 测试 OKX 链代币价格变化数据获取 |
| 2 | +const GeckoTerminalAPI = { |
| 3 | + BASE_URL: 'https://api.geckoterminal.com/api/v2' |
| 4 | +}; |
| 5 | + |
| 6 | +const SUPPORTED_NETWORKS = { |
| 7 | + okx: { |
| 8 | + id: 'x-layer', |
| 9 | + name: 'X Layer (OKX)', |
| 10 | + coingecko_id: 'x-layer', |
| 11 | + address_pattern: /^0x[a-fA-F0-9]{40}$/, |
| 12 | + native_token: 'okb' |
| 13 | + } |
| 14 | +}; |
| 15 | + |
| 16 | +async function testPriceChanges() { |
| 17 | + console.log('🧪 测试 OKX 链代币价格变化数据获取'); |
| 18 | + console.log('=' .repeat(60)); |
| 19 | + |
| 20 | + // 测试 XDOG 代币 |
| 21 | + const tokenAddress = '0x0cc24c51bf89c00c5affbfcf5e856c25ecbdb48e'; |
| 22 | + const network = 'okx'; |
| 23 | + |
| 24 | + try { |
| 25 | + const networkConfig = SUPPORTED_NETWORKS[network]; |
| 26 | + if (!networkConfig) { |
| 27 | + console.error(`不支持的网络: ${network}`); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + console.log(`\n1️⃣ 测试代币: ${tokenAddress}`); |
| 32 | + console.log(` 网络: ${networkConfig.name} (${networkConfig.id})`); |
| 33 | + |
| 34 | + // 使用 GeckoTerminal API 获取代币信息(包含交易池数据) |
| 35 | + const url = `${GeckoTerminalAPI.BASE_URL}/networks/${networkConfig.id}/tokens/${tokenAddress}?include=top_pools`; |
| 36 | + console.log(`\n2️⃣ API 请求: ${url}`); |
| 37 | + |
| 38 | + const response = await fetch(url, { |
| 39 | + headers: { |
| 40 | + 'Accept': 'application/json', |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + if (!response.ok) { |
| 45 | + console.log(`❌ API响应失败: ${response.status}`); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const data = await response.json(); |
| 50 | + console.log('\n3️⃣ 基本代币信息:'); |
| 51 | + |
| 52 | + const tokenData = data.data; |
| 53 | + const attributes = tokenData.attributes; |
| 54 | + |
| 55 | + console.log(` 名称: ${attributes.name}`); |
| 56 | + console.log(` 符号: ${attributes.symbol}`); |
| 57 | + console.log(` 当前价格: $${attributes.price_usd}`); |
| 58 | + console.log(` 市值: $${attributes.market_cap_usd}`); |
| 59 | + |
| 60 | + // 从交易池数据中获取价格变化信息 |
| 61 | + console.log('\n4️⃣ 价格变化数据:'); |
| 62 | + |
| 63 | + let priceChange24h = 0; |
| 64 | + let priceChange7d = 0; |
| 65 | + |
| 66 | + if (data.included && data.included.length > 0) { |
| 67 | + console.log(` 找到 ${data.included.length} 个交易池`); |
| 68 | + |
| 69 | + // 获取主要交易池的价格变化数据 |
| 70 | + const mainPool = data.included[0]; |
| 71 | + console.log(` 主要交易池 ID: ${mainPool.id}`); |
| 72 | + |
| 73 | + if (mainPool && mainPool.attributes && mainPool.attributes.price_change_percentage) { |
| 74 | + const priceChanges = mainPool.attributes.price_change_percentage; |
| 75 | + |
| 76 | + console.log(' 所有可用的价格变化数据:'); |
| 77 | + Object.entries(priceChanges).forEach(([period, change]) => { |
| 78 | + console.log(` ${period}: ${change}%`); |
| 79 | + }); |
| 80 | + |
| 81 | + priceChange24h = parseFloat(priceChanges.h24) || 0; |
| 82 | + |
| 83 | + // 使用6小时数据估算7天变化 |
| 84 | + if (priceChanges.h6) { |
| 85 | + priceChange7d = (parseFloat(priceChanges.h6) || 0) * 4; |
| 86 | + } |
| 87 | + |
| 88 | + console.log(`\n ✅ 处理后的数据:`); |
| 89 | + console.log(` 24小时变化: ${priceChange24h}%`); |
| 90 | + console.log(` 7天变化(估算): ${priceChange7d}%`); |
| 91 | + |
| 92 | + } else { |
| 93 | + console.log(' ❌ 交易池数据中没有价格变化信息'); |
| 94 | + } |
| 95 | + } else { |
| 96 | + console.log(' ❌ 没有找到交易池数据'); |
| 97 | + } |
| 98 | + |
| 99 | + // 构建最终的代币数据对象 |
| 100 | + console.log('\n5️⃣ 最终代币数据对象:'); |
| 101 | + const cryptoData = { |
| 102 | + id: `gt-${network}-${tokenAddress}`, |
| 103 | + symbol: attributes.symbol?.toUpperCase() || 'UNKNOWN', |
| 104 | + name: attributes.name || attributes.symbol || 'Unknown Token', |
| 105 | + image: attributes.image_url || '', |
| 106 | + current_price: parseFloat(attributes.price_usd) || 0, |
| 107 | + price_change_percentage_24h: priceChange24h, |
| 108 | + price_change_percentage_7d: priceChange7d, |
| 109 | + market_cap: parseFloat(attributes.market_cap_usd) || 0, |
| 110 | + market_cap_rank: 0, |
| 111 | + total_volume: parseFloat(attributes.volume_usd?.h24) || 0, |
| 112 | + high_24h: 0, |
| 113 | + low_24h: 0, |
| 114 | + circulating_supply: parseFloat(attributes.normalized_total_supply) || 0, |
| 115 | + total_supply: parseFloat(attributes.normalized_total_supply) || parseFloat(attributes.total_supply) || 0, |
| 116 | + last_updated: new Date().toISOString(), |
| 117 | + }; |
| 118 | + |
| 119 | + console.log(' 代币数据:', JSON.stringify(cryptoData, null, 2)); |
| 120 | + |
| 121 | + } catch (error) { |
| 122 | + console.error('❌ 测试失败:', error); |
| 123 | + } |
| 124 | + |
| 125 | + console.log('\n' + '='.repeat(60)); |
| 126 | + console.log('🏁 测试完成'); |
| 127 | +} |
| 128 | + |
| 129 | +// 运行测试 |
| 130 | +testPriceChanges().catch(console.error); |
0 commit comments