@rango-dev/widget-embedded 0.1.16-next.0 → 0.1.16-next.2

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 (45) hide show
  1. package/dist/QueueManager.d.ts.map +1 -1
  2. package/dist/components/Layout.d.ts.map +1 -1
  3. package/dist/index.d.ts +9 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/pages/Home.d.ts.map +1 -1
  6. package/dist/store/wallets.d.ts +6 -15
  7. package/dist/store/wallets.d.ts.map +1 -1
  8. package/dist/types/index.d.ts +1 -0
  9. package/dist/types/index.d.ts.map +1 -1
  10. package/dist/types/wallets.d.ts +7 -0
  11. package/dist/types/wallets.d.ts.map +1 -0
  12. package/dist/utils/routing.d.ts +2 -3
  13. package/dist/utils/routing.d.ts.map +1 -1
  14. package/dist/utils/swap.d.ts +8 -9
  15. package/dist/utils/swap.d.ts.map +1 -1
  16. package/dist/utils/wallets.d.ts +11 -12
  17. package/dist/utils/wallets.d.ts.map +1 -1
  18. package/dist/widget-embedded.cjs.development.js +82 -83
  19. package/dist/widget-embedded.cjs.development.js.map +1 -1
  20. package/dist/widget-embedded.cjs.production.min.js +82 -83
  21. package/dist/widget-embedded.cjs.production.min.js.map +1 -1
  22. package/dist/widget-embedded.esm.js +82 -83
  23. package/dist/widget-embedded.esm.js.map +1 -1
  24. package/package.json +2 -6
  25. package/src/QueueManager.tsx +4 -5
  26. package/src/components/Layout.tsx +27 -23
  27. package/src/components/TokenInfo.tsx +5 -5
  28. package/src/hooks/useConfirmSwap.ts +2 -2
  29. package/src/index.tsx +144 -14
  30. package/src/pages/ConfirmSwapPage.tsx +2 -2
  31. package/src/pages/Home.tsx +5 -3
  32. package/src/store/bestRoute.ts +7 -7
  33. package/src/store/wallets.ts +32 -46
  34. package/src/types/index.ts +1 -0
  35. package/src/types/wallets.ts +7 -0
  36. package/src/utils/routing.ts +2 -3
  37. package/src/utils/swap.ts +12 -11
  38. package/src/utils/wallets.ts +59 -52
  39. package/dist/App.d.ts +0 -9
  40. package/dist/App.d.ts.map +0 -1
  41. package/dist/lib.d.ts +0 -10
  42. package/dist/lib.d.ts.map +0 -1
  43. package/src/App.tsx +0 -123
  44. package/src/app.css +0 -12
  45. package/src/lib.tsx +0 -144
package/src/utils/swap.ts CHANGED
@@ -6,14 +6,14 @@ import {
6
6
  SwapResult,
7
7
  Token,
8
8
  } from 'rango-sdk';
9
- import { Account } from '../store/wallets';
9
+ import { ConnectedWallet } from '../store/wallets';
10
10
  import { ZERO } from '../constants/numbers';
11
11
  import { numberToString } from './numbers';
12
12
  import { WalletType } from '@rango-dev/wallets-shared';
13
13
  import { getRequiredBalanceOfWallet } from './routing';
14
- import { getRequiredChains, SelectedWallet } from './wallets';
14
+ import { getRequiredChains } from './wallets';
15
15
  import { LoadingStatus } from '../store/meta';
