@pear-protocol/symmio-client 0.1.6 → 0.1.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.
- package/dist/react/index.d.mts +2 -1
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +87 -62
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +87 -62
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/react/index.mjs
CHANGED
|
@@ -24403,8 +24403,8 @@ function SymmProvider({
|
|
|
24403
24403
|
setAccessToken(null);
|
|
24404
24404
|
return;
|
|
24405
24405
|
}
|
|
24406
|
-
setAccessToken(null);
|
|
24407
24406
|
if (previousAddress && (addressChanged || chainChanged)) {
|
|
24407
|
+
setAccessToken(null);
|
|
24408
24408
|
clearCachedToken(previousAddress, previousChainId);
|
|
24409
24409
|
}
|
|
24410
24410
|
}, [address, walletClient, chainId]);
|
|
@@ -24487,7 +24487,8 @@ var symmKeys = {
|
|
|
24487
24487
|
notifications: (address, chainId) => ["symm", "notifications", address, chainId],
|
|
24488
24488
|
unreadCount: (address, chainId) => ["symm", "unreadCount", address, chainId],
|
|
24489
24489
|
availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
|
|
24490
|
-
delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId]
|
|
24490
|
+
delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
|
|
24491
|
+
chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
|
|
24491
24492
|
};
|
|
24492
24493
|
|
|
24493
24494
|
// src/react/hooks/use-symm-delegation.ts
|
|
@@ -24634,6 +24635,7 @@ function useSymmInstantTrade(params) {
|
|
|
24634
24635
|
execute
|
|
24635
24636
|
};
|
|
24636
24637
|
}
|
|
24638
|
+
var EMPTY_ACCOUNTS = [];
|
|
24637
24639
|
function useSymmAccounts(userAddress) {
|
|
24638
24640
|
const { symmioClient } = useSymmContext();
|
|
24639
24641
|
const queryClient = useQueryClient();
|
|
@@ -24664,7 +24666,7 @@ function useSymmAccounts(userAddress) {
|
|
|
24664
24666
|
}
|
|
24665
24667
|
});
|
|
24666
24668
|
return {
|
|
24667
|
-
accounts: accountsQuery.data ??
|
|
24669
|
+
accounts: accountsQuery.data ?? EMPTY_ACCOUNTS,
|
|
24668
24670
|
count: accountsQuery.data?.length ?? 0,
|
|
24669
24671
|
isLoading: accountsQuery.isLoading,
|
|
24670
24672
|
error: accountsQuery.error,
|
|
@@ -24679,21 +24681,23 @@ function useSymmApproval(params) {
|
|
|
24679
24681
|
const { owner, amount, spender, collateralToken } = params;
|
|
24680
24682
|
const resolvedSpender = spender ?? symmioClient?.addresses.multiAccount;
|
|
24681
24683
|
const resolvedToken = collateralToken ?? symmioClient?.addresses.collateral;
|
|
24684
|
+
const selectWithAmount = useCallback(
|
|
24685
|
+
(data) => ({
|
|
24686
|
+
...data,
|
|
24687
|
+
state: data.allowance >= amount ? "APPROVED" /* APPROVED */ : "NOT_APPROVED" /* NOT_APPROVED */
|
|
24688
|
+
}),
|
|
24689
|
+
[amount]
|
|
24690
|
+
);
|
|
24682
24691
|
const approvalQuery = useQuery({
|
|
24683
24692
|
queryKey: symmKeys.approval(owner, resolvedSpender),
|
|
24684
24693
|
queryFn: async () => {
|
|
24685
|
-
const [
|
|
24686
|
-
symmioClient.approval.getState(
|
|
24687
|
-
resolvedToken,
|
|
24688
|
-
owner,
|
|
24689
|
-
resolvedSpender,
|
|
24690
|
-
amount
|
|
24691
|
-
),
|
|
24694
|
+
const [allowance, balance] = await Promise.all([
|
|
24692
24695
|
symmioClient.approval.getAllowance(resolvedToken, owner, resolvedSpender),
|
|
24693
24696
|
symmioClient.approval.getBalance(resolvedToken, owner)
|
|
24694
24697
|
]);
|
|
24695
|
-
return {
|
|
24698
|
+
return { allowance, balance };
|
|
24696
24699
|
},
|
|
24700
|
+
select: selectWithAmount,
|
|
24697
24701
|
enabled: !!symmioClient && !!owner && !!resolvedSpender && !!resolvedToken
|
|
24698
24702
|
});
|
|
24699
24703
|
const approve2 = useMutation({
|
|
@@ -24837,18 +24841,19 @@ function useSymmSignature(userAddress) {
|
|
|
24837
24841
|
function useSymmAvailableMargin(params) {
|
|
24838
24842
|
const { symmioClient } = useSymmContext();
|
|
24839
24843
|
const { accountAddress, upnl } = params;
|
|
24844
|
+
const resolvedUpnl = upnl ?? 0n;
|
|
24845
|
+
const selectWithUpnl = useCallback(
|
|
24846
|
+
(stats) => ({
|
|
24847
|
+
...stats,
|
|
24848
|
+
upnl: resolvedUpnl,
|
|
24849
|
+
availableForOrder: calculateAvailableForOrder(stats, resolvedUpnl)
|
|
24850
|
+
}),
|
|
24851
|
+
[resolvedUpnl]
|
|
24852
|
+
);
|
|
24840
24853
|
const query = useQuery({
|
|
24841
24854
|
queryKey: symmKeys.availableMargin(accountAddress, symmioClient?.chainId),
|
|
24842
|
-
queryFn:
|
|
24843
|
-
|
|
24844
|
-
const resolvedUpnl = upnl ?? 0n;
|
|
24845
|
-
const availableForOrder = calculateAvailableForOrder(stats, resolvedUpnl);
|
|
24846
|
-
return {
|
|
24847
|
-
...stats,
|
|
24848
|
-
upnl: resolvedUpnl,
|
|
24849
|
-
availableForOrder
|
|
24850
|
-
};
|
|
24851
|
-
},
|
|
24855
|
+
queryFn: () => symmioClient.stats.getPartyAStats(accountAddress),
|
|
24856
|
+
select: selectWithUpnl,
|
|
24852
24857
|
enabled: !!symmioClient && !!accountAddress,
|
|
24853
24858
|
staleTime: 1e4
|
|
24854
24859
|
});
|
|
@@ -25068,37 +25073,37 @@ function useSymmMarkets(params) {
|
|
|
25068
25073
|
refetch: query.refetch
|
|
25069
25074
|
};
|
|
25070
25075
|
}
|
|
25076
|
+
var EMPTY_MARKETS = [];
|
|
25077
|
+
var EMPTY_SYMBOLS = [];
|
|
25078
|
+
var EMPTY_MAP_BY_ID = /* @__PURE__ */ new Map();
|
|
25079
|
+
var EMPTY_MAP_BY_SYMBOL = /* @__PURE__ */ new Map();
|
|
25071
25080
|
function useSymmHedgerMarkets(params) {
|
|
25072
25081
|
const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
|
|
25073
25082
|
const chainId = params?.chainId ?? ctxChainId;
|
|
25074
25083
|
const searchText = params?.searchText?.trim();
|
|
25075
25084
|
const isEnabled = params?.enabled ?? true;
|
|
25085
|
+
const { enabled: _, ...restParams } = params ?? {};
|
|
25076
25086
|
const request = {
|
|
25077
|
-
...
|
|
25087
|
+
...restParams,
|
|
25078
25088
|
chainId,
|
|
25079
25089
|
searchText: searchText || void 0
|
|
25080
25090
|
};
|
|
25081
25091
|
const query = useQuery({
|
|
25082
25092
|
queryKey: symmKeys.hedgerMarkets(request),
|
|
25083
|
-
queryFn:
|
|
25084
|
-
const { enabled: _enabled, ...queryRequest } = request;
|
|
25085
|
-
return symmCoreClient.markets.listSymmHedger(queryRequest);
|
|
25086
|
-
},
|
|
25093
|
+
queryFn: () => symmCoreClient.markets.listSymmHedger(request),
|
|
25087
25094
|
enabled: !!symmCoreClient && isEnabled,
|
|
25088
25095
|
staleTime: 3e4
|
|
25089
25096
|
});
|
|
25090
25097
|
const data = query.data ?? null;
|
|
25091
|
-
const emptyMap = /* @__PURE__ */ new Map();
|
|
25092
|
-
const emptySymbolMap = /* @__PURE__ */ new Map();
|
|
25093
25098
|
return {
|
|
25094
25099
|
data,
|
|
25095
|
-
markets: data?.markets ??
|
|
25096
|
-
rawMarkets: data?.rawMarkets ??
|
|
25097
|
-
filteredMarkets: data?.filteredMarkets ??
|
|
25098
|
-
allSymbols: data?.allSymbols ??
|
|
25099
|
-
filteredSymbols: data?.filteredSymbols ??
|
|
25100
|
-
marketsById: data?.marketsById ??
|
|
25101
|
-
marketsBySymbol: data?.marketsBySymbol ??
|
|
25100
|
+
markets: data?.markets ?? EMPTY_MARKETS,
|
|
25101
|
+
rawMarkets: data?.rawMarkets ?? EMPTY_MARKETS,
|
|
25102
|
+
filteredMarkets: data?.filteredMarkets ?? EMPTY_MARKETS,
|
|
25103
|
+
allSymbols: data?.allSymbols ?? EMPTY_SYMBOLS,
|
|
25104
|
+
filteredSymbols: data?.filteredSymbols ?? EMPTY_SYMBOLS,
|
|
25105
|
+
marketsById: data?.marketsById ?? EMPTY_MAP_BY_ID,
|
|
25106
|
+
marketsBySymbol: data?.marketsBySymbol ?? EMPTY_MAP_BY_SYMBOL,
|
|
25102
25107
|
category: data?.category ?? params?.category ?? "all",
|
|
25103
25108
|
resolvedSearchText: data?.searchText ?? searchText ?? "",
|
|
25104
25109
|
isLoading: query.isLoading,
|
|
@@ -25190,13 +25195,15 @@ function useSymmNotifications(params) {
|
|
|
25190
25195
|
refetch: notificationsQuery.refetch
|
|
25191
25196
|
};
|
|
25192
25197
|
}
|
|
25198
|
+
function asUnsubscribeFn(value) {
|
|
25199
|
+
return typeof value === "function" ? value : null;
|
|
25200
|
+
}
|
|
25193
25201
|
function useSymmWs(params) {
|
|
25194
25202
|
const { symmCoreClient, chainId: ctxChainId } = useSymmContext();
|
|
25195
25203
|
const queryClient = useQueryClient();
|
|
25196
25204
|
const { accountAddress } = params;
|
|
25197
25205
|
const chainId = params.chainId ?? ctxChainId;
|
|
25198
25206
|
const [isConnected, setIsConnected] = useState(false);
|
|
25199
|
-
const cleanupRef = useRef([]);
|
|
25200
25207
|
useEffect(() => {
|
|
25201
25208
|
if (!symmCoreClient || !accountAddress) {
|
|
25202
25209
|
setIsConnected(false);
|
|
@@ -25204,61 +25211,75 @@ function useSymmWs(params) {
|
|
|
25204
25211
|
}
|
|
25205
25212
|
const ws = symmCoreClient.ws;
|
|
25206
25213
|
const addr = accountAddress;
|
|
25214
|
+
const unsubscribers = [];
|
|
25207
25215
|
const removeOnConnect = ws.onConnect(() => setIsConnected(true));
|
|
25208
25216
|
const removeOnDisconnect = ws.onDisconnect(() => setIsConnected(false));
|
|
25209
|
-
|
|
25217
|
+
unsubscribers.push(removeOnConnect, removeOnDisconnect);
|
|
25218
|
+
const positionsUnsub = asUnsubscribeFn(ws.subscribeToPositions(addr, chainId, () => {
|
|
25210
25219
|
queryClient.invalidateQueries({
|
|
25211
25220
|
queryKey: symmKeys.positions(accountAddress, chainId)
|
|
25212
25221
|
});
|
|
25213
|
-
});
|
|
25214
|
-
|
|
25222
|
+
}));
|
|
25223
|
+
if (positionsUnsub) unsubscribers.push(positionsUnsub);
|
|
25224
|
+
const openOrdersUnsub = asUnsubscribeFn(ws.subscribeToOpenOrders(addr, chainId, () => {
|
|
25215
25225
|
queryClient.invalidateQueries({
|
|
25216
25226
|
queryKey: symmKeys.openOrders(accountAddress, chainId)
|
|
25217
25227
|
});
|
|
25218
|
-
});
|
|
25219
|
-
|
|
25228
|
+
}));
|
|
25229
|
+
if (openOrdersUnsub) unsubscribers.push(openOrdersUnsub);
|
|
25230
|
+
const tradesUnsub = asUnsubscribeFn(ws.subscribeToTrades(addr, chainId, () => {
|
|
25220
25231
|
queryClient.invalidateQueries({
|
|
25221
25232
|
queryKey: symmKeys.tradeHistory(accountAddress, chainId)
|
|
25222
25233
|
});
|
|
25223
|
-
});
|
|
25224
|
-
|
|
25234
|
+
}));
|
|
25235
|
+
if (tradesUnsub) unsubscribers.push(tradesUnsub);
|
|
25236
|
+
const accountSummaryUnsub = asUnsubscribeFn(ws.subscribeToAccountSummary(addr, chainId, () => {
|
|
25225
25237
|
queryClient.invalidateQueries({
|
|
25226
25238
|
queryKey: symmKeys.balances(accountAddress, chainId)
|
|
25227
25239
|
});
|
|
25228
25240
|
queryClient.invalidateQueries({
|
|
25229
25241
|
queryKey: symmKeys.accountSummary(accountAddress, chainId)
|
|
25230
25242
|
});
|
|
25231
|
-
});
|
|
25232
|
-
|
|
25243
|
+
}));
|
|
25244
|
+
if (accountSummaryUnsub) unsubscribers.push(accountSummaryUnsub);
|
|
25245
|
+
const notificationsUnsub = asUnsubscribeFn(ws.subscribeToNotifications(addr, chainId, () => {
|
|
25233
25246
|
queryClient.invalidateQueries({
|
|
25234
25247
|
queryKey: symmKeys.notifications(accountAddress, chainId)
|
|
25235
25248
|
});
|
|
25236
25249
|
queryClient.invalidateQueries({
|
|
25237
25250
|
queryKey: symmKeys.unreadCount(accountAddress, chainId)
|
|
25238
25251
|
});
|
|
25239
|
-
});
|
|
25240
|
-
|
|
25252
|
+
}));
|
|
25253
|
+
if (notificationsUnsub) unsubscribers.push(notificationsUnsub);
|
|
25254
|
+
const tpslUnsub = asUnsubscribeFn(ws.subscribeToTpsl(addr, chainId, () => {
|
|
25241
25255
|
queryClient.invalidateQueries({
|
|
25242
25256
|
queryKey: symmKeys.tpslOrders(accountAddress, chainId)
|
|
25243
25257
|
});
|
|
25244
|
-
});
|
|
25245
|
-
|
|
25258
|
+
}));
|
|
25259
|
+
if (tpslUnsub) unsubscribers.push(tpslUnsub);
|
|
25260
|
+
const twapUnsub = asUnsubscribeFn(ws.subscribeToTwapOrders(addr, chainId, () => {
|
|
25246
25261
|
queryClient.invalidateQueries({
|
|
25247
25262
|
queryKey: symmKeys.twapOrders(accountAddress, chainId)
|
|
25248
25263
|
});
|
|
25249
|
-
});
|
|
25250
|
-
|
|
25264
|
+
}));
|
|
25265
|
+
if (twapUnsub) unsubscribers.push(twapUnsub);
|
|
25266
|
+
const executionsUnsub = asUnsubscribeFn(ws.subscribeToExecutions(addr, chainId, () => {
|
|
25251
25267
|
queryClient.invalidateQueries({
|
|
25252
25268
|
queryKey: symmKeys.positions(accountAddress, chainId)
|
|
25253
25269
|
});
|
|
25254
25270
|
queryClient.invalidateQueries({
|
|
25255
25271
|
queryKey: symmKeys.portfolio(accountAddress, chainId)
|
|
25256
25272
|
});
|
|
25257
|
-
});
|
|
25258
|
-
|
|
25273
|
+
}));
|
|
25274
|
+
if (executionsUnsub) unsubscribers.push(executionsUnsub);
|
|
25259
25275
|
return () => {
|
|
25260
|
-
|
|
25261
|
-
|
|
25276
|
+
if (unsubscribers.length > 2) {
|
|
25277
|
+
unsubscribers.forEach((unsubscribe) => unsubscribe());
|
|
25278
|
+
} else {
|
|
25279
|
+
removeOnConnect();
|
|
25280
|
+
removeOnDisconnect();
|
|
25281
|
+
ws.unsubscribeAll();
|
|
25282
|
+
}
|
|
25262
25283
|
};
|
|
25263
25284
|
}, [symmCoreClient, accountAddress, chainId, queryClient]);
|
|
25264
25285
|
return { isConnected };
|
|
@@ -25585,8 +25606,12 @@ function useSymmTokenSelectionMetadata(selection) {
|
|
|
25585
25606
|
const isUnsupported = unsupportedSymbols.length > 0;
|
|
25586
25607
|
const unavailableReason = isUnsupported ? `Binance market data is unavailable for ${unsupportedSymbols.join(", ")}.` : null;
|
|
25587
25608
|
const symbolsKey = [...selectedSymbols].sort().join(",");
|
|
25609
|
+
const positionKey = [
|
|
25610
|
+
longTokens.map((token) => `${token.symbol}:${token.weight}`).join("|"),
|
|
25611
|
+
shortTokens.map((token) => `${token.symbol}:${token.weight}`).join("|")
|
|
25612
|
+
].join("::");
|
|
25588
25613
|
const query = useQuery({
|
|
25589
|
-
queryKey:
|
|
25614
|
+
queryKey: symmKeys.chartMetadata(symbolsKey, positionKey),
|
|
25590
25615
|
queryFn: async () => {
|
|
25591
25616
|
const allSymbols = [
|
|
25592
25617
|
...longTokens.map((t) => ({ symbol: t.symbol, side: "long" })),
|
|
@@ -26021,7 +26046,6 @@ function useSymmChartCandles(selection) {
|
|
|
26021
26046
|
low: longValues.l * shortValues.l,
|
|
26022
26047
|
close: longValues.c * shortValues.c
|
|
26023
26048
|
};
|
|
26024
|
-
if (!bar) return;
|
|
26025
26049
|
listenersRef.current.forEach((cb) => {
|
|
26026
26050
|
try {
|
|
26027
26051
|
cb(bar);
|
|
@@ -26046,11 +26070,14 @@ function useSymmChartCandles(selection) {
|
|
|
26046
26070
|
}
|
|
26047
26071
|
}, [emitRealtimeBar, isUnsupported, selectedSymbols]);
|
|
26048
26072
|
useEffect(() => {
|
|
26073
|
+
if (listenersRef.current.size > 0) {
|
|
26074
|
+
setupWsSubscriptions();
|
|
26075
|
+
}
|
|
26049
26076
|
return () => {
|
|
26050
26077
|
wsUnsubsRef.current.forEach((unsub) => unsub());
|
|
26051
26078
|
wsUnsubsRef.current = [];
|
|
26052
26079
|
};
|
|
26053
|
-
}, [
|
|
26080
|
+
}, [setupWsSubscriptions]);
|
|
26054
26081
|
const fetchBasketCandles = useCallback(
|
|
26055
26082
|
async (start, end, interval) => {
|
|
26056
26083
|
const longSymbol = longTokens[0]?.symbol;
|
|
@@ -26216,10 +26243,8 @@ function useSymmPerformanceOverlays(selection) {
|
|
|
26216
26243
|
weight: -token.weight
|
|
26217
26244
|
}))
|
|
26218
26245
|
], [longTokens, shortTokens]);
|
|
26219
|
-
const
|
|
26220
|
-
|
|
26221
|
-
}, [overlays]);
|
|
26222
|
-
return { overlays, generateOverlaySymbols };
|
|
26246
|
+
const overlaySymbols = useMemo(() => overlays.map((o) => o.symbol), [overlays]);
|
|
26247
|
+
return { overlays, overlaySymbols };
|
|
26223
26248
|
}
|
|
26224
26249
|
function getSymmErrorMessage(error) {
|
|
26225
26250
|
if (error instanceof SymmioSDKError) return error.message;
|