@pear-protocol/hyperliquid-sdk 0.1.16 → 0.1.18
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.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +53 -8
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -593,7 +593,7 @@ interface HLChannelDataMap {
|
|
|
593
593
|
allDexsAssetCtxs: AllDexsAssetCtxsData;
|
|
594
594
|
userFills: any;
|
|
595
595
|
}
|
|
596
|
-
type UserAbstraction = 'dexAbstraction' | 'disabled' | 'unifiedAccount';
|
|
596
|
+
type UserAbstraction = 'dexAbstraction' | 'disabled' | 'unifiedAccount' | 'portfolioMargin';
|
|
597
597
|
interface WebData3UserState {
|
|
598
598
|
agentAddress?: string;
|
|
599
599
|
agentValidUntil?: number;
|
package/dist/index.js
CHANGED
|
@@ -639,9 +639,28 @@ const useHyperliquidData = create((set) => ({
|
|
|
639
639
|
perpMetasByDex: state.perpMetasByDex,
|
|
640
640
|
}),
|
|
641
641
|
})),
|
|
642
|
-
clearUserData: () => set({
|
|
643
|
-
|
|
644
|
-
|
|
642
|
+
clearUserData: () => set((state) => {
|
|
643
|
+
const refreshedMetadata = refreshTokenMetadata(state, {
|
|
644
|
+
activeAssetData: null,
|
|
645
|
+
});
|
|
646
|
+
// Remove user-scoped fields even when upstream metadata inputs are not ready.
|
|
647
|
+
const tokenMetadata = Object.fromEntries(Object.entries(refreshedMetadata).map(([symbol, metadata]) => [
|
|
648
|
+
symbol,
|
|
649
|
+
metadata
|
|
650
|
+
? {
|
|
651
|
+
...metadata,
|
|
652
|
+
leverage: undefined,
|
|
653
|
+
maxTradeSzs: undefined,
|
|
654
|
+
availableToTrade: undefined,
|
|
655
|
+
}
|
|
656
|
+
: null,
|
|
657
|
+
]));
|
|
658
|
+
return {
|
|
659
|
+
aggregatedClearingHouseState: null,
|
|
660
|
+
rawClearinghouseStates: null,
|
|
661
|
+
activeAssetData: null,
|
|
662
|
+
tokenMetadata,
|
|
663
|
+
};
|
|
645
664
|
}),
|
|
646
665
|
}));
|
|
647
666
|
|
|
@@ -918,6 +937,7 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
|
|
|
918
937
|
const [subscribedCandleTokens, setSubscribedCandleTokens] = useState([]);
|
|
919
938
|
const [clearinghouseStateReceived, setClearinghouseStateReceived] = useState(false);
|
|
920
939
|
const prevCandleIntervalRef = useRef(null);
|
|
940
|
+
const prevActiveAssetAddressRef = useRef(null);
|
|
921
941
|
const pingIntervalRef = useRef(null);
|
|
922
942
|
const wsRef = useRef(null);
|
|
923
943
|
const reconnectAttemptsRef = useRef(0);
|
|
@@ -1344,12 +1364,37 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
|
|
|
1344
1364
|
userSummary,
|
|
1345
1365
|
sendJsonMessage,
|
|
1346
1366
|
]);
|
|
1367
|
+
// activeAssetData is user-scoped; reset subscriptions/data whenever address changes.
|
|
1368
|
+
useEffect(() => {
|
|
1369
|
+
if (!isConnected)
|
|
1370
|
+
return;
|
|
1371
|
+
const previousAddress = prevActiveAssetAddressRef.current;
|
|
1372
|
+
if (previousAddress === address)
|
|
1373
|
+
return;
|
|
1374
|
+
if (previousAddress) {
|
|
1375
|
+
subscribedTokens.forEach((token) => {
|
|
1376
|
+
const unsubscribeMessage = {
|
|
1377
|
+
method: "unsubscribe",
|
|
1378
|
+
subscription: {
|
|
1379
|
+
type: "activeAssetData",
|
|
1380
|
+
user: previousAddress,
|
|
1381
|
+
coin: token,
|
|
1382
|
+
},
|
|
1383
|
+
};
|
|
1384
|
+
sendJsonMessage(unsubscribeMessage);
|
|
1385
|
+
});
|
|
1386
|
+
}
|
|
1387
|
+
// Clear user-derived leverage/available-to-trade data before re-subscribing.
|
|
1388
|
+
setActiveAssetData(null);
|
|
1389
|
+
setSubscribedTokens([]);
|
|
1390
|
+
prevActiveAssetAddressRef.current = address;
|
|
1391
|
+
}, [isConnected, address, subscribedTokens, sendJsonMessage, setActiveAssetData]);
|
|
1347
1392
|
// Handle token subscriptions for activeAssetData
|
|
1348
1393
|
useEffect(() => {
|
|
1349
1394
|
if (!isConnected || !address)
|
|
1350
1395
|
return;
|
|
1351
|
-
const effectiveTokens = selectedTokenSymbols;
|
|
1352
|
-
const tokensToSubscribe = effectiveTokens.filter((token) =>
|
|
1396
|
+
const effectiveTokens = selectedTokenSymbols.filter((token) => token);
|
|
1397
|
+
const tokensToSubscribe = effectiveTokens.filter((token) => !subscribedTokens.includes(token));
|
|
1353
1398
|
const tokensToUnsubscribe = subscribedTokens.filter((token) => !effectiveTokens.includes(token));
|
|
1354
1399
|
// Unsubscribe from tokens no longer in the list
|
|
1355
1400
|
tokensToUnsubscribe.forEach((token) => {
|
|
@@ -1376,7 +1421,7 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
|
|
|
1376
1421
|
sendJsonMessage(subscribeMessage);
|
|
1377
1422
|
});
|
|
1378
1423
|
if (tokensToSubscribe.length > 0 || tokensToUnsubscribe.length > 0) {
|
|
1379
|
-
setSubscribedTokens(effectiveTokens
|
|
1424
|
+
setSubscribedTokens(effectiveTokens);
|
|
1380
1425
|
tokensToSubscribe.forEach((token) => deleteActiveAssetData(token));
|
|
1381
1426
|
}
|
|
1382
1427
|
}, [
|
|
@@ -1385,7 +1430,7 @@ const useHyperliquidNativeWebSocket = ({ address, enabled = true, onUserFills, }
|
|
|
1385
1430
|
selectedTokenSymbols,
|
|
1386
1431
|
subscribedTokens,
|
|
1387
1432
|
sendJsonMessage,
|
|
1388
|
-
|
|
1433
|
+
deleteActiveAssetData,
|
|
1389
1434
|
]);
|
|
1390
1435
|
// Handle candle subscriptions for tokens and interval changes
|
|
1391
1436
|
useEffect(() => {
|
|
@@ -7100,7 +7145,7 @@ const buildPositionValue = (rawPositions, clearinghouseState, getAssetByName) =>
|
|
|
7100
7145
|
mappedPosition.marginUsed += mappedPositionAssets.marginUsed;
|
|
7101
7146
|
entryMarginUsed += mappedPositionAssets.entryMarginUsed;
|
|
7102
7147
|
mappedPosition.entryRatio *= Math.pow(longAsset.entryPrice, mappedPositionAssets.initialWeight);
|
|
7103
|
-
mappedPosition.markRatio *= Math.pow(currentPrice, mappedPositionAssets.
|
|
7148
|
+
mappedPosition.markRatio *= Math.pow(currentPrice, mappedPositionAssets.initialWeight);
|
|
7104
7149
|
return mappedPositionAssets;
|
|
7105
7150
|
});
|
|
7106
7151
|
mappedPosition.shortAssets = position.shortAssets.map((shortAsset) => {
|
package/dist/types.d.ts
CHANGED
|
@@ -565,7 +565,7 @@ export interface HLChannelDataMap {
|
|
|
565
565
|
allDexsAssetCtxs: AllDexsAssetCtxsData;
|
|
566
566
|
userFills: any;
|
|
567
567
|
}
|
|
568
|
-
export type UserAbstraction = 'dexAbstraction' | 'disabled' | 'unifiedAccount';
|
|
568
|
+
export type UserAbstraction = 'dexAbstraction' | 'disabled' | 'unifiedAccount' | 'portfolioMargin';
|
|
569
569
|
export interface WebData3UserState {
|
|
570
570
|
agentAddress?: string;
|
|
571
571
|
agentValidUntil?: number;
|