123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- /**
- * Pacifica数据接口客户端
- *
- * 负责处理Pacifica DEX的所有数据查询接口
- * 包括价格、订单簿、K线、交易历史等
- */
- import { UniversalHttpClient } from '@/utils/universalHttpClient'
- import { HttpClientRequest, HttpClientResponse } from '@/types/httpClient'
- import {
- PacificaPriceData,
- PacificaTicker,
- PacificaOrderBook,
- PacificaKline,
- PacificaTrade,
- PacificaAggTrade,
- PacificaSymbol,
- PacificaMarketDataParams,
- PacificaApiResponse,
- PacificaListResponse,
- } from '@/types/pacifica'
- export class PacificaDataClient {
- private httpClient: UniversalHttpClient
- private accountId: string
- constructor(httpClient: UniversalHttpClient, accountId: string) {
- this.httpClient = httpClient
- this.accountId = accountId
- }
- // ======================== 市场数据接口 ========================
- /**
- * 获取交易对信息
- */
- async getExchangeInfo(): Promise<PacificaApiResponse<{ symbols: PacificaSymbol[] }>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: '/api/v3/exchangeInfo',
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{ symbols: PacificaSymbol[] }>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取单个交易对最新价格
- */
- async getPrice(symbol: string): Promise<PacificaApiResponse<PacificaPriceData>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/ticker/price?symbol=${symbol}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaPriceData>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取所有交易对价格
- */
- async getAllPrices(): Promise<PacificaApiResponse<PacificaPriceData[]>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: '/api/v3/ticker/price',
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaPriceData[]>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取24hr价格变动统计
- */
- async getTicker24hr(symbol?: string): Promise<PacificaApiResponse<PacificaTicker | PacificaTicker[]>> {
- const url = symbol ? `/api/v3/ticker/24hr?symbol=${symbol}` : '/api/v3/ticker/24hr'
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaTicker | PacificaTicker[]>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取订单簿深度
- */
- async getOrderBook(symbol: string, limit: number = 100): Promise<PacificaApiResponse<PacificaOrderBook>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/depth?symbol=${symbol}&limit=${limit}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaOrderBook>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取最近成交记录
- */
- async getRecentTrades(symbol: string, limit: number = 500): Promise<PacificaApiResponse<PacificaTrade[]>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/trades?symbol=${symbol}&limit=${limit}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaTrade[]>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取历史成交记录
- */
- async getHistoricalTrades(
- symbol: string,
- params: PacificaMarketDataParams = {},
- ): Promise<PacificaApiResponse<PacificaTrade[]>> {
- const queryParams = new URLSearchParams()
- queryParams.append('symbol', symbol)
- if (params.limit) queryParams.append('limit', params.limit.toString())
- if (params.fromId) queryParams.append('fromId', params.fromId.toString())
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/historicalTrades?${queryParams.toString()}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaTrade[]>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取聚合交易记录
- */
- async getAggTrades(
- symbol: string,
- params: PacificaMarketDataParams = {},
- ): Promise<PacificaApiResponse<PacificaAggTrade[]>> {
- const queryParams = new URLSearchParams()
- queryParams.append('symbol', symbol)
- if (params.limit) queryParams.append('limit', params.limit.toString())
- if (params.fromId) queryParams.append('fromId', params.fromId.toString())
- if (params.startTime) queryParams.append('startTime', params.startTime.toString())
- if (params.endTime) queryParams.append('endTime', params.endTime.toString())
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/aggTrades?${queryParams.toString()}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaAggTrade[]>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取K线数据
- */
- async getKlines(
- symbol: string,
- interval: string,
- params: PacificaMarketDataParams = {},
- ): Promise<PacificaApiResponse<PacificaKline[]>> {
- const queryParams = new URLSearchParams()
- queryParams.append('symbol', symbol)
- queryParams.append('interval', interval)
- if (params.limit) queryParams.append('limit', params.limit.toString())
- if (params.startTime) queryParams.append('startTime', params.startTime.toString())
- if (params.endTime) queryParams.append('endTime', params.endTime.toString())
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/klines?${queryParams.toString()}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<any[]>(request)
- // 转换原始K线数据为结构化对象
- const klines: PacificaKline[] = response.data.map((kline: any[]) => ({
- symbol,
- openTime: kline[0],
- open: kline[1],
- high: kline[2],
- low: kline[3],
- close: kline[4],
- volume: kline[5],
- closeTime: kline[6],
- quoteVolume: kline[7],
- trades: kline[8],
- takerBuyBaseVolume: kline[9],
- takerBuyQuoteVolume: kline[10],
- interval,
- firstTradeId: 0,
- lastTradeId: 0,
- }))
- return this.wrapResponse({
- ...response,
- data: klines,
- })
- }
- /**
- * 获取平均价格
- */
- async getAvgPrice(symbol: string): Promise<PacificaApiResponse<{ mins: number; price: string }>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/avgPrice?symbol=${symbol}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{ mins: number; price: string }>(request)
- return this.wrapResponse(response)
- }
- /**
- * 获取服务器时间
- */
- async getServerTime(): Promise<PacificaApiResponse<{ serverTime: number }>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: '/api/v3/time',
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{ serverTime: number }>(request)
- return this.wrapResponse(response)
- }
- /**
- * 测试连接
- */
- async ping(): Promise<PacificaApiResponse<{}>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: '/api/v3/ping',
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{}>(request)
- return this.wrapResponse(response)
- }
- // ======================== 批量查询接口 ========================
- /**
- * 批量获取多个交易对价格
- */
- async getBatchPrices(symbols: string[]): Promise<PacificaApiResponse<PacificaPriceData[]>> {
- const symbolsParam = JSON.stringify(symbols)
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/ticker/price?symbols=${encodeURIComponent(symbolsParam)}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaPriceData[]>(request)
- return this.wrapResponse(response)
- }
- /**
- * 批量获取多个交易对24hr统计
- */
- async getBatch24hrTickers(symbols: string[]): Promise<PacificaApiResponse<PacificaTicker[]>> {
- const symbolsParam = JSON.stringify(symbols)
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'GET',
- url: `/api/v3/ticker/24hr?symbols=${encodeURIComponent(symbolsParam)}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<PacificaTicker[]>(request)
- return this.wrapResponse(response)
- }
- // ======================== 实时数据流接口 ========================
- /**
- * 获取WebSocket连接的监听密钥
- */
- async getListenKey(): Promise<PacificaApiResponse<{ listenKey: string }>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'POST',
- url: '/api/v3/userDataStream',
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{ listenKey: string }>(request)
- return this.wrapResponse(response)
- }
- /**
- * 延长监听密钥有效期
- */
- async keepAliveListenKey(listenKey: string): Promise<PacificaApiResponse<{}>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'PUT',
- url: `/api/v3/userDataStream?listenKey=${listenKey}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{}>(request)
- return this.wrapResponse(response)
- }
- /**
- * 关闭监听密钥
- */
- async closeListenKey(listenKey: string): Promise<PacificaApiResponse<{}>> {
- const request: HttpClientRequest = {
- platform: 'pacifica',
- accountId: this.accountId,
- method: 'DELETE',
- url: `/api/v3/userDataStream?listenKey=${listenKey}`,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- const response = await this.httpClient.request<{}>(request)
- return this.wrapResponse(response)
- }
- // ======================== 辅助方法 ========================
- /**
- * 包装响应数据为统一格式
- */
- private wrapResponse<T>(response: HttpClientResponse<T>): PacificaApiResponse<T> {
- return {
- data: response.data,
- success: response.status >= 200 && response.status < 300,
- code: response.status,
- message: response.statusText,
- timestamp: Date.now(),
- }
- }
- /**
- * 构建查询参数字符串
- */
- private buildQueryString(params: Record<string, any>): string {
- const queryParams = new URLSearchParams()
- Object.entries(params).forEach(([key, value]) => {
- if (value !== undefined && value !== null) {
- queryParams.append(key, value.toString())
- }
- })
- return queryParams.toString()
- }
- /**
- * 格式化符号参数
- */
- private formatSymbol(symbol: string): string {
- return symbol.toUpperCase()
- }
- /**
- * 验证时间间隔参数
- */
- private validateInterval(interval: string): boolean {
- const validIntervals = ['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '3d', '1w', '1M']
- return validIntervals.includes(interval)
- }
- /**
- * 获取客户端配置
- */
- getClientConfig() {
- return {
- accountId: this.accountId,
- platform: 'pacifica',
- endpoints: {
- exchangeInfo: '/api/v3/exchangeInfo',
- price: '/api/v3/ticker/price',
- ticker24hr: '/api/v3/ticker/24hr',
- orderBook: '/api/v3/depth',
- trades: '/api/v3/trades',
- klines: '/api/v3/klines',
- ping: '/api/v3/ping',
- time: '/api/v3/time',
- },
- }
- }
- }
|