@rango-dev/widget-embedded 0.5.1-next.7 → 0.5.1-next.9

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 (38) hide show
  1. package/dist/QueueManager.d.ts.map +1 -1
  2. package/dist/Wallets.d.ts +13 -0
  3. package/dist/Wallets.d.ts.map +1 -0
  4. package/dist/Widget.d.ts +3 -2
  5. package/dist/Widget.d.ts.map +1 -1
  6. package/dist/components/AppRouter.d.ts.map +1 -1
  7. package/dist/components/WidgetEvents.d.ts +2 -0
  8. package/dist/components/WidgetEvents.d.ts.map +1 -0
  9. package/dist/hooks/useSelectLanguage.d.ts.map +1 -1
  10. package/dist/hooks/useWalletProviders.d.ts +6 -0
  11. package/dist/hooks/useWalletProviders.d.ts.map +1 -0
  12. package/dist/index.d.ts +6 -2
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +4 -4
  16. package/dist/pages/Home.d.ts.map +1 -1
  17. package/dist/pages/WalletsPage.d.ts +2 -2
  18. package/dist/pages/WalletsPage.d.ts.map +1 -1
  19. package/dist/types/config.d.ts +6 -1
  20. package/dist/types/config.d.ts.map +1 -1
  21. package/dist/utils/providers.d.ts +11 -0
  22. package/dist/utils/providers.d.ts.map +1 -0
  23. package/package.json +4 -4
  24. package/src/QueueManager.tsx +0 -25
  25. package/src/Wallets.tsx +100 -0
  26. package/src/Widget.tsx +45 -90
  27. package/src/components/AppRouter.tsx +0 -2
  28. package/src/components/WidgetEvents.tsx +46 -0
  29. package/src/hooks/useSelectLanguage.ts +2 -0
  30. package/src/hooks/useWalletProviders.ts +20 -0
  31. package/src/index.ts +16 -1
  32. package/src/pages/Home.tsx +2 -4
  33. package/src/pages/WalletsPage.tsx +8 -4
  34. package/src/types/config.ts +6 -1
  35. package/src/utils/providers.ts +53 -0
  36. package/dist/mockData/pendingSwap.d.ts +0 -2
  37. package/dist/mockData/pendingSwap.d.ts.map +0 -1
  38. package/src/mockData/pendingSwap.ts +0 -607
@@ -1 +1 @@
1
- {"version":3,"file":"Home.d.ts","sourceRoot":"","sources":["../../src/pages/Home.tsx"],"names":[],"mappings":";AAmEA,wBAAgB,IAAI,gBA+JnB"}
1
+ {"version":3,"file":"Home.d.ts","sourceRoot":"","sources":["../../src/pages/Home.tsx"],"names":[],"mappings":";AAmEA,wBAAgB,IAAI,gBA6JnB"}
@@ -1,7 +1,7 @@
1
1
  /// <reference types="react" />
2
- import { WalletType } from '@rango-dev/wallets-shared';
2
+ import { WidgetConfig } from '../types';
3
3
  interface PropTypes {
4
- supportedWallets?: WalletType[];
4
+ supportedWallets: WidgetConfig['wallets'];
5
5
  multiWallets: boolean;
6
6
  }
7
7
  export declare function WalletsPage({ supportedWallets, multiWallets }: PropTypes): JSX.Element;
@@ -1 +1 @@
1
- {"version":3,"file":"WalletsPage.d.ts","sourceRoot":"","sources":["../../src/pages/WalletsPage.tsx"],"names":[],"mappings":";AAUA,OAAO,EACL,UAAU,EAGX,MAAM,2BAA2B,CAAC;AAWnC,UAAU,SAAS;IACjB,gBAAgB,CAAC,EAAE,UAAU,EAAE,CAAC;IAChC,YAAY,EAAE,OAAO,CAAC;CACvB;AAsBD,wBAAgB,WAAW,CAAC,EAAE,gBAAgB,EAAE,YAAY,EAAE,EAAE,SAAS,eA2FxE"}
1
+ {"version":3,"file":"WalletsPage.d.ts","sourceRoot":"","sources":["../../src/pages/WalletsPage.tsx"],"names":[],"mappings":";AAwBA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAGxC,UAAU,SAAS;IACjB,gBAAgB,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC;IAC1C,YAAY,EAAE,OAAO,CAAC;CACvB;AAyBD,wBAAgB,WAAW,CAAC,EAAE,gBAAgB,EAAE,YAAY,EAAE,EAAE,SAAS,eA0FxE"}
@@ -1,5 +1,6 @@
1
1
  import { Asset } from 'rango-sdk';
