@pear-protocol/hyperliquid-sdk 0.0.4 → 0.0.7

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.
@@ -3,7 +3,7 @@ import { ReadyState } from 'react-use-websocket';
3
3
  import { PearHyperliquidClient } from './client';
4
4
  import { PearHyperliquidConfig } from './types';
5
5
  import { PearMigrationSDK } from './migration-sdk';
6
- import type { PaginatedTradeHistoryResponseDto, OpenPositionDto, OpenLimitOrderDto, AccountSummaryResponseDto } from './types';
6
+ import type { PaginatedTradeHistoryResponseDto, OpenPositionDto, OpenLimitOrderDto, AccountSummaryResponseDto, WebData2Response, WsAllMidsData } from './types';
7
7
  interface WebSocketData {
8
8
  tradeHistories: PaginatedTradeHistoryResponseDto | null;
9
9
  openPositions: OpenPositionDto[] | null;
@@ -19,6 +19,11 @@ export interface PearHyperliquidContextType {
19
19
  isConnected: boolean;
20
20
  data: WebSocketData;
21
21
  lastError: string | null;
22
+ nativeConnectionStatus: ReadyState;
23
+ nativeIsConnected: boolean;
24
+ nativeLastError: string | null;
25
+ webData2: WebData2Response | null;
26
+ allMids: WsAllMidsData | null;
22
27
  }
23
28
  export declare const PearHyperliquidContext: React.Context<PearHyperliquidContextType | undefined>;
24
29
  interface PearHyperliquidProviderProps {
package/dist/types.d.ts CHANGED
@@ -212,7 +212,7 @@ export type WebSocketConnectionState = 'connecting' | 'connected' | 'disconnecte
212
212
  /**
213
213
  * WebSocket channels
214
214
  */
215
- export type WebSocketChannel = 'trade-histories' | 'open-positions' | 'open-orders' | 'account-summary';
215
+ export type WebSocketChannel = 'trade-histories' | 'open-positions' | 'open-orders' | 'account-summary' | 'webData2' | 'allMids';
216
216
  /**
217
217
  * WebSocket subscription message
218
218
  */
@@ -402,3 +402,121 @@ export interface AddressState {
402
402
  autoConnect: boolean;
403
403
  previousAddresses: string[];
404
404
  }
405
+ /**
406
+ * WebSocket message from HyperLiquid native API
407
+ */
408
+ export interface WebSocketMessage {
409
+ method: "subscribe" | "unsubscribe";
410
+ subscription: {
411
+ type: string;
412
+ coin?: string;
413
+ interval?: string;
414
+ user?: string;
415
+ aggregateByTime?: boolean;
416
+ };
417
+ }
418
+ /**
419
+ * WebSocket response from HyperLiquid native API
420
+ */
421
+ export interface WebSocketResponse {
422
+ channel: string;
423
+ data: any;
424
+ }
425
+ /**
426
+ * All mids data structure
427
+ */
428
+ export interface WsAllMidsData {
429
+ mids: Record<string, string>;
430
+ }
431
+ /**
432
+ * Asset context data
433
+ */
434
+ export interface AssetCtx {
435
+ funding: string;
436
+ openInterest: string;
437
+ prevDayPx: string;
438
+ dayNtlVlm: string;
439
+ markPx: string;
440
+ midPx?: string;
441
+ impactPxs?: string[];
442
+ oraclePx: string;
443
+ }
444
+ /**
445
+ * Universe asset metadata
446
+ */
447
+ export interface UniverseAsset {
448
+ name: string;
449
+ szDecimals: number;
450
+ maxLeverage: number;
451
+ onlyIsolated: boolean;
452
+ }
453
+ /**
454
+ * WebData2 response structure
455
+ */
456
+ export interface WebData2Response {
457
+ assetCtxs: AssetCtx[];
458
+ meta: {
459
+ universe: UniverseAsset[];
460
+ };
461
+ clearinghouseState: {
462
+ assetPositions: AssetPosition[];
463
+ crossMaintenanceMarginUsed: string;
464
+ crossMarginSummary: {
465
+ accountValue: string;
466
+ totalMarginUsed: string;
467
+ totalNtlPos: string;
468
+ totalRawUsd: string;
469
+ };
470
+ marginSummary: {
471
+ accountValue: string;
472
+ totalMarginUsed: string;
473
+ totalNtlPos: string;
474
+ totalRawUsd: string;
475
+ };
476
+ time: number;
477
+ withdrawable: string;
478
+ };
479
+ perpsAtOpenInterestCap?: string[];
480
+ }
481
+ /**
482
+ * Asset position data
483
+ */
484
+ export interface AssetPosition {
485
+ position: {
486
+ coin: string;
487
+ entryPx?: string;
488
+ leverage: {
489
+ type: string;
490
+ value: number;
491
+ };
492
+ liquidationPx?: string;
493
+ marginUsed: string;
494
+ maxLeverage: number;
495
+ positionValue: string;
496
+ returnOnEquity: string;
497
+ szi: string;
498
+ unrealizedPnl: string;
499
+ };
500
+ type: string;
501
+ cumFunding: {
502
+ allTime: string;
503
+ sinceChange: string;
504
+ sinceOpen: string;
505
+ };
506
+ }
507
+ /**
508
+ * Asset information detail
509
+ */
510
+ export interface AssetInformationDetail {
511
+ name: string;
512
+ funding: string;
513
+ openInterest: string;
514
+ prevDayPx: string;
515
+ dayNtlVlm: string;
516
+ oraclePx: string;
517
+ markPx: string;
518
+ midPx?: string;
519
+ dayBaseVlm: string;
520
+ priceChange: number;
521
+ assetIndex: number;
522
+ }
@@ -0,0 +1,41 @@
1
+ import type { AccountSummaryResponseDto, WebData2Response, OpenLimitOrderDto } from '../types';
2
+ /**
3
+ * Account summary calculation utility class
4
+ */
5
+ export declare class AccountSummaryCalculator {
6
+ private webData2;
7
+ constructor(webData2: WebData2Response | null);
8
+ /**
9
+ * Calculate account summary from webData2 and platform orders
10
+ */
11
+ calculateAccountSummary(platformAccountSummary: AccountSummaryResponseDto | null, platformOpenOrders: OpenLimitOrderDto[] | null, agentWalletAddress?: string, agentWalletStatus?: string): AccountSummaryResponseDto | null;
12
+ /**
13
+ * Calculate total USD value of open limit orders
14
+ */
15
+ private calculateTotalLimitOrderValue;
16
+ /**
17
+ * Get real-time clearinghouse state from webData2
18
+ */
19
+ getClearinghouseState(): {
20
+ assetPositions: import("../types").AssetPosition[];
21
+ crossMaintenanceMarginUsed: string;
22
+ crossMarginSummary: {
23
+ accountValue: string;
24
+ totalMarginUsed: string;
25
+ totalNtlPos: string;
26
+ totalRawUsd: string;
27
+ };
28
+ marginSummary: {
29
+ accountValue: string;
30
+ totalMarginUsed: string;
31
+ totalNtlPos: string;
32
+ totalRawUsd: string;
33
+ };
34
+ time: number;
35
+ withdrawable: string;
36
+ } | null;
37
+ /**
38
+ * Check if real-time data is available
39
+ */
40
+ hasRealTimeData(): boolean;
41
+ }
@@ -0,0 +1,76 @@
1
+ import type { OpenPositionDto, AssetPosition, WebData2Response, WsAllMidsData } from '../types';
2
+ /**
3
+ * Position side enum for calculations
4
+ */
5
+ export declare enum PositionSide {
6
+ LONG = "LONG",
7
+ SHORT = "SHORT"
8
+ }
9
+ /**
10
+ * Position calculation utility class
11
+ */
12
+ export declare class PositionCalculator {
13
+ private webData2;
14
+ private allMids;
15
+ constructor(webData2: WebData2Response | null, allMids: WsAllMidsData | null);
16
+ /**
17
+ * Get market price for a coin from allMids data
18
+ */
19
+ getMarketPrice(coin: string): number;
20
+ /**
21
+ * Get user positions from webData2
22
+ */
23
+ getUserPositions(): AssetPosition[];
24
+ /**
25
+ * Calculate updated open positions by syncing platform positions with HyperLiquid data
26
+ */
27
+ calculateOpenPositions(platformPositions: OpenPositionDto[]): OpenPositionDto[];
28
+ /**
29
+ * Sync a single position with HyperLiquid data
30
+ */
31
+ private syncPositionWithHyperliquid;
32
+ /**
33
+ * Group assets by their base currency
34
+ */
35
+ private groupAssetsByBaseCurrency;
36
+ /**
37
+ * Sync a group of assets (same base currency) with HyperLiquid position data
38
+ */
39
+ private syncAssetGroupWithHyperliquid;
40
+ /**
41
+ * Determine sync status based on asset sync results
42
+ */
43
+ private determineSyncStatus;
44
+ /**
45
+ * Build updated position with synced data
46
+ */
47
+ private buildUpdatedPosition;
48
+ /**
49
+ * Map sync result to PositionAssetDetailDto
50
+ */
51
+ private mapSyncResultToAssetDto;
52
+ /**
53
+ * Calculate entry ratio (weighted long entry value / weighted short entry value)
54
+ */
55
+ private calculateEntryRatio;
56
+ /**
57
+ * Calculate mark ratio (weighted long mark value / weighted short mark value)
58
+ */
59
+ private calculateMarkRatio;
60
+ /**
61
+ * Calculate net funding from sync results
62
+ */
63
+ private calculateNetFundingFromSyncResults;
64
+ /**
65
+ * Calculate total unrealized PnL from sync results
66
+ */
67
+ private calculateTotalUnrealizedPnlFromSyncResults;
68
+ /**
69
+ * Calculate current total position value using market prices
70
+ */
71
+ private calculateCurrentTotalPositionValue;
72
+ /**
73
+ * Calculate entry total position value using entry prices
74
+ */
75
+ private calculateEntryTotalPositionValue;
76
+ }
@@ -0,0 +1,20 @@
1
+ import { ReadyState } from 'react-use-websocket';
2
+ import type { PaginatedTradeHistoryResponseDto, OpenPositionDto, OpenLimitOrderDto, AccountSummaryResponseDto } from './types';
3
+ interface WebSocketData {
4
+ tradeHistories: PaginatedTradeHistoryResponseDto | null;
5
+ openPositions: OpenPositionDto[] | null;
6
+ openOrders: OpenLimitOrderDto[] | null;
7
+ accountSummary: AccountSummaryResponseDto | null;
8
+ }
9
+ export interface UseHyperliquidWebSocketProps {
10
+ wsUrl: string;
11
+ address: string | null;
12
+ }
13
+ export interface UseHyperliquidWebSocketReturn {
14
+ data: WebSocketData;
15
+ connectionStatus: ReadyState;
16
+ isConnected: boolean;
17
+ lastError: string | null;
18
+ }
19
+ export declare const useHyperliquidWebSocket: ({ wsUrl, address }: UseHyperliquidWebSocketProps) => UseHyperliquidWebSocketReturn;
20
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.0.4",
3
+ "version": "0.0.7",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",