@pear-protocol/hyperliquid-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,536 @@
1
+ import React, { ReactNode } from 'react';
2
+
3
+ /**
4
+ * Raw value data for trade history positions
5
+ */
6
+ interface RawValueDto {
7
+ coin: string;
8
+ size: string;
9
+ leverage: number;
10
+ openPx: string;
11
+ closePx: string;
12
+ marginUsed: string;
13
+ positionValue: string;
14
+ }
15
+ /**
16
+ * Trade history data structure from V1 API
17
+ */
18
+ interface TradeHistoryV1Dto {
19
+ pearId: string;
20
+ openedDate: string;
21
+ closedDate: string;
22
+ time: string;
23
+ pair: string;
24
+ direction: Record<string, string>;
25
+ closedPrice: number;
26
+ openedPrice: number;
27
+ size: string;
28
+ longPositionValue: number;
29
+ shortPositionValue: number;
30
+ positionValue: number;
31
+ fee: number;
32
+ builderFee: number;
33
+ closedPNL: number;
34
+ closedPnlPercentage: number;
35
+ leverage: number;
36
+ rawLongValue: RawValueDto;
37
+ rawShortValue: RawValueDto;
38
+ }
39
+ /**
40
+ * Request payload for syncing trade history
41
+ */
42
+ interface SyncTradeHistoryDto {
43
+ address: string;
44
+ data: TradeHistoryV1Dto[];
45
+ }
46
+ /**
47
+ * Response from sync trade history endpoint
48
+ */
49
+ interface SyncTradeHistoryResponseDto {
50
+ success: boolean;
51
+ positionId: string;
52
+ tradeHistoryId: string;
53
+ message: string;
54
+ }
55
+ /**
56
+ * Base API error response
57
+ */
58
+ interface ApiErrorResponse {
59
+ statusCode: number;
60
+ message: string;
61
+ error?: string;
62
+ }
63
+ /**
64
+ * Configuration options for the SDK
65
+ */
66
+ interface PearHyperliquidConfig {
67
+ baseUrl: string;
68
+ timeout?: number;
69
+ headers?: Record<string, string>;
70
+ }
71
+ /**
72
+ * HTTP client response wrapper
73
+ */
74
+ interface ApiResponse<T> {
75
+ data: T;
76
+ status: number;
77
+ headers: Record<string, string>;
78
+ }
79
+ /**
80
+ * Hook state interface for migration operations
81
+ */
82
+ interface MigrationHookState<T> {
83
+ loading: boolean;
84
+ error: ApiErrorResponse | null;
85
+ data: T | null;
86
+ }
87
+ /**
88
+ * PnL data structure for open positions
89
+ */
90
+ interface PnlDto {
91
+ value: number;
92
+ percentage: number;
93
+ }
94
+ /**
95
+ * Take profit and stop loss data structure
96
+ */
97
+ interface TpSlDto {
98
+ takeProfit?: number;
99
+ stopLoss?: number;
100
+ }
101
+ /**
102
+ * Position side data structure for open positions
103
+ */
104
+ interface PositionSideDto {
105
+ coin: string;
106
+ size: string;
107
+ leverage: number;
108
+ entryPx: string;
109
+ marginUsed: string;
110
+ liquidationPx: string | undefined;
111
+ unrealizedPnl: string;
112
+ roe: string;
113
+ positionValue: string;
114
+ funding: string;
115
+ }
116
+ /**
117
+ * Open position data structure from V1 API
118
+ */
119
+ interface OpenPositionV1Dto {
120
+ pair: string;
121
+ leverage: number;
122
+ size: number;
123
+ longPositionValue?: number;
124
+ shortPositionValue?: number;
125
+ positionValue: number;
126
+ entryPrice: number;
127
+ markPrice: number;
128
+ pnl: PnlDto;
129
+ liqPrice: string | number;
130
+ margin: number;
131
+ funding: number;
132
+ pearId: string;
133
+ openedDate: string;
134
+ tpsl: TpSlDto;
135
+ long: PositionSideDto;
136
+ short: PositionSideDto;
137
+ }
138
+ /**
139
+ * Request payload for syncing open positions
140
+ */
141
+ interface SyncOpenPositionDto {
142
+ address: string;
143
+ data: OpenPositionV1Dto[];
144
+ }
145
+ /**
146
+ * Response from sync open positions endpoint
147
+ */
148
+ interface SyncOpenPositionResponseDto {
149
+ success: boolean;
150
+ positionIds: string[];
151
+ count: number;
152
+ message: string;
153
+ }
154
+ /**
155
+ * Open order data structure from V1 API
156
+ */
157
+ interface OpenOrderV1Dto {
158
+ id: string;
159
+ time: string;
160
+ pair: string;
161
+ type: string;
162
+ orderValue: number;
163
+ price: number;
164
+ triggerConditions: string;
165
+ status: string;
166
+ leverage?: string;
167
+ }
168
+ /**
169
+ * Request payload for syncing open orders
170
+ */
171
+ interface SyncOpenOrderDto {
172
+ address: string;
173
+ data: OpenOrderV1Dto[];
174
+ }
175
+ /**
176
+ * Response from sync open orders endpoint
177
+ */
178
+ interface SyncOpenOrderResponseDto {
179
+ success: boolean;
180
+ orderIds: string[];
181
+ count: number;
182
+ message: string;
183
+ }
184
+ /**
185
+ * Migration hooks interface
186
+ */
187
+ interface MigrationHooks {
188
+ tradeHistory: {
189
+ sync: (payload: SyncTradeHistoryDto) => Promise<ApiResponse<SyncTradeHistoryResponseDto>>;
190
+ loading: boolean;
191
+ error: ApiErrorResponse | null;
192
+ data: SyncTradeHistoryResponseDto | null;
193
+ reset: () => void;
194
+ };
195
+ openPositions: {
196
+ sync: (payload: SyncOpenPositionDto) => Promise<ApiResponse<SyncOpenPositionResponseDto>>;
197
+ loading: boolean;
198
+ error: ApiErrorResponse | null;
199
+ data: SyncOpenPositionResponseDto | null;
200
+ reset: () => void;
201
+ };
202
+ openOrders: {
203
+ sync: (payload: SyncOpenOrderDto) => Promise<ApiResponse<SyncOpenOrderResponseDto>>;
204
+ loading: boolean;
205
+ error: ApiErrorResponse | null;
206
+ data: SyncOpenOrderResponseDto | null;
207
+ reset: () => void;
208
+ };
209
+ }
210
+ /**
211
+ * WebSocket connection states
212
+ */
213
+ type WebSocketConnectionState = 'connecting' | 'connected' | 'disconnected' | 'error';
214
+ /**
215
+ * WebSocket channels
216
+ */
217
+ type WebSocketChannel = 'trade-histories' | 'open-positions' | 'open-orders' | 'account-summary';
218
+ /**
219
+ * WebSocket subscription message
220
+ */
221
+ interface WebSocketSubscribeMessage {
222
+ action?: 'subscribe' | 'unsubscribe';
223
+ address: string;
224
+ }
225
+ /**
226
+ * WebSocket response message
227
+ */
228
+ interface WebSocketResponse {
229
+ success?: boolean;
230
+ message?: string;
231
+ error?: string;
232
+ }
233
+ /**
234
+ * WebSocket data message
235
+ */
236
+ interface WebSocketDataMessage<T = unknown> {
237
+ channel: WebSocketChannel;
238
+ data: T;
239
+ }
240
+ /**
241
+ * Trade history asset data
242
+ */
243
+ interface TradeHistoryAssetDataDto {
244
+ coin: string;
245
+ entryPrice: number;
246
+ limitPrice: number;
247
+ size: number;
248
+ externalFeePaid: number;
249
+ builderFeePaid: number;
250
+ realizedPnl: number;
251
+ }
252
+ /**
253
+ * Trade history data structure
254
+ */
255
+ interface TradeHistoryDataDto {
256
+ tradeHistoryId: string;
257
+ positionId: string;
258
+ address: string;
259
+ externalFeePaid: number;
260
+ builderFeePaid: number;
261
+ realizedPnl: number;
262
+ totalValue: number;
263
+ entryRatio: number;
264
+ exitRatio: number;
265
+ leverage: number;
266
+ longAssets: TradeHistoryAssetDataDto[];
267
+ shortAssets: TradeHistoryAssetDataDto[];
268
+ createdAt: string;
269
+ }
270
+ /**
271
+ * Paginated trade history response
272
+ */
273
+ interface PaginatedTradeHistoryResponseDto {
274
+ data: TradeHistoryDataDto[];
275
+ hasMore: boolean;
276
+ nextCursor?: string;
277
+ count: number;
278
+ }
279
+ /**
280
+ * Cumulative funding information
281
+ */
282
+ interface CumFundingDto {
283
+ allTime: number;
284
+ sinceChange: number;
285
+ sinceOpen: number;
286
+ }
287
+ /**
288
+ * Position asset detail
289
+ */
290
+ interface PositionAssetDetailDto {
291
+ coin: string;
292
+ entryPrice: number;
293
+ platformSize: number;
294
+ actualSize: number;
295
+ cumFunding: CumFundingDto;
296
+ marginUsed: number;
297
+ positionValue: number;
298
+ unrealizedPnl: number;
299
+ liquidationPrice: number;
300
+ isExternallyModified: boolean;
301
+ }
302
+ /**
303
+ * Position sync status
304
+ */
305
+ type PositionSyncStatus = 'SYNCED' | 'EXTERNALLY_MODIFIED' | 'EXTERNALLY_CLOSED' | 'PAIR_BROKEN';
306
+ /**
307
+ * Open position data structure
308
+ */
309
+ interface OpenPositionDto {
310
+ positionId: string;
311
+ address: string;
312
+ leverage: number;
313
+ stopLoss: number | null;
314
+ takeProfit: number | null;
315
+ entryRatio: number;
316
+ markRatio: number;
317
+ netFunding: number;
318
+ positionValue: number;
319
+ marginUsed: number;
320
+ unrealizedPnl: number;
321
+ longAssets: PositionAssetDetailDto[];
322
+ shortAssets: PositionAssetDetailDto[];
323
+ syncStatus: PositionSyncStatus;
324
+ lastSyncAt: string;
325
+ createdAt: string;
326
+ updatedAt: string;
327
+ }
328
+ /**
329
+ * Order asset data
330
+ */
331
+ interface OrderAssetDto {
332
+ asset: string;
333
+ weight: number;
334
+ }
335
+ /**
336
+ * Order status
337
+ */
338
+ type OrderStatus = 'OPEN' | 'PARTIALLY_FILLED' | 'PROCESSING';
339
+ /**
340
+ * Open limit order data structure
341
+ */
342
+ interface OpenLimitOrderDto {
343
+ orderId: string;
344
+ address: string;
345
+ leverage: number;
346
+ usdValue: number;
347
+ limitRatio: number;
348
+ stopLoss: number | null;
349
+ takeProfit: number | null;
350
+ status: OrderStatus;
351
+ longAssets: OrderAssetDto[];
352
+ shortAssets: OrderAssetDto[];
353
+ createdAt: string;
354
+ updatedAt: string;
355
+ }
356
+ /**
357
+ * Cross margin summary
358
+ */
359
+ interface CrossMarginSummaryDto {
360
+ accountValue: string;
361
+ totalMarginUsed: string;
362
+ totalNtlPos: string;
363
+ totalRawUsd: string;
364
+ }
365
+ /**
366
+ * Margin summary
367
+ */
368
+ interface MarginSummaryDto {
369
+ accountValue: string;
370
+ totalMarginUsed: string;
371
+ totalNtlPos: string;
372
+ totalRawUsd: string;
373
+ }
374
+ /**
375
+ * Balance summary
376
+ */
377
+ interface BalanceSummaryDto {
378
+ crossMaintenanceMarginUsed: string;
379
+ crossMarginSummary: CrossMarginSummaryDto;
380
+ marginSummary: MarginSummaryDto;
381
+ time: number;
382
+ withdrawable: string;
383
+ }
384
+ /**
385
+ * Agent wallet information
386
+ */
387
+ interface AgentWalletDto {
388
+ address: string;
389
+ status: string;
390
+ }
391
+ /**
392
+ * Account balance response (renamed to AccountSummaryResponseDto for consistency)
393
+ */
394
+ interface AccountSummaryResponseDto {
395
+ balanceSummary: BalanceSummaryDto;
396
+ agentWallet: AgentWalletDto;
397
+ }
398
+
399
+ /**
400
+ * Main SDK client for Pear Protocol Hyperliquid API integration
401
+ */
402
+ declare class PearHyperliquidClient {
403
+ private httpClient;
404
+ private baseUrl;
405
+ constructor(config: PearHyperliquidConfig);
406
+ /**
407
+ * Get the configured base URL
408
+ */
409
+ getBaseUrl(): string;
410
+ /**
411
+ * Update request headers
412
+ */
413
+ setHeaders(headers: Record<string, string>): void;
414
+ /**
415
+ * Set authorization header
416
+ */
417
+ setAuthToken(token: string): void;
418
+ /**
419
+ * Make a generic HTTP request
420
+ */
421
+ private makeRequest;
422
+ /**
423
+ * Sync trade history data from old database structure to new database format
424
+ * @param payload - Trade history data with user address
425
+ * @returns Promise with sync result
426
+ */
427
+ syncTradeHistory(payload: SyncTradeHistoryDto): Promise<ApiResponse<SyncTradeHistoryResponseDto>>;
428
+ /**
429
+ * Sync open positions data from old database structure to new database format
430
+ * @param payload - Open positions data with user address
431
+ * @returns Promise with sync result
432
+ */
433
+ syncOpenPositions(payload: SyncOpenPositionDto): Promise<ApiResponse<SyncOpenPositionResponseDto>>;
434
+ /**
435
+ * Sync open orders data from old database structure to new database format
436
+ * @param payload - Open orders data with user address
437
+ * @returns Promise with sync result
438
+ */
439
+ syncOpenOrders(payload: SyncOpenOrderDto): Promise<ApiResponse<SyncOpenOrderResponseDto>>;
440
+ }
441
+
442
+ /**
443
+ * Main Migration SDK Class - Simple request/response pattern
444
+ */
445
+ declare class PearMigrationSDK {
446
+ private client;
447
+ private isSyncRunning;
448
+ constructor(client: PearHyperliquidClient);
449
+ /**
450
+ * Sync trade history data - can only run one at a time
451
+ * If called while already running, returns immediately without making request
452
+ */
453
+ syncTradeHistory(payload: SyncTradeHistoryDto): Promise<ApiResponse<SyncTradeHistoryResponseDto> | null>;
454
+ /**
455
+ * Sync open positions data - can only run one at a time
456
+ * If called while already running, returns immediately without making request
457
+ */
458
+ syncOpenPositions(payload: SyncOpenPositionDto): Promise<ApiResponse<SyncOpenPositionResponseDto> | null>;
459
+ /**
460
+ * Sync open orders data - can only run one at a time
461
+ * If called while already running, returns immediately without making request
462
+ */
463
+ syncOpenOrders(payload: SyncOpenOrderDto): Promise<ApiResponse<SyncOpenOrderResponseDto> | null>;
464
+ /**
465
+ * Check if sync is currently running
466
+ */
467
+ isSyncInProgress(): boolean;
468
+ /**
469
+ * Get the underlying client instance
470
+ */
471
+ getClient(): PearHyperliquidClient;
472
+ /**
473
+ * Set authorization token on the client
474
+ */
475
+ setAuthToken(token: string): void;
476
+ /**
477
+ * Set custom headers on the client
478
+ */
479
+ setHeaders(headers: Record<string, string>): void;
480
+ /**
481
+ * Get base URL from client
482
+ */
483
+ getBaseUrl(): string;
484
+ }
485
+
486
+ interface PearHyperliquidProviderProps {
487
+ config: PearHyperliquidConfig;
488
+ /**
489
+ * WebSocket server URL
490
+ * @default 'wss://hl-v2.pearprotocol.io/ws'
491
+ */
492
+ wsUrl?: string;
493
+ children: ReactNode;
494
+ }
495
+ /**
496
+ * React Provider for PearHyperliquidClient
497
+ */
498
+ declare const PearHyperliquidProvider: React.FC<PearHyperliquidProviderProps>;
499
+ /**
500
+ * Hook to use PearHyperliquidClient from context
501
+ */
502
+ declare const usePearHyperliquidClient: () => PearHyperliquidClient;
503
+ /**
504
+ * Hook to use migration SDK from context
505
+ */
506
+ declare const useMigrationSDK: () => PearMigrationSDK;
507
+
508
+ /**
509
+ * Hook to manage address (login/logout functionality)
510
+ */
511
+ declare const useAddress: () => {
512
+ address: string | null;
513
+ setAddress: (address: string | null) => void;
514
+ clearAddress: () => void;
515
+ isLoggedIn: boolean;
516
+ };
517
+
518
+ /**
519
+ * Hook to access trade histories
520
+ */
521
+ declare const useTradeHistories: () => PaginatedTradeHistoryResponseDto | null;
522
+ /**
523
+ * Hook to access open positions
524
+ */
525
+ declare const useOpenPositions: () => OpenPositionDto[] | null;
526
+ /**
527
+ * Hook to access open orders
528
+ */
529
+ declare const useOpenOrders: () => OpenLimitOrderDto[] | null;
530
+ /**
531
+ * Hook to access account summary
532
+ */
533
+ declare const useAccountSummary: () => AccountSummaryResponseDto | null;
534
+
535
+ export { PearHyperliquidClient, PearHyperliquidProvider, PearMigrationSDK, PearHyperliquidClient as default, useAccountSummary, useAddress, useMigrationSDK, useOpenOrders, useOpenPositions, usePearHyperliquidClient, useTradeHistories };
536
+ export type { AccountSummaryResponseDto, AgentWalletDto, ApiErrorResponse, ApiResponse, BalanceSummaryDto, CrossMarginSummaryDto, CumFundingDto, MarginSummaryDto, MigrationHookState, MigrationHooks, OpenLimitOrderDto, OpenOrderV1Dto, OpenPositionDto, OpenPositionV1Dto, OrderAssetDto, OrderStatus, PaginatedTradeHistoryResponseDto, PearHyperliquidConfig, PnlDto, PositionAssetDetailDto, PositionSideDto, PositionSyncStatus, RawValueDto, SyncOpenOrderDto, SyncOpenOrderResponseDto, SyncOpenPositionDto, SyncOpenPositionResponseDto, SyncTradeHistoryDto, SyncTradeHistoryResponseDto, TpSlDto, TradeHistoryAssetDataDto, TradeHistoryDataDto, TradeHistoryV1Dto, WebSocketChannel, WebSocketConnectionState, WebSocketDataMessage, WebSocketResponse, WebSocketSubscribeMessage };