2
2
  import { WalletType } from '@rango-dev/wallets-shared';
3
+ import { ProviderInterface } from '@rango-dev/wallets-core';
3
4
  /**
4
5
  * The above type defines a set of optional color properties for a widget.
5
6
  * @property {string} background
@@ -99,6 +100,9 @@ export type BlockchainAndTokenConfig = {
99
100
  * @property {WidgetTheme} theme - The `theme` property is a part of the `WidgetConfig` type and is
100
101
  * used to specify the visual theme of the widget. It is of type `WidgetTheme`, which is an interface
101
102
  * that defines the various properties of the theme, such as colors, fonts, and others.
103
+ * @property {boolean} externalWallets
104
+ * If `externalWallets` is `true`, you should add `WidgetWallets` to your app.
105
+
102
106
  */
103
107
  export type WidgetConfig = {
104
108
  apiKey: string;
@@ -107,10 +111,11 @@ export type WidgetConfig = {
107
111
  from?: BlockchainAndTokenConfig;
108
112
  to?: BlockchainAndTokenConfig;
109
113
  liquiditySources?: string[];
110
- wallets?: WalletType[];
114
+ wallets?: (WalletType | ProviderInterface)[];
111
115
  multiWallets?: boolean;
112
116
  customDestination?: boolean;
113
117
  language?: string;
114
118
  theme?: WidgetTheme;
119
+ externalWallets?: boolean;
115
120
  };
116
121
  //# sourceMappingURL=config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/types/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;;;;;;;;;;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IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,wBAAwB,CAAC;IAChC,EAAE,CAAC,EAAE,wBAAwB,CAAC;IAC9B,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE,CAAC,UAAU,GAAG,iBAAiB,CAAC,EAAE,CAAC;IAC7C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { ProviderInterface } from '@rango-dev/wallets-core';
2
+ import { WidgetConfig } from '../types';
3
+ /**
4
+ *
5
+ * Generate a list of providers by passing a provider name (e.g. metamask) or a custom provider which implemented ProviderInterface.
6
+ * @returns ProviderInterface[] a list of ProviderInterface
7
+ *
8
+ */
9
+ export declare function matchAndGenerateProviders(providers: WidgetConfig['wallets']): ProviderInterface[];
10
+ export declare function configWalletsToWalletName(config: WidgetConfig['wallets']): string[];
11
+ //# sourceMappingURL=providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../../src/utils/providers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,YAAY,CAAC,SAAS,CAAC,GACjC,iBAAiB,EAAE,CA8BrB;AAED,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,YAAY,CAAC,SAAS,CAAC,GAC9B,MAAM,EAAE,CAMV"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.5.1-next.7",
3
+ "version": "0.5.1-next.9",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -19,10 +19,10 @@
19
19
  "dependencies": {
20
20
  "@rango-dev/provider-all": "^0.5.1-next.5",
21
21
  "@rango-dev/queue-manager-core": "^0.5.1-next.1",
22
- "@rango-dev/queue-manager-rango-preset": "^0.5.1-next.7",
22
+ "@rango-dev/queue-manager-rango-preset": "^0.5.1-next.9",
23
23
  "@rango-dev/queue-manager-react": "^0.5.1-next.1",
24
- "@rango-dev/ui": "^0.5.1-next.5",
25
- "@rango-dev/wallets-core": "^0.5.1-next.4",
24
+ "@rango-dev/ui": "^0.5.1-next.8",
25
+ "@rango-dev/wallets-core": "^0.5.1-next.5",
26
26
  "@rango-dev/wallets-shared": "^0.5.1-next.10",
27
27
  "bignumber.js": "^9.1.1",
28
28
  "copy-to-clipboard": "^3.3.3",
@@ -4,7 +4,6 @@ import {
4
4
  makeQueueDefinition,
5
5
  SwapQueueContext,
6
6
  checkWaitingForNetworkChange,
7
- SwapProgressNotification,
8
7
  } from '@rango-dev/queue-manager-rango-preset';
9
8
  import { useWallets } from '@rango-dev/wallets-core';
10
9
  import {
@@ -33,7 +32,6 @@ function QueueManager(props: PropsWithChildren) {
33
32
  API_KEY: getConfig('API_KEY'),
34
33
  });
35
34
  }, []);
