@rango-dev/widget-embedded 0.23.1-next.24 → 0.23.1-next.26

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.
Files changed (30) hide show
  1. package/dist/components/AppRouter.d.ts +1 -6
  2. package/dist/components/AppRouter.d.ts.map +1 -1
  3. package/dist/components/SwapDetailsModal/SwapDetailsModal.WalletState.d.ts.map +1 -1
  4. package/dist/constants/index.d.ts +1 -1
  5. package/dist/constants/index.d.ts.map +1 -1
  6. package/dist/containers/App/App.d.ts.map +1 -1
  7. package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
  8. package/dist/containers/Wallets/Wallets.types.d.ts +13 -2
  9. package/dist/containers/Wallets/Wallets.types.d.ts.map +1 -1
  10. package/dist/containers/Widget/Widget.d.ts.map +1 -1
  11. package/dist/hooks/useBootstrap/index.d.ts +2 -0
  12. package/dist/hooks/useBootstrap/index.d.ts.map +1 -0
  13. package/dist/hooks/useBootstrap/useBootstrap.d.ts +2 -0
  14. package/dist/hooks/useBootstrap/useBootstrap.d.ts.map +1 -0
  15. package/dist/hooks/useWalletList.d.ts.map +1 -1
  16. package/dist/index.js +2 -2
  17. package/dist/index.js.map +4 -4
  18. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  19. package/package.json +2 -1
  20. package/src/components/AppRouter.tsx +1 -29
  21. package/src/components/SwapDetailsModal/SwapDetailsModal.WalletState.tsx +5 -1
  22. package/src/constants/index.ts +1 -1
  23. package/src/containers/App/App.tsx +4 -25
  24. package/src/containers/Wallets/Wallets.tsx +27 -10
  25. package/src/containers/Wallets/Wallets.types.ts +13 -2
  26. package/src/containers/Widget/Widget.tsx +1 -7
  27. package/src/hooks/useBootstrap/index.ts +1 -0
  28. package/src/hooks/useBootstrap/useBootstrap.ts +72 -0
  29. package/src/hooks/useWalletList.ts +12 -0
  30. package/src/pages/ConfirmSwapPage.tsx +6 -2
