@pear-protocol/hyperliquid-sdk 0.1.12 → 0.1.13

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.
Files changed (2) hide show
  1. package/dist/index.js +68 -13
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -384,19 +384,19 @@ class TokenMetadataExtractor {
384
384
  if (!assetCtx) {
385
385
  return null;
386
386
  }
387
- // Get current price - prefer assetCtx.midPx as it's already index-matched,
388
- // fall back to allMids lookup if midPx is null
387
+ // Get current price - prefer allMids (real-time WebSocket data),
388
+ // fall back to assetCtx.midPx if not available
389
389
  const actualSymbol = foundAsset.name;
390
390
  let currentPrice = 0;
391
- // Fallback: assetCtx.midPx (already properly indexed)
392
- if (!currentPrice || isNaN(currentPrice)) {
393
- const currentPriceStr = allMids.mids[actualSymbol] || allMids.mids[symbol];
394
- currentPrice = currentPriceStr ? parseFloat(currentPriceStr) : 0;
395
- }
396
- // Primary source: allMids lookup
391
+ // Fallback: assetCtx.midPx (from REST API, less frequent)
397
392
  if (assetCtx.midPx) {
398
393
  currentPrice = parseFloat(assetCtx.midPx);
399
394
  }
395
+ // Primary source: allMids (real-time WebSocket data)
396
+ const currentPriceStr = allMids.mids[actualSymbol] || allMids.mids[symbol];
397
+ if (currentPriceStr) {
398
+ currentPrice = parseFloat(currentPriceStr);
399
+ }
400
400
  // Get previous day price
401
401
  const prevDayPrice = parseFloat(assetCtx.prevDayPx);
402
402
  // Calculate 24h price change
@@ -906,6 +906,7 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
906
906
  const reconnectAttemptsRef = useRef(0);
907
907
  const manualCloseRef = useRef(false);
908
908
  const onUserFillsRef = useRef(onUserFills);
909
+ const reconnectTimeoutRef = useRef(null);
909
910
  const [readyState, setReadyState] = useState(ReadyState.CONNECTING);
910
911
  // Keep the ref updated with the latest callback
911
912
  useEffect(() => {
@@ -1045,6 +1046,11 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1045
1046
  console.log('[HyperLiquid WS] connect() called, enabled:', enabled);
1046
1047
  if (!enabled)
1047
1048
  return;
1049
+ // Clear any pending reconnect timeout
1050
+ if (reconnectTimeoutRef.current) {
1051
+ clearTimeout(reconnectTimeoutRef.current);
1052
+ reconnectTimeoutRef.current = null;
1053
+ }
1048
1054
  try {
1049
1055
  // Avoid opening multiple sockets if one is already active or connecting
1050
1056
  if (wsRef.current &&
@@ -1070,12 +1076,17 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1070
1076
  };
1071
1077
  ws.onclose = () => {
1072
1078
  setReadyState(ReadyState.CLOSED);
1073
- if (!manualCloseRef.current && reconnectAttemptsRef.current < 5) {
1079
+ // Reset subscription state so effects will resubscribe on reconnect
1080
+ setSubscribedAddress(null);
1081
+ setSubscribedTokens([]);
1082
+ setSubscribedCandleTokens([]);
1083
+ setClearinghouseStateReceived(false);
1084
+ if (!manualCloseRef.current) {
1074
1085
  reconnectAttemptsRef.current += 1;
1075
- if (reconnectAttemptsRef.current === 5) {
1076
- console.error('[HyperLiquid WS] Reconnection stopped after 5 attempts');
1077
- }
1078
- setTimeout(() => connect(), 3000);
1086
+ // Exponential backoff: 1s, 2s, 4s, 8s, 16s, 30s (max), then stay at 30s
1087
+ const delay = Math.min(1000 * Math.pow(2, reconnectAttemptsRef.current - 1), 30000);
1088
+ console.log(`[HyperLiquid WS] Reconnecting in ${delay}ms (attempt ${reconnectAttemptsRef.current})`);
1089
+ reconnectTimeoutRef.current = setTimeout(() => connect(), delay);
1079
1090
  }
1080
1091
  };
1081
1092
  }
@@ -1086,9 +1097,53 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1086
1097
  useEffect(() => {
1087
1098
  console.log('[HyperLiquid WS] Connection effect running - calling connect()');
1088
1099
  connect();
1100
+ // Handle online/offline events to reconnect when internet is restored
1101
+ const handleOnline = () => {
1102
+ console.log('[HyperLiquid WS] Browser went online, attempting reconnect');
1103
+ // Reset reconnect attempts when internet comes back
1104
+ reconnectAttemptsRef.current = 0;
1105
+ // Clear any pending reconnect timeout
1106
+ if (reconnectTimeoutRef.current) {
1107
+ clearTimeout(reconnectTimeoutRef.current);
1108
+ reconnectTimeoutRef.current = null;
1109
+ }
1110
+ // Reset subscription state so effects will resubscribe on reconnect
1111
+ setSubscribedAddress(null);
1112
+ setSubscribedTokens([]);
1113
+ setSubscribedCandleTokens([]);
1114
+ setClearinghouseStateReceived(false);
1115
+ // Close existing socket if in a bad state
1116
+ if (wsRef.current &&
1117
+ wsRef.current.readyState !== WebSocket.OPEN &&
1118
+ wsRef.current.readyState !== WebSocket.CONNECTING) {
1119
+ try {
1120
+ wsRef.current.close();
1121
+ }
1122
+ catch (_a) { }
1123
+ wsRef.current = null;
1124
+ }
1125
+ // Attempt to reconnect
1126
+ connect();
1127
+ };
1128
+ const handleOffline = () => {
1129
+ console.log('[HyperLiquid WS] Browser went offline');
1130
+ // Clear pending reconnect timeout since we're offline
1131
+ if (reconnectTimeoutRef.current) {
1132
+ clearTimeout(reconnectTimeoutRef.current);
1133
+ reconnectTimeoutRef.current = null;
1134
+ }
1135
+ };
1136
+ window.addEventListener('online', handleOnline);
1137
+ window.addEventListener('offline', handleOffline);
1089
1138
  return () => {
1090
1139
  console.log('[HyperLiquid WS] Connection effect cleanup - closing existing connection');
1140
+ window.removeEventListener('online', handleOnline);
1141
+ window.removeEventListener('offline', handleOffline);
1091
1142
  manualCloseRef.current = true;
1143
+ if (reconnectTimeoutRef.current) {
1144
+ clearTimeout(reconnectTimeoutRef.current);
1145
+ reconnectTimeoutRef.current = null;
1146
+ }
1092
1147
  if (wsRef.current) {
1093
1148
  try {
1094
1149
  wsRef.current.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",