36
- const getOneOfWalletsDetails = useWalletsStore.use.getOneOfWalletsDetails();
37
35
 
38
36
  const { blockchains } = useMetaStore.use.meta();
39
37
  const connectedWallets = useWalletsStore.use.connectedWallets();
@@ -70,28 +68,6 @@ function QueueManager(props: PropsWithChildren) {
70
68
  return walletAndSupportedChainsNames(supportedChains);
71
69
  };
72
70
  const allProviders = providers();
73
- const notifier = (data: SwapProgressNotification) => {
74
- const lastStep = data.swap?.steps[data.swap.steps.length - 1];
75
- const outputAmount = data.step?.outputAmount || '';
76
- const step = data.step || lastStep;
77
-
78
- if (
79
- data.eventType === 'task_completed' ||
80
- (data.eventType === 'step_completed_with_output' &&
81
- lastStep?.id !== data.step?.id) ||
82
- !!outputAmount
83
- ) {
84
- const fromAccount = connectedWallets.find(
85
- (account) => account.chain === step?.fromBlockchain
86
- );
87
- const toAccount =
88
- step?.fromBlockchain !== step?.toBlockchain &&
89
- connectedWallets.find((wallet) => wallet.chain === step?.toBlockchain);
90
-
91
- fromAccount && getOneOfWalletsDetails(fromAccount);
92
- toAccount && getOneOfWalletsDetails(toAccount);
93
- }
94
- };
95
71
 
