pacifica_ws_example.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import 'dotenv/config'
  2. import { PacificaClient } from '../src/exchanges/pacifica/PacificaClient'
  3. import { PacificaAdapter } from '../src/exchanges/pacifica/PacificaAdapter'
  4. async function main() {
  5. const symbol = process.env.PACIFICA_TEST_SYMBOL || 'BTC'
  6. const client = new PacificaClient({
  7. baseUrl: process.env.PACIFICA_BASE_URL || 'https://api.pacifica.fi',
  8. wsUrl: process.env.PACIFICA_WS_URL || 'wss://ws.pacifica.fi/ws',
  9. apiKey: process.env.PACIFICA_API_KEY,
  10. privateKey: process.env.PACIFICA_ACCOUNT_PRIVATE_KEY || process.env.PACIFICA_PRIVATE_KEY,
  11. account: process.env.PACIFICA_ACCOUNT,
  12. agentWallet: process.env.PACIFICA_AGENT_WALLET,
  13. agentPrivateKey: process.env.PACIFICA_AGENT_PRIVATE_KEY,
  14. })
  15. const ex = new PacificaAdapter(client)
  16. // Events
  17. ex.ws().on('depth', (d: any) => {
  18. // eslint-disable-next-line no-console
  19. console.log('[ws.depth]', symbol, 'bidsTop=', d?.bids?.[0], 'asksTop=', d?.asks?.[0])
  20. })
  21. ex.ws().on('orders', (msg: any) => {
  22. // eslint-disable-next-line no-console
  23. console.log('[ws.orders]', JSON.stringify(msg).slice(0, 300))
  24. })
  25. ex.ws().on('balance', (msg: any) => {
  26. // eslint-disable-next-line no-console
  27. console.log('[ws.balance]', JSON.stringify(msg).slice(0, 300))
  28. })
  29. ex.ws().on('trades', (msg: any) => {
  30. // eslint-disable-next-line no-console
  31. console.log('[ws.trades]', JSON.stringify(msg).slice(0, 300))
  32. })
  33. ex.ws().on('ws_error', (e: any) => {
  34. // eslint-disable-next-line no-console
  35. console.error('[ws.error]', e?.message || e)
  36. })
  37. ex.ws().on('ws_close', (info: any) => {
  38. // eslint-disable-next-line no-console
  39. console.log('[ws.close]', info)
  40. })
  41. ex.ws().on('account_info', (msg: any) => {
  42. // eslint-disable-next-line no-console
  43. console.log('[ws.account_info]', JSON.stringify(msg).slice(0, 300))
  44. })
  45. ex.ws().on('account_positions', (msg: any) => {
  46. // eslint-disable-next-line no-console
  47. console.log('[ws.account_positions]', JSON.stringify(msg).slice(0, 300))
  48. })
  49. // Subscribe
  50. ex.subscribeDepth(symbol)
  51. try {
  52. ex.subscribeOrders()
  53. } catch {}
  54. try {
  55. ex.subscribeBalance()
  56. } catch {}
  57. try {
  58. ex.subscribeTrades(symbol)
  59. } catch {}
  60. try {
  61. ;(ex as any).subscribeAccountInfo()
  62. } catch {}
  63. try {
  64. ;(ex as any).subscribeAccountPositions()
  65. } catch {}
  66. // 价格总线(所有符号)
  67. try {
  68. // 直接通过内部通用订阅工具
  69. ;(ex as any).subscribeChannel?.('prices', {}, (msg: any) => {
  70. if (msg?.channel === 'prices') {
  71. // eslint-disable-next-line no-console
  72. console.log('[ws.prices]', Array.isArray(msg?.data) ? msg.data.slice(0, 1) : msg?.data)
  73. }
  74. })
  75. } catch {}
  76. // Exit after a short window
  77. const secs = Number(process.env.PACIFICA_WS_DURATION_SEC || '20')
  78. // eslint-disable-next-line no-console
  79. console.log(`[pacifica.ws] running ~${secs}s ...`)
  80. await new Promise(r => setTimeout(r, secs * 1000))
  81. }
  82. main().catch(e => {
  83. // eslint-disable-next-line no-console
  84. console.error('ws example error', e?.message || e)
  85. process.exit(1)
  86. })