@pear-protocol/hyperliquid-sdk 0.0.12 → 0.0.15

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,25 +3,10 @@ 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 { RawPositionDto, OpenLimitOrderDto, AccountSummaryResponseDto, WebData2Response, WsAllMidsData, TradeHistoryDataDto, TokenSelection, TokenConflict } from './types';
7
- interface WebSocketData {
8
- tradeHistories: TradeHistoryDataDto[] | null;
9
- openPositions: RawPositionDto[] | null;
10
- openOrders: OpenLimitOrderDto[] | null;
11
- accountSummary: AccountSummaryResponseDto | null;
12
- }
13
6
  export interface TokenSelectorConfig {
14
7
  isLong: boolean;
15
8
  index: number;
16
9
  }
17
- interface TokenSelectionState {
18
- longTokens: TokenSelection[];
19
- shortTokens: TokenSelection[];
20
- openTokenSelector: boolean;
21
- selectorConfig: TokenSelectorConfig | null;
22
- openConflictModal: boolean;
23
- conflicts: TokenConflict[];
24
- }
25
10
  export interface PearHyperliquidContextType {
26
11
  client: PearHyperliquidClient;
27
12
  migrationSDK: PearMigrationSDK;
@@ -29,15 +14,10 @@ export interface PearHyperliquidContextType {
29
14
  setAddress: (address: string | null) => void;
30
15
  connectionStatus: ReadyState;
31
16
  isConnected: boolean;
32
- data: WebSocketData;
33
17
  lastError: string | null;
34
18
  nativeConnectionStatus: ReadyState;
35
19
  nativeIsConnected: boolean;
36
20
  nativeLastError: string | null;
37
- webData2: WebData2Response | null;
38
- allMids: WsAllMidsData | null;
39
- tokenSelection: TokenSelectionState;
40
- setTokenSelection: React.Dispatch<React.SetStateAction<TokenSelectionState>>;
41
21
  }
42
22
  export declare const PearHyperliquidContext: React.Context<PearHyperliquidContextType | undefined>;
43
23
  interface PearHyperliquidProviderProps {
@@ -0,0 +1,14 @@
1
+ import type { CandleData, CandleInterval } from "../types";
2
+ interface HistoricalRange {
3
+ start: number;
4
+ end: number;
5
+ }
6
+ interface TokenHistoricalPriceData {
7
+ symbol: string;
8
+ interval: CandleInterval;
9
+ candles: CandleData[];
10
+ oldestTime: number | null;
11
+ latestTime: number | null;
12
+ }
13
+ export declare const useHistoricalPriceDataStore: any;
14
+ export type { HistoricalRange, TokenHistoricalPriceData };
@@ -0,0 +1 @@
1
+ export declare const useHyperliquidData: any;
@@ -0,0 +1,21 @@
1
+ import type { ActiveAssetData, TokenSelection, TokenMetadata, WebData2Response, WsAllMidsData } from "../types";
2
+ export interface TokenSelectionMetadataState {
3
+ isPriceDataReady: boolean;
4
+ isLoading: boolean;
5
+ longTokensMetadata: Record<string, TokenMetadata | null>;
6
+ shortTokensMetadata: Record<string, TokenMetadata | null>;
7
+ weightedRatio: number;
8
+ weightedRatio24h: number;
9
+ sumNetFunding: number;
10
+ maxLeverage: number;
11
+ minMargin: number;
12
+ leverageMatched: boolean;
13
+ recompute: (args: {
14
+ webData2: WebData2Response | null;
15
+ allMids: WsAllMidsData | null;
16
+ activeAssetData: Record<string, ActiveAssetData> | null;
17
+ longTokens: TokenSelection[];
18
+ shortTokens: TokenSelection[];
19
+ }) => void;
20
+ }
21
+ export declare const useTokenSelectionMetadataStore: any;
@@ -0,0 +1 @@
1
+ export declare const useUserData: any;
@@ -1,33 +1,25 @@
1
- import type { TokenSelection, TokenConflict } from '../types';
2
- import { TokenSelectorConfig } from '../provider';
3
- export type { TokenSelectorConfig };
4
- export interface UseTokenSelectionReturn {
1
+ import type { CandleInterval, TokenConflict, TokenSelection } from "../types";
2
+ import type { TokenSelectorConfig } from "../provider";
3
+ interface UserSelectionState {
5
4
  longTokens: TokenSelection[];
6
5
  shortTokens: TokenSelection[];
7
6
  openTokenSelector: boolean;
8
7
  selectorConfig: TokenSelectorConfig | null;
9
8
  openConflictModal: boolean;
10
9
  conflicts: TokenConflict[];
11
- isLoading: boolean;
12
- isPriceDataReady: boolean;
13
- weightedRatio: number;
14
- weightedRatio24h: number;
15
- sumNetFunding: number;
16
- maxLeverage: number;
17
- minMargin: number;
10
+ candleInterval: CandleInterval;
18
11
  setLongTokens: (tokens: TokenSelection[]) => void;
19
12
  setShortTokens: (tokens: TokenSelection[]) => void;
20
- updateTokenWeight: (isLong: boolean, index: number, newWeight: number) => void;
21
- addToken: (isLong: boolean) => void;
22
- removeToken: (isLong: boolean, index: number) => void;
23
- handleTokenSelect: (selectedToken: string) => void;
24
13
  setOpenTokenSelector: (open: boolean) => void;
25
14
  setSelectorConfig: (config: TokenSelectorConfig | null) => void;
26
15
  setOpenConflictModal: (open: boolean) => void;
27
16
  setConflicts: (conflicts: TokenConflict[]) => void;
17
+ setCandleInterval: (interval: CandleInterval) => void;
18
+ updateTokenWeight: (isLong: boolean, index: number, newWeight: number) => void;
19
+ addToken: (isLong: boolean) => void;
20
+ removeToken: (isLong: boolean, index: number) => void;
21
+ handleTokenSelect: (selectedToken: string) => void;
28
22
  resetToDefaults: () => void;
29
23
  }
30
- /**
31
- * Hook to access token selection state using provider's WebSocket data
32
- */
33
- export declare const useTokenSelection: () => UseTokenSelectionReturn;
24
+ export declare const useUserSelection: any;
25
+ export type { UserSelectionState };
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' | 'webData2' | 'allMids';
215
+ export type WebSocketChannel = 'trade-histories' | 'open-positions' | 'open-orders' | 'account-summary' | 'webData2' | 'allMids' | 'activeAssetData';
216
216
  /**
217
217
  * WebSocket subscription message
218
218
  */
@@ -452,7 +452,8 @@ export interface UniverseAsset {
452
452
  name: string;
453
453
  szDecimals: number;
454
454
  maxLeverage: number;
455
- onlyIsolated: boolean;
455
+ onlyIsolated?: boolean;
456
+ isDelisted?: boolean;
456
457
  }
457
458
  /**
458
459
  * WebData2 response structure
@@ -548,6 +549,24 @@ export interface RawPositionDto {
548
549
  longAssets: RawAssetDto[];
549
550
  shortAssets: RawAssetDto[];
550
551
  }
552
+ /**
553
+ * Leverage information from activeAssetData
554
+ */
555
+ export interface LeverageInfo {
556
+ type: string;
557
+ value: number;
558
+ }
559
+ /**
560
+ * Active asset data from WebSocket
561
+ */
562
+ export interface ActiveAssetData {
563
+ user: string;
564
+ coin: string;
565
+ leverage: LeverageInfo;
566
+ maxTradeSzs: [string, string];
567
+ availableToTrade: [string, string];
568
+ markPx: string;
569
+ }
551
570
  /**
552
571
  * Token metadata from WebData2 and AllMids
553
572
  */
@@ -562,6 +581,9 @@ export interface TokenMetadata {
562
581
  oraclePrice: number;
563
582
  openInterest: string;
564
583
  dayVolume: string;
584
+ leverage?: LeverageInfo;
585
+ maxTradeSzs?: [string, string];
586
+ availableToTrade?: [string, string];
565
587
  }
566
588
  /**
567
589
  * Enhanced token selection with weight and metadata for basket trading
@@ -569,7 +591,6 @@ export interface TokenMetadata {
569
591
  export interface TokenSelection {
570
592
  symbol: string;
571
593
  weight: number;
572
- metadata?: TokenMetadata;
573
594
  }
574
595
  /**
575
596
  * Token conflict information for position conflicts
@@ -579,3 +600,56 @@ export interface TokenConflict {
579
600
  conflictType: 'long' | 'short';
580
601
  conflictMessage: string;
581
602
  }
603
+ export interface AssetMarketData {
604
+ asset: AssetCtx;
605
+ universe: UniverseAsset;
606
+ }
607
+ /**
608
+ * Candle interval options
609
+ */
610
+ export type CandleInterval = "1m" | "3m" | "5m" | "15m" | "30m" | "1h" | "2h" | "4h" | "8h" | "12h" | "1d" | "3d" | "1w" | "1M";
611
+ /**
612
+ * Candle data structure from WebSocket
613
+ */
614
+ export interface CandleData {
615
+ t: number;
616
+ T: number;
617
+ s: string;
618
+ i: string;
619
+ o: string;
620
+ c: string;
621
+ h: string;
622
+ l: string;
623
+ v: string;
624
+ n: number;
625
+ }
626
+ /**
627
+ * Candle chart data organized by symbol only
628
+ * Since new candles always have latest timestamp, no need to store per interval
629
+ */
630
+ export type CandleChartData = Record<string, CandleData>;
631
+ /**
632
+ * Historical candle data request
633
+ */
634
+ export interface CandleSnapshotRequest {
635
+ req: {
636
+ coin: string;
637
+ startTime: number;
638
+ endTime: number;
639
+ interval: CandleInterval;
640
+ };
641
+ type: "candleSnapshot";
642
+ }
643
+ /**
644
+ * Weighted candle data for chart visualization
645
+ */
646
+ export interface WeightedCandleData {
647
+ t: number;
648
+ T: number;
649
+ o: number;
650
+ c: number;
651
+ h: number;
652
+ l: number;
653
+ v: number;
654
+ n: number;
655
+ }
@@ -0,0 +1,24 @@
1
+ import type { CandleData, TokenSelection, WeightedCandleData } from '../types';
2
+ /**
3
+ * Create efficient timestamp-based lookup maps for candle data
4
+ */
5
+ export declare const createCandleLookups: (tokenCandles: Record<string, CandleData[]>) => Record<string, Map<number, CandleData>>;
6
+ /**
7
+ * Calculate weighted ratio for a specific price type (open, high, low, close)
8
+ * Uses the formula: LONG_PRODUCT * SHORT_PRODUCT
9
+ * Where LONG_PRODUCT = ∏(PRICE^(WEIGHT/100)) for long tokens
10
+ * And SHORT_PRODUCT = ∏(PRICE^-(WEIGHT/100)) for short tokens
11
+ *
12
+ * Optimized version that uses Map lookups instead of Array.find()
13
+ */
14
+ export declare const calculateWeightedRatio: (longTokens: TokenSelection[], shortTokens: TokenSelection[], candleLookups: Record<string, Map<number, CandleData>>, timestamp: number, priceType: "o" | "h" | "l" | "c") => number;
15
+ /**
16
+ * Get all unique timestamps where ALL required symbols have data
17
+ * Optimized version that uses Map lookups
18
+ */
19
+ export declare const getCompleteTimestamps: (candleLookups: Record<string, Map<number, CandleData>>, requiredSymbols: string[]) => number[];
20
+ /**
21
+ * Compute basket candles from individual token candles using weighted ratios
22
+ * Optimized version that creates lookup maps once and reuses them
23
+ */
24
+ export declare const computeBasketCandles: (longTokens: TokenSelection[], shortTokens: TokenSelection[], tokenCandles: Record<string, CandleData[]>) => WeightedCandleData[];
@@ -1,4 +1,4 @@
1
- import type { WebData2Response, WsAllMidsData, TokenMetadata } from '../types';
1
+ import type { WebData2Response, WsAllMidsData, TokenMetadata, ActiveAssetData } from '../types';
2
2
  /**
3
3
  * Extracts token metadata from WebData2 and AllMids data
4
4
  */
@@ -8,17 +8,19 @@ export declare class TokenMetadataExtractor {
8
8
  * @param symbol - Token symbol
9
9
  * @param webData2 - WebData2 response containing asset context and universe data
10
10
  * @param allMids - AllMids data containing current prices
11
+ * @param activeAssetData - Optional active asset data containing leverage information
11
12
  * @returns TokenMetadata or null if token not found
12
13
  */
13
- static extractTokenMetadata(symbol: string, webData2: WebData2Response | null, allMids: WsAllMidsData | null): TokenMetadata | null;
14
+ static extractTokenMetadata(symbol: string, webData2: WebData2Response | null, allMids: WsAllMidsData | null, activeAssetData?: Record<string, ActiveAssetData> | null): TokenMetadata | null;
14
15
  /**
15
16
  * Extracts metadata for multiple tokens
16
17
  * @param symbols - Array of token symbols
17
18
  * @param webData2 - WebData2 response
18
19
  * @param allMids - AllMids data
20
+ * @param activeAssetData - Optional active asset data containing leverage information
19
21
  * @returns Record of symbol to TokenMetadata
20
22
  */
21
- static extractMultipleTokensMetadata(symbols: string[], webData2: WebData2Response | null, allMids: WsAllMidsData | null): Record<string, TokenMetadata | null>;
23
+ static extractMultipleTokensMetadata(symbols: string[], webData2: WebData2Response | null, allMids: WsAllMidsData | null, activeAssetData?: Record<string, ActiveAssetData> | null): Record<string, TokenMetadata | null>;
22
24
  /**
23
25
  * Checks if token data is available in WebData2
24
26
  * @param symbol - Token symbol
@@ -1,20 +1,10 @@
1
1
  import { ReadyState } from 'react-use-websocket';
2
- import type { OpenLimitOrderDto, AccountSummaryResponseDto, RawPositionDto, TradeHistoryDataDto } from './types';
3
- interface WebSocketData {
4
- tradeHistories: TradeHistoryDataDto[] | null;
5
- openPositions: RawPositionDto[] | null;
6
- openOrders: OpenLimitOrderDto[] | null;
7
- accountSummary: AccountSummaryResponseDto | null;
8
- }
9
2
  export interface UseHyperliquidWebSocketProps {
10
3
  wsUrl: string;
11
4
  address: string | null;
12
5
  }
13
- export interface UseHyperliquidWebSocketReturn {
14
- data: WebSocketData;
6
+ export declare const useHyperliquidWebSocket: ({ wsUrl, address }: UseHyperliquidWebSocketProps) => {
15
7
  connectionStatus: ReadyState;
16
8
  isConnected: boolean;
17
9
  lastError: string | null;
18
- }
19
- export declare const useHyperliquidWebSocket: ({ wsUrl, address }: UseHyperliquidWebSocketProps) => UseHyperliquidWebSocketReturn;
20
- export {};
10
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.0.12",
3
+ "version": "0.0.15",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -15,12 +15,12 @@
15
15
  "clean": "rimraf dist"
16
16
  },
17
17
  "dependencies": {
18
- "axios": "^1.6.0"
18
+ "react-use-websocket": "^4.8.1"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "react": ">=16.8.0",
22
22
  "react-dom": ">=16.8.0",
23
- "react-use-websocket": "^4.8.1"
23
+ "axios": "^1.6.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@rollup/plugin-commonjs": "^25.0.0",
@@ -1,5 +0,0 @@
1
- import type { AccountSummaryResponseDto, WebData2Response, OpenLimitOrderDto } from '../types';
2
- /**
3
- * Hook that calculates account summary by syncing platform data with HyperLiquid real-time data
4
- */
5
- export declare const useCalculatedAccountSummary: (platformAccountSummary: AccountSummaryResponseDto | null, platformOpenOrders: OpenLimitOrderDto[] | null, webData2: WebData2Response | null, agentWalletAddress?: string, agentWalletStatus?: string) => AccountSummaryResponseDto | null;
@@ -1,5 +0,0 @@
1
- import type { OpenPositionDto, RawPositionDto, WebData2Response, WsAllMidsData } from '../types';
2
- /**
3
- * Hook that calculates open positions by syncing platform positions with HyperLiquid real-time data
4
- */
5
- export declare const useCalculatedOpenPositions: (platformPositions: RawPositionDto[] | null, webData2: WebData2Response | null, allMids: WsAllMidsData | null) => OpenPositionDto[] | null;