@pear-protocol/symmio-client 0.3.0 → 0.3.1

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.
@@ -4,8 +4,8 @@
4
4
  var react = require('react');
5
5
  var symmCore = require('@pear-protocol/symm-core');
6
6
  var zustand = require('zustand');
7
- var jsxRuntime = require('react/jsx-runtime');
8
7
  var reactQuery = require('@tanstack/react-query');
8
+ var jsxRuntime = require('react/jsx-runtime');
9
9
  var siwe = require('siwe');
10
10
  var viem = require('viem');
11
11
 
@@ -490,6 +490,280 @@ function useBinanceWs(params) {
490
490
  };
491
491
  }, [symmCoreClient, chainId, subscribeSymbol, unsubscribeSymbol]);
492
492
  }
493
+
494
+ // src/react/query-keys.ts
495
+ var symmKeys = {
496
+ all: ["symm"],
497
+ balancesRoot: ["symm", "balances"],
498
+ accountSummaryRoot: ["symm", "accountSummary"],
499
+ accountDataRoot: ["symm", "accountData"],
500
+ approvalRoot: ["symm", "approval"],
501
+ positionsRoot: ["symm", "positions"],
502
+ openOrdersRoot: ["symm", "openOrders"],
503
+ tradeHistoryRoot: ["symm", "tradeHistory"],
504
+ portfolioRoot: ["symm", "portfolio"],
505
+ tpslOrdersRoot: ["symm", "tpslOrders"],
506
+ twapOrdersRoot: ["symm", "twapOrders"],
507
+ triggerOrdersRoot: ["symm", "triggerOrders"],
508
+ accounts: (address, chainId) => ["symm", "accounts", address, chainId],
509
+ accountsApi: (address, chainId) => ["symm", "accountsApi", address, chainId],
510
+ accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
511
+ accountsWithPositions: (address, chainId) => ["symm", "accountsWithPositions", address, chainId],
512
+ accountSummary: (address, chainId) => ["symm", "accountSummary", address, chainId],
513
+ accountData: (address, chainId, upnl) => ["symm", "accountData", address, chainId, upnl],
514
+ signature: (address, chainId) => ["symm", "signature", address, chainId],
515
+ auth: (accountAddress, chainId, signerAddress) => ["symm", "auth", accountAddress, chainId, signerAddress],
516
+ approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
517
+ balances: (address, chainId) => ["symm", "balances", address, chainId],
518
+ positions: (params) => ["symm", "positions", params],
519
+ openOrders: (params) => ["symm", "openOrders", params],
520
+ tradeHistory: (params) => ["symm", "tradeHistory", params],
521
+ tpslOrders: (address, chainId) => ["symm", "tpslOrders", address, chainId],
522
+ tpslOrdersList: (params) => ["symm", "tpslOrders", params],
523
+ twapOrders: (address, chainId) => ["symm", "twapOrders", address, chainId],
524
+ triggerOrders: (params) => ["symm", "triggerOrders", params],
525
+ triggerConfig: (orderId) => ["symm", "triggerConfig", orderId],
526
+ markets: (chainId, search, pageSize) => ["symm", "markets", chainId, search, pageSize],
527
+ hedgerMarketById: (id, chainId) => ["symm", "hedgerMarketById", id, chainId],
528
+ hedgerMarketBySymbol: (symbol, chainId) => ["symm", "hedgerMarketBySymbol", symbol, chainId],
529
+ lockedParams: (marketName, leverage, chainId) => ["symm", "lockedParams", marketName, leverage, chainId],
530
+ hedgerMarkets: (request) => ["symm", "hedgerMarkets", request],
531
+ fundingRates: (chainId) => ["symm", "fundingRates", chainId],
532
+ fundingPayments: (params) => ["symm", "fundingPayments", params],
533
+ fundingHistory: (params) => ["symm", "fundingHistory", params],
534
+ portfolio: (params) => ["symm", "portfolio", params],
535
+ notifications: (params) => ["symm", "notifications", params],
536
+ unreadCount: (params) => ["symm", "unreadCount", params],
537
+ availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
538
+ pendingIds: (address, chainId) => ["symm", "pendingIds", address, chainId],
539
+ pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
540
+ twapOrder: (orderId) => ["symm", "twapOrder", orderId],
541
+ delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
542
+ chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
543
+ };
544
+ var useSymmWsStore = zustand.create((set) => ({
545
+ isConnected: false,
546
+ setConnected: (isConnected) => set({ isConnected })
547
+ }));
548
+
549
+ // src/react/hooks/use-symm-ws.ts
550
+ function asUnsubscribeFn(value) {
551
+ return typeof value === "function" ? value : null;
552
+ }
553
+ function logSymmWs(event, details) {
554
+ if (typeof window === "undefined") {
555
+ return;
556
+ }
557
+ console.debug("[symm-ws]", event, details ?? {});
558
+ }
559
+ function useSymmWs(params = {}) {
560
+ const ctx = react.useContext(SymmContext);
561
+ const queryClient = reactQuery.useQueryClient();
562
+ const isConnected = useSymmWsStore((state) => state.isConnected);
563
+ const setConnected = useSymmWsStore((state) => state.setConnected);
564
+ const symmCoreClient = params.symmCoreClient ?? ctx?.symmCoreClient ?? null;
565
+ const accountAddress = params.accountAddress ?? ctx?.address;
566
+ const chainId = params.chainId ?? ctx?.chainId ?? 42161;
567
+ react.useEffect(() => {
568
+ if (!symmCoreClient || !accountAddress) {
569
+ logSymmWs("setup:skip", {
570
+ hasClient: !!symmCoreClient,
571
+ accountAddress,
572
+ chainId
573
+ });
574
+ setConnected(false);
575
+ return;
576
+ }
577
+ const ws = symmCoreClient.ws;
578
+ const addr = accountAddress;
579
+ const unsubscribers = [];
580
+ let cancelled = false;
581
+ logSymmWs("setup:start", {
582
+ accountAddress: addr,
583
+ chainId,
584
+ alreadyConnected: typeof ws.isConnected === "function" ? ws.isConnected() : void 0
585
+ });
586
+ const removeOnConnect = ws.onConnect(() => {
587
+ logSymmWs("connection:connected", {
588
+ accountAddress: addr,
589
+ chainId
590
+ });
591
+ setConnected(true);
592
+ });
593
+ const removeOnDisconnect = ws.onDisconnect(() => {
594
+ logSymmWs("connection:disconnected", {
595
+ accountAddress: addr,
596
+ chainId
597
+ });
598
+ setConnected(false);
599
+ });
600
+ const removeOnError = asUnsubscribeFn(
601
+ ws.onError?.((error) => {
602
+ logSymmWs("connection:error", {
603
+ accountAddress: addr,
604
+ chainId,
605
+ error: error instanceof Error ? error.message : String(error)
606
+ });
607
+ })
608
+ );
609
+ const removeOnWelcome = asUnsubscribeFn(
610
+ ws.onWelcome?.((message) => {
611
+ logSymmWs("connection:welcome", {
612
+ accountAddress: addr,
613
+ chainId,
614
+ message
615
+ });
616
+ })
617
+ );
618
+ unsubscribers.push(removeOnConnect, removeOnDisconnect);
619
+ if (removeOnError) unsubscribers.push(removeOnError);
620
+ if (removeOnWelcome) unsubscribers.push(removeOnWelcome);
621
+ const positionsUnsub = asUnsubscribeFn(
622
+ ws.subscribeToPositions(addr, chainId, () => {
623
+ logSymmWs("message:positions", {
624
+ accountAddress: addr,
625
+ chainId
626
+ });
627
+ queryClient.invalidateQueries({
628
+ queryKey: symmKeys.positionsRoot
629
+ });
630
+ })
631
+ );
632
+ if (positionsUnsub) unsubscribers.push(positionsUnsub);
633
+ const openOrdersUnsub = asUnsubscribeFn(
634
+ ws.subscribeToOpenOrders(addr, chainId, () => {
635
+ logSymmWs("message:open-orders", {
636
+ accountAddress: addr,
637
+ chainId
638
+ });
639
+ queryClient.invalidateQueries({
640
+ queryKey: symmKeys.openOrdersRoot
641
+ });
642
+ })
643
+ );
644
+ if (openOrdersUnsub) unsubscribers.push(openOrdersUnsub);
645
+ const tradesUnsub = asUnsubscribeFn(
646
+ ws.subscribeToTrades(addr, chainId, () => {
647
+ logSymmWs("message:trades", {
648
+ accountAddress: addr,
649
+ chainId
650
+ });
651
+ queryClient.invalidateQueries({
652
+ queryKey: symmKeys.tradeHistoryRoot
653
+ });
654
+ })
655
+ );
656
+ if (tradesUnsub) unsubscribers.push(tradesUnsub);
657
+ const accountSummaryUnsub = asUnsubscribeFn(
658
+ ws.subscribeToAccountSummary(addr, chainId, () => {
659
+ logSymmWs("message:account-summary", {
660
+ accountAddress: addr,
661
+ chainId
662
+ });
663
+ queryClient.invalidateQueries({
664
+ queryKey: symmKeys.balances(accountAddress, chainId)
665
+ });
666
+ queryClient.invalidateQueries({
667
+ queryKey: symmKeys.accountSummary(accountAddress, chainId)
668
+ });
669
+ })
670
+ );
671
+ if (accountSummaryUnsub) unsubscribers.push(accountSummaryUnsub);
672
+ const notificationsUnsub = asUnsubscribeFn(
673
+ ws.subscribeToNotifications(addr, chainId, () => {
674
+ logSymmWs("message:notifications", {
675
+ accountAddress: addr,
676
+ chainId
677
+ });
678
+ queryClient.invalidateQueries({
679
+ queryKey: symmKeys.notifications({ accountAddress, chainId })
680
+ });
681
+ queryClient.invalidateQueries({
682
+ queryKey: symmKeys.unreadCount({ accountAddress, chainId })
683
+ });
684
+ })
685
+ );
686
+ if (notificationsUnsub) unsubscribers.push(notificationsUnsub);
687
+ const tpslUnsub = asUnsubscribeFn(
688
+ ws.subscribeToTpsl(addr, chainId, () => {
689
+ logSymmWs("message:tpsl", {
690
+ accountAddress: addr,
691
+ chainId
692
+ });
693
+ queryClient.invalidateQueries({ queryKey: symmKeys.tpslOrdersRoot });
694
+ queryClient.invalidateQueries({ queryKey: symmKeys.openOrdersRoot });
695
+ })
696
+ );
697
+ if (tpslUnsub) unsubscribers.push(tpslUnsub);
698
+ const twapUnsub = asUnsubscribeFn(
699
+ ws.subscribeToTwapOrders(addr, chainId, () => {
700
+ logSymmWs("message:twap-orders", {
701
+ accountAddress: addr,
702
+ chainId
703
+ });
704
+ queryClient.invalidateQueries({ queryKey: symmKeys.twapOrdersRoot });
705
+ queryClient.invalidateQueries({ queryKey: symmKeys.openOrdersRoot });
706
+ })
707
+ );
708
+ if (twapUnsub) unsubscribers.push(twapUnsub);
709
+ const triggerOrdersUnsub = asUnsubscribeFn(
710
+ ws.subscribeToTriggerOrders(addr, chainId, () => {
711
+ logSymmWs("message:trigger-orders", {
712
+ accountAddress: addr,
713
+ chainId
714
+ });
715
+ queryClient.invalidateQueries({ queryKey: symmKeys.triggerOrdersRoot });
716
+ queryClient.invalidateQueries({ queryKey: symmKeys.openOrdersRoot });
717
+ })
718
+ );
719
+ if (triggerOrdersUnsub) unsubscribers.push(triggerOrdersUnsub);
720
+ const executionsUnsub = asUnsubscribeFn(
721
+ ws.subscribeToExecutions(addr, chainId, () => {
722
+ logSymmWs("message:executions", {
723
+ accountAddress: addr,
724
+ chainId
725
+ });
726
+ queryClient.invalidateQueries({
727
+ queryKey: symmKeys.positionsRoot
728
+ });
729
+ queryClient.invalidateQueries({
730
+ queryKey: symmKeys.portfolioRoot
731
+ });
732
+ })
733
+ );
734
+ if (executionsUnsub) unsubscribers.push(executionsUnsub);
735
+ void ws.connect().then(() => {
736
+ if (cancelled) {
737
+ return;
738
+ }
739
+ logSymmWs("connection:connect-called", {
740
+ accountAddress: addr,
741
+ chainId
742
+ });
743
+ }).catch((error) => {
744
+ if (cancelled) {
745
+ return;
746
+ }
747
+ logSymmWs("connection:connect-failed", {
748
+ accountAddress: addr,
749
+ chainId,
750
+ error: error instanceof Error ? error.message : String(error)
751
+ });
752
+ setConnected(false);
753
+ });
754
+ return () => {
755
+ cancelled = true;
756
+ logSymmWs("cleanup:start", {
757
+ accountAddress: addr,
758
+ chainId
759
+ });
760
+ unsubscribers.forEach((unsubscribe) => unsubscribe());
761
+ ws.disconnect();
762
+ setConnected(false);
763
+ };
764
+ }, [symmCoreClient, accountAddress, chainId, queryClient, setConnected]);
765
+ return { isConnected };
766
+ }
493
767
  function SymmProvider({
494
768
  chainId = 42161,
495
769
  address,
@@ -522,6 +796,11 @@ function SymmProvider({
522
796
  symmCoreClient,
523
797
  chainId
524
798
  });
799
+ useSymmWs({
800
+ symmCoreClient,
801
+ accountAddress: address,
802
+ chainId
803
+ });
525
804
  return /* @__PURE__ */ jsxRuntime.jsx(SymmContext.Provider, { value, children });
526
805
  }
527
806
 
@@ -704,57 +983,6 @@ async function fetchAccessTokenEntry(walletClient, signerAddress, accountAddress
704
983
  return cachedToken;
705
984
  }
706
985
 
707
- // src/react/query-keys.ts
708
- var symmKeys = {
709
- all: ["symm"],
710
- balancesRoot: ["symm", "balances"],
711
- accountSummaryRoot: ["symm", "accountSummary"],
712
- accountDataRoot: ["symm", "accountData"],
713
- approvalRoot: ["symm", "approval"],
714
- positionsRoot: ["symm", "positions"],
715
- openOrdersRoot: ["symm", "openOrders"],
716
- tradeHistoryRoot: ["symm", "tradeHistory"],
717
- portfolioRoot: ["symm", "portfolio"],
718
- tpslOrdersRoot: ["symm", "tpslOrders"],
719
- twapOrdersRoot: ["symm", "twapOrders"],
720
- triggerOrdersRoot: ["symm", "triggerOrders"],
721
- accounts: (address, chainId) => ["symm", "accounts", address, chainId],
722
- accountsApi: (address, chainId) => ["symm", "accountsApi", address, chainId],
723
- accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
724
- accountsWithPositions: (address, chainId) => ["symm", "accountsWithPositions", address, chainId],
725
- accountSummary: (address, chainId) => ["symm", "accountSummary", address, chainId],
726
- accountData: (address, chainId, upnl) => ["symm", "accountData", address, chainId, upnl],
727
- signature: (address, chainId) => ["symm", "signature", address, chainId],
728
- auth: (accountAddress, chainId, signerAddress) => ["symm", "auth", accountAddress, chainId, signerAddress],
729
- approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
730
- balances: (address, chainId) => ["symm", "balances", address, chainId],
731
- positions: (params) => ["symm", "positions", params],
732
- openOrders: (params) => ["symm", "openOrders", params],
733
- tradeHistory: (params) => ["symm", "tradeHistory", params],
734
- tpslOrders: (address, chainId) => ["symm", "tpslOrders", address, chainId],
735
- tpslOrdersList: (params) => ["symm", "tpslOrders", params],
736
- twapOrders: (address, chainId) => ["symm", "twapOrders", address, chainId],
737
- triggerOrders: (params) => ["symm", "triggerOrders", params],
738
- triggerConfig: (orderId) => ["symm", "triggerConfig", orderId],
739
- markets: (chainId, search, pageSize) => ["symm", "markets", chainId, search, pageSize],
740
- hedgerMarketById: (id, chainId) => ["symm", "hedgerMarketById", id, chainId],
741
- hedgerMarketBySymbol: (symbol, chainId) => ["symm", "hedgerMarketBySymbol", symbol, chainId],
742
- lockedParams: (marketName, leverage, chainId) => ["symm", "lockedParams", marketName, leverage, chainId],
743
- hedgerMarkets: (request) => ["symm", "hedgerMarkets", request],
744
- fundingRates: (chainId) => ["symm", "fundingRates", chainId],
745
- fundingPayments: (params) => ["symm", "fundingPayments", params],
746
- fundingHistory: (params) => ["symm", "fundingHistory", params],
747
- portfolio: (params) => ["symm", "portfolio", params],
748
- notifications: (params) => ["symm", "notifications", params],
749
- unreadCount: (params) => ["symm", "unreadCount", params],
750
- availableMargin: (address, chainId) => ["symm", "availableMargin", address, chainId],
751
- pendingIds: (address, chainId) => ["symm", "pendingIds", address, chainId],
752
- pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
753
- twapOrder: (orderId) => ["symm", "twapOrder", orderId],
754
- delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
755
- chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
756
- };
757
-
758
986
  // src/react/auth-cache.ts
759
987
  function getAuthQueryData(queryClient, accountAddress, chainId, signerAddress) {
760
988
  return queryClient.getQueryData(
@@ -26921,10 +27149,6 @@ function getSymmErrorMessage(error) {
26921
27149
  if (error instanceof Error) return error.message;
26922
27150
  return "An unexpected error occurred.";
26923
27151
  }
26924
- var useSymmWsStore = zustand.create((set) => ({
26925
- isConnected: false,
26926
- setConnected: (isConnected) => set({ isConnected })
26927
- }));
26928
27152
 
26929
27153
  exports.SymmProvider = SymmProvider;
26930
27154
  exports.getSymmErrorMessage = getSymmErrorMessage;