@@ -1 +1 @@
1
- {"version":3,"file":"ConfirmSwapPage.d.ts","sourceRoot":"","sources":["../../src/pages/ConfirmSwapPage.tsx"],"names":[],"mappings":"AAoBA,OAAO,KAAuD,MAAM,OAAO,CAAC;AAmD5E,wBAAgB,eAAe,sBAmU9B"}
1
+ {"version":3,"file":"ConfirmSwapPage.d.ts","sourceRoot":"","sources":["../../src/pages/ConfirmSwapPage.tsx"],"names":[],"mappings":"AAoBA,OAAO,KAAuD,MAAM,OAAO,CAAC;AAmD5E,wBAAgB,eAAe,sBAuU9B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.23.1-next.24",
3
+ "version": "0.23.1-next.26",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -21,6 +21,7 @@
21
21
  "dependencies": {
22
22
  "@lingui/core": "4.2.1",
23
23
  "@lingui/react": "4.2.1",
24
+ "@rango-dev/logging-core": "^0.1.1-next.0",
24
25
  "@rango-dev/provider-all": "^0.28.1-next.2",
25
26
  "@rango-dev/queue-manager-core": "^0.26.0",
26
27
  "@rango-dev/queue-manager-rango-preset": "^0.28.1-next.3",
@@ -1,39 +1,11 @@
1
- import type { WalletType } from '@rango-dev/wallets-shared';
2
1
  import type { PropsWithChildren } from 'react';
3
2
 
4
- import { useQueueManager } from '@rango-dev/queue-manager-rango-preset';
5
- import { useWallets } from '@rango-dev/wallets-react';
6
- import { isEvmBlockchain } from 'rango-types';
7
3
  import React, { Fragment } from 'react';
8
4
  import { MemoryRouter, useInRouterContext } from 'react-router';
9
5
 
10
- import { useForceAutoConnect } from '../hooks/useForceAutoConnect';
11
- import { useAppStore } from '../store/AppStore';
12
-
13
- export function AppRouter({
14
- children,
15
- ...props
16
- }: PropsWithChildren & {
17
- lastConnectedWallet: string;
18
- disconnectedWallet: WalletType | undefined;
19
- clearDisconnectedWallet: () => void;
20
- }) {
6
+ export function AppRouter({ children }: PropsWithChildren) {
21
7
  const isRouterInContext = useInRouterContext();
22
8
  const Router = isRouterInContext ? Fragment : MemoryRouter;
23
- const blockchains = useAppStore().blockchains();
24
- const { canSwitchNetworkTo } = useWallets();
25
-
26
- useForceAutoConnect();
27
-
28
- const evmChains = blockchains.filter(isEvmBlockchain);
29
-
30
- useQueueManager({
31
- lastConnectedWallet: props.lastConnectedWallet,
32
- clearDisconnectedWallet: props.clearDisconnectedWallet,
33
- disconnectedWallet: props.disconnectedWallet,
34
- evmChains,
35
- canSwitchNetworkTo,
36
- });
37
9
 
38
10
  return <Router>{children}</Router>;
39
11
  }
@@ -1,5 +1,6 @@
1
1
  import type { WalletStateContentProps } from './SwapDetailsModal.types';
2
2
 
3
+ import { debug } from '@rango-dev/logging-core';
3
4
  import { MessageBox, Wallet } from '@rango-dev/ui';
4
5
  import { useWallets } from '@rango-dev/wallets-react';
5
6
  import React from 'react';
@@ -39,7 +40,10 @@ export const WalletStateContent = (props: WalletStateContentProps) => {
39
40
  state={walletState}
40
41
  link={walletInfo.installLink}
41
42
  disabled={walletButtonDisabled}
42
- onClick={async () => connect(walletType)}
43
+ // TODO we need to show an error modal when user reject the connection
44
+ onClick={async () =>
45
+ connect(walletType).catch((error) => debug(error))
46
+ }
43
47
  />
44
48
  </WalletContainer>
45
49
  )}
@@ -2,7 +2,7 @@ import { UI_ID } from '@rango-dev/ui';
2
2
 
3
3
  export const RANGO_PUBLIC_API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32';
4
4
  export const DEFAULT_BASE_URL = 'https://api.rango.exchange';
5
- export const SCANNER_BASE_URL = 'https://scan.rango.exchange';
5
+ export const SCANNER_BASE_URL = 'https://explorer.rango.exchange';
6
6
 
7
7
  export const WIDGET_UI_ID = {
8
8
  SWAP_BOX_ID: 'rango-swap-box',
@@ -1,48 +1,27 @@
1
- import type { WalletType } from '@rango-dev/wallets-shared';
2
-
3
1
  import { I18nManager } from '@rango-dev/ui';
4
- import React, { useContext, useEffect, useState } from 'react';
2
+ import React from 'react';
5
3
 
6
4
  import { AppRouter } from '../../components/AppRouter';
7
5
  import { AppRoutes } from '../../components/AppRoutes';
8
6
  import { WidgetEvents } from '../../components/WidgetEvents';
9
- import { globalFont } from '../../globalStyles';
10
- import { useFetchApiConfig } from '../../hooks/useFetchApiConfig';
7
+ import { useBootstrap } from '../../hooks/useBootstrap';
11
8
  import { useLanguage } from '../../hooks/useLanguage';
12
9
  import { useTheme } from '../../hooks/useTheme';
13
10
  import { useAppStore } from '../../store/AppStore';
14
- import { useNotificationStore } from '../../store/notification';
15
- import { WidgetContext } from '../Wallets';
16
11
 
17
12
  import { MainContainer } from './App.styles';
18
13
 
19
14
  export function Main() {
20
- globalFont();
21
-
15
+ useBootstrap();
22
16
  const { config } = useAppStore();
23
17
  const { activeTheme } = useTheme(config?.theme || {});
24
18
  const { activeLanguage } = useLanguage();
25
- const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
26
- useState<string>('');
27
- const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
28
- const widgetContext = useContext(WidgetContext);
29
- const { fetchApiConfig } = useFetchApiConfig();
30
- useEffect(() => {
31
- void useNotificationStore.persist.rehydrate();
32
- widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
33
- void fetchApiConfig().catch(console.log);
34
- }, []);
35
19
 
36
20
  return (
37
21
  <I18nManager language={activeLanguage}>
38
22
  <MainContainer id="swap-container" className={activeTheme()}>
39
23
  <WidgetEvents />
40
- <AppRouter
41
- lastConnectedWallet={lastConnectedWalletWithNetwork}
42
- disconnectedWallet={disconnectedWallet}
43
- clearDisconnectedWallet={() => {
44
- setDisconnectedWallet(undefined);
45
- }}>
24
+ <AppRouter>
46
25
  <AppRoutes />
47
26
  </AppRouter>
48
27
  </MainContainer>
@@ -1,5 +1,5 @@
1
1
  import type {
2
- OnConnectHandler,
2
+ OnWalletConnectionChange,
3
3
  PropTypes,
4
4
  WidgetContextInterface,
5
5
  } from './Wallets.types';
@@ -10,7 +10,7 @@ import type { PropsWithChildren } from 'react';
10
10
  import { Events, Provider } from '@rango-dev/wallets-react';
11
11
  import { type Network } from '@rango-dev/wallets-shared';
12
12
  import { isEvmBlockchain } from 'rango-sdk';
13
- import React, { createContext, useEffect, useRef } from 'react';
13
+ import React, { createContext, useEffect, useMemo, useRef } from 'react';
14
14
 
15
15
  import { useWalletProviders } from '../../hooks/useWalletProviders';
16
16
  import { AppStoreProvider, useAppStore } from '../../store/AppStore';
@@ -25,6 +25,9 @@ export const WidgetContext = createContext<WidgetContextInterface>({
25
25
  onConnectWallet: () => {
26
26
  return;
27
27
  },
28
+ onDisconnectWallet: () => {
29
+ return;
30
+ },
28
31
  });
29
32
 
30
33
  function Main(props: PropsWithChildren<PropTypes>) {
@@ -36,7 +39,8 @@ function Main(props: PropsWithChildren<PropTypes>) {
36
39
  };
37
40
  const { providers } = useWalletProviders(props.config.wallets, walletOptions);
38
41
  const { connectWallet, disconnectWallet } = useWalletsStore();
39
- const onConnectWalletHandler = useRef<OnConnectHandler>();
42
+ const onConnectWalletHandler = useRef<OnWalletConnectionChange>();
43
+ const onDisconnectWalletHandler = useRef<OnWalletConnectionChange>();
40
44
 
41
45
  useEffect(() => {
42
46
  void fetchMeta().catch(console.log);
@@ -70,6 +74,13 @@ function Main(props: PropsWithChildren<PropTypes>) {
70
74
  }
71
75
  } else {
72
76
  disconnectWallet(type);
77
+ if (!!onDisconnectWalletHandler.current) {
78
+ onDisconnectWalletHandler.current(type);
79
+ } else {
80
+ console.warn(
81
+ `onDisconnectWallet handler hasn't been set. Are you sure?`
82
+ );
83
+ }
73
84
  }
74
85
  }
75
86
  if (event === Events.ACCOUNTS && state.connected) {
@@ -102,14 +113,20 @@ function Main(props: PropsWithChildren<PropTypes>) {
102
113
  };
103
114
  const isActiveTab = useUiStore.use.isActiveTab();
104
115
 
116
+ const handlers = useMemo(
117
+ () => ({
118
+ onConnectWallet: (handler: OnWalletConnectionChange) => {
119
+ onConnectWalletHandler.current = handler;
120
+ },
121
+ onDisconnectWallet: (handler: OnWalletConnectionChange) => {
122
+ onDisconnectWalletHandler.current = handler;
123
+ },
124
+ }),
125
+ []
126
+ );
127
+
105
128
  return (
106
- <WidgetContext.Provider
107
- // eslint-disable-next-line react/jsx-no-constructed-context-values
108
- value={{
109
- onConnectWallet: (handler) => {
110
- onConnectWalletHandler.current = handler;
111
- },
112
- }}>
129
+ <WidgetContext.Provider value={handlers}>
113
130
  <Provider
114
131
  allBlockChains={blockchains}
115
132
  providers={providers}
@@ -1,9 +1,20 @@
1
1
  import type { WidgetConfig } from '../../types';
2
2
  import type { EventHandler } from '@rango-dev/wallets-react';
3
3
 
4
- export type OnConnectHandler = (key: string) => void;
4
+ export type OnWalletConnectionChange = (key: string) => void;
5
5
  export interface WidgetContextInterface {
6
- onConnectWallet(handler: OnConnectHandler): void;
6
+ /**
7
+ * A wallet connection handler, utilized within the wallet provider,
8
+ * is linked to the useBootstrap hook for synchronizing the state of the last connected wallet.
9
+ * It's important not to override this handler in other locations.
10
+ */
11
+ onConnectWallet(handler: OnWalletConnectionChange): void;
12
+ /**
13
+ * A wallet disconnection handler, utilized within the wallet provider,
14
+ * is linked to the useBootstrap hook for synchronizing the state of the last disconnected wallet.
15
+ * It's important not to override this handler in other locations.
16
+ */
17
+ onDisconnectWallet(handler: OnWalletConnectionChange): void;
7
18
  }
8
19
 
9
20
  export interface PropTypes {
@@ -1,19 +1,13 @@
1
1
  import type { WidgetProps } from './Widget.types';
2
2
  import type { PropsWithChildren } from 'react';
3
3
 
4
- import React, { useEffect } from 'react';
4
+ import React from 'react';
5
5
 
6
6
  import { DEFAULT_CONFIG } from '../../store/slices/config';
7
- import { tabManager } from '../../store/ui';
8
7
  import { Main } from '../App';
9
8
  import { WidgetProvider } from '../WidgetProvider';
10
9
 
11
10
  export function Widget(props: PropsWithChildren<WidgetProps>) {
12
- useEffect(() => {
13
- tabManager.init();
14
- return tabManager.destroy;
15
- }, []);
16
-
17
11
  if (!props.config?.externalWallets) {
18
12
  return (
19
13
  <WidgetProvider config={props.config ?? DEFAULT_CONFIG}>
@@ -0,0 +1 @@
1
+ export { useBootstrap } from './useBootstrap';
@@ -0,0 +1,72 @@
1
+ import type { WalletType } from '../..';
2
+
3
+ import { useQueueManager } from '@rango-dev/queue-manager-rango-preset';
4
+ import { splitWalletNetwork } from '@rango-dev/wallets-shared';
5
+ import { isEvmBlockchain } from 'rango-sdk';
6
+ import { useContext, useEffect, useState } from 'react';
7
+
8
+ import { useWallets } from '../..';
9
+ import { WidgetContext } from '../../containers/Wallets';
10
+ import { globalFont } from '../../globalStyles';
11
+ import { useAppStore } from '../../store/AppStore';
12
+ import { useNotificationStore } from '../../store/notification';
13
+ import { tabManager } from '../../store/ui';
14
+ import { useFetchApiConfig } from '../useFetchApiConfig';
15
+ import { useForceAutoConnect } from '../useForceAutoConnect';
16
+
17
+ export function useBootstrap() {
18
+ globalFont();
19
+ useForceAutoConnect();
20
+ const blockchains = useAppStore().blockchains();
21
+ const { canSwitchNetworkTo } = useWallets();
22
+ const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
23
+ useState('');
24
+ const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
25
+ const widgetContext = useContext(WidgetContext);
26
+
27
+ const evmChains = blockchains.filter(isEvmBlockchain);
28
+
29
+ const { fetchApiConfig } = useFetchApiConfig();
30
+
31
+ // At the moment, we only detect the disconnection of EVM wallets.
32
+ useQueueManager({
33
+ lastConnectedWallet: lastConnectedWalletWithNetwork,
34
+ clearDisconnectedWallet: () => {
35
+ setDisconnectedWallet(undefined);
36
+ },
37
+ disconnectedWallet: disconnectedWallet,
38
+ evmChains,
39
+ canSwitchNetworkTo,
40
+ });
41
+
42
+ useEffect(() => {
43
+ tabManager.init();
44
+
45
+ if (!useNotificationStore.persist.hasHydrated()) {
46
+ void useNotificationStore.persist.rehydrate();
47
+ }
48
+
49
+ /**
50
+ * Add handlers to the widget context for connecting and disconnecting wallets.
51
+ * We pass these handlers to the wallet provider to keep track of the last connected and last disconnected wallets.
52
+ * The 'queue-manager' uses these details to spot any changes in the connection status of the required wallet during the swap process.
53
+ *
54
+ * with this approch anyone who use the widget context can override these handlers and causes some bugs happens.
55
+ * it the future we should add a safer soloution like considering array of handlers .
56
+ * https://github.com/rango-exchange/rango-client/pull/630/files#r1518846728
57
+ */
58
+ widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
59
+ widgetContext.onDisconnectWallet((walletType) => {
60
+ setDisconnectedWallet(walletType);
61
+ if (
62
+ walletType === splitWalletNetwork(lastConnectedWalletWithNetwork)?.[0]
63
+ ) {
64
+ setLastConnectedWalletWithNetwork('');
65
+ }
66
+ });
67
+
68
+ void fetchApiConfig().catch(console.log);
69
+
70
+ return tabManager.destroy;
71
+ }, []);
72
+ }
@@ -109,6 +109,18 @@ export function useWalletList(params: Params) {
109
109
  * but we have ethereum injected
110
110
  */
111
111
  const shouldShowDefaultInjectedWallet = (wallets: WalletInfo[]) => {
112
+ // don't show default injected wallet when it's not installed
113
+ const defaultWallet = wallets.find(
114
+ (wallet) => wallet.type === WalletTypes.DEFAULT
115
+ );
116
+ if (!defaultWallet || defaultWallet.state === WalletState.NOT_INSTALLED) {
117
+ return false;
118
+ }
119
+
120
+ /*
121
+ * if we have another evm wallet installed (except wallet connect),
122
+ * there is no need to show default injected wallet anymore
123
+ */
112
124
  const isEvmWalletInstalledExceptDefault = wallets.filter(
113
125
  (wallet) =>
114
126
  wallet.state != WalletState.NOT_INSTALLED &&
@@ -87,6 +87,7 @@ export function ConfirmSwapPage() {
87
87
  const { connectedWallets } = useWalletsStore();
88
88
  const showWalletsOnInit = !quoteWalletsConfirmed;
89
89
  const [showWallets, setShowWallets] = useState(false);
90
+ const [isConfirming, setIsConfirming] = useState(false);
90
91
  const { isActiveTab } = useUiStore();
91
92
  const disabledLiquiditySources = useAppStore().getDisabledLiquiditySources();
92
93
  const prevDisabledLiquiditySources = useRef(disabledLiquiditySources);
@@ -137,7 +138,9 @@ export function ConfirmSwapPage() {
137
138
  };
138
139
 
139
140
  const onConfirm = async () => {
141
+ setIsConfirming(true);
140
142
  await addNewSwap();
143
+ setIsConfirming(false);
141
144
  };
142
145
 
143
146
  const onStartConfirmSwap = async () => {
@@ -326,7 +329,7 @@ export function ConfirmSwapPage() {
326
329
  type="primary"
327
330
  size="large"
328
331
  fullWidth
329
- loading={fetchingConfirmationQuote}
332
+ loading={fetchingConfirmationQuote || isConfirming}
330
333
  disabled={!!confirmSwapResult.error || !isActiveTab}
331
334
  onClick={onStartConfirmSwap}>
332
335
  {i18n.t('Start Swap')}
@@ -336,7 +339,8 @@ export function ConfirmSwapPage() {
336
339
  variant="contained"
337
340
  type="primary"
338
341
  size="large"
339
- loading={fetchingConfirmationQuote}
342
+ loading={fetchingConfirmationQuote || isConfirming}
343
+ disabled={!isActiveTab}
340
344
  onClick={setShowWallets.bind(null, true)}>
341
345
  <WalletIcon size={24} />
342
346
  </IconButton>