real_order_test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env tsx
  2. /**
  3. * 真实下单测试
  4. * ⚠️ 警告:这将执行真实的交易订单!
  5. */
  6. import { Config, SmartAccountDiscovery } from './src/config/simpleEnv.js'
  7. import { PacificaProxyClient } from './src/exchanges/pacifica/PacificaProxyClient.js'
  8. async function sleep(ms: number) {
  9. return new Promise(resolve => setTimeout(resolve, ms))
  10. }
  11. async function realOrderTest() {
  12. console.log('🚨 真实下单测试')
  13. console.log('⚠️ 警告:这将执行真实的交易订单!')
  14. // 安全检查
  15. if (!process.argv.includes('--confirm')) {
  16. console.log('❌ 需要添加 --confirm 参数确认执行真实交易')
  17. console.log('命令: tsx real_order_test.ts --confirm')
  18. return
  19. }
  20. console.log('='.repeat(60))
  21. try {
  22. const accounts = SmartAccountDiscovery.discoverPacifica()
  23. if (accounts.length === 0) {
  24. console.log('❌ 未发现Pacifica账户')
  25. return
  26. }
  27. const account = accounts[0]
  28. console.log(`✅ 使用账户: ${account.account.substring(0, 8)}...`)
  29. console.log(`✅ 代理状态: ${Config.proxy.isAnyConfigured() ? '启用' : '禁用'}`)
  30. const client = new PacificaProxyClient({
  31. account: account.account,
  32. privateKey: account.privateKey,
  33. })
  34. console.log('\n📊 第一步: 测试连接')
  35. const connectionTest = await client.testConnection()
  36. if (!connectionTest.success) {
  37. console.log('❌ 连接测试失败:', connectionTest.error)
  38. return
  39. }
  40. console.log(`✅ 连接成功,延迟: ${connectionTest.latency}ms`)
  41. // 等待一段时间避免速率限制
  42. console.log('\n⏱️ 等待5秒避免速率限制...')
  43. await sleep(5000)
  44. console.log('\n💰 第二步: 执行小额测试订单')
  45. console.log('参数:')
  46. console.log(' - 符号: BTCUSDT')
  47. console.log(' - 数量: 0.0001')
  48. console.log(' - 方向: ask (卖出,减仓)')
  49. console.log(' - 滑点: 5%')
  50. console.log(' - 只减仓: true')
  51. const orderParams = {
  52. account: account.account,
  53. symbol: 'BTCUSDT',
  54. amount: '0.0001', // 更小的金额
  55. side: 'ask' as const, // 卖出减仓
  56. reduceOnly: true, // 只减仓,不增加仓位
  57. slippagePercent: '5.0', // 更大的滑点容忍度
  58. }
  59. console.log('\n⚡ 发送订单...')
  60. const startTime = Date.now()
  61. const result = await client.createMarketOrder(orderParams)
  62. const duration = Date.now() - startTime
  63. console.log(`✅ 下单成功!用时: ${duration}ms`)
  64. console.log('\n📋 订单结果:')
  65. console.log(JSON.stringify(result, null, 2))
  66. // 如果有多个账户,测试对冲
  67. if (accounts.length > 1) {
  68. console.log('\n⏱️ 等待10秒后执行对冲订单...')
  69. await sleep(10000)
  70. console.log('\n🔄 第三步: 执行对冲订单')
  71. const hedgeAccount = accounts[1]
  72. console.log(`使用对冲账户: ${hedgeAccount.account.substring(0, 8)}...`)
  73. const hedgeClient = new PacificaProxyClient({
  74. account: hedgeAccount.account,
  75. privateKey: hedgeAccount.privateKey,
  76. })
  77. const hedgeParams = {
  78. account: hedgeAccount.account,
  79. symbol: 'BTCUSDT',
  80. amount: '0.001',
  81. side: 'ask' as const, // 卖出对冲
  82. reduceOnly: false,
  83. slippagePercent: '3.0',
  84. }
  85. console.log('⚡ 发送对冲订单...')
  86. const hedgeResult = await hedgeClient.createMarketOrder(hedgeParams)
  87. console.log('✅ 对冲下单成功!')
  88. console.log('\n📋 对冲订单结果:')
  89. console.log(JSON.stringify(hedgeResult, null, 2))
  90. }
  91. console.log('\n🎉 真实下单测试完成!')
  92. } catch (error: any) {
  93. console.log('\n❌ 测试失败:', error.message)
  94. if (error.message.includes('429')) {
  95. console.log('💡 提示: 遇到速率限制,请等待更长时间后重试')
  96. }
  97. if (error.stack) {
  98. console.log('\n🔍 详细错误:')
  99. console.log(error.stack)
  100. }
  101. }
  102. }
  103. function showHelp() {
  104. console.log(`
  105. 🔥 真实下单测试工具
  106. ⚠️ 警告:这将执行真实的交易订单并消耗资金!
  107. 使用方法:
  108. tsx real_order_test.ts --confirm 执行真实交易
  109. tsx real_order_test.ts --help 显示此帮助
  110. 功能特性:
  111. ✅ 速率限制处理 (自动重试)
  112. ✅ 连接测试验证
  113. ✅ 小额测试订单 (0.001)
  114. ✅ 多账户对冲支持
  115. ✅ 详细日志输出
  116. ✅ 错误处理和重试
  117. 安全措施:
  118. 🛡️ 需要明确确认参数
  119. 🛡️ 最小交易金额
  120. 🛡️ 详细的操作日志
  121. 🛡️ 速率限制保护
  122. 环境要求:
  123. 🔧 配置有效的Pacifica账户
  124. 🔧 足够的账户余额
  125. 🔧 网络连接稳定
  126. `)
  127. }
  128. // 运行测试
  129. if (import.meta.url === `file://${process.argv[1]}`) {
  130. if (process.argv.includes('--help') || process.argv.includes('-h')) {
  131. showHelp()
  132. } else {
  133. realOrderTest()
  134. }
  135. }