pacifica_real_test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env tsx
  2. /**
  3. * Pacifica实际连接测试
  4. * 使用代理测试多账户连接和同一平台对冲
  5. */
  6. import { Config, SmartAccountDiscovery } from '../src/config/simpleEnv.js'
  7. import { PacificaProxyClient } from '../src/exchanges/pacifica/PacificaProxyClient.js'
  8. import { SamePlatformHedgingManager } from '../src/core/hedging/SamePlatformHedgingManager.js'
  9. import { logger } from '../src/utils/logger.js'
  10. async function pacificaRealTest() {
  11. console.log('🌊 Pacifica实际连接测试')
  12. console.log('='.repeat(50))
  13. try {
  14. // 1. 检查配置
  15. console.log('\n📋 第一步: 检查配置...')
  16. console.log(`代理状态: ${Config.proxy.isAnyConfigured() ? '✅ 启用' : '❌ 禁用'}`)
  17. if (Config.proxy.isAnyConfigured()) {
  18. const proxyUrl = Config.proxy.getUrl('pacifica')
  19. if (proxyUrl) {
  20. // 脱敏显示代理URL
  21. const masked = proxyUrl.replace(/:\/\/(.*?)@/, '://***:***@')
  22. console.log(`代理URL: ${masked}`)
  23. }
  24. }
  25. // 2. 发现Pacifica账户
  26. console.log('\n🔍 第二步: 发现Pacifica账户...')
  27. const accounts = SmartAccountDiscovery.discoverPacifica()
  28. console.log(`发现 ${accounts.length} 个Pacifica账户`)
  29. if (accounts.length === 0) {
  30. console.log('❌ 未发现Pacifica账户,请检查环境变量配置')
  31. return
  32. }
  33. // 3. 测试第一个账户连接
  34. console.log('\n🌐 第三步: 测试第一个账户连接...')
  35. const account1 = accounts[0]
  36. console.log(`测试账户: ${account1.name}`)
  37. console.log(`账户ID: ${account1.account.substring(0, 8)}...${account1.account.slice(-8)}`)
  38. const client1 = new PacificaProxyClient()
  39. try {
  40. const connectionTest1 = await client1.testConnection()
  41. console.log('✅ 账户1连接测试结果:')
  42. console.log(` 成功: ${connectionTest1.success}`)
  43. console.log(` 延迟: ${connectionTest1.latency}ms`)
  44. console.log(` 代理: ${connectionTest1.proxyUsed ? '启用' : '禁用'}`)
  45. if (connectionTest1.serverTime) {
  46. console.log(` 服务器时间: ${new Date(connectionTest1.serverTime).toISOString()}`)
  47. }
  48. if (connectionTest1.error) {
  49. console.log(` 错误: ${connectionTest1.error}`)
  50. }
  51. } catch (error: any) {
  52. console.log(`❌ 账户1连接失败: ${error.message}`)
  53. }
  54. // 4. 测试第二个账户(如果存在)
  55. if (accounts.length > 1) {
  56. console.log('\n🌐 第四步: 测试第二个账户连接...')
  57. const account2 = accounts[1]
  58. console.log(`测试账户: ${account2.name}`)
  59. console.log(`账户ID: ${account2.account.substring(0, 8)}...${account2.account.slice(-8)}`)
  60. const client2 = new PacificaProxyClient()
  61. try {
  62. const connectionTest2 = await client2.testConnection()
  63. console.log('✅ 账户2连接测试结果:')
  64. console.log(` 成功: ${connectionTest2.success}`)
  65. console.log(` 延迟: ${connectionTest2.latency}ms`)
  66. console.log(` 代理: ${connectionTest2.proxyUsed ? '启用' : '禁用'}`)
  67. if (connectionTest2.serverTime) {
  68. console.log(` 服务器时间: ${new Date(connectionTest2.serverTime).toISOString()}`)
  69. }
  70. if (connectionTest2.error) {
  71. console.log(` 错误: ${connectionTest2.error}`)
  72. }
  73. } catch (error: any) {
  74. console.log(`❌ 账户2连接失败: ${error.message}`)
  75. }
  76. }
  77. // 5. 测试同一平台对冲管理器
  78. if (accounts.length >= 2) {
  79. console.log('\n🔄 第五步: 测试同一平台对冲管理器...')
  80. const hedgeManager = new SamePlatformHedgingManager('pacifica')
  81. // 添加账户到对冲管理器
  82. const account1 = accounts[0]
  83. const account2 = accounts[1]
  84. hedgeManager.addAccount('pacifica-main', {
  85. account: account1.account,
  86. privateKey: account1.privateKey,
  87. })
  88. hedgeManager.addAccount('pacifica-hedge', {
  89. account: account2.account,
  90. privateKey: account2.privateKey,
  91. })
  92. // 创建对冲对
  93. hedgeManager.createHedgePair('btc-usd-hedge', 'pacifica-main', 'pacifica-hedge', 'BTC-USD', 1.0)
  94. // 获取对冲状态
  95. const hedgeStatuses = hedgeManager.getHedgePairStatuses()
  96. console.log('✅ 对冲对创建成功:')
  97. hedgeStatuses.forEach(status => {
  98. console.log(` 对冲对: ${status.pairId}`)
  99. console.log(` 交易对: ${status.symbol}`)
  100. console.log(` 多头账户: ${status.longAccount}`)
  101. console.log(` 空头账户: ${status.shortAccount}`)
  102. console.log(` 状态: ${status.isActive ? '激活' : '停用'}`)
  103. console.log(` 净敞口: ${status.netExposure}`)
  104. })
  105. }
  106. // 6. 测试基础API调用
  107. console.log('\n📡 第六步: 测试基础API调用...')
  108. const testClient = new PacificaProxyClient()
  109. try {
  110. console.log('测试服务器时间API...')
  111. const serverTimeResult = await testClient.getServerTime()
  112. console.log(`✅ 服务器时间: ${new Date(serverTimeResult.time).toISOString()}`)
  113. } catch (error: any) {
  114. console.log(`❌ 服务器时间API失败: ${error.message}`)
  115. }
  116. try {
  117. console.log('测试交易对信息API...')
  118. const symbolsResult = await testClient.getSymbols()
  119. console.log(`✅ 获取到交易对信息`)
  120. if (Array.isArray(symbolsResult)) {
  121. console.log(` 交易对数量: ${symbolsResult.length}`)
  122. } else if (symbolsResult && typeof symbolsResult === 'object') {
  123. const keys = Object.keys(symbolsResult)
  124. console.log(` 数据字段: ${keys.join(', ')}`)
  125. }
  126. } catch (error: any) {
  127. console.log(`❌ 交易对信息API失败: ${error.message}`)
  128. }
  129. try {
  130. console.log('测试最新价格API...')
  131. const pricesResult = await testClient.getPrices()
  132. console.log(`✅ 价格数据获取成功`)
  133. if (typeof pricesResult === 'object' && pricesResult) {
  134. const keys = Object.keys(pricesResult)
  135. console.log(` 价格数据包含 ${keys.length} 个字段`)
  136. if (keys.length > 0) {
  137. console.log(` 示例字段: ${keys.slice(0, 3).join(', ')}`)
  138. }
  139. }
  140. } catch (error: any) {
  141. console.log(`❌ 价格API失败: ${error.message}`)
  142. }
  143. console.log('\n🎉 Pacifica实际连接测试完成!')
  144. console.log('\n📊 测试总结:')
  145. console.log(` ✅ 发现账户: ${accounts.length} 个`)
  146. console.log(` ✅ 代理配置: ${Config.proxy.isAnyConfigured() ? '已启用' : '未启用'}`)
  147. console.log(` ✅ 同平台对冲: ${accounts.length >= 2 ? '支持' : '需要至少2个账户'}`)
  148. } catch (error: any) {
  149. console.error('❌ Pacifica实际连接测试失败:', error)
  150. logger.error('Pacifica实际连接测试失败', { error: error.message, stack: error.stack })
  151. }
  152. }
  153. // 运行测试
  154. if (import.meta.url === `file://${process.argv[1]}`) {
  155. pacificaRealTest()
  156. }