@pear-protocol/symmio-client 0.2.10 → 0.2.12

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.
@@ -1,9 +1,9 @@
1
1
  'use client';
2
- import { createContext, useContext, useEffect, useMemo, useRef, useCallback } from 'react';
2
+ import { createContext, useContext, useMemo, useRef, useCallback, useEffect } from 'react';
3
3
  import { createSymmSDK, isAuthExpiredError, isNetworkError, isInsufficientMarginError, isRateLimitedError, isTimeoutError } from '@pear-protocol/symm-core';
4
- import { useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
5
- import { create } from 'zustand';
6
4
  import { jsx } from 'react/jsx-runtime';
5
+ import { create } from 'zustand';
6
+ import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query';
7
7
  import { isAddress, encodeFunctionData } from 'viem';
8
8
 
9
9
  var SymmContext = createContext(null);
@@ -14,145 +14,6 @@ function useSymmContext() {
14
14
  }
15
15
  return ctx;
16
16
  }
17
-
18
- // src/react/query-keys.ts
19
- var symmKeys = {
20
- all: ["symm"],
21
- accounts: (address, chainId) => ["symm", "accounts", address, chainId],
22
- accountsApi: (address, chainId) => ["symm", "accountsApi", address, chainId],
23
- accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
24
- accountsWithPositions: (address, chainId) => ["symm", "accountsWithPositions", address, chainId],
25
- accountSummary: (address, chainId) => ["symm", "accountSummary", address, chainId],
26
- accountData: (address, chainId, upnl) => ["symm", "accountData", address, chainId, upnl],
27
- signature: (address, chainId) => ["symm", "signature", address, chainId],
28
- approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
29
- balances: (address, chainId) => ["symm", "balances", address, chainId],
30
- positions: (params) => ["symm", "positions", params],
31
- openOrders: (params) => ["symm", "openOrders", params],
32
- tradeHistory: (params) => ["symm", "tradeHistory", params],
33
- tpslOrders: (address, chainId) => ["symm", "tpslOrders", address, chainId],
34
- tpslOrdersList: (params) => ["symm", "tpslOrders", params],
35
- twapOrders: (address, chainId) => ["symm", "twapOrders", address, chainId],
36
- triggerOrders: (params) => ["symm", "triggerOrders", params],
37
- triggerConfig: (orderId) => ["symm", "triggerConfig", orderId],
38
- markets: (chainId, search) => ["symm", "markets", chainId, search],
39
- hedgerMarketById: (id, chainId) => ["symm", "hedgerMarketById", id, chainId],
40
- hedgerMarketBySymbol: (symbol, chainId) => ["symm", "hedgerMarketBySymbol", symbol, chainId],
41
- lockedParams: (marketName, leverage, chainId) => ["symm", "lockedParams", marketName, leverage, chainId],
42
- hedgerMarkets: (request) => ["symm", "hedgerMarkets", request],
43
- fundingRates: (chainId) => ["symm", "fundingRates", chainId],
44
- fundingPayments: (params) => ["symm", "fundingPayments", params],
45
- fundingHistory: (params) => ["symm", "fundingHistory", params],
46
- portfolio: (params) => ["symm", "portfolio", params],
47
- notifications: (address, chainId) => ["symm", "notifications", address, chainId],
48
- unreadCount: (address, chainId) => ["symm", "unreadCount", address, chainId],
49
- availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
50
- pendingIds: (address, chainId) => ["symm", "pendingIds", address, chainId],
51
- pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
52
- twapOrder: (orderId) => ["symm", "twapOrder", orderId],
53
- delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
54
- chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
55
- };
56
- var useSymmWsStore = create((set) => ({
57
- isConnected: false,
58
- setConnected: (isConnected) => set({ isConnected })
59
- }));
60
-
61
- // src/react/hooks/use-symm-ws.ts
62
- function asUnsubscribeFn(value) {
63
- return typeof value === "function" ? value : null;
64
- }
65
- function useSymmWs(params = {}) {
66
- const { symmCoreClient: ctxClient, address: ctxAddress, chainId: ctxChainId } = useSymmContext();
67
- const queryClient = useQueryClient();
68
- const isConnected = useSymmWsStore((state) => state.isConnected);
69
- const setConnected = useSymmWsStore((state) => state.setConnected);
70
- const symmCoreClient = params.symmCoreClient ?? ctxClient;
71
- const accountAddress = params.accountAddress ?? ctxAddress;
72
- const chainId = params.chainId ?? ctxChainId;
73
- useEffect(() => {
74
- if (!symmCoreClient || !accountAddress) {
75
- setConnected(false);
76
- return;
77
- }
78
- const ws = symmCoreClient.ws;
79
- const addr = accountAddress;
80
- const unsubscribers = [];
81
- const removeOnConnect = ws.onConnect(() => setConnected(true));
82
- const removeOnDisconnect = ws.onDisconnect(() => setConnected(false));
83
- unsubscribers.push(removeOnConnect, removeOnDisconnect);
84
- const positionsUnsub = asUnsubscribeFn(ws.subscribeToPositions(addr, chainId, () => {
85
- queryClient.invalidateQueries({
86
- queryKey: ["symm", "positions"]
87
- });
88
- }));
89
- if (positionsUnsub) unsubscribers.push(positionsUnsub);
90
- const openOrdersUnsub = asUnsubscribeFn(ws.subscribeToOpenOrders(addr, chainId, () => {
91
- queryClient.invalidateQueries({
92
- queryKey: ["symm", "openOrders"]
93
- });
94
- }));
95
- if (openOrdersUnsub) unsubscribers.push(openOrdersUnsub);
96
- const tradesUnsub = asUnsubscribeFn(ws.subscribeToTrades(addr, chainId, () => {
97
- queryClient.invalidateQueries({
98
- queryKey: ["symm", "tradeHistory"]
99
- });
100
- }));
101
- if (tradesUnsub) unsubscribers.push(tradesUnsub);
102
- const accountSummaryUnsub = asUnsubscribeFn(ws.subscribeToAccountSummary(addr, chainId, () => {
103
- queryClient.invalidateQueries({
104
- queryKey: symmKeys.balances(accountAddress, chainId)
105
- });
106
- queryClient.invalidateQueries({
107
- queryKey: symmKeys.accountSummary(accountAddress, chainId)
108
- });
109
- }));
110
- if (accountSummaryUnsub) unsubscribers.push(accountSummaryUnsub);
111
- const notificationsUnsub = asUnsubscribeFn(ws.subscribeToNotifications(addr, chainId, () => {
112
- queryClient.invalidateQueries({
113
- queryKey: symmKeys.notifications(accountAddress, chainId)
114
- });
115
- queryClient.invalidateQueries({
116
- queryKey: symmKeys.unreadCount(accountAddress, chainId)
117
- });
118
- }));
119
- if (notificationsUnsub) unsubscribers.push(notificationsUnsub);
120
- const tpslUnsub = asUnsubscribeFn(ws.subscribeToTpsl(addr, chainId, () => {
121
- queryClient.invalidateQueries({ queryKey: ["symm", "tpslOrders"] });
122
- queryClient.invalidateQueries({ queryKey: ["symm", "openOrders"] });
123
- }));
124
- if (tpslUnsub) unsubscribers.push(tpslUnsub);
125
- const twapUnsub = asUnsubscribeFn(ws.subscribeToTwapOrders(addr, chainId, () => {
126
- queryClient.invalidateQueries({ queryKey: ["symm", "twapOrders"] });
127
- queryClient.invalidateQueries({ queryKey: ["symm", "openOrders"] });
128
- }));
129
- if (twapUnsub) unsubscribers.push(twapUnsub);
130
- const triggerOrdersUnsub = asUnsubscribeFn(ws.subscribeToTriggerOrders(addr, chainId, () => {
131
- queryClient.invalidateQueries({ queryKey: ["symm", "triggerOrders"] });
132
- queryClient.invalidateQueries({ queryKey: ["symm", "openOrders"] });
133
- }));
134
- if (triggerOrdersUnsub) unsubscribers.push(triggerOrdersUnsub);
135
- const executionsUnsub = asUnsubscribeFn(ws.subscribeToExecutions(addr, chainId, () => {
136
- queryClient.invalidateQueries({
137
- queryKey: ["symm", "positions"]
138
- });
139
- queryClient.invalidateQueries({
140
- queryKey: ["symm", "portfolio"]
141
- });
142
- }));
143
- if (executionsUnsub) unsubscribers.push(executionsUnsub);
144
- return () => {
145
- if (unsubscribers.length > 2) {
146
- unsubscribers.forEach((unsubscribe) => unsubscribe());
147
- } else {
148
- removeOnConnect();
149
- removeOnDisconnect();
150
- ws.unsubscribeAll();
151
- }
152
- };
153
- }, [symmCoreClient, accountAddress, chainId, queryClient, setConnected]);
154
- return { isConnected };
155
- }
156
17
  function SymmProvider({
157
18
  chainId = 42161,
158
19
  address,
@@ -167,7 +28,6 @@ function SymmProvider({
167
28
  defaultChainId: chainId
168
29
  });
169
30
  }, [chainId, symmCoreConfig.apiUrl, symmCoreConfig.wsUrl]);
