asterConfig.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { AsterWsConfig, AsterAuthConfig } from '../adapters/exchanges/aster/types'
  2. export interface AsterConfig {
  3. // WebSocket 配置
  4. ws: {
  5. url: string
  6. pingInterval: number
  7. pongTimeout: number
  8. reconnectInterval: number
  9. maxReconnectAttempts: number
  10. }
  11. // HTTP API 配置
  12. http: {
  13. baseUrl: string
  14. }
  15. // 鉴权配置
  16. auth: {
  17. user: string
  18. signer: string
  19. privateKey: string
  20. }
  21. // 订阅配置
  22. subscribe: {
  23. symbols: string[]
  24. }
  25. // 日志配置
  26. log: {
  27. level: string
  28. }
  29. }
  30. /**
  31. * 从环境变量读取 Aster DEX 配置
  32. */
  33. export function loadAsterConfig(): AsterConfig {
  34. const required = ['ASTER_WS_URL', 'ASTER_ORDER_USER', 'ASTER_ORDER_SIGNER', 'PRIVATE_KEY']
  35. for (const key of required) {
  36. if (!process.env[key]) {
  37. throw new Error(`缺少必需的环境变量: ${key}`)
  38. }
  39. }
  40. return {
  41. ws: {
  42. url: process.env.ASTER_WS_URL!,
  43. pingInterval: parseInt(process.env.ASTER_WS_PING_INTERVAL || '30000'),
  44. pongTimeout: parseInt(process.env.ASTER_WS_PONG_TIMEOUT || '10000'),
  45. reconnectInterval: parseInt(process.env.ASTER_WS_RECONNECT_INTERVAL || '5000'),
  46. maxReconnectAttempts: parseInt(process.env.ASTER_WS_MAX_RECONNECT_ATTEMPTS || '10'),
  47. },
  48. http: {
  49. baseUrl: process.env.ASTER_HTTP_BASE || 'https://fapi.asterdex.com',
  50. },
  51. auth: {
  52. user: process.env.ASTER_ORDER_USER!,
  53. signer: process.env.ASTER_ORDER_SIGNER!,
  54. privateKey: process.env.PRIVATE_KEY!,
  55. },
  56. subscribe: {
  57. symbols: (process.env.ASTER_SUBSCRIBE_SYMBOLS || 'BTCUSDT,ETHUSDT').split(',').map(s => s.trim()),
  58. },
  59. log: {
  60. level: process.env.LOG_LEVEL || 'info',
  61. },
  62. }
  63. }
  64. /**
  65. * 转换为 AsterWsConfig
  66. */
  67. export function toAsterWsConfig(config: AsterConfig): AsterWsConfig {
  68. return {
  69. wsUrl: config.ws.url,
  70. pingIntervalMs: config.ws.pingInterval,
  71. pongTimeoutMs: config.ws.pongTimeout,
  72. autoReconnect: true,
  73. reconnectIntervalMs: config.ws.reconnectInterval,
  74. }
  75. }
  76. /**
  77. * 转换为 AsterAuthConfig
  78. */
  79. export function toAsterAuthConfig(config: AsterConfig): AsterAuthConfig {
  80. return {
  81. type: 'signer',
  82. user: config.auth.user,
  83. signer: config.auth.signer,
  84. privateKey: config.auth.privateKey,
  85. loginMethod: 'login',
  86. }
  87. }