@pear-protocol/hyperliquid-sdk 0.0.22 → 0.0.26

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.
@@ -1,15 +1,11 @@
1
1
  import React, { ReactNode } from 'react';
2
2
  import { ReadyState } from 'react-use-websocket';
3
- import { PearHyperliquidClient } from './client';
4
- import { PearHyperliquidConfig } from './types';
5
- import { PearMigrationSDK } from './migration-sdk';
6
- export interface TokenSelectorConfig {
7
- isLong: boolean;
8
- index: number;
9
- }
3
+ import type { UserProfile } from './types';
4
+ import { AuthStatus } from './types';
5
+ import type { AgentWalletState } from './types';
10
6
  export interface PearHyperliquidContextType {
11
- client: PearHyperliquidClient;
12
- migrationSDK: PearMigrationSDK;
7
+ apiBaseUrl: string;
8
+ wsUrl: string;
13
9
  address: string | null;
14
10
  setAddress: (address: string | null) => void;
15
11
  connectionStatus: ReadyState;
@@ -18,27 +14,65 @@ export interface PearHyperliquidContextType {
18
14
  nativeConnectionStatus: ReadyState;
19
15
  nativeIsConnected: boolean;
20
16
  nativeLastError: string | null;
17
+ authStatus: AuthStatus;
18
+ isAuthenticated: boolean;
19
+ accessToken: string | null;
20
+ user: UserProfile | null;
21
+ authError: string | null;
22
+ getEip712: (address: string) => Promise<any>;
23
+ loginWithSignedMessage: (address: string, signature: string, timestamp: number) => Promise<void>;
24
+ loginWithPrivyToken: (address: string, appId: string, accessToken: string) => Promise<void>;
25
+ refreshTokens: () => Promise<any>;
26
+ logout: () => Promise<void>;
27
+ agentWallet: AgentWalletState;
28
+ isAgentWalletReady: boolean;
29
+ agentWalletError: string | null;
30
+ agentWalletLoading: boolean;
31
+ refreshAgentWalletStatus: () => Promise<any>;
32
+ createAgentWallet: () => Promise<any>;
33
+ notifyAgentWalletApproved: () => Promise<any>;
21
34
  }
22
35
  export declare const PearHyperliquidContext: React.Context<PearHyperliquidContextType | undefined>;
23
36
  interface PearHyperliquidProviderProps {
24
- config: PearHyperliquidConfig;
25
- /**
26
- * WebSocket server URL
27
- * @default 'wss://hl-v2.pearprotocol.io/ws'
28
- */
29
- wsUrl?: string;
30
37
  children: ReactNode;
38
+ apiBaseUrl?: string;
39
+ clientId?: string;
40
+ wsUrl?: string;
31
41
  }
32
42
  /**
33
43
  * React Provider for PearHyperliquidClient
34
44
  */
35
45
  export declare const PearHyperliquidProvider: React.FC<PearHyperliquidProviderProps>;
36
46
  /**
37
- * Hook to use PearHyperliquidClient from context
47
+ * Hook to access the entire Pear Hyperliquid context.
48
+ * Prefer using the more specific hooks below when possible.
49
+ */
50
+ export declare function usePearHyperliquid(): PearHyperliquidContextType;
51
+ /**
52
+ * Provider-aware Auth hook. Uses auth state/actions provided by PearHyperliquidProvider.
53
+ * Callers do not need to pass baseUrl/clientId.
38
54
  */
39
- export declare const usePearHyperliquidClient: () => PearHyperliquidClient;
55
+ export declare function usePearAuth(): {
56
+ readonly status: AuthStatus;
57
+ readonly isAuthenticated: boolean;
58
+ readonly user: UserProfile | null;
59
+ readonly error: string | null;
60
+ readonly getEip712: (address: string) => Promise<any>;
61
+ readonly loginWithSignedMessage: (address: string, signature: string, timestamp: number) => Promise<void>;
62
+ readonly loginWithPrivyToken: (address: string, appId: string, accessToken: string) => Promise<void>;
63
+ readonly refreshTokens: () => Promise<any>;
64
+ readonly logout: () => Promise<void>;
65
+ };
40
66
  /**
41
- * Hook to use migration SDK from context
67
+ * Provider-aware Agent Wallet hook. Uses agent wallet state/actions provided by PearHyperliquidProvider.
42
68
  */
43
- export declare const useMigrationSDK: () => PearMigrationSDK;
69
+ export declare function usePearAgentWallet(): {
70
+ readonly agentWallet: AgentWalletState;
71
+ readonly isReady: boolean;
72
+ readonly loading: boolean;
73
+ readonly error: string | null;
74
+ readonly refreshAgentWalletStatus: () => Promise<any>;
75
+ readonly createAgentWallet: () => Promise<any>;
76
+ readonly notifyAgentWalletApproved: () => Promise<any>;
77
+ };
44
78
  export {};
@@ -1,5 +1,4 @@
1
- import type { CandleInterval, TokenConflict, TokenSelection } from "../types";
2
- import type { TokenSelectorConfig } from "../provider";
1
+ import type { CandleInterval, TokenConflict, TokenSelection, TokenSelectorConfig } from "../types";
3
2
  interface UserSelectionState {
4
3
  longTokens: TokenSelection[];
5
4
  shortTokens: TokenSelection[];
package/dist/types.d.ts CHANGED
@@ -1,71 +1,8 @@
1
- /**
2
- * Raw value data for trade history positions
3
- */
4
- export interface RawValueDto {
5
- coin: string;
6
- size: string;
7
- leverage: number;
8
- openPx: string;
9
- closePx: string;
10
- marginUsed: string;
11
- positionValue: string;
12
- }
13
- /**
14
- * Trade history data structure from V1 API
15
- */
16
- export interface TradeHistoryV1Dto {
17
- pearId: string;
18
- openedDate: string;
19
- closedDate: string;
20
- time: string;
21
- pair: string;
22
- direction: Record<string, string>;
23
- closedPrice: number;
24
- openedPrice: number;
25
- size: string;
26
- longPositionValue: number;
27
- shortPositionValue: number;
28
- positionValue: number;
29
- fee: number;
30
- builderFee: number;
31
- closedPNL: number;
32
- closedPnlPercentage: number;
33
- leverage: number;
34
- rawLongValue: RawValueDto;
35
- rawShortValue: RawValueDto;
36
- }
37
- /**
38
- * Request payload for syncing trade history
39
- */
40
- export interface SyncTradeHistoryDto {
41
- address: string;
42
- data: TradeHistoryV1Dto[];
43
- }
44
- /**
45
- * Response from sync trade history endpoint
46
- */
47
- export interface SyncTradeHistoryResponseDto {
48
- success: boolean;
49
- positionId: string;
50
- tradeHistoryId: string;
51
- message: string;
52
- }
53
- /**
54
- * Base API error response
55
- */
56
1
  export interface ApiErrorResponse {
57
2
  statusCode: number;
58
3
  message: string;
59
4
  error?: string;
60
5
  }
61
- /**
62
- * Configuration options for the SDK
63
- */
64
- export interface PearHyperliquidConfig {
65
- baseUrl: string;
66
- timeout?: number;
67
- headers?: Record<string, string>;
68
- }
69
6
  /**
70
7
  * HTTP client response wrapper
71
8
  */
@@ -74,136 +11,38 @@ export interface ApiResponse<T> {
74
11
  status: number;
75
12
  headers: Record<string, string>;
76
13
  }
77
- /**
78
- * Hook state interface for migration operations
79
- */
80
- export interface MigrationHookState<T> {
81
- loading: boolean;
82
- error: ApiErrorResponse | null;
83
- data: T | null;
84
- }
85
- /**
86
- * PnL data structure for open positions
87
- */
88
- export interface PnlDto {
89
- value: number;
90
- percentage: number;
91
- }
92
- /**
93
- * Take profit and stop loss data structure
94
- */
95
- export interface TpSlDto {
96
- takeProfit?: number;
97
- stopLoss?: number;
14
+ export interface ExternalLiquidationDto {
15
+ liquidatedUser: string;
16
+ markPx: string;
17
+ method: string;
98
18
  }
99
- /**
100
- * Position side data structure for open positions
101
- */
102
- export interface PositionSideDto {
19
+ export interface ExternalFillDto {
103
20
  coin: string;
104
- size: string;
105
- leverage: number;
106
- entryPx: string;
107
- marginUsed: string;
108
- liquidationPx: string | undefined;
109
- unrealizedPnl: string;
110
- roe: string;
111
- positionValue: string;
112
- funding: string;
113
- }
114
- /**
115
- * Open position data structure from V1 API
116
- */
117
- export interface OpenPositionV1Dto {
118
- pair: string;
119
- leverage: number;
120
- size: number;
121
- longPositionValue?: number;
122
- shortPositionValue?: number;
123
- positionValue: number;
124
- entryPrice: number;
125
- markPrice: number;
126
- pnl: PnlDto;
127
- liqPrice: string | number;
128
- margin: number;
129
- funding: number;
130
- pearId: string;
131
- openedDate: string;
132
- tpsl: TpSlDto;
133
- long: PositionSideDto;
134
- short: PositionSideDto;
135
- }
136
- /**
137
- * Request payload for syncing open positions
138
- */
139
- export interface SyncOpenPositionDto {
140
- address: string;
141
- data: OpenPositionV1Dto[];
142
- }
143
- /**
144
- * Response from sync open positions endpoint
145
- */
146
- export interface SyncOpenPositionResponseDto {
147
- success: boolean;
148
- positionIds: string[];
149
- count: number;
150
- message: string;
151
- }
152
- /**
153
- * Open order data structure from V1 API
154
- */
155
- export interface OpenOrderV1Dto {
156
- id: string;
157
- time: string;
158
- pair: string;
159
- type: string;
160
- orderValue: number;
161
- price: number;
162
- triggerConditions: string;
163
- status: string;
164
- leverage?: string;
21
+ px: string;
22
+ sz: string;
23
+ side: 'B' | 'A';
24
+ time: number;
25
+ dir: string;
26
+ fee: string;
27
+ builderFee?: string;
28
+ oid?: string | number;
29
+ tid?: string | number;
30
+ cloid?: string | null;
31
+ hash?: string | null;
32
+ feeToken?: string | null;
33
+ liquidation?: ExternalLiquidationDto | null;
34
+ }
35
+ export interface SyncFillsRequestDto {
36
+ user: string;
37
+ fills: ExternalFillDto[];
165
38
  }
166
- /**
167
- * Request payload for syncing open orders
168
- */
169
- export interface SyncOpenOrderDto {
170
- address: string;
171
- data: OpenOrderV1Dto[];
172
- }
173
- /**
174
- * Response from sync open orders endpoint
175
- */
176
- export interface SyncOpenOrderResponseDto {
177
- success: boolean;
178
- orderIds: string[];
179
- count: number;
180
- message: string;
181
- }
182
- /**
183
- * Migration hooks interface
184
- */
185
- export interface MigrationHooks {
186
- tradeHistory: {
187
- sync: (payload: SyncTradeHistoryDto) => Promise<ApiResponse<SyncTradeHistoryResponseDto>>;
188
- loading: boolean;
189
- error: ApiErrorResponse | null;
190
- data: SyncTradeHistoryResponseDto | null;
191
- reset: () => void;
192
- };
193
- openPositions: {
194
- sync: (payload: SyncOpenPositionDto) => Promise<ApiResponse<SyncOpenPositionResponseDto>>;
195
- loading: boolean;
196
- error: ApiErrorResponse | null;
197
- data: SyncOpenPositionResponseDto | null;
198
- reset: () => void;
199
- };
200
- openOrders: {
201
- sync: (payload: SyncOpenOrderDto) => Promise<ApiResponse<SyncOpenOrderResponseDto>>;
202
- loading: boolean;
203
- error: ApiErrorResponse | null;
204
- data: SyncOpenOrderResponseDto | null;
205
- reset: () => void;
206
- };
39
+ export interface SyncFillsResponseDto {
40
+ insertedFills: number;
41
+ skippedDuplicates: number;
42
+ createdOrders: number;
43
+ updatedPositions: number;
44
+ createdPositions: number;
45
+ closedPositions: number;
207
46
  }
208
47
  /**
209
48
  * WebSocket connection states
@@ -257,6 +96,7 @@ export interface TradeHistoryDataDto {
257
96
  externalFeePaid: number;
258
97
  builderFeePaid: number;
259
98
  realizedPnl: number;
99
+ totalEntryValue: number;
260
100
  totalValue: number;
261
101
  entryRatio: number;
262
102
  exitRatio: number;
@@ -283,15 +123,16 @@ export interface PositionAssetDetailDto {
283
123
  actualSize: number;
284
124
  cumFunding: CumFundingDto;
285
125
  marginUsed: number;
126
+ entryPositionValue: number;
286
127
  positionValue: number;
287
128
  unrealizedPnl: number;
288
129
  liquidationPrice: number;
289
- isExternallyModified: boolean;
130
+ initialWeight: number;
131
+ }
132
+ export interface TpSlThreshold {
133
+ type: 'PERCENTAGE' | 'DOLLAR' | 'POSITION_VALUE';
134
+ value: number;
290
135
  }
291
- /**
292
- * Position sync status
293
- */
294
- export type PositionSyncStatus = 'SYNCED' | 'EXTERNALLY_MODIFIED' | 'EXTERNALLY_CLOSED' | 'PAIR_BROKEN';
295
136
  /**
296
137
  * Open position data structure
297
138
  */
@@ -299,18 +140,17 @@ export interface OpenPositionDto {
299
140
  positionId: string;
300
141
  address: string;
301
142
  leverage: number;
302
- stopLoss: number | null;
303
- takeProfit: number | null;
143
+ stopLoss: TpSlThreshold | null;
144
+ takeProfit: TpSlThreshold | null;
304
145
  entryRatio: number;
305
146
  markRatio: number;
306
147
  netFunding: number;
148
+ entryPositionValue: number;
307
149
  positionValue: number;
308
150
  marginUsed: number;
309
151
  unrealizedPnl: number;
310
152
  longAssets: PositionAssetDetailDto[];
311
153
  shortAssets: PositionAssetDetailDto[];
312
- syncStatus: PositionSyncStatus;
313
- lastSyncAt: string;
314
154
  createdAt: string;
315
155
  updatedAt: string;
316
156
  }
@@ -406,6 +246,95 @@ export interface AddressState {
406
246
  autoConnect: boolean;
407
247
  previousAddresses: string[];
408
248
  }
249
+ export declare enum AuthStatus {
250
+ Idle = "idle",
251
+ Authenticating = "authenticating",
252
+ Authenticated = "authenticated",
253
+ Error = "error"
254
+ }
255
+ export interface UseAuthOptions {
256
+ baseUrl: string;
257
+ clientId: string;
258
+ }
259
+ export interface UserProfile {
260
+ userId: string;
261
+ address: string;
262
+ appId: string;
263
+ }
264
+ export interface EIP712AuthDetails {
265
+ primaryType: string;
266
+ domain: {
267
+ name: string;
268
+ version: string;
269
+ chainId: number;
270
+ verifyingContract: string;
271
+ };
272
+ types: Record<string, Array<{
273
+ name: string;
274
+ type: string;
275
+ }>>;
276
+ message: Record<string, unknown>;
277
+ timestamp: number;
278
+ }
279
+ export interface GetEIP712MessageResponse extends EIP712AuthDetails {
280
+ }
281
+ export interface PrivyAuthDetails {
282
+ appId: string;
283
+ accessToken: string;
284
+ }
285
+ export interface AuthenticateRequest {
286
+ method: 'eip712' | 'api_key' | 'privy_access_token';
287
+ address: string;
288
+ clientId: string;
289
+ details: {
290
+ signature: string;
291
+ timestamp: number;
292
+ } | {
293
+ apiKey: string;
294
+ } | PrivyAuthDetails;
295
+ }
296
+ export interface AuthenticateResponse {
297
+ accessToken: string;
298
+ refreshToken: string;
299
+ tokenType: string;
300
+ expiresIn: number;
301
+ address: string;
302
+ clientId: string;
303
+ }
304
+ export interface RefreshTokenRequest {
305
+ refreshToken: string;
306
+ }
307
+ export interface RefreshTokenResponse {
308
+ accessToken: string;
309
+ refreshToken: string;
310
+ tokenType: string;
311
+ expiresIn: number;
312
+ }
313
+ export interface LogoutRequest {
314
+ refreshToken: string;
315
+ }
316
+ export interface LogoutResponse {
317
+ message: string;
318
+ }
319
+ export type AgentWalletStatus = 'ACTIVE' | 'EXPIRED' | 'NOT_FOUND';
320
+ export interface GetAgentWalletResponseDto {
321
+ agentWalletAddress?: string;
322
+ agentName: string;
323
+ status: AgentWalletStatus;
324
+ }
325
+ export interface CreateAgentWalletResponseDto {
326
+ agentWalletAddress: string;
327
+ message: string;
328
+ }
329
+ export interface UseAgentWalletOptions {
330
+ baseUrl: string;
331
+ }
332
+ export interface AgentWalletState {
333
+ address: string | null;
334
+ name: string | null;
335
+ status: AgentWalletStatus | 'PENDING' | null;
336
+ isActive: boolean;
337
+ }
409
338
  /**
410
339
  * WebSocket message from HyperLiquid native API
411
340
  */
@@ -620,16 +549,13 @@ export type CandleInterval = "1m" | "3m" | "5m" | "15m" | "30m" | "1h" | "2h" |
620
549
  * Candle data structure from WebSocket
621
550
  */
622
551
  export interface CandleData {
552
+ s?: string;
623
553
  t: number;
624
554
  T: number;
625
- s: string;
626
- i: string;
627
- o: string;
628
- c: string;
629
- h: string;
630
- l: string;
631
- v: string;
632
- n: number;
555
+ o: number;
556
+ c: number;
557
+ h: number;
558
+ l: number;
633
559
  }
634
560
  /**
635
561
  * Candle chart data organized by symbol only
@@ -648,16 +574,7 @@ export interface CandleSnapshotRequest {
648
574
  };
649
575
  type: "candleSnapshot";
650
576
  }
651
- /**
652
- * Weighted candle data for chart visualization
653
- */
654
- export interface WeightedCandleData {
655
- t: number;
656
- T: number;
657
- o: number;
658
- c: number;
659
- h: number;
660
- l: number;
661
- v: number;
662
- n: number;
577
+ export interface TokenSelectorConfig {
578
+ isLong: boolean;
579
+ index: number;
663
580
  }
@@ -1,4 +1,4 @@
1
- import type { CandleData, TokenSelection, WeightedCandleData } from '../types';
1
+ import type { CandleData, TokenSelection } from '../types';
2
2
  /**
3
3
  * Create efficient timestamp-based lookup maps for candle data
4
4
  */
@@ -21,4 +21,4 @@ export declare const getCompleteTimestamps: (candleLookups: Record<string, Map<n
21
21
  * Compute basket candles from individual token candles using weighted ratios
22
22
  * Optimized version that creates lookup maps once and reuses them
23
23
  */
24
- export declare const computeBasketCandles: (longTokens: TokenSelection[], shortTokens: TokenSelection[], tokenCandles: Record<string, CandleData[]>) => WeightedCandleData[];
24
+ export declare const computeBasketCandles: (longTokens: TokenSelection[], shortTokens: TokenSelection[], tokenCandles: Record<string, CandleData[]>) => CandleData[];
@@ -0,0 +1,3 @@
1
+ import { ApiErrorResponse } from '../types';
2
+ export declare function toApiError(error: unknown): ApiErrorResponse;
3
+ export declare function joinUrl(baseUrl: string, path: string): string;
@@ -1,22 +1,2 @@
1
- import type { OpenPositionDto, WebData2Response, WsAllMidsData, RawPositionDto } from '../types';
2
- export declare class PositionProcessor {
3
- private webData2;
4
- private allMids;
5
- constructor(webData2: WebData2Response | null, allMids: WsAllMidsData | null);
6
- execute(rawPositions: RawPositionDto[]): OpenPositionDto[];
7
- private getUserPositions;
8
- private getMarketPrice;
9
- private calculatePlatformTotalsByAsset;
10
- private extractBaseCurrency;
11
- private syncPositionWithAggregateData;
12
- private determineSyncStatus;
13
- private syncAssetWithAggregateData;
14
- private mapPositionToDtoWithSyncData;
15
- private mapAssetToDetailDto;
16
- private calculateEntryRatio;
17
- private calculateMarkRatio;
18
- private calculateNetFundingFromSyncResults;
19
- private calculateTotalUnrealizedPnlFromSyncResults;
20
- private calculateCurrentTotalPositionValue;
21
- private calculateEntryTotalPositionValue;
22
- }
1
+ import { OpenPositionDto, RawPositionDto, WebData2Response, WsAllMidsData } from "../types";
2
+ export declare const buildPositionValue: (rawPositions: RawPositionDto[], webData2: WebData2Response, allMids: WsAllMidsData) => OpenPositionDto[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.0.22",
3
+ "version": "0.0.26",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,59 +0,0 @@
1
- import { PearHyperliquidClient } from './client';
2
- import { SyncTradeHistoryDto, SyncTradeHistoryResponseDto, SyncOpenPositionDto, SyncOpenPositionResponseDto, SyncOpenOrderDto, SyncOpenOrderResponseDto, ApiResponse } from './types';
3
- /**
4
- * Main Migration SDK Class - Simple request/response pattern
5
- */
6
- export declare class PearMigrationSDK {
7
- private client;
8
- private isTradeHistorySyncRunning;
9
- private isOpenPositionsSyncRunning;
10
- private isOpenOrdersSyncRunning;
11
- constructor(client: PearHyperliquidClient);
12
- /**
13
- * Sync trade history data - can only run one at a time
14
- * If called while already running, returns immediately without making request
15
- */
16
- syncTradeHistory(payload: SyncTradeHistoryDto): Promise<ApiResponse<SyncTradeHistoryResponseDto> | null>;
17
- /**
18
- * Sync open positions data - can only run one at a time
19
- * If called while already running, returns immediately without making request
20
- */
21
- syncOpenPositions(payload: SyncOpenPositionDto): Promise<ApiResponse<SyncOpenPositionResponseDto> | null>;
22
- /**
23
- * Sync open orders data - can only run one at a time
24
- * If called while already running, returns immediately without making request
25
- */
26
- syncOpenOrders(payload: SyncOpenOrderDto): Promise<ApiResponse<SyncOpenOrderResponseDto> | null>;
27
- /**
28
- * Check if any sync is currently running
29
- */
30
- isSyncInProgress(): boolean;
31
- /**
32
- * Check if trade history sync is currently running
33
- */
34
- isTradeHistorySyncInProgress(): boolean;
35
- /**
36
- * Check if open positions sync is currently running
37
- */
38
- isOpenPositionsSyncInProgress(): boolean;
39
- /**
40
- * Check if open orders sync is currently running
41
- */
42
- isOpenOrdersSyncInProgress(): boolean;
43
- /**
44
- * Get the underlying client instance
45
- */
46
- getClient(): PearHyperliquidClient;
47
- /**
48
- * Set authorization token on the client
49
- */
50
- setAuthToken(token: string): void;
51
- /**
52
- * Set custom headers on the client
53
- */
54
- setHeaders(headers: Record<string, string>): void;
55
- /**
56
- * Get base URL from client
57
- */
58
- getBaseUrl(): string;
59
- }