import 'dotenv/config' import { PacificaClient } from '../src/exchanges/pacifica/PacificaClient' import { PacificaAdapter } from '../src/exchanges/pacifica/PacificaAdapter' async function main() { const symbol = process.env.PACIFICA_TEST_SYMBOL || 'BTC' const client = new PacificaClient({ baseUrl: process.env.PACIFICA_BASE_URL || 'https://api.pacifica.fi', wsUrl: process.env.PACIFICA_WS_URL || 'wss://ws.pacifica.fi/ws', apiKey: process.env.PACIFICA_API_KEY, privateKey: process.env.PACIFICA_ACCOUNT_PRIVATE_KEY || process.env.PACIFICA_PRIVATE_KEY, account: process.env.PACIFICA_ACCOUNT, agentWallet: process.env.PACIFICA_AGENT_WALLET, agentPrivateKey: process.env.PACIFICA_AGENT_PRIVATE_KEY, }) const ex = new PacificaAdapter(client) // Events ex.ws().on('depth', (d: any) => { // eslint-disable-next-line no-console console.log('[ws.depth]', symbol, 'bidsTop=', d?.bids?.[0], 'asksTop=', d?.asks?.[0]) }) ex.ws().on('orders', (msg: any) => { // eslint-disable-next-line no-console console.log('[ws.orders]', JSON.stringify(msg).slice(0, 300)) }) ex.ws().on('balance', (msg: any) => { // eslint-disable-next-line no-console console.log('[ws.balance]', JSON.stringify(msg).slice(0, 300)) }) ex.ws().on('trades', (msg: any) => { // eslint-disable-next-line no-console console.log('[ws.trades]', JSON.stringify(msg).slice(0, 300)) }) ex.ws().on('ws_error', (e: any) => { // eslint-disable-next-line no-console console.error('[ws.error]', e?.message || e) }) ex.ws().on('ws_close', (info: any) => { // eslint-disable-next-line no-console console.log('[ws.close]', info) }) ex.ws().on('account_info', (msg: any) => { // eslint-disable-next-line no-console console.log('[ws.account_info]', JSON.stringify(msg).slice(0, 300)) }) ex.ws().on('account_positions', (msg: any) => { // eslint-disable-next-line no-console console.log('[ws.account_positions]', JSON.stringify(msg).slice(0, 300)) }) // Subscribe ex.subscribeDepth(symbol) try { ex.subscribeOrders() } catch {} try { ex.subscribeBalance() } catch {} try { ex.subscribeTrades(symbol) } catch {} try { ;(ex as any).subscribeAccountInfo() } catch {} try { ;(ex as any).subscribeAccountPositions() } catch {} // 价格总线(所有符号) try { // 直接通过内部通用订阅工具 ;(ex as any).subscribeChannel?.('prices', {}, (msg: any) => { if (msg?.channel === 'prices') { // eslint-disable-next-line no-console console.log('[ws.prices]', Array.isArray(msg?.data) ? msg.data.slice(0, 1) : msg?.data) } }) } catch {} // Exit after a short window const secs = Number(process.env.PACIFICA_WS_DURATION_SEC || '20') // eslint-disable-next-line no-console console.log(`[pacifica.ws] running ~${secs}s ...`) await new Promise(r => setTimeout(r, secs * 1000)) } main().catch(e => { // eslint-disable-next-line no-console console.error('ws example error', e?.message || e) process.exit(1) })