| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- #!/usr/bin/env tsx
- /**
- * Pacifica实际连接测试
- * 使用代理测试多账户连接和同一平台对冲
- */
- import { Config, SmartAccountDiscovery } from '../src/config/simpleEnv.js'
- import { PacificaProxyClient } from '../src/exchanges/pacifica/PacificaProxyClient.js'
- import { SamePlatformHedgingManager } from '../src/core/hedging/SamePlatformHedgingManager.js'
- import { logger } from '../src/utils/logger.js'
- async function pacificaRealTest() {
- console.log('🌊 Pacifica实际连接测试')
- console.log('='.repeat(50))
- try {
- // 1. 检查配置
- console.log('\n📋 第一步: 检查配置...')
- console.log(`代理状态: ${Config.proxy.isAnyConfigured() ? '✅ 启用' : '❌ 禁用'}`)
- if (Config.proxy.isAnyConfigured()) {
- const proxyUrl = Config.proxy.getUrl('pacifica')
- if (proxyUrl) {
- // 脱敏显示代理URL
- const masked = proxyUrl.replace(/:\/\/(.*?)@/, '://***:***@')
- console.log(`代理URL: ${masked}`)
- }
- }
- // 2. 发现Pacifica账户
- console.log('\n🔍 第二步: 发现Pacifica账户...')
- const accounts = SmartAccountDiscovery.discoverPacifica()
- console.log(`发现 ${accounts.length} 个Pacifica账户`)
- if (accounts.length === 0) {
- console.log('❌ 未发现Pacifica账户,请检查环境变量配置')
- return
- }
- // 3. 测试第一个账户连接
- console.log('\n🌐 第三步: 测试第一个账户连接...')
- const account1 = accounts[0]
- console.log(`测试账户: ${account1.name}`)
- console.log(`账户ID: ${account1.account.substring(0, 8)}...${account1.account.slice(-8)}`)
- const client1 = new PacificaProxyClient()
- try {
- const connectionTest1 = await client1.testConnection()
- console.log('✅ 账户1连接测试结果:')
- console.log(` 成功: ${connectionTest1.success}`)
- console.log(` 延迟: ${connectionTest1.latency}ms`)
- console.log(` 代理: ${connectionTest1.proxyUsed ? '启用' : '禁用'}`)
- if (connectionTest1.serverTime) {
- console.log(` 服务器时间: ${new Date(connectionTest1.serverTime).toISOString()}`)
- }
- if (connectionTest1.error) {
- console.log(` 错误: ${connectionTest1.error}`)
- }
- } catch (error: any) {
- console.log(`❌ 账户1连接失败: ${error.message}`)
- }
- // 4. 测试第二个账户(如果存在)
- if (accounts.length > 1) {
- console.log('\n🌐 第四步: 测试第二个账户连接...')
- const account2 = accounts[1]
- console.log(`测试账户: ${account2.name}`)
- console.log(`账户ID: ${account2.account.substring(0, 8)}...${account2.account.slice(-8)}`)
- const client2 = new PacificaProxyClient()
- try {
- const connectionTest2 = await client2.testConnection()
- console.log('✅ 账户2连接测试结果:')
- console.log(` 成功: ${connectionTest2.success}`)
- console.log(` 延迟: ${connectionTest2.latency}ms`)
- console.log(` 代理: ${connectionTest2.proxyUsed ? '启用' : '禁用'}`)
- if (connectionTest2.serverTime) {
- console.log(` 服务器时间: ${new Date(connectionTest2.serverTime).toISOString()}`)
- }
- if (connectionTest2.error) {
- console.log(` 错误: ${connectionTest2.error}`)
- }
- } catch (error: any) {
- console.log(`❌ 账户2连接失败: ${error.message}`)
- }
- }
- // 5. 测试同一平台对冲管理器
- if (accounts.length >= 2) {
- console.log('\n🔄 第五步: 测试同一平台对冲管理器...')
- const hedgeManager = new SamePlatformHedgingManager('pacifica')
- // 添加账户到对冲管理器
- const account1 = accounts[0]
- const account2 = accounts[1]
- hedgeManager.addAccount('pacifica-main', {
- account: account1.account,
- privateKey: account1.privateKey,
- })
- hedgeManager.addAccount('pacifica-hedge', {
- account: account2.account,
- privateKey: account2.privateKey,
- })
- // 创建对冲对
- hedgeManager.createHedgePair('btc-usd-hedge', 'pacifica-main', 'pacifica-hedge', 'BTC-USD', 1.0)
- // 获取对冲状态
- const hedgeStatuses = hedgeManager.getHedgePairStatuses()
- console.log('✅ 对冲对创建成功:')
- hedgeStatuses.forEach(status => {
- console.log(` 对冲对: ${status.pairId}`)
- console.log(` 交易对: ${status.symbol}`)
- console.log(` 多头账户: ${status.longAccount}`)
- console.log(` 空头账户: ${status.shortAccount}`)
- console.log(` 状态: ${status.isActive ? '激活' : '停用'}`)
- console.log(` 净敞口: ${status.netExposure}`)
- })
- }
- // 6. 测试基础API调用
- console.log('\n📡 第六步: 测试基础API调用...')
- const testClient = new PacificaProxyClient()
- try {
- console.log('测试服务器时间API...')
- const serverTimeResult = await testClient.getServerTime()
- console.log(`✅ 服务器时间: ${new Date(serverTimeResult.time).toISOString()}`)
- } catch (error: any) {
- console.log(`❌ 服务器时间API失败: ${error.message}`)
- }
- try {
- console.log('测试交易对信息API...')
- const symbolsResult = await testClient.getSymbols()
- console.log(`✅ 获取到交易对信息`)
- if (Array.isArray(symbolsResult)) {
- console.log(` 交易对数量: ${symbolsResult.length}`)
- } else if (symbolsResult && typeof symbolsResult === 'object') {
- const keys = Object.keys(symbolsResult)
- console.log(` 数据字段: ${keys.join(', ')}`)
- }
- } catch (error: any) {
- console.log(`❌ 交易对信息API失败: ${error.message}`)
- }
- try {
- console.log('测试最新价格API...')
- const pricesResult = await testClient.getPrices()
- console.log(`✅ 价格数据获取成功`)
- if (typeof pricesResult === 'object' && pricesResult) {
- const keys = Object.keys(pricesResult)
- console.log(` 价格数据包含 ${keys.length} 个字段`)
- if (keys.length > 0) {
- console.log(` 示例字段: ${keys.slice(0, 3).join(', ')}`)
- }
- }
- } catch (error: any) {
- console.log(`❌ 价格API失败: ${error.message}`)
- }
- console.log('\n🎉 Pacifica实际连接测试完成!')
- console.log('\n📊 测试总结:')
- console.log(` ✅ 发现账户: ${accounts.length} 个`)
- console.log(` ✅ 代理配置: ${Config.proxy.isAnyConfigured() ? '已启用' : '未启用'}`)
- console.log(` ✅ 同平台对冲: ${accounts.length >= 2 ? '支持' : '需要至少2个账户'}`)
- } catch (error: any) {
- console.error('❌ Pacifica实际连接测试失败:', error)
- logger.error('Pacifica实际连接测试失败', { error: error.message, stack: error.stack })
- }
- }
- // 运行测试
- if (import.meta.url === `file://${process.argv[1]}`) {
- pacificaRealTest()
- }
|