#!/usr/bin/env tsx /** * 真实下单测试 * ⚠️ 警告:这将执行真实的交易订单! */ import { Config, SmartAccountDiscovery } from './src/config/simpleEnv.js' import { PacificaProxyClient } from './src/exchanges/pacifica/PacificaProxyClient.js' async function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)) } async function realOrderTest() { console.log('🚨 真实下单测试') console.log('⚠️ 警告:这将执行真实的交易订单!') // 安全检查 if (!process.argv.includes('--confirm')) { console.log('❌ 需要添加 --confirm 参数确认执行真实交易') console.log('命令: tsx real_order_test.ts --confirm') return } console.log('='.repeat(60)) try { const accounts = SmartAccountDiscovery.discoverPacifica() if (accounts.length === 0) { console.log('❌ 未发现Pacifica账户') return } const account = accounts[0] console.log(`✅ 使用账户: ${account.account.substring(0, 8)}...`) console.log(`✅ 代理状态: ${Config.proxy.isAnyConfigured() ? '启用' : '禁用'}`) const client = new PacificaProxyClient({ account: account.account, privateKey: account.privateKey, }) console.log('\n📊 第一步: 测试连接') const connectionTest = await client.testConnection() if (!connectionTest.success) { console.log('❌ 连接测试失败:', connectionTest.error) return } console.log(`✅ 连接成功,延迟: ${connectionTest.latency}ms`) // 等待一段时间避免速率限制 console.log('\n⏱️ 等待5秒避免速率限制...') await sleep(5000) console.log('\n💰 第二步: 执行小额测试订单') console.log('参数:') console.log(' - 符号: BTCUSDT') console.log(' - 数量: 0.0001') console.log(' - 方向: ask (卖出,减仓)') console.log(' - 滑点: 5%') console.log(' - 只减仓: true') const orderParams = { account: account.account, symbol: 'BTCUSDT', amount: '0.0001', // 更小的金额 side: 'ask' as const, // 卖出减仓 reduceOnly: true, // 只减仓,不增加仓位 slippagePercent: '5.0', // 更大的滑点容忍度 } console.log('\n⚡ 发送订单...') const startTime = Date.now() const result = await client.createMarketOrder(orderParams) const duration = Date.now() - startTime console.log(`✅ 下单成功!用时: ${duration}ms`) console.log('\n📋 订单结果:') console.log(JSON.stringify(result, null, 2)) // 如果有多个账户,测试对冲 if (accounts.length > 1) { console.log('\n⏱️ 等待10秒后执行对冲订单...') await sleep(10000) console.log('\n🔄 第三步: 执行对冲订单') const hedgeAccount = accounts[1] console.log(`使用对冲账户: ${hedgeAccount.account.substring(0, 8)}...`) const hedgeClient = new PacificaProxyClient({ account: hedgeAccount.account, privateKey: hedgeAccount.privateKey, }) const hedgeParams = { account: hedgeAccount.account, symbol: 'BTCUSDT', amount: '0.001', side: 'ask' as const, // 卖出对冲 reduceOnly: false, slippagePercent: '3.0', } console.log('⚡ 发送对冲订单...') const hedgeResult = await hedgeClient.createMarketOrder(hedgeParams) console.log('✅ 对冲下单成功!') console.log('\n📋 对冲订单结果:') console.log(JSON.stringify(hedgeResult, null, 2)) } console.log('\n🎉 真实下单测试完成!') } catch (error: any) { console.log('\n❌ 测试失败:', error.message) if (error.message.includes('429')) { console.log('💡 提示: 遇到速率限制,请等待更长时间后重试') } if (error.stack) { console.log('\n🔍 详细错误:') console.log(error.stack) } } } function showHelp() { console.log(` 🔥 真实下单测试工具 ⚠️ 警告:这将执行真实的交易订单并消耗资金! 使用方法: tsx real_order_test.ts --confirm 执行真实交易 tsx real_order_test.ts --help 显示此帮助 功能特性: ✅ 速率限制处理 (自动重试) ✅ 连接测试验证 ✅ 小额测试订单 (0.001) ✅ 多账户对冲支持 ✅ 详细日志输出 ✅ 错误处理和重试 安全措施: 🛡️ 需要明确确认参数 🛡️ 最小交易金额 🛡️ 详细的操作日志 🛡️ 速率限制保护 环境要求: 🔧 配置有效的Pacifica账户 🔧 足够的账户余额 🔧 网络连接稳定 `) } // 运行测试 if (import.meta.url === `file://${process.argv[1]}`) { if (process.argv.includes('--help') || process.argv.includes('-h')) { showHelp() } else { realOrderTest() } }