debug_pacifica_api.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Pacifica API 详细调试脚本
  3. */
  4. import 'dotenv/config'
  5. async function testPacificaAPI() {
  6. console.log('🔍 调试 Pacifica API 连接...')
  7. const baseUrl = process.env.PACIFICA_BASE_URL || 'https://api.pacifica.fi'
  8. const testEndpoint = '/api/v1/info'
  9. const fullUrl = `${baseUrl}${testEndpoint}`
  10. console.log(`📡 测试 URL: ${fullUrl}`)
  11. // 测试1: 使用 fetch 直接调用,不设任何头
  12. console.log('\n🧪 测试1: 直接 fetch 调用,无请求头')
  13. try {
  14. const response = await fetch(fullUrl, {
  15. method: 'GET',
  16. })
  17. console.log(`📊 状态码: ${response.status}`)
  18. console.log(`📋 响应头:`)
  19. response.headers.forEach((value, key) => {
  20. console.log(` ${key}: ${value}`)
  21. })
  22. if (response.ok) {
  23. const data = await response.json()
  24. console.log(`✅ 成功! 获取到 ${Array.isArray(data) ? data.length : '未知数量'} 项数据`)
  25. } else {
  26. const text = await response.text()
  27. console.log(`❌ 失败: ${text.slice(0, 200)}`)
  28. }
  29. } catch (error) {
  30. console.error('❌ 测试1失败:', error)
  31. }
  32. // 测试2: 添加标准 User-Agent
  33. console.log('\n🧪 测试2: 添加 User-Agent')
  34. try {
  35. const response = await fetch(fullUrl, {
  36. method: 'GET',
  37. headers: {
  38. 'User-Agent': 'Pacifica-API-Client/1.0',
  39. },
  40. })
  41. console.log(`📊 状态码: ${response.status}`)
  42. if (response.ok) {
  43. const data = await response.json()
  44. console.log(`✅ 成功! 获取到 ${Array.isArray(data) ? data.length : '未知数量'} 项数据`)
  45. } else {
  46. const text = await response.text()
  47. console.log(`❌ 失败: ${text.slice(0, 200)}`)
  48. }
  49. } catch (error) {
  50. console.error('❌ 测试2失败:', error)
  51. }
  52. // 测试3: 添加更多标准头
  53. console.log('\n🧪 测试3: 添加标准 HTTP 头')
  54. try {
  55. const response = await fetch(fullUrl, {
  56. method: 'GET',
  57. headers: {
  58. 'User-Agent': 'Pacifica-API-Client/1.0',
  59. Accept: 'application/json',
  60. 'Content-Type': 'application/json',
  61. },
  62. })
  63. console.log(`📊 状态码: ${response.status}`)
  64. if (response.ok) {
  65. const data = await response.json()
  66. console.log(`✅ 成功! 获取到 ${Array.isArray(data) ? data.length : '未知数量'} 项数据`)
  67. // 显示前3个项目
  68. if (Array.isArray(data) && data.length > 0) {
  69. console.log('📋 前3个市场:')
  70. data.slice(0, 3).forEach((item: any) => {
  71. console.log(` ${item.symbol}: tick_size=${item.tick_size}, lot_size=${item.lot_size}`)
  72. })
  73. }
  74. } else {
  75. const text = await response.text()
  76. console.log(`❌ 失败: ${text.slice(0, 200)}`)
  77. }
  78. } catch (error) {
  79. console.error('❌ 测试3失败:', error)
  80. }
  81. // 测试4: 测试其他可能的端点
  82. console.log('\n🧪 测试4: 尝试其他端点')
  83. const alternativeEndpoints = ['/api/v1/markets', '/api/v1/market-info', '/info', '/markets']
  84. for (const endpoint of alternativeEndpoints) {
  85. const testUrl = `${baseUrl}${endpoint}`
  86. console.log(`\n🔍 测试端点: ${testUrl}`)
  87. try {
  88. const response = await fetch(testUrl, {
  89. method: 'GET',
  90. headers: {
  91. 'User-Agent': 'Pacifica-API-Client/1.0',
  92. Accept: 'application/json',
  93. },
  94. })
  95. console.log(`📊 状态码: ${response.status}`)
  96. if (response.ok) {
  97. const data = await response.json()
  98. console.log(`✅ 端点 ${endpoint} 可用! 数据类型: ${Array.isArray(data) ? 'array' : typeof data}`)
  99. if (Array.isArray(data)) {
  100. console.log(` 数组长度: ${data.length}`)
  101. }
  102. }
  103. } catch (error) {
  104. console.log(`❌ 端点 ${endpoint} 不可用`)
  105. }
  106. }
  107. // 测试5: 使用我们现有的 PacificaClient
  108. console.log('\n🧪 测试5: 使用现有的 PacificaClient')
  109. try {
  110. const { PacificaClient } = await import('./src/exchanges/pacifica/PacificaClient')
  111. const client = new PacificaClient({
  112. baseUrl: process.env.PACIFICA_BASE_URL || 'https://api.pacifica.fi',
  113. wsUrl: process.env.PACIFICA_WS_URL || 'wss://ws.pacifica.fi/ws',
  114. account: process.env.PACIFICA_ACCOUNT,
  115. privateKey: process.env.PACIFICA_ACCOUNT_PRIVATE_KEY,
  116. agentWallet: process.env.PACIFICA_AGENT_WALLET,
  117. agentPrivateKey: process.env.PACIFICA_AGENT_PRIVATE_KEY,
  118. })
  119. console.log('🔧 PacificaClient 创建成功')
  120. const data = await client.getPublic<any>('/api/v1/info')
  121. console.log(`✅ PacificaClient 调用成功! 获取到 ${Array.isArray(data) ? data.length : '未知数量'} 项数据`)
  122. if (Array.isArray(data) && data.length > 0) {
  123. console.log('📋 前3个市场信息:')
  124. data.slice(0, 3).forEach((market: any) => {
  125. console.log(` ${market.symbol}: tick_size=${market.tick_size}, lot_size=${market.lot_size}`)
  126. })
  127. } else {
  128. console.log('📄 返回的数据结构:', JSON.stringify(data, null, 2))
  129. }
  130. } catch (error) {
  131. console.error('❌ 测试5失败:', error)
  132. }
  133. }
  134. testPacificaAPI().catch(console.error)