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

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 +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,OA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CG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;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.8",
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.8",
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.7",
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",
@@ -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,7 @@ 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';
25
22
 
26
23
  const MainContainer = styled('div', {
27
24
  width: '100%',
@@ -34,23 +31,17 @@ export type WidgetProps = {
34
31
  config?: WidgetConfig;
35
32
  };
36
33
 
37
- export const Widget: React.FC<WidgetProps> = ({ config }) => {
34
+ export function Main(props: PropsWithChildren<WidgetProps>) {
35
+ const { config } = props;
38
36
  globalFont(config?.theme?.fontFamily || 'Roboto');
39
37
 
40
- const { activeTheme } = useTheme({ ...config?.theme });
41
- const { blockchains } = useMetaStore.use.meta();
42
- const disconnectWallet = useWalletsStore.use.disconnectWallet();
43
- const connectWallet = useWalletsStore.use.connectWallet();
38
+ const { activeTheme } = useTheme(config?.theme || {});
44
39
  const { changeLanguage } = useSelectLanguage();
45
- const clearConnectedWallet = useWalletsStore.use.clearConnectedWallet();
46
40
  const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
47
41
  useState<string>('');
48
42
  const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
49
43
  const currentPage = useUiStore.use.currentPage();
50
-
51
- const evmBasedChainNames = blockchains
52
- .filter(isEvmBlockchain)
53
- .map((chain) => chain.name);
44
+ const widgetContext = useContext(WidgetContext);
54
45
 
55
46
  useMemo(() => {
56
47
  if (config?.apiKey) {
@@ -60,77 +51,39 @@ export const Widget: React.FC<WidgetProps> = ({ config }) => {
60
51
  }
61
52
  }, [config]);
62
53
 
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
54
  useEffect(() => {
112
55
  changeLanguage(config?.language || 'en');
113
56
  }, [config?.language]);
114
57
 
58
+ useEffect(() => {
59
+ widgetContext.onConnectWallet(setLastConnectedWalletWithNetwork);
60
+ }, []);
61
+
115
62
  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>
63
+ <MainContainer id="swap-container" className={activeTheme}>
64
+ <QueueManager>
65
+ <SwapContainer fixedHeight={currentPage !== navigationRoutes.home}>
66
+ <AppRouter
67
+ lastConnectedWallet={lastConnectedWalletWithNetwork}
68
+ disconnectedWallet={disconnectedWallet}
69
+ clearDisconnectedWallet={() => {
70
+ setDisconnectedWallet(undefined);
71
+ }}>
72
+ <Layout config={config} />
73
+ </AppRouter>
74
+ </SwapContainer>
75
+ </QueueManager>
76
+ </MainContainer>
135
77
  );
136
- };
78
+ }
79
+
80
+ export function Widget(props: PropsWithChildren<WidgetProps>) {
81
+ if (!props.config?.externalWallets) {
82
+ return (
83
+ <WidgetWallets providers={props.config?.wallets}>
84
+ <Main {...props} />
85
+ </WidgetWallets>
86
+ );
87
+ }
88
+ return <Main {...props} />;
89
+ }
@@ -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,8 @@ 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';
9
11
 
10
12
  export type {
11
13
  WidgetConfig,
@@ -15,4 +17,5 @@ export type {
15
17
  BlockchainAndTokenConfig,
16
18
  WidgetProps,
17
19
  };
18
- export { Widget };
20
+ export { Widget, WidgetWallets };
21
+ export { useWallets };
@@ -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
  };
@@ -0,0 +1,53 @@
1
+ import { allProviders } from '@rango-dev/provider-all';
2
+ import { ProviderInterface } from '@rango-dev/wallets-core';
3
+ import { WidgetConfig } from '../types';
4
+
5
+ /**
6
+ *
7
+ * Generate a list of providers by passing a provider name (e.g. metamask) or a custom provider which implemented ProviderInterface.
8
+ * @returns ProviderInterface[] a list of ProviderInterface
9
+ *
10
+ */
11
+ export function matchAndGenerateProviders(
12
+ providers: WidgetConfig['wallets']
13
+ ): ProviderInterface[] {
14
+ const all = allProviders();
15
+
16
+ if (providers) {
17
+ const selectedProviders: ProviderInterface[] = [];
18
+
19
+ providers.forEach((requestedProvider) => {
20
+ // There are two types of provider we get, the first one is only passing the wallet name
21
+ // then we will match the wallet name with our providers (@rango-dev/provider-*).
22
+ // The second way is passing a custom provider which implemented ProviderInterface.
23
+ if (typeof requestedProvider === 'string') {
24
+ const result: ProviderInterface | undefined = all.find((provider) => {
25
+ return provider.config.type === requestedProvider;
26
+ });
27
+ if (result) {
28
+ selectedProviders.push(result);
29
+ } else {
30
+ console.warn(
31
+ `Couldn't find ${requestedProvider} provider. Please make sure you are passing the correct name.`
32
+ );
33
+ }
34
+ } else {
35
+ // It's a custom provider so we directly push it to the list.
36
+ selectedProviders.push(requestedProvider);
37
+ }
38
+ });
39
+ return selectedProviders;
40
+ }
41
+
42
+ return all;
43
+ }
44
+
45
+ export function configWalletsToWalletName(
46
+ config: WidgetConfig['wallets']
47
+ ): string[] {
48
+ const providers = matchAndGenerateProviders(config);
49
+ const names = providers.map((provider) => {
50
+ return provider.config.type;
51
+ });
52
+ return names;
53
+ }
@@ -1,2 +0,0 @@
1
- export declare const pendingSwap: any;
2
- //# sourceMappingURL=pendingSwap.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pendingSwap.d.ts","sourceRoot":"","sources":["../../src/mockData/pendingSwap.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,WAAW,EAAE,GA6lBzB,CAAC"}