@pear-protocol/hyperliquid-sdk 0.1.6 → 0.1.8

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.
@@ -2,6 +2,7 @@ import type { GetEIP712MessageResponse } from "../types";
2
2
  export declare function useAuth(): {
3
3
  readonly isReady: boolean;
4
4
  readonly isAuthenticated: boolean;
5
+ readonly address: string | null;
5
6
  readonly accessToken: string | null;
6
7
  readonly refreshToken: string | null;
7
8
  readonly getEip712: (address: string) => Promise<GetEIP712MessageResponse>;
package/dist/index.d.ts CHANGED
@@ -1419,6 +1419,7 @@ declare function usePortfolio(): UsePortfolioResult;
1419
1419
  declare function useAuth(): {
1420
1420
  readonly isReady: boolean;
1421
1421
  readonly isAuthenticated: boolean;
1422
+ readonly address: string | null;
1422
1423
  readonly accessToken: string | null;
1423
1424
  readonly refreshToken: string | null;
1424
1425
  readonly getEip712: (address: string) => Promise<GetEIP712MessageResponse>;
package/dist/index.js CHANGED
@@ -903,8 +903,14 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
903
903
  const wsRef = useRef(null);
904
904
  const reconnectAttemptsRef = useRef(0);
905
905
  const manualCloseRef = useRef(false);
906
+ const onUserFillsRef = useRef(onUserFills);
906
907
  const [readyState, setReadyState] = useState(ReadyState.CONNECTING);
908
+ // Keep the ref updated with the latest callback
909
+ useEffect(() => {
910
+ onUserFillsRef.current = onUserFills;
911
+ }, [onUserFills]);
907
912
  const handleMessage = useCallback((event) => {
913
+ var _a;
908
914
  try {
909
915
  const message = JSON.parse(event.data);
910
916
  // Handle subscription responses
@@ -924,7 +930,7 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
924
930
  switch (response.channel) {
925
931
  case 'userFills':
926
932
  {
927
- const maybePromise = onUserFills === null || onUserFills === void 0 ? void 0 : onUserFills();
933
+ const maybePromise = (_a = onUserFillsRef.current) === null || _a === void 0 ? void 0 : _a.call(onUserFillsRef);
928
934
  if (maybePromise instanceof Promise) {
929
935
  maybePromise.catch((err) => console.error('[HyperLiquid WS] userFills callback error', err));
930
936
  }
@@ -1031,9 +1037,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1031
1037
  setRawClearinghouseStates,
1032
1038
  setAssetContextsByDex,
1033
1039
  setSpotState,
1034
- onUserFills,
1035
1040
  ]);
1036
1041
  const connect = useCallback(() => {
1042
+ console.log('[HyperLiquid WS] connect() called, enabled:', enabled);
1037
1043
  if (!enabled)
1038
1044
  return;
1039
1045
  try {
@@ -1041,11 +1047,13 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1041
1047
  if (wsRef.current &&
1042
1048
  (wsRef.current.readyState === WebSocket.OPEN ||
1043
1049
  wsRef.current.readyState === WebSocket.CONNECTING)) {
1050
+ console.log('[HyperLiquid WS] connect() returning early - socket already exists, readyState:', wsRef.current.readyState);
1044
1051
  return;
1045
1052
  }
1053
+ console.log('[HyperLiquid WS] Creating new WebSocket connection');
1046
1054
  manualCloseRef.current = false;
1047
1055
  setReadyState(ReadyState.CONNECTING);
1048
- const ws = new WebSocket("wss://api.hyperliquid.xyz/ws");
1056
+ const ws = new WebSocket('wss://api.hyperliquid.xyz/ws');
1049
1057
  wsRef.current = ws;
1050
1058
  ws.onopen = () => {
1051
1059
  reconnectAttemptsRef.current = 0;
@@ -1054,8 +1062,8 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1054
1062
  };
1055
1063
  ws.onmessage = handleMessage;
1056
1064
  ws.onerror = (event) => {
1057
- console.error("[HyperLiquid WS] Connection error:", event);
1058
- setLastError("WebSocket error");
1065
+ console.error('[HyperLiquid WS] Connection error:', event);
1066
+ setLastError('WebSocket error');
1059
1067
  };
1060
1068
  ws.onclose = () => {
1061
1069
  setReadyState(ReadyState.CLOSED);
@@ -1073,8 +1081,10 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1073
1081
  }
1074
1082
  }, [handleMessage, enabled]);
1075
1083
  useEffect(() => {
1084
+ console.log('[HyperLiquid WS] Connection effect running - calling connect()');
1076
1085
  connect();
1077
1086
  return () => {
1087
+ console.log('[HyperLiquid WS] Connection effect cleanup - closing existing connection');
1078
1088
  manualCloseRef.current = true;
1079
1089
  if (wsRef.current) {
1080
1090
  try {
@@ -1096,7 +1106,7 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1096
1106
  if (isConnected) {
1097
1107
  // Send ping every 30 seconds
1098
1108
  pingIntervalRef.current = setInterval(() => {
1099
- sendJsonMessage({ method: "ping" });
1109
+ sendJsonMessage({ method: 'ping' });
1100
1110
  }, 30000);
1101
1111
  }
1102
1112
  else {
@@ -1114,18 +1124,27 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1114
1124
  }, [isConnected, sendJsonMessage]);
1115
1125
  // Handle address subscription changes
1116
1126
  useEffect(() => {
1117
- if (!isConnected)
1127
+ const DEFAULT_ADDRESS = '0x0000000000000000000000000000000000000000';
1128
+ const userAddress = (address || DEFAULT_ADDRESS).toLowerCase();
1129
+ const normalizedSubscribedAddress = (subscribedAddress === null || subscribedAddress === void 0 ? void 0 : subscribedAddress.toLowerCase()) || null;
1130
+ console.log('[HyperLiquid WS] Address subscription effect running');
1131
+ console.log('[HyperLiquid WS] address:', address, 'userAddress:', userAddress, 'subscribedAddress:', subscribedAddress, 'normalizedSubscribedAddress:', normalizedSubscribedAddress);
1132
+ console.log('[HyperLiquid WS] isConnected:', isConnected);
1133
+ if (normalizedSubscribedAddress === userAddress) {
1134
+ console.log('[HyperLiquid WS] Address unchanged, skipping subscription update');
1118
1135
  return;
1119
- const DEFAULT_ADDRESS = "0x0000000000000000000000000000000000000000";
1120
- const userAddress = address || DEFAULT_ADDRESS;
1121
- if (subscribedAddress === userAddress)
1136
+ }
1137
+ if (!isConnected) {
1138
+ console.log('[HyperLiquid WS] Not connected, skipping subscription update');
1122
1139
  return;
1140
+ }
1123
1141
  // Unsubscribe from previous address if exists
1124
1142
  if (subscribedAddress) {
1143
+ console.log('[HyperLiquid WS] Unsubscribing from previous address:', subscribedAddress);
1125
1144
  const unsubscribeMessage = {
1126
- method: "unsubscribe",
1145
+ method: 'unsubscribe',
1127
1146
  subscription: {
1128
- type: "webData3",
1147
+ type: 'webData3',
1129
1148
  user: subscribedAddress,
1130
1149
  },
1131
1150
  };
@@ -1142,9 +1161,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1142
1161
  sendJsonMessage(unsubscribeSpotState);
1143
1162
  }
1144
1163
  const unsubscribeAllDexsClearinghouseState = {
1145
- method: "unsubscribe",
1164
+ method: 'unsubscribe',
1146
1165
  subscription: {
1147
- type: "allDexsClearinghouseState",
1166
+ type: 'allDexsClearinghouseState',
1148
1167
  user: subscribedAddress,
1149
1168
  },
1150
1169
  };
@@ -1159,42 +1178,34 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1159
1178
  sendJsonMessage(unsubscribeUserFills);
1160
1179
  }
1161
1180
  const subscribeWebData3 = {
1162
- method: "subscribe",
1163
- subscription: {
1164
- type: "webData3",
1165
- user: userAddress,
1166
- },
1167
- };
1168
- // Subscribe to allDexsClearinghouseState with the same payload as webData3
1169
- const subscribeAllDexsClearinghouseState = {
1170
- method: "subscribe",
1181
+ method: 'subscribe',
1171
1182
  subscription: {
1172
- type: "allDexsClearinghouseState",
1183
+ type: 'webData3',
1173
1184
  user: userAddress,
1174
1185
  },
1175
1186
  };
1176
1187
  // Subscribe to allMids
1177
1188
  const subscribeAllMids = {
1178
- method: "subscribe",
1189
+ method: 'subscribe',
1179
1190
  subscription: {
1180
- type: "allMids",
1181
- dex: "ALL_DEXS",
1191
+ type: 'allMids',
1192
+ dex: 'ALL_DEXS',
1182
1193
  },
1183
1194
  };
1184
1195
  // Subscribe to allDexsAssetCtxs (no payload params, global feed)
1185
1196
  const subscribeAllDexsAssetCtxs = {
1186
- method: "subscribe",
1197
+ method: 'subscribe',
1187
1198
  subscription: {
1188
- type: "allDexsAssetCtxs",
1199
+ type: 'allDexsAssetCtxs',
1189
1200
  },
1190
1201
  };
1202
+ console.log('[HyperLiquid WS] Subscribing to new address:', userAddress);
1191
1203
  sendJsonMessage(subscribeWebData3);
1192
- sendJsonMessage(subscribeAllDexsClearinghouseState);
1193
1204
  sendJsonMessage(subscribeAllMids);
1194
1205
  sendJsonMessage(subscribeAllDexsAssetCtxs);
1195
1206
  // Subscribe to spotState for real-time spot balances (USDH, USDC, etc.)
1196
1207
  // Only subscribe if we have a real user address (not the default)
1197
- if (userAddress !== DEFAULT_ADDRESS) {
1208
+ if (userAddress !== DEFAULT_ADDRESS.toLowerCase()) {
1198
1209
  const subscribeSpotState = {
1199
1210
  method: 'subscribe',
1200
1211
  subscription: {
@@ -1204,9 +1215,22 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1204
1215
  };
1205
1216
  sendJsonMessage(subscribeSpotState);
1206
1217
  }
1218
+ // Subscribe to allDexsClearinghouseState with the same payload as webData3
1219
+ // Only subscribe if we have a real user address (not the default)
1220
+ if (userAddress !== DEFAULT_ADDRESS.toLowerCase()) {
1221
+ const subscribeAllDexsClearinghouseState = {
1222
+ method: 'subscribe',
1223
+ subscription: {
1224
+ type: 'allDexsClearinghouseState',
1225
+ user: userAddress,
1226
+ },
1227
+ };
1228
+ sendJsonMessage(subscribeAllDexsClearinghouseState);
1229
+ }
1207
1230
  setSubscribedAddress(userAddress);
1208
1231
  // Clear previous data when address changes
1209
- if (subscribedAddress && subscribedAddress !== userAddress) {
1232
+ if (normalizedSubscribedAddress &&
1233
+ normalizedSubscribedAddress !== userAddress) {
1210
1234
  // clear aggregatedClearingHouseState
1211
1235
  setAggregatedClearingHouseState(null);
1212
1236
  setRawClearinghouseStates(null);
@@ -1225,7 +1249,10 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1225
1249
  setSpotState,
1226
1250
  ]);
1227
1251
  useEffect(() => {
1228
- if (!isConnected || !subscribedAddress || !clearinghouseStateReceived || !userSummary)
1252
+ if (!isConnected ||
1253
+ !subscribedAddress ||
1254
+ !clearinghouseStateReceived ||
1255
+ !userSummary)
1229
1256
  return;
1230
1257
  const subscribeUserFills = {
1231
1258
  method: 'subscribe',
@@ -1235,7 +1262,13 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1235
1262
  },
1236
1263
  };
1237
1264
  sendJsonMessage(subscribeUserFills);
1238
- }, [isConnected, subscribedAddress, clearinghouseStateReceived, userSummary, sendJsonMessage]);
1265
+ }, [
1266
+ isConnected,
1267
+ subscribedAddress,
1268
+ clearinghouseStateReceived,
1269
+ userSummary,
1270
+ sendJsonMessage,
1271
+ ]);
1239
1272
  // Handle token subscriptions for activeAssetData
1240
1273
  useEffect(() => {
1241
1274
  if (!isConnected || !address)
@@ -1246,9 +1279,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1246
1279
  // Unsubscribe from tokens no longer in the list
1247
1280
  tokensToUnsubscribe.forEach((token) => {
1248
1281
  const unsubscribeMessage = {
1249
- method: "unsubscribe",
1282
+ method: 'unsubscribe',
1250
1283
  subscription: {
1251
- type: "activeAssetData",
1284
+ type: 'activeAssetData',
1252
1285
  user: address,
1253
1286
  coin: token,
1254
1287
  },
@@ -1258,9 +1291,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1258
1291
  // Subscribe to new tokens
1259
1292
  tokensToSubscribe.forEach((token) => {
1260
1293
  const subscribeMessage = {
1261
- method: "subscribe",
1294
+ method: 'subscribe',
1262
1295
  subscription: {
1263
- type: "activeAssetData",
1296
+ type: 'activeAssetData',
1264
1297
  user: address,
1265
1298
  coin: token,
1266
1299
  },
@@ -1289,9 +1322,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1289
1322
  if (prevInterval && prevInterval !== candleInterval) {
1290
1323
  subscribedCandleTokens.forEach((token) => {
1291
1324
  const unsubscribeMessage = {
1292
- method: "unsubscribe",
1325
+ method: 'unsubscribe',
1293
1326
  subscription: {
1294
- type: "candle",
1327
+ type: 'candle',
1295
1328
  coin: token,
1296
1329
  interval: prevInterval,
1297
1330
  },
@@ -1306,9 +1339,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1306
1339
  // Unsubscribe from tokens no longer in the list
1307
1340
  tokensToUnsubscribe.forEach((token) => {
1308
1341
  const unsubscribeMessage = {
1309
- method: "unsubscribe",
1342
+ method: 'unsubscribe',
1310
1343
  subscription: {
1311
- type: "candle",
1344
+ type: 'candle',
1312
1345
  coin: token,
1313
1346
  interval: candleInterval,
1314
1347
  },
@@ -1318,9 +1351,9 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
1318
1351
  // Subscribe to new tokens
1319
1352
  tokensToSubscribe.forEach((token) => {
1320
1353
  const subscribeMessage = {
1321
- method: "subscribe",
1354
+ method: 'subscribe',
1322
1355
  subscription: {
1323
- type: "candle",
1356
+ type: 'candle',
1324
1357
  coin: token,
1325
1358
  interval: candleInterval,
1326
1359
  },
@@ -1423,6 +1456,11 @@ const useAccountSummary = () => {
1423
1456
  const aggregatedClearingHouseState = useHyperliquidData((state) => state.aggregatedClearingHouseState);
1424
1457
  const registeredAgentWallets = useUserData((state) => state.userExtraAgents);
1425
1458
  const isLoading = useMemo(() => {
1459
+ console.log('[USE ACCOUNT SUMMARY DEBUG] Loading check:', {
1460
+ platformAccountSummary,
1461
+ registeredAgentWallets,
1462
+ aggregatedClearingHouseState,
1463
+ });
1426
1464
  return !platformAccountSummary || !registeredAgentWallets || !aggregatedClearingHouseState;
1427
1465
  }, [platformAccountSummary, registeredAgentWallets, aggregatedClearingHouseState]);
1428
1466
  // Create calculator and compute account summary
@@ -7530,6 +7568,7 @@ function useAuth() {
7530
7568
  const setRefreshToken = useUserData((s) => s.setRefreshToken);
7531
7569
  const isAuthenticated = useUserData((s) => s.isAuthenticated);
7532
7570
  const setIsAuthenticated = useUserData((s) => s.setIsAuthenticated);
7571
+ const address = useUserData((s) => s.address);
7533
7572
  const setAddress = useUserData((s) => s.setAddress);
7534
7573
  useEffect(() => {
7535
7574
  if (typeof window == "undefined") {
@@ -7642,6 +7681,7 @@ function useAuth() {
7642
7681
  return {
7643
7682
  isReady,
7644
7683
  isAuthenticated,
7684
+ address,
7645
7685
  accessToken,
7646
7686
  refreshToken: refreshToken$1,
7647
7687
  getEip712,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",