@pear-protocol/symmio-client 0.2.10 → 0.2.11

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