170
- useSymmWs({ symmCoreClient, accountAddress: address, chainId });
171
31
  const value = useMemo(
172
32
  () => ({
173
33
  symmCoreClient,
@@ -987,6 +847,45 @@ async function hasDelegatedAccess(publicClient, multiAccount, params) {
987
847
  return Boolean(result);
988
848
  }
989
849
 
850
+ // src/react/query-keys.ts
851
+ var symmKeys = {
852
+ all: ["symm"],
853
+ accounts: (address, chainId) => ["symm", "accounts", address, chainId],
854
+ accountsApi: (address, chainId) => ["symm", "accountsApi", address, chainId],
855
+ accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
856
+ accountsWithPositions: (address, chainId) => ["symm", "accountsWithPositions", address, chainId],
857
+ accountSummary: (address, chainId) => ["symm", "accountSummary", address, chainId],
858
+ accountData: (address, chainId, upnl) => ["symm", "accountData", address, chainId, upnl],
859
+ signature: (address, chainId) => ["symm", "signature", address, chainId],
860
+ approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
861
+ balances: (address, chainId) => ["symm", "balances", address, chainId],
862
+ positions: (params) => ["symm", "positions", params],
863
+ openOrders: (params) => ["symm", "openOrders", params],
864
+ tradeHistory: (params) => ["symm", "tradeHistory", params],
865
+ tpslOrders: (address, chainId) => ["symm", "tpslOrders", address, chainId],
866
+ tpslOrdersList: (params) => ["symm", "tpslOrders", params],
867
+ twapOrders: (address, chainId) => ["symm", "twapOrders", address, chainId],
868
+ triggerOrders: (params) => ["symm", "triggerOrders", params],
869
+ triggerConfig: (orderId) => ["symm", "triggerConfig", orderId],
870
+ markets: (chainId, search) => ["symm", "markets", chainId, search],
871
+ hedgerMarketById: (id, chainId) => ["symm", "hedgerMarketById", id, chainId],
872
+ hedgerMarketBySymbol: (symbol, chainId) => ["symm", "hedgerMarketBySymbol", symbol, chainId],
873
+ lockedParams: (marketName, leverage, chainId) => ["symm", "lockedParams", marketName, leverage, chainId],
874
+ hedgerMarkets: (request) => ["symm", "hedgerMarkets", request],
875
+ fundingRates: (chainId) => ["symm", "fundingRates", chainId],
876
+ fundingPayments: (params) => ["symm", "fundingPayments", params],
877
+ fundingHistory: (params) => ["symm", "fundingHistory", params],
878
+ portfolio: (params) => ["symm", "portfolio", params],
879
+ notifications: (address, chainId) => ["symm", "notifications", address, chainId],
880
+ unreadCount: (address, chainId) => ["symm", "unreadCount", address, chainId],
881
+ availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
882
+ pendingIds: (address, chainId) => ["symm", "pendingIds", address, chainId],
883
+ pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
884
+ twapOrder: (orderId) => ["symm", "twapOrder", orderId],
885
+ delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
886
+ chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
887
+ };
888
+
990
889
  // src/react/hooks/use-symm-delegation.ts
991
890
  function useSymmDelegation(params) {
992
891
  const { chainId, address, symmioConfig } = useSymmContext();
@@ -24707,6 +24606,7 @@ function useSymmPositions(params) {
24707
24606
  }),
24708
24607
  enabled: internalEnabled && (params.query?.enabled ?? true)
24709
24608
  });
24609
+ console.log("positions query", { query });
24710
24610
  return {
24711
24611
  positions: query.data?.data?.positions ?? [],
24712
24612
  isLoading: query.isLoading,
@@ -25284,6 +25184,128 @@ function useSymmTwapOrder(params) {
25284
25184
  refetch: query.refetch
25285
25185
  };
25286
25186
  }
25187
+ var useSymmWsStore = create((set) => ({
25188
+ isConnected: false,
25189
+ setConnected: (isConnected) => set({ isConnected })
25190
+ }));
25191
+
25192
+ // src/react/hooks/use-symm-ws.ts
25193
+ function asUnsubscribeFn(value) {
25194
+ return typeof value === "function" ? value : null;
25195
+ }
25196
+ function useSymmWs(params = {}) {
25197
+ const {
25198
+ symmCoreClient: ctxClient,
25199
+ address: ctxAddress,
25200
+ chainId: ctxChainId
25201
+ } = useSymmContext();
25202
+ const queryClient = useQueryClient();
25203
+ const isConnected = useSymmWsStore((state) => state.isConnected);
25204
+ const setConnected = useSymmWsStore((state) => state.setConnected);
25205
+ const symmCoreClient = params.symmCoreClient ?? ctxClient;
25206
+ const accountAddress = params.accountAddress ?? ctxAddress;
25207
+ const chainId = params.chainId ?? ctxChainId;
25208
+ useEffect(() => {
25209
+ if (!symmCoreClient || !accountAddress) {
25210
+ setConnected(false);
25211
+ return;
25212
+ }
25213
+ const ws = symmCoreClient.ws;
25214
+ const addr = accountAddress;
25215
+ const unsubscribers = [];
25216
+ const removeOnConnect = ws.onConnect(() => setConnected(true));
25217
+ const removeOnDisconnect = ws.onDisconnect(() => setConnected(false));
25218
+ unsubscribers.push(removeOnConnect, removeOnDisconnect);
25219
+ const positionsUnsub = asUnsubscribeFn(
25220
+ ws.subscribeToPositions(addr, chainId, () => {
25221
+ queryClient.invalidateQueries({
25222
+ queryKey: ["symm", "positions"]
25223
+ });
25224
+ })
25225
+ );
25226
+ if (positionsUnsub) unsubscribers.push(positionsUnsub);
25227
+ const openOrdersUnsub = asUnsubscribeFn(
25228
+ ws.subscribeToOpenOrders(addr, chainId, () => {
25229
+ queryClient.invalidateQueries({
25230
+ queryKey: ["symm", "openOrders"]
25231
+ });
25232
+ })
25233
+ );
25234
+ if (openOrdersUnsub) unsubscribers.push(openOrdersUnsub);
25235
+ const tradesUnsub = asUnsubscribeFn(
25236
+ ws.subscribeToTrades(addr, chainId, () => {
25237
+ queryClient.invalidateQueries({
25238
+ queryKey: ["symm", "tradeHistory"]
25239
+ });
25240
+ })
25241
+ );
25242
+ if (tradesUnsub) unsubscribers.push(tradesUnsub);
25243
+ const accountSummaryUnsub = asUnsubscribeFn(
25244
+ ws.subscribeToAccountSummary(addr, chainId, () => {
25245
+ queryClient.invalidateQueries({
25246
+ queryKey: symmKeys.balances(accountAddress, chainId)
25247
+ });
25248
+ queryClient.invalidateQueries({
25249
+ queryKey: symmKeys.accountSummary(accountAddress, chainId)
25250
+ });
25251
+ })
25252
+ );
25253
+ if (accountSummaryUnsub) unsubscribers.push(accountSummaryUnsub);
25254
+ const notificationsUnsub = asUnsubscribeFn(
25255
+ ws.subscribeToNotifications(addr, chainId, () => {
25256
+ queryClient.invalidateQueries({
25257
+ queryKey: symmKeys.notifications(accountAddress, chainId)
25258
+ });
25259
+ queryClient.invalidateQueries({
25260
+ queryKey: symmKeys.unreadCount(accountAddress, chainId)
25261
+ });
25262
+ })
25263
+ );
25264
+ if (notificationsUnsub) unsubscribers.push(notificationsUnsub);
25265
+ const tpslUnsub = asUnsubscribeFn(
25266
+ ws.subscribeToTpsl(addr, chainId, () => {
25267
+ queryClient.invalidateQueries({ queryKey: ["symm", "tpslOrders"] });
25268
+ queryClient.invalidateQueries({ queryKey: ["symm", "openOrders"] });
25269
+ })
25270
+ );
25271
+ if (tpslUnsub) unsubscribers.push(tpslUnsub);
25272
+ const twapUnsub = asUnsubscribeFn(
25273
+ ws.subscribeToTwapOrders(addr, chainId, () => {
25274
+ queryClient.invalidateQueries({ queryKey: ["symm", "twapOrders"] });
25275
+ queryClient.invalidateQueries({ queryKey: ["symm", "openOrders"] });
25276
+ })
25277
+ );
25278
+ if (twapUnsub) unsubscribers.push(twapUnsub);
25279
+ const triggerOrdersUnsub = asUnsubscribeFn(
25280
+ ws.subscribeToTriggerOrders(addr, chainId, () => {
25281
+ queryClient.invalidateQueries({ queryKey: ["symm", "triggerOrders"] });
25282
+ queryClient.invalidateQueries({ queryKey: ["symm", "openOrders"] });
25283
+ })
25284
+ );
25285
+ if (triggerOrdersUnsub) unsubscribers.push(triggerOrdersUnsub);
25286
+ const executionsUnsub = asUnsubscribeFn(
25287
+ ws.subscribeToExecutions(addr, chainId, () => {
25288
+ queryClient.invalidateQueries({
25289
+ queryKey: ["symm", "positions"]
25290
+ });
25291
+ queryClient.invalidateQueries({
25292
+ queryKey: ["symm", "portfolio"]
25293
+ });
25294
+ })
25295
+ );
25296
+ if (executionsUnsub) unsubscribers.push(executionsUnsub);
25297
+ return () => {
25298
+ if (unsubscribers.length > 2) {
25299
+ unsubscribers.forEach((unsubscribe) => unsubscribe());
25300
+ } else {
25301
+ removeOnConnect();
25302
+ removeOnDisconnect();
25303
+ ws.unsubscribeAll();
25304
+ }
25305
+ };
25306
+ }, [symmCoreClient, accountAddress, chainId, queryClient, setConnected]);
25307
+ return { isConnected };
25308
+ }
25287
25309
  var STABLE_SYMBOLS = /* @__PURE__ */ new Set(["USDC", "USD", "USDT", "USDE", "USDH", "USDT0"]);
25288
25310
  function useSymmChartSelection(input) {
25289
25311
  const {