96
72
  const context: SwapQueueContext = {
97
73
  meta: {
@@ -112,7 +88,6 @@ function QueueManager(props: PropsWithChildren) {
112
88
  connect,
113
89
  state,
114
90
  isMobileWallet,
115
- notifier,
116
91
  };
117
92
 
118
93
  return (
@@ -0,0 +1,100 @@
1
+ import React, { PropsWithChildren } from 'react';
2
+ import { useMetaStore } from './store/meta';
3
+ import { Events, Provider } from '@rango-dev/wallets-core';
4
+ import { EventHandler } from '@rango-dev/wallets-core';
5
+ import { Network } from '@rango-dev/wallets-shared';
6
+ import {
7
+ prepareAccountsForWalletStore,
8
+ walletAndSupportedChainsNames,
9
+ } from './utils/wallets';
10
+ import { useWalletsStore } from './store/wallets';
11
+ import { isEvmBlockchain } from 'rango-sdk';
12
+ import './i18n';
13
+ import { useWalletProviders } from './hooks/useWalletProviders';
14
+ import { WidgetConfig } from './types';
15
+ import { createContext, useRef } from 'react';
16
+
17
+ type OnConnectHandler = (key: string) => void;
18
+ interface WidgetContextInterface {
19
+ onConnectWallet(handler: OnConnectHandler): void;
20
+ }
21
+
22
+ export const WidgetContext = createContext<WidgetContextInterface>({
23
+ onConnectWallet: () => {
24
+ return;
25
+ },
26
+ });
27
+
28
+ export function WidgetWallets(
29
+ props: PropsWithChildren<{
30
+ providers: WidgetConfig['wallets'];
31
+ }>
32
+ ) {
33
+ const { blockchains } = useMetaStore.use.meta();
34
+ const { providers } = useWalletProviders(props.providers);
35
+ const disconnectWallet = useWalletsStore.use.disconnectWallet();
36
+ const connectWallet = useWalletsStore.use.connectWallet();
37
+ const onConnectWalletHandler = useRef<OnConnectHandler>();
38
+
39
+ const evmBasedChainNames = blockchains
40
+ .filter(isEvmBlockchain)
41
+ .map((chain) => chain.name);
42
+
43
+ const onUpdateState: EventHandler = (
44
+ type,
45
+ event,
46
+ value,
47
+ state,
48
+ supportedChains
49
+ ) => {
50
+ if (event === Events.ACCOUNTS) {
51
+ if (value) {
52
+ const supportedChainNames: Network[] | null =
53
+ walletAndSupportedChainsNames(supportedChains);
54
+ const data = prepareAccountsForWalletStore(
55
+ type,
56
+ value,
57
+ evmBasedChainNames,
58
+ supportedChainNames
59
+ );
60
+ connectWallet(data);
61
+ } else {
62
+ disconnectWallet(type);
63
+ }
64
+ }
65
+ if (event === Events.ACCOUNTS && state.connected) {
66
+ const key = `${type}-${state.network}-${value}`;
67
+
68
+ if (state.connected) {
69
+ if (!!onConnectWalletHandler.current)
70
+ onConnectWalletHandler.current(key);
71
+ else
72
+ console.warn(
73
+ `onConnectWallet handler hasn't been set. Are you sure?`
74
+ );
75
+ }
76
+ }
77
+
78
+ if (event === Events.NETWORK && state.network) {
79
+ const key = `${type}-${state.network}`;
80
+ if (!!onConnectWalletHandler.current) onConnectWalletHandler.current(key);
81
+ else
82
+ console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
83
+ }
84
+ };
85
+ return (
86
+ <WidgetContext.Provider
87
+ value={{
88
+ onConnectWallet: (handler) => {
89
+ onConnectWalletHandler.current = handler;
90
+ },
91
+ }}>
92
+ <Provider
93
+ allBlockChains={blockchains}
94
+ providers={providers}
95
+ onUpdateState={onUpdateState}>
96
+ {props.children}
97
+ </Provider>
98
+ </WidgetContext.Provider>
99
+ );
100
+ }
package/src/Widget.tsx CHANGED
@@ -1,20 +1,16 @@
1
1
  import { SwapContainer, styled } from '@rango-dev/ui';
2
- import React, { useEffect, useMemo, useState } from 'react';
2
+ import React, {
3
+ PropsWithChildren,
4
+ useContext,
5
+ useEffect,
6
+ useMemo,
7
+ useState,
8
+ } from 'react';
3
9
  import { AppRouter } from './components/AppRouter';
4
- import { useMetaStore } from './store/meta';
5
- import { Events, Provider } from '@rango-dev/wallets-core';
6
- import { allProviders } from '@rango-dev/provider-all';
7
- import { EventHandler } from '@rango-dev/wallets-core/dist/wallet';
8
- import { Network, WalletType } from '@rango-dev/wallets-shared';
9
- import {
10
- prepareAccountsForWalletStore,
11
- walletAndSupportedChainsNames,
12
- } from './utils/wallets';
13
- import { useWalletsStore } from './store/wallets';
10
+ import { WalletType } from '@rango-dev/wallets-shared';
14
11
  import { Layout } from './components/Layout';
15
12
  import { globalFont } from './globalStyles';
16
13
  import { useTheme } from './hooks/useTheme';
17
- import { isEvmBlockchain } from 'rango-sdk';
18
14
  import { WidgetConfig } from './types';
19
15
  import useSelectLanguage from './hooks/useSelectLanguage';
20
16
  import './i18n';
@@ -22,6 +18,8 @@ import QueueManager from './QueueManager';
22
18
  import { useUiStore } from './store/ui';
23
19
  import { navigationRoutes } from './constants/navigationRoutes';
24
20
  import { initConfig } from './utils/configs';
21
+ import { WidgetContext, WidgetWallets } from './Wallets';
22
+ import { WidgetEvents } from './components/WidgetEvents';
25
23
 
26
24
  const MainContainer = styled('div', {
27
25
  width: '100%',
@@ -34,23 +32,17 @@ export type WidgetProps = {
34
32
  config?: WidgetConfig;
35
33
  };
36
34
 
37
- export const Widget: React.FC<WidgetProps> = ({ config }) => {
35
+ export function Main(props: PropsWithChildren<WidgetProps>) {
36
+ const { config } = props;
38
37
  globalFont(config?.theme?.fontFamily || 'Roboto');
39
38
 
40
- const { activeTheme } = useTheme({ ...config?.theme });
41
- const { blockchains } = useMetaStore.use.meta();
42
- const disconnectWallet = useWalletsStore.use.disconnectWallet();
43
- const connectWallet = useWalletsStore.use.connectWallet();
39
+ const { activeTheme } = useTheme(config?.theme || {});
44
40
  const { changeLanguage } = useSelectLanguage();
45
- const clearConnectedWallet = useWalletsStore.use.clearConnectedWallet();
46
41
  const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
47
42
  useState<string>('');
48
43
  const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
49
44
  const currentPage = useUiStore.use.currentPage();
50
-
51
- const evmBasedChainNames = blockchains
52
- .filter(isEvmBlockchain)
53
- .map((chain) => chain.name);
45
+ const widgetContext = useContext(WidgetContext);
54
46
 
55
47
  useMemo(() => {
56
48
  if (config?.apiKey) {
@@ -60,77 +52,40 @@ export const Widget: React.FC<WidgetProps> = ({ config }) => {
60
52
  }
61
53
  }, [config]);
62
54
 
63
- const onUpdateState: EventHandler = (
64
- type,
65
- event,
66
- value,
67
- state,
68
- supportedChains
69
- ) => {
70
- if (event === Events.ACCOUNTS) {
71
- if (value) {
72
- const supportedChainNames: Network[] | null =
73
- walletAndSupportedChainsNames(supportedChains);
74
- const data = prepareAccountsForWalletStore(
75
- type,
76
- value,
77
- evmBasedChainNames,
78
- supportedChainNames
79
- );
80
- connectWallet(data);
81
- } else {
82
- disconnectWallet(type);
83
- }
84
- }
85
- if (event === Events.ACCOUNTS && state.connected) {
86
- const key = `${type}-${state.network}-${value}`;
87
-
88
- if (state.connected) {
89
- setLastConnectedWalletWithNetwork(key);
90
- }
91
- }
92
-
93
- if (event === Events.NETWORK && state.network) {
94
- const key = `${type}-${state.network}`;
95
- setLastConnectedWalletWithNetwork(key);
96
- }
97
- };
98
- let providers = allProviders();
99
-
100
- useEffect(() => {
101
- const wallets = config?.wallets;
102
- clearConnectedWallet();
103
- providers = !wallets
104
- ? allProviders()
105
- : allProviders().filter((provider) => {
106
- const type = provider.config.type;
107
- return wallets.find((w) => w === type);
108
- });
109
- }, [config?.wallets]);
110
-
111
55
  useEffect(() => {
112
56
  changeLanguage(config?.language || 'en');
113
57
  }, [config?.language]);
114
58
 
59
+ useEffect(() => {
60
+ widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
61
+ }, []);
62
+
115
63
  return (
116
- <Provider
117
- allBlockChains={blockchains}
118
- providers={providers}
119
- onUpdateState={onUpdateState}>
120
- <MainContainer id="swap-container" className={activeTheme}>
121
- <QueueManager>
122
- <SwapContainer fixedHeight={currentPage !== navigationRoutes.home}>
123
- <AppRouter
124
- lastConnectedWallet={lastConnectedWalletWithNetwork}
125
- disconnectedWallet={disconnectedWallet}
126
- clearDisconnectedWallet={() => {
127
- setDisconnectedWallet(undefined);
128
- }}>
129
- <Layout config={config} />
130
- </AppRouter>
131
- </SwapContainer>
132
- </QueueManager>
133
- </MainContainer>
134
- </Provider>
64
+ <MainContainer id="swap-container" className={activeTheme}>
65
+ <QueueManager>
66
+ <WidgetEvents />
67
+ <SwapContainer fixedHeight={currentPage !== navigationRoutes.home}>
68
+ <AppRouter
69
+ lastConnectedWallet={lastConnectedWalletWithNetwork}
70
+ disconnectedWallet={disconnectedWallet}
71
+ clearDisconnectedWallet={() => {
72
+ setDisconnectedWallet(undefined);
73
+ }}>
74
+ <Layout config={config} />
75
+ </AppRouter>
76
+ </SwapContainer>
77
+ </QueueManager>
78
+ </MainContainer>
135
79
  );
136
- };
80
+ }
81
+
82
+ export function Widget(props: PropsWithChildren<WidgetProps>) {
83
+ if (!props.config?.externalWallets) {
84
+ return (
85
+ <WidgetWallets providers={props.config?.wallets}>
86
+ <Main {...props} />
87
+ </WidgetWallets>
88
+ );
89
+ }
90
+ return <Main {...props} />;
91
+ }
@@ -27,8 +27,6 @@ export function AppRouter({
27
27
  clearDisconnectedWallet: props.clearDisconnectedWallet,
28
28
  disconnectedWallet: props.disconnectedWallet,
29
29
  evmChains,
30
- // eslint-disable-next-line @typescript-eslint/no-empty-function
31
- notifier: () => {},
32
30
  canSwitchNetworkTo,
33
31
  });
34
32
 
@@ -0,0 +1,46 @@
1
+ import { useEffect } from 'react';
2
+ import {
3
+ useEvents,
4
+ MainEvents,
5
+ StepEventType,
6
+ StepExecutionEventStatus,
7
+ isApprovalTX,
8
+ } from '@rango-dev/queue-manager-rango-preset';
9
+ import { useWalletsStore } from '../store/wallets';
10
+
11
+ export function WidgetEvents() {
12
+ const connectedWallets = useWalletsStore.use.connectedWallets();
13
+ const getOneOfWalletsDetails = useWalletsStore.use.getOneOfWalletsDetails();
14
+
15
+ const widgetEvents = useEvents();
16
+
17
+ useEffect(() => {
18
+ widgetEvents.on(MainEvents.StepEvent, (widgetEvent) => {
19
+ const { event, step } = widgetEvent;
20
+ console.log({ event: widgetEvent });
21
+ const shouldRefetchBalance =
22
+ (event.type === StepEventType.TX_EXECUTION &&
23
+ event.status === StepExecutionEventStatus.TX_SENT &&
24
+ isApprovalTX(step)) ||
25
+ event.type === StepEventType.SUCCEEDED;
26
+
27
+ if (shouldRefetchBalance) {
28
+ const fromAccount = connectedWallets.find(
29
+ (account) => account.chain === step?.fromBlockchain
30
+ );
31
+ const toAccount =
32
+ step?.fromBlockchain !== step?.toBlockchain &&
33
+ connectedWallets.find(
34
+ (wallet) => wallet.chain === step?.toBlockchain
35
+ );
36
+
37
+ fromAccount && getOneOfWalletsDetails(fromAccount);
38
+ toAccount && getOneOfWalletsDetails(toAccount);
39
+ }
40
+ });
41
+
42
+ return () => widgetEvents.all.clear();
43
+ }, [widgetEvents, connectedWallets.length]);
44
+
45
+ return null;
46
+ }
@@ -2,10 +2,12 @@ import { useTranslation } from 'react-i18next';
2
2
 
3
3
  export default function useSelectLanguage() {
4
4
  const { i18n } = useTranslation();
5
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
5
6
  // @ts-ignore
6
7
  const currentLanguage = i18n?.language || 'en';
7
8
  const changeLanguage = (value: string) => {
8
9
  if (currentLanguage !== value) {
10
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
9
11
  // @ts-ignore
10
12
  i18n.changeLanguage(value);
11
13
  }
@@ -0,0 +1,20 @@
1
+ import { ProviderInterface } from '@rango-dev/wallets-core';
2
+ import { useEffect } from 'react';
3
+ import { useWalletsStore } from '../store/wallets';
4
+ import { WidgetConfig } from '../types';
5
+ import { matchAndGenerateProviders } from '../utils/providers';
6
+
7
+ export function useWalletProviders(providers: WidgetConfig['wallets']) {
8
+ const clearConnectedWallet = useWalletsStore.use.clearConnectedWallet();
9
+ let generateProviders: ProviderInterface[] =
10
+ matchAndGenerateProviders(undefined);
11
+
12
+ useEffect(() => {
13
+ clearConnectedWallet();
14
+ generateProviders = matchAndGenerateProviders(providers);
15
+ }, [providers]);
16
+
17
+ return {
18
+ providers: generateProviders,
19
+ };
20
+ }
package/src/index.ts CHANGED
@@ -6,6 +6,16 @@ import {
6
6
  } from './types';
7
7
  import { WidgetProps, Widget } from './Widget';
8
8
  import { WalletType } from '@rango-dev/wallets-shared';
9
+ import { WidgetWallets } from './Wallets';
10
+ import { useWallets } from '@rango-dev/wallets-core';
11
+ import {
12
+ useEvents as useWidgetEvents,
13
+ MainEvents,
14
+ RouteEvent,
15
+ StepEvent,
16
+ Route,
17
+ Step,
18
+ } from '@rango-dev/queue-manager-rango-preset';
9
19
 
10
20
  export type {
11
21
  WidgetConfig,
@@ -14,5 +24,10 @@ export type {
14
24
  WidgetColors,
15
25
  BlockchainAndTokenConfig,
16
26
  WidgetProps,
27
+ RouteEvent,
28
+ StepEvent,
29
+ Route,
30
+ Step,
17
31
  };
18
- export { Widget };
32
+ export { Widget, WidgetWallets };
33
+ export { useWallets, useWidgetEvents, MainEvents };
@@ -159,8 +159,7 @@ export function Home() {
159
159
  onClick={() => {
160
160
  switchFromAndTo();
161
161
  setCount((prev) => prev + 1);
162
- }}
163
- >
162
+ }}>
164
163
  <VerticalSwapIcon size={32} />
165
164
  {isRouterInContext && <SwithFromAndTo count={count} />}
166
165
  </Button>
@@ -216,8 +215,7 @@ export function Home() {
216
215
  else {
217
216
  navigate(navigationRoutes.confirmSwap, { replace: true });
218
217
  }
219
- }}
220
- >
218
+ }}>
221
219
  {swapButtonState.title}
222
220
  </Button>
223
221
  <BottomLogo />
@@ -22,9 +22,11 @@ import { useTranslation } from 'react-i18next';
22
22
  import { useMetaStore } from '../store/meta';
23
23
  import { Spinner } from '@rango-dev/ui';
24
24
  import { LoadingFailedAlert } from '@rango-dev/ui';
25
+ import { WidgetConfig } from '../types';
26
+ import { configWalletsToWalletName } from '../utils/providers';
25
27
 
26
28
  interface PropTypes {
27
- supportedWallets?: WalletType[];
29
+ supportedWallets: WidgetConfig['wallets'];
28
30
  multiWallets: boolean;
29
31
  }
30
32
 
@@ -48,13 +50,16 @@ const LoaderContainer = styled('div', {
48
50
  const AlertContainer = styled('div', {
49
51
  paddingBottom: '$16',
50
52
  });
53
+
54
+ const ALL_SUPPORTED_WALLETS = Object.values(WalletTypes);
55
+
51
56
  export function WalletsPage({ supportedWallets, multiWallets }: PropTypes) {
52
57
  const { navigateBackFrom } = useNavigateBack();
53
58
  const { state, disconnect, getWalletInfo, connect } = useWallets();
54
59
  const wallets = getlistWallet(
55
60
  state,
56
61
  getWalletInfo,
57
- supportedWallets || Object.values(WalletTypes)
62
+ configWalletsToWalletName(supportedWallets) || ALL_SUPPORTED_WALLETS
58
63
  );
59
64
  const walletsRef = useRef<WalletInfo[]>();
60
65
 
@@ -112,8 +117,7 @@ export function WalletsPage({ supportedWallets, multiWallets }: PropTypes) {
112
117
  <SecondaryPage
113
118
  title={t('Select Wallet') || ''}
114
119
  textField={false}
115
- onBack={navigateBackFrom.bind(null, navigationRoutes.wallets)}
116
- >
120
+ onBack={navigateBackFrom.bind(null, navigationRoutes.wallets)}>
117
121
  <>
118
122
  {walletErrorMessage && (
119
123
  <AlertContainer>
@@ -1,5 +1,6 @@
1
1
  import { Asset } from 'rango-sdk';
2
2
  import { WalletType } from '@rango-dev/wallets-shared';
3
+ import { ProviderInterface } from '@rango-dev/wallets-core';
3
4
 
4
5
  /**
5
6
  * The above type defines a set of optional color properties for a widget.
@@ -100,6 +101,9 @@ export type BlockchainAndTokenConfig = {
100
101
  * @property {WidgetTheme} theme - The `theme` property is a part of the `WidgetConfig` type and is
101
102
  * used to specify the visual theme of the widget. It is of type `WidgetTheme`, which is an interface
102
103
  * that defines the various properties of the theme, such as colors, fonts, and others.
104
+ * @property {boolean} externalWallets
105
+ * If `externalWallets` is `true`, you should add `WidgetWallets` to your app.
106
+
103
107
  */
104
108
  export type WidgetConfig = {
105
109
  apiKey: string;
@@ -108,9 +112,10 @@ export type WidgetConfig = {
108
112
  from?: BlockchainAndTokenConfig;
109
113
  to?: BlockchainAndTokenConfig;
110
114
  liquiditySources?: string[];
111
- wallets?: WalletType[];
115
+ wallets?: (WalletType | ProviderInterface)[];
112
116
  multiWallets?: boolean;
113
117
  customDestination?: boolean;
114
118
  language?: string;
115
119
  theme?: WidgetTheme;
120
+ externalWallets?: boolean;
116
121
  };