16
- import { ConvertedToken, SwapButtonState } from '../types';
16
+ import { ConvertedToken, SwapButtonState, Wallet } from '../types';
17
17
  import {
18
18
  PendingSwapNetworkStatus,
19
19
  PendingSwapStep,
@@ -122,7 +122,7 @@ export function LimitErrorMessage(bestRoute: BestRouteResponse | null): {
122
122
 
123
123
  export function getSwapButtonState(
124
124
  loadingMetaStatus: LoadingStatus,
125
- accounts: Account[],
125
+ connectedWallets: ConnectedWallet[],
126
126
  loading: boolean,
127
127
  bestRoute: BestRouteResponse | null,
128
128
  hasLimitError: boolean,
@@ -133,7 +133,8 @@ export function getSwapButtonState(
133
133
  ): SwapButtonState {
134
134
  if (loadingMetaStatus !== 'success')
135
135
  return { title: 'Connect Wallet', disabled: true };
136
- if (accounts.length == 0) return { title: 'Connect Wallet', disabled: false };
136
+ if (connectedWallets.length == 0)
137
+ return { title: 'Connect Wallet', disabled: false };
137
138
  if (loading) return { title: 'Finding Best Route...', disabled: true };
138
139
  else if (!inputAmount || inputAmount === '0')
139
140
  return { title: 'Enter an amount', disabled: true };
@@ -276,7 +277,7 @@ export function hasProperSlippage(
276
277
 
277
278
  export function hasEnoughBalance(
278
279
  route: BestRouteResponse,
279
- selectedWallets: SelectedWallet[]
280
+ selectedWallets: Wallet[]
280
281
  ) {
281
282
  const fee = route.validationStatus;
282
283
 
@@ -297,7 +298,7 @@ export function hasEnoughBalance(
297
298
 
298
299
  export function hasEnoughBalanceAndProperSlippage(
299
300
  route: BestRouteResponse,
300
- selectedWallets: SelectedWallet[],
301
+ selectedWallets: Wallet[],
301
302
  userSlippage: string,
302
303
  minRequiredSlippage: string | null
303
304
  ): boolean {
@@ -311,8 +312,8 @@ export function createBestRouteRequestBody(
311
312
  fromToken: Token,
312
313
  toToken: Token,
313
314
  inputAmount: string,
314
- wallets: Account[],
315
- selectedWallets: SelectedWallet[],
315
+ wallets: ConnectedWallet[],
316
+ selectedWallets: Wallet[],
316
317
  disabledLiquiditySources: string[],
317
318
  slippage: number,
318
319
  affiliateRef: string | null,
@@ -379,7 +380,7 @@ export function createBestRouteRequestBody(
379
380
  return requestBody;
380
381
  }
381
382
 
382
- export function getWalletsForNewSwap(selectedWallets: SelectedWallet[]) {
383
+ export function getWalletsForNewSwap(selectedWallets: Wallet[]) {
383
384
  const wallets = selectedWallets.reduce(
384
385
  (
385
386
  selectedWalletsMap: {
@@ -432,7 +433,7 @@ export function isOutputAmountChangedALot(
432
433
 
433
434
  export function getBalanceWarnings(
434
435
  route: BestRouteResponse,
435
- selectedWallets: SelectedWallet[]
436
+ selectedWallets: Wallet[]
436
437
  ) {
437
438
  const fee = route.validationStatus;
438
439
  const requiredWallets = getRequiredChains(route);
@@ -22,11 +22,12 @@ import {
22
22
  } from 'rango-sdk';
23
23
  import { isCosmosBlockchain } from 'rango-types';
24
24
  import { readAccountAddress } from '@rango-dev/wallets-core';
25
- import { Account, Balance, TokenBalance } from '../store/wallets';
25
+ import { ConnectedWallet, TokenBalance } from '../store/wallets';
26
26
  import { numberToString } from './numbers';
27
27
  import BigNumber from 'bignumber.js';
28
28
  import { TokenWithBalance } from '../pages/SelectTokenPage';
29
29
  import { ZERO } from '../constants/numbers';
30
+ import { Wallet } from '../types';
30
31
 
31
32
  export function getStateWallet(state: WalletState): WalletStatus {
32
33
  switch (true) {
@@ -96,11 +97,11 @@ export function prepareAccountsForWalletStore(
96
97
  accounts: string[],
97
98
  evmBasedChains: string[],
98
99
  supportedChainNames: Network[] | null
99
- ): Account[] {
100
- const result: Account[] = [];
100
+ ): Wallet[] {
101
+ const result: Wallet[] = [];
101
102
 
102
103
  function addAccount(network: Network, address: string) {
103
- const newAccount: Account = {
104
+ const newAccount: Wallet = {
104
105
  address,
105
106
  chain: network,
106
107
  walletType: wallet,
@@ -171,56 +172,57 @@ export function getRequiredChains(route: BestRouteResponse | null) {
171
172
  return wallets;
172
173
  }
173
174
 
174
- export interface SelectedWallet extends Account {}
175
- type Blockchain = { name: string; accounts: Balance[] };
175
+ type Blockchain = { name: string; accounts: ConnectedWallet[] };
176
176
 
177
177
  export function getSelectableWallets(
178
- accounts: Account[],
179
- selectedWallets: SelectedWallet[],
178
+ connectedWallets: ConnectedWallet[],
179
+ selectedWallets: Wallet[],
180
180
  getWalletInfo: (type: WalletType) => WalletInfo
181
181
  ): SelectableWallet[] {
182
- const connectedWallets: SelectableWallet[] = accounts.map((account) => ({
183
- address: account.address,
184
- walletType: account.walletType,
185
- chain: account.chain,
186
- image: getWalletInfo(account.walletType).img,
187
- name: getWalletInfo(account.walletType).name,
188
- selected: !!selectedWallets.find(
189
- (wallet) =>
190
- wallet.chain === account.chain &&
191
- wallet.walletType === account.walletType
192
- ),
193
- }));
194
-
195
- return connectedWallets;
182
+ const selectableWallets: SelectableWallet[] = connectedWallets.map(
183
+ (connectedWallet) => ({
184
+ address: connectedWallet.address,
185
+ walletType: connectedWallet.walletType,
186
+ chain: connectedWallet.chain,
187
+ image: getWalletInfo(connectedWallet.walletType).img,
188
+ name: getWalletInfo(connectedWallet.walletType).name,
189
+ selected: !!selectedWallets.find(
190
+ (selectedWallet) =>
191
+ selectedWallet.chain === connectedWallet.chain &&
192
+ selectedWallet.walletType === connectedWallet.walletType
193
+ ),
194
+ })
195
+ );
196
+
197
+ return selectableWallets;
196
198
  }
197
199
 
198
200
  export function getBalanceFromWallet(
199
- balances: Balance[],
201
+ connectedWallets: ConnectedWallet[],
200
202
  chain: string,
201
203
  symbol: string,
202
204
  address: string | null
203
205
  ): TokenBalance | null {
204
- if (balances.length === 0) return null;
206
+ if (connectedWallets.length === 0) return null;
205
207
 
206
- const selectedChainBalances = balances.filter(
207
- (balance) => balance.chain === chain
208
+ const selectedChainWallets = connectedWallets.filter(
209
+ (wallet) => wallet.chain === chain
208
210
  );
209
- if (selectedChainBalances.length === 0) return null;
211
+ if (selectedChainWallets.length === 0) return null;
210
212
 
211
213
  return (
212
- selectedChainBalances
214
+ selectedChainWallets
213
215
  .map(
214
- (a) =>
215
- a.balances?.find(
216
- (bl) =>
217
- (address !== null && bl.address === address) ||
216
+ (wallet) =>
217
+ wallet.balances?.find(
218
+ (balance) =>
219
+ (address !== null && balance.address === address) ||
218
220
  (address === null &&
219
- bl.address === address &&
220
- bl.symbol === symbol)
221
+ balance.address === address &&
222
+ balance.symbol === symbol)
221
223
  ) || null
222
224
  )
223
- .filter((b) => b !== null)
225
+ .filter((balance) => balance !== null)
224
226
  .sort(
225
227
  (a, b) => parseFloat(b?.amount || '0') - parseFloat(a?.amount || '1')
226
228
  )
@@ -228,19 +230,22 @@ export function getBalanceFromWallet(
228
230
  );
229
231
  }
230
232
 
231
- export function isAccountAndBalanceMatched(account: Account, balance: Balance) {
233
+ export function isAccountAndWalletMatched(
234
+ account: Wallet,
235
+ connectedWallet: ConnectedWallet
236
+ ) {
232
237
  return (
233
- account.address === balance.address &&
234
- account.chain === balance.chain &&
235
- account.walletType === balance.walletType
238
+ account.address === connectedWallet.address &&
239
+ account.chain === connectedWallet.chain &&
240
+ account.walletType === connectedWallet.walletType
236
241
  );
237
242
  }
238
243
 
239
244
  export function makeBalanceFor(
240
- account: Account,
245
+ wallet: Wallet,
241
246
  retrivedBalance: WalletDetail,
242
247
  tokens: Token[]
243
- ): Balance {
248
+ ): ConnectedWallet {
244
249
  const {
245
250
  address,
246
251
  blockChain: chain,
@@ -253,7 +258,7 @@ export function makeBalanceFor(
253
258
  loading: false,
254
259
  error: false,
255
260
  explorerUrl,
256
- walletType: account.walletType,
261
+ walletType: wallet.walletType,
257
262
  balances:
258
263
  balances?.map((tokenBalance) => ({
259
264
  chain,
@@ -277,14 +282,16 @@ export function makeBalanceFor(
277
282
  };
278
283
  }
279
284
 
280
- export function resetBalanceState(balance: Balance): Balance {
281
- return { ...balance, loading: false, error: true };
285
+ export function resetConnectedWalletState(
286
+ connectedWallet: ConnectedWallet
287
+ ): ConnectedWallet {
288
+ return { ...connectedWallet, loading: false, error: true };
282
289
  }
283
290
 
284
- export const calculateWalletUsdValue = (balance: Balance[]) => {
291
+ export const calculateWalletUsdValue = (connectedWallet: ConnectedWallet[]) => {
285
292
  const uniqueAccountAddresses = new Set<string | null>();
286
- const uniqueBalane: Balance[] = balance?.reduce(
287
- (acc: Balance[], current: Balance) => {
293
+ const uniqueBalane: ConnectedWallet[] = connectedWallet?.reduce(
294
+ (acc: ConnectedWallet[], current: ConnectedWallet) => {
288
295
  return acc.findIndex(
289
296
  (i) => i.address === current.address && i.chain === current.chain
290
297
  ) === -1
@@ -391,13 +398,13 @@ export const getKeplrCompatibleConnectedWallets = (
391
398
 
392
399
  export function getTokensWithBalance(
393
400
  tokens: TokenWithBalance[],
394
- balances: Balance[]
401
+ connectedWallets: ConnectedWallet[]
395
402
  ): TokenWithBalance[] {
396
403
  return tokens.map(({ balance, ...otherProps }) => {
397
404
  const tokenAmount = numberToString(
398
405
  new BigNumber(
399
406
  getBalanceFromWallet(
400
- balances,
407
+ connectedWallets,
401
408
  otherProps.blockchain,
402
409
  otherProps.symbol,
403
410
  otherProps.address
@@ -410,7 +417,7 @@ export function getTokensWithBalance(
410
417
  tokenUsdValue = numberToString(
411
418
  new BigNumber(
412
419
  getBalanceFromWallet(
413
- balances,
420
+ connectedWallets,
414
421
  otherProps.blockchain,
415
422
  otherProps.symbol,
416
423
  otherProps.address
@@ -430,7 +437,7 @@ export function getTokensWithBalance(
430
437
  export function getSortedTokens(
431
438
  chain: BlockchainMeta | null,
432
439
  tokens: Token[],
433
- balances: Balance[],
440
+ connectedWallets: ConnectedWallet[],
434
441
  otherChainTokens: TokenWithBalance[]
435
442
  ): TokenWithBalance[] {
436
443
  const fromChainEqueulsToToChain = chain?.name === otherChainTokens[0]?.name;
@@ -438,7 +445,7 @@ export function getSortedTokens(
438
445
  const filteredTokens = tokens.filter(
439
446
  (token) => token.blockchain === chain?.name
440
447
  );
441
- return sortTokens(getTokensWithBalance(filteredTokens, balances));
448
+ return sortTokens(getTokensWithBalance(filteredTokens, connectedWallets));
442
449
  }
443
450
 
444
451
  export function tokensAreEqual(
package/dist/App.d.ts DELETED
@@ -1,9 +0,0 @@
1
- /// <reference types="react" />
2
- import './app.css';
3
- import { WidgetConfig } from './types';
4
- import './i18n';
5
- export declare type WidgetProps = {
6
- config?: WidgetConfig;
7
- };
8
- export declare function App({ config }: WidgetProps): JSX.Element;
9
- //# sourceMappingURL=App.d.ts.map
package/dist/App.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["src/App.tsx"],"names":[],"mappings":";AAIA,OAAO,WAAW,CAAC;AAenB,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,QAAQ,CAAC;AAOhB,oBAAY,WAAW,GAAG;IACxB,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB,CAAC;AAEF,wBAAgB,GAAG,CAAC,EAAE,MAAM,EAAE,EAAE,WAAW,eA2F1C"}
package/dist/lib.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import React from 'react';
2
- import { WalletType } from '@rango-dev/wallets-shared';
3
- import { WidgetTheme, WidgetConfig, WidgetColors, BlockchainAndTokenConfig } from './types';
4
- import './i18n';
5
- export { WidgetConfig, WalletType, WidgetTheme, WidgetColors, BlockchainAndTokenConfig, };
6
- export declare type WidgetProps = {
7
- config?: WidgetConfig;
8
- };
9
- export declare const Widget: React.FC<WidgetProps>;
10
- //# sourceMappingURL=lib.d.ts.map
package/dist/lib.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["src/lib.tsx"],"names":[],"mappings":"AACA,OAAO,KAAuC,MAAM,OAAO,CAAC;AAM5D,OAAO,EAAW,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAUhE,OAAO,EACL,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAEjB,OAAO,QAAQ,CAAC;AAMhB,OAAO,EACL,YAAY,EACZ,UAAU,EACV,WAAW,EACX,YAAY,EACZ,wBAAwB,GACzB,CAAC;AAEF,oBAAY,WAAW,GAAG;IACxB,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,WAAW,CAqGxC,CAAC"}
package/src/App.tsx DELETED
@@ -1,123 +0,0 @@
1
- import { SwapContainer } from '@rango-dev/ui';
2
- import React, { useEffect, useMemo, useState } from 'react';
3
- import { AppRouter } from './components/AppRouter';
4
- import { useMetaStore } from './store/meta';
5
- import './app.css';
6
- import { Events, Provider } from '@rango-dev/wallets-core';
7
- import { allProviders } from '@rango-dev/provider-all';
8
- import { EventHandler } from '@rango-dev/wallets-core/dist/wallet';
9
- import { Network, WalletType } from '@rango-dev/wallets-shared';
10
- import {
11
- prepareAccountsForWalletStore,
12
- walletAndSupportedChainsNames,
13
- } from './utils/wallets';
14
- import { useWalletsStore } from './store/wallets';
15
- import { Layout } from './components/Layout';
16
- import { globalFont } from './globalStyles';
17
- import { useTheme } from './hooks/useTheme';
18
- import QueueManager from './QueueManager';
19
- import { isEvmBlockchain } from 'rango-sdk';
20
- import { WidgetConfig } from './types';
21
- import './i18n';
22
- import { useUiStore } from './store/ui';
23
- import { navigationRoutes } from './constants/navigationRoutes';
24
- import { initConfig } from './utils/configs';
25
-
26
- const providers = allProviders();
27
-
28
- export type WidgetProps = {
29
- config?: WidgetConfig;
30
- };
31
-
32
- export function App({ config }: WidgetProps) {
33
- globalFont(config?.theme?.fontFamily || 'Roboto');
34
- const { activeTheme } = useTheme({ ...config?.theme });
35
- useMemo(() => {
36
- if (config?.apiKey) {
37
- initConfig({
38
- API_KEY: config?.apiKey,
39
- });
40
- }
41
- }, [config]);
42
-
43
- const { blockchains } = useMetaStore.use.meta();
44
- const disconnectWallet = useWalletsStore.use.disconnectWallet();
45
- const connectWallet = useWalletsStore.use.connectWallet();
46
- const currentPage = useUiStore.use.currentPage();
47
-
48
- const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
49
- useState<string>('');
50
- const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
51
-
52
- const evmBasedChainNames = blockchains
53
- .filter(isEvmBlockchain)
54
- .map((chain) => chain.name);
55
-
56
- const themeBackground = activeTheme?.colors?.background?.value;
57
-
58
- useEffect(() => {
59
- document.body.style.background = themeBackground;
60
- }, [themeBackground]);
61
-
62
- const onUpdateState: EventHandler = (
63
- type,
64
- event,
65
- value,
66
- state,
67
- supportedChains
68
- ) => {
69
- if (event === Events.ACCOUNTS) {
70
- if (value) {
71
- const supportedChainNames: Network[] | null =
72
- walletAndSupportedChainsNames(supportedChains);
73
- const data = prepareAccountsForWalletStore(
74
- type,
75
- value,
76
- evmBasedChainNames,
77
- supportedChainNames
78
- );
79
- connectWallet(data);
80
- } else {
81
- disconnectWallet(type);
82
- setDisconnectedWallet(type);
83
- }
84
- }
85
-
86
- if (event === Events.ACCOUNTS && state.connected) {
87
- const key = `${type}-${state.network}-${value}`;
88
-
89
- if (state.connected) {
90
- setLastConnectedWalletWithNetwork(key);
91
- }
92
- }
93
-
94
- if (event === Events.NETWORK && state.network) {
95
- const key = `${type}-${state.network}`;
96
- setLastConnectedWalletWithNetwork(key);
97
- }
98
- };
99
-
100
- return (
101
- <Provider
102
- allBlockChains={blockchains}
103
- providers={providers}
104
- onUpdateState={onUpdateState}
105
- >
106
- <div id="pageContainer" className={activeTheme}>
107
- <QueueManager>
108
- <SwapContainer fixedHeight={currentPage !== navigationRoutes.home}>
109
- <AppRouter
110
- lastConnectedWallet={lastConnectedWalletWithNetwork}
111
- disconnectedWallet={disconnectedWallet}
112
- clearDisconnectedWallet={() => {
113
- setDisconnectedWallet(undefined);
114
- }}
115
- >
116
- <Layout config={config} />
117
- </AppRouter>
118
- </SwapContainer>
119
- </QueueManager>
120
- </div>
121
- </Provider>
122
- );
123
- }
package/src/app.css DELETED
@@ -1,12 +0,0 @@
1
- @import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
2
-
3
- #app {
4
- height: 100vh;
5
- }
6
- #pageContainer {
7
- display: flex;
8
- align-items: center;
9
- justify-content: center;
10
- height: 100%;
11
- width: 100%;
12
- }
package/src/lib.tsx DELETED
@@ -1,144 +0,0 @@
1
- import { SwapContainer } from '@rango-dev/ui';
2
- import React, { useEffect, useMemo, useState } from 'react';
3
- 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';
14
- import { Layout } from './components/Layout';
15
- import { globalFont } from './globalStyles';
16
- import { useTheme } from './hooks/useTheme';
17
- import { isEvmBlockchain } from 'rango-sdk';
18
- import {
19
- WidgetTheme,
20
- WidgetConfig,
21
- WidgetColors,
22
- BlockchainAndTokenConfig,
23
- } from './types';
24
- import useSelectLanguage from './hooks/useSelectLanguage';
25
- import './i18n';
26
- import QueueManager from './QueueManager';
27
- import { useUiStore } from './store/ui';
28
- import { navigationRoutes } from './constants/navigationRoutes';
29
- import { initConfig } from './utils/configs';
30
-
31
- export {
32
- WidgetConfig,
33
- WalletType,
34
- WidgetTheme,
35
- WidgetColors,
36
- BlockchainAndTokenConfig,
37
- };
38
-
39
- export type WidgetProps = {
40
- config?: WidgetConfig;
41
- };
42
-
43
- export const Widget: React.FC<WidgetProps> = ({ config }) => {
44
- globalFont(config?.theme?.fontFamily || 'Roboto');
45
-
46
- const { activeTheme } = useTheme({ ...config?.theme });
47
- const { blockchains } = useMetaStore.use.meta();
48
- const disconnectWallet = useWalletsStore.use.disconnectWallet();
49
- const connectWallet = useWalletsStore.use.connectWallet();
50
- const { changeLanguage } = useSelectLanguage();
51
- const clearConnectedWallet = useWalletsStore.use.clearConnectedWallet();
52
- const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
53
- useState<string>('');
54
- const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
55
- const currentPage = useUiStore.use.currentPage();
56
-
57
- const evmBasedChainNames = blockchains
58
- .filter(isEvmBlockchain)
59
- .map((chain) => chain.name);
60
-
61
- useMemo(() => {
62
- if (config?.apiKey) {
63
- initConfig({
64
- API_KEY: config?.apiKey,
65
- });
66
- }
67
- }, [config]);
68
-
69
- const onUpdateState: EventHandler = (
70
- type,
71
- event,
72
- value,
73
- state,
74
- supportedChains
75
- ) => {
76
- if (event === Events.ACCOUNTS) {
77
- if (value) {
78
- const supportedChainNames: Network[] | null =
79
- walletAndSupportedChainsNames(supportedChains);
80
- const data = prepareAccountsForWalletStore(
81
- type,
82
- value,
83
- evmBasedChainNames,
84
- supportedChainNames
85
- );
86
- connectWallet(data);
87
- } else {
88
- disconnectWallet(type);
89
- }
90
- }
91
- if (event === Events.ACCOUNTS && state.connected) {
92
- const key = `${type}-${state.network}-${value}`;
93
-
94
- if (state.connected) {
95
- setLastConnectedWalletWithNetwork(key);
96
- }
97
- }
98
-
99
- if (event === Events.NETWORK && state.network) {
100
- const key = `${type}-${state.network}`;
101
- setLastConnectedWalletWithNetwork(key);
102
- }
103
- };
104
- let providers = allProviders();
105
-
106
- useEffect(() => {
107
- const wallets = config?.wallets;
108
- clearConnectedWallet();
109
- providers = !wallets
110
- ? allProviders()
111
- : allProviders().filter((provider) => {
112
- const type = provider.config.type;
113
- return wallets.find((w) => w === type);
114
- });
115
- }, [config?.wallets]);
116
-
117
- useEffect(() => {
118
- changeLanguage(config?.language || 'en');
119
- }, [config?.language]);
120
-
121
- return (
122
- <Provider
123
- allBlockChains={blockchains}
124
- providers={providers}
125
- onUpdateState={onUpdateState}
126
- >
127
- <div className={activeTheme}>
128
- <QueueManager>
129
- <SwapContainer fixedHeight={currentPage !== navigationRoutes.home}>
130
- <AppRouter
131
- lastConnectedWallet={lastConnectedWalletWithNetwork}
132
- disconnectedWallet={disconnectedWallet}
133
- clearDisconnectedWallet={() => {
134
- setDisconnectedWallet(undefined);
135
- }}
136
- >
137
- <Layout config={config} />
138
- </AppRouter>
139
- </SwapContainer>
140
- </QueueManager>
141
- </div>
142
- </Provider>
143
- );
144
- };