RestAdapter.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { PacificaClient } from './PacificaClient'
  2. export class PacificaRestAdapter {
  3. constructor(private client: PacificaClient) {}
  4. async getPrices() {
  5. const response = await this.client.getPublic<any>('/api/v1/info/prices')
  6. const data = response?.data ?? response
  7. return Array.isArray(data) ? data : []
  8. }
  9. async getFundingHistory(params: { symbol: string; limit?: number; offset?: number }) {
  10. const { symbol, limit, offset } = params
  11. const qs = [`symbol=${symbol}`]
  12. if (limit !== undefined) qs.push(`limit=${limit}`)
  13. if (offset !== undefined) qs.push(`offset=${offset}`)
  14. const response = await this.client.getPublic<any>(`/api/v1/funding_rate/history?${qs.join('&')}`)
  15. const data = response?.data ?? response
  16. return Array.isArray(data) ? data : []
  17. }
  18. async getMarketInfo(symbol?: string) {
  19. const response = await this.client.getPublic<any>(this.client.endpoints.symbols)
  20. const data = response?.data ?? response
  21. if (!Array.isArray(data)) return data
  22. return symbol ? data.find(item => String(item.symbol) === symbol) : data
  23. }
  24. async getOrderBook(params: { symbol: string; aggLevel?: number }) {
  25. const url = `${this.client.endpoints.depth}?symbol=${params.symbol}&agg_level=${params.aggLevel ?? 1}`
  26. const response = await this.client.getPublic<any>(url)
  27. const payload = response?.data ?? response
  28. return {
  29. symbol: payload?.s,
  30. bids: Array.isArray(payload?.l?.[0])
  31. ? payload.l[0].map((lvl: any) => ({
  32. price: String(lvl?.p ?? ''),
  33. amount: String(lvl?.a ?? ''),
  34. count: lvl?.n ?? 0,
  35. }))
  36. : [],
  37. asks: Array.isArray(payload?.l?.[1])
  38. ? payload.l[1].map((lvl: any) => ({
  39. price: String(lvl?.p ?? ''),
  40. amount: String(lvl?.a ?? ''),
  41. count: lvl?.n ?? 0,
  42. }))
  43. : [],
  44. timestamp: Number(payload?.t ?? Date.now()),
  45. }
  46. }
  47. }