@lifi/widget 3.0.0-alpha.21 → 3.0.0-alpha.23

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 (147) hide show
  1. package/App.tsx +16 -17
  2. package/AppDrawer.tsx +2 -2
  3. package/_cjs/App.js +2 -3
  4. package/_cjs/App.js.map +1 -1
  5. package/_cjs/AppDrawer.d.ts +2 -2
  6. package/_cjs/components/AccountAvatar.js +2 -2
  7. package/_cjs/components/AccountAvatar.js.map +1 -1
  8. package/_cjs/components/AmountInput/AmountInputEndAdornment.js +11 -7
  9. package/_cjs/components/AmountInput/AmountInputEndAdornment.js.map +1 -1
  10. package/_cjs/components/Routes/RoutesExpanded.js +1 -1
  11. package/_cjs/components/Routes/RoutesExpanded.js.map +1 -1
  12. package/_cjs/components/TokenAvatar/TokenAvatar.style.js +4 -0
  13. package/_cjs/components/TokenAvatar/TokenAvatar.style.js.map +1 -1
  14. package/_cjs/components/TokenList/TokenList.js +5 -2
  15. package/_cjs/components/TokenList/TokenList.js.map +1 -1
  16. package/_cjs/components/TokenList/VirtualizedTokenList.js +41 -19
  17. package/_cjs/components/TokenList/VirtualizedTokenList.js.map +1 -1
  18. package/_cjs/components/TokenList/types.d.ts +3 -2
  19. package/_cjs/config/version.d.ts +1 -1
  20. package/_cjs/config/version.js +1 -1
  21. package/_cjs/hooks/index.d.ts +0 -1
  22. package/_cjs/hooks/index.js +0 -1
  23. package/_cjs/hooks/index.js.map +1 -1
  24. package/_cjs/hooks/useAvailableChains.js +3 -3
  25. package/_cjs/hooks/useAvailableChains.js.map +1 -1
  26. package/_cjs/hooks/useDebouncedWatch.d.ts +1 -1
  27. package/_cjs/hooks/useRoutes.js +49 -25
  28. package/_cjs/hooks/useRoutes.js.map +1 -1
  29. package/_cjs/hooks/useSettingMonitor.js +8 -12
  30. package/_cjs/hooks/useSettingMonitor.js.map +1 -1
  31. package/_cjs/hooks/useToken.d.ts +1 -1
  32. package/_cjs/hooks/useTokenAddressBalance.d.ts +3 -2
  33. package/_cjs/hooks/useTokenAddressBalance.js.map +1 -1
  34. package/_cjs/hooks/useTokenBalances.d.ts +3 -2
  35. package/_cjs/hooks/useTokenBalances.js +33 -14
  36. package/_cjs/hooks/useTokenBalances.js.map +1 -1
  37. package/_cjs/hooks/useTokens.d.ts +3 -1
  38. package/_cjs/hooks/useTokens.js +44 -31
  39. package/_cjs/hooks/useTokens.js.map +1 -1
  40. package/_cjs/i18n/en.json +3 -1
  41. package/_cjs/pages/RoutesPage/RoutesPage.js +1 -1
  42. package/_cjs/pages/RoutesPage/RoutesPage.js.map +1 -1
  43. package/_cjs/pages/SelectEnabledToolsPage/SelectEnabledToolsPage.js +10 -27
  44. package/_cjs/pages/SelectEnabledToolsPage/SelectEnabledToolsPage.js.map +1 -1
  45. package/_cjs/pages/SettingsPage/BridgeAndExchangeSettings.js +2 -2
  46. package/_cjs/pages/SettingsPage/BridgeAndExchangeSettings.js.map +1 -1
  47. package/_cjs/stores/form/createFormStore.js +10 -6
  48. package/_cjs/stores/form/createFormStore.js.map +1 -1
  49. package/_cjs/stores/form/types.d.ts +3 -14
  50. package/_cjs/stores/form/types.js.map +1 -1
  51. package/_cjs/stores/form/useFieldController.d.ts +1 -1
  52. package/_cjs/stores/form/useFieldValues.d.ts +1 -1
  53. package/_cjs/stores/settings/types.d.ts +10 -9
  54. package/_cjs/stores/settings/types.js.map +1 -1
  55. package/_cjs/stores/settings/useSettingsStore.js +40 -30
  56. package/_cjs/stores/settings/useSettingsStore.js.map +1 -1
  57. package/_cjs/types/token.d.ts +1 -0
  58. package/_cjs/types/widget.d.ts +9 -8
  59. package/_esm/App.js +2 -3
  60. package/_esm/App.js.map +1 -1
  61. package/_esm/AppDrawer.d.ts +2 -2
  62. package/_esm/components/AccountAvatar.js +2 -2
  63. package/_esm/components/AccountAvatar.js.map +1 -1
  64. package/_esm/components/AmountInput/AmountInputEndAdornment.js +11 -7
  65. package/_esm/components/AmountInput/AmountInputEndAdornment.js.map +1 -1
  66. package/_esm/components/Routes/RoutesExpanded.js +1 -1
  67. package/_esm/components/Routes/RoutesExpanded.js.map +1 -1
  68. package/_esm/components/TokenAvatar/TokenAvatar.style.js +4 -0
  69. package/_esm/components/TokenAvatar/TokenAvatar.style.js.map +1 -1
  70. package/_esm/components/TokenList/TokenList.js +6 -3
  71. package/_esm/components/TokenList/TokenList.js.map +1 -1
  72. package/_esm/components/TokenList/VirtualizedTokenList.js +41 -19
  73. package/_esm/components/TokenList/VirtualizedTokenList.js.map +1 -1
  74. package/_esm/components/TokenList/types.d.ts +3 -2
  75. package/_esm/config/version.d.ts +1 -1
  76. package/_esm/config/version.js +1 -1
  77. package/_esm/hooks/index.d.ts +0 -1
  78. package/_esm/hooks/index.js +0 -1
  79. package/_esm/hooks/index.js.map +1 -1
  80. package/_esm/hooks/useAvailableChains.js +3 -3
  81. package/_esm/hooks/useAvailableChains.js.map +1 -1
  82. package/_esm/hooks/useDebouncedWatch.d.ts +1 -1
  83. package/_esm/hooks/useRoutes.js +49 -25
  84. package/_esm/hooks/useRoutes.js.map +1 -1
  85. package/_esm/hooks/useSettingMonitor.js +8 -12
  86. package/_esm/hooks/useSettingMonitor.js.map +1 -1
  87. package/_esm/hooks/useToken.d.ts +1 -1
  88. package/_esm/hooks/useTokenAddressBalance.d.ts +3 -2
  89. package/_esm/hooks/useTokenAddressBalance.js.map +1 -1
  90. package/_esm/hooks/useTokenBalances.d.ts +3 -2
  91. package/_esm/hooks/useTokenBalances.js +33 -14
  92. package/_esm/hooks/useTokenBalances.js.map +1 -1
  93. package/_esm/hooks/useTokens.d.ts +3 -1
  94. package/_esm/hooks/useTokens.js +44 -31
  95. package/_esm/hooks/useTokens.js.map +1 -1
  96. package/_esm/i18n/en.json +3 -1
  97. package/_esm/pages/RoutesPage/RoutesPage.js +1 -1
  98. package/_esm/pages/RoutesPage/RoutesPage.js.map +1 -1
  99. package/_esm/pages/SelectEnabledToolsPage/SelectEnabledToolsPage.js +10 -27
  100. package/_esm/pages/SelectEnabledToolsPage/SelectEnabledToolsPage.js.map +1 -1
  101. package/_esm/pages/SettingsPage/BridgeAndExchangeSettings.js +2 -2
  102. package/_esm/pages/SettingsPage/BridgeAndExchangeSettings.js.map +1 -1
  103. package/_esm/stores/form/createFormStore.js +10 -6
  104. package/_esm/stores/form/createFormStore.js.map +1 -1
  105. package/_esm/stores/form/types.d.ts +3 -14
  106. package/_esm/stores/form/types.js.map +1 -1
  107. package/_esm/stores/form/useFieldController.d.ts +1 -1
  108. package/_esm/stores/form/useFieldValues.d.ts +1 -1
  109. package/_esm/stores/settings/types.d.ts +10 -9
  110. package/_esm/stores/settings/types.js.map +1 -1
  111. package/_esm/stores/settings/useSettingsStore.js +40 -30
  112. package/_esm/stores/settings/useSettingsStore.js.map +1 -1
  113. package/_esm/types/token.d.ts +1 -0
  114. package/_esm/types/widget.d.ts +9 -8
  115. package/components/AccountAvatar.tsx +4 -7
  116. package/components/AmountInput/AmountInputEndAdornment.tsx +15 -12
  117. package/components/Routes/RoutesExpanded.tsx +1 -1
  118. package/components/TokenAvatar/TokenAvatar.style.tsx +4 -0
  119. package/components/TokenList/TokenList.tsx +8 -2
  120. package/components/TokenList/VirtualizedTokenList.tsx +63 -41
  121. package/components/TokenList/types.ts +3 -2
  122. package/config/version.ts +1 -1
  123. package/hooks/index.ts +0 -1
  124. package/hooks/useAvailableChains.ts +7 -4
  125. package/hooks/useRoutes.ts +58 -31
  126. package/hooks/useSettingMonitor.ts +20 -21
  127. package/hooks/useTokenAddressBalance.ts +2 -1
  128. package/hooks/useTokenBalances.ts +34 -29
  129. package/hooks/useTokens.ts +65 -36
  130. package/i18n/en.json +3 -1
  131. package/package.json +9 -9
  132. package/pages/RoutesPage/RoutesPage.tsx +1 -1
  133. package/pages/SelectEnabledToolsPage/SelectEnabledToolsPage.tsx +16 -35
  134. package/pages/SettingsPage/BridgeAndExchangeSettings.tsx +2 -2
  135. package/stores/form/createFormStore.ts +15 -15
  136. package/stores/form/types.ts +3 -14
  137. package/stores/settings/types.ts +10 -13
  138. package/stores/settings/useSettingsStore.ts +51 -53
  139. package/types/token.ts +1 -0
  140. package/types/widget.ts +10 -9
  141. package/_cjs/hooks/useFeaturedTokens.d.ts +0 -1
  142. package/_cjs/hooks/useFeaturedTokens.js +0 -11
  143. package/_cjs/hooks/useFeaturedTokens.js.map +0 -1
  144. package/_esm/hooks/useFeaturedTokens.d.ts +0 -1
  145. package/_esm/hooks/useFeaturedTokens.js +0 -7
  146. package/_esm/hooks/useFeaturedTokens.js.map +0 -1
  147. package/hooks/useFeaturedTokens.ts +0 -12
@@ -3,51 +3,54 @@ import { useVirtualizer } from '@tanstack/react-virtual';
3
3
  import type { FC } from 'react';
4
4
  import { useEffect } from 'react';
5
5
  import { useTranslation } from 'react-i18next';
6
- import { useAccount } from '../../hooks';
6
+ import type { TokenAmount } from '../../types';
7
7
  import { TokenListItem, TokenListItemSkeleton } from './TokenListItem';
8
8
  import type { VirtualizedTokenListProps } from './types';
9
9
 
10
10
  export const VirtualizedTokenList: FC<VirtualizedTokenListProps> = ({
11
+ account,
11
12
  tokens,
12
- featuredTokensLength,
13
13
  scrollElementRef,
14
14
  chainId,
15
15
  chain,
16
16
  isLoading,
17
17
  isBalanceLoading,
18
- showFeatured,
18
+ showCategories,
19
19
  onClick,
20
20
  }) => {
21
- const { account } = useAccount({ chainType: chain?.chainType });
22
21
  const { t } = useTranslation();
23
22
 
24
- const hasFeaturedTokens = !!featuredTokensLength && showFeatured;
25
- const featuredTokensLastIndex = (featuredTokensLength ?? 0) - 1;
26
- const tokensLastIndex = tokens.length - 1;
27
-
28
23
  const { getVirtualItems, getTotalSize, scrollToIndex } = useVirtualizer({
29
24
  count: tokens.length,
30
25
  overscan: 10,
31
26
  paddingEnd: 12,
32
27
  getScrollElement: () => scrollElementRef.current,
33
28
  estimateSize: (index) => {
34
- // heigth of TokenListItem
29
+ // Base size for TokenListItem
35
30
  let size = 64;
36
- if (!hasFeaturedTokens) {
31
+ // Early return if categories are not shown
32
+ if (!showCategories) {
37
33
  return size;
38
34
  }
39
- if (index === 0 && tokens[index]?.featured) {
40
- // height of startAdornment
35
+
36
+ const currentToken = tokens[index];
37
+ const previousToken = tokens[index - 1];
38
+
39
+ // Adjust size for the first featured token
40
+ if (currentToken.featured && index === 0) {
41
41
  size += 24;
42
42
  }
43
- if (
44
- index === featuredTokensLastIndex &&
45
- index !== tokensLastIndex &&
46
- tokens[index]?.featured
47
- ) {
48
- // height of endAdornment
43
+
44
+ // Adjust size based on changes between the current and previous tokens
45
+ const isCategoryChanged =
46
+ (previousToken?.amount && !currentToken.amount) ||
47
+ (previousToken?.featured && !currentToken.featured) ||
48
+ (previousToken?.popular && !currentToken.popular);
49
+
50
+ if (isCategoryChanged) {
49
51
  size += 32;
50
52
  }
53
+
51
54
  return size;
52
55
  },
53
56
  getItemKey: (index) => `${tokens[index].address}-${index}`,
@@ -74,43 +77,62 @@ export const VirtualizedTokenList: FC<VirtualizedTokenListProps> = ({
74
77
  return (
75
78
  <List style={{ height: getTotalSize() }} disablePadding>
76
79
  {getVirtualItems().map((item) => {
77
- const token = tokens[item.index];
80
+ const currentToken = tokens[item.index];
81
+ const previousToken: TokenAmount | undefined = tokens[item.index - 1];
82
+
83
+ const isFirstFeaturedToken = currentToken.featured && item.index === 0;
84
+ const isTransitionFromFeaturedTokens =
85
+ previousToken?.featured && !currentToken.featured;
86
+ const isTransitionFromMyTokens =
87
+ previousToken?.amount && !currentToken.amount;
88
+ const isTransitionToMyTokens =
89
+ isTransitionFromFeaturedTokens && currentToken.amount;
90
+ const isTransitionToPopularTokens =
91
+ (isTransitionFromFeaturedTokens || isTransitionFromMyTokens) &&
92
+ currentToken.popular;
93
+ const shouldShowAllTokensCategory =
94
+ (isTransitionFromMyTokens ||
95
+ isTransitionFromFeaturedTokens ||
96
+ (previousToken?.popular && !currentToken.popular)) &&
97
+ showCategories;
98
+
99
+ const startAdornmentLabel = (() => {
100
+ if (isFirstFeaturedToken) {
101
+ return t('main.featuredTokens');
102
+ }
103
+ if (isTransitionToMyTokens) {
104
+ return t('main.myTokens');
105
+ }
106
+ if (isTransitionToPopularTokens) {
107
+ return t('main.popularTokens');
108
+ }
109
+ if (shouldShowAllTokensCategory) {
110
+ return t('main.allTokens');
111
+ }
112
+ return null;
113
+ })();
114
+
78
115
  return (
79
116
  <TokenListItem
80
117
  key={item.key}
81
118
  onClick={onClick}
82
119
  size={item.size}
83
120
  start={item.start}
84
- token={token}
121
+ token={currentToken}
85
122
  chain={chain}
86
123
  isBalanceLoading={isBalanceLoading}
87
124
  showBalance={account.isConnected}
88
125
  startAdornment={
89
- hasFeaturedTokens && token.featured && item.index === 0 ? (
90
- <Typography
91
- fontSize={14}
92
- fontWeight={600}
93
- lineHeight={1}
94
- px={2}
95
- pb={1.25}
96
- >
97
- {t('main.featuredTokens')}
98
- </Typography>
99
- ) : null
100
- }
101
- endAdornment={
102
- hasFeaturedTokens &&
103
- token.featured &&
104
- item.index === featuredTokensLastIndex &&
105
- item.index !== tokensLastIndex ? (
126
+ startAdornmentLabel ? (
106
127
  <Typography
107
128
  fontSize={14}
108
129
  fontWeight={600}
109
- lineHeight={1}
110
- px={2}
111
- py={1.25}
130
+ lineHeight="16px"
131
+ px={1.5}
132
+ pt={isFirstFeaturedToken ? 0 : 1}
133
+ pb={1}
112
134
  >
113
- {t('main.otherTokens')}
135
+ {startAdornmentLabel}
114
136
  </Typography>
115
137
  ) : null
116
138
  }
@@ -1,5 +1,6 @@
1
1
  import type { ExtendedChain } from '@lifi/sdk';
2
2
  import type { MouseEventHandler, MutableRefObject } from 'react';
3
+ import type { Account } from '../../hooks';
3
4
  import type { FormType } from '../../stores';
4
5
  import type { TokenAmount } from '../../types';
5
6
 
@@ -10,14 +11,14 @@ export interface TokenListProps {
10
11
  }
11
12
 
12
13
  export interface VirtualizedTokenListProps {
14
+ account: Account;
13
15
  tokens: TokenAmount[];
14
- featuredTokensLength?: number;
15
16
  scrollElementRef: MutableRefObject<HTMLElement | null>;
16
17
  isLoading: boolean;
17
18
  isBalanceLoading: boolean;
18
19
  chainId?: number;
19
20
  chain?: ExtendedChain;
20
- showFeatured?: boolean;
21
+ showCategories?: boolean;
21
22
  onClick(tokenAddress: string): void;
22
23
  }
23
24
 
package/config/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/widget';
2
- export const version = '3.0.0-alpha.21';
2
+ export const version = '3.0.0-alpha.23';
package/hooks/index.ts CHANGED
@@ -7,7 +7,6 @@ export * from './useContentHeight';
7
7
  export * from './useDebouncedWatch';
8
8
  export * from './useDefaultElementId';
9
9
  export * from './useExpandableVariant';
10
- export * from './useFeaturedTokens';
11
10
  export * from './useFromTokenSufficiency';
12
11
  export * from './useGasRecommendation';
13
12
  export * from './useGasRefuel';
@@ -5,17 +5,20 @@ import { useCallback } from 'react';
5
5
  import { useHasExternalWalletProvider, useWidgetConfig } from '../providers';
6
6
  import { isItemAllowed } from '../utils';
7
7
 
8
+ const supportedChainTypes = [ChainType.EVM, ChainType.SVM];
9
+
8
10
  export const useAvailableChains = () => {
9
11
  const { chains } = useWidgetConfig();
10
12
  const { providers } = useHasExternalWalletProvider();
11
13
  const { data, isLoading } = useQuery({
12
14
  queryKey: ['chains', providers, chains?.types] as const,
13
15
  queryFn: async ({ queryKey: [, providers, chainTypes] }) => {
16
+ const chainTypesRequest = (
17
+ providers.length > 0 ? providers : supportedChainTypes
18
+ ).filter((chainType) => isItemAllowed(chainType, chainTypes));
19
+
14
20
  const availableChains = await getChains({
15
- chainTypes: (providers.length > 0
16
- ? providers
17
- : Object.values(ChainType)
18
- ).filter((chainType) => isItemAllowed(chainType, chainTypes)),
21
+ chainTypes: chainTypesRequest,
19
22
  });
20
23
  config.setChains(availableChains);
21
24
  return availableChains;
@@ -21,17 +21,17 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
21
21
  const queryClient = useQueryClient();
22
22
  const swapOnly = useSwapOnly();
23
23
  const {
24
- slippage,
25
- enabledBridges,
24
+ disabledBridges,
25
+ disabledExchanges,
26
26
  enabledAutoRefuel,
27
- enabledExchanges,
28
27
  routePriority,
28
+ slippage,
29
29
  } = useSettings([
30
- 'slippage',
31
- 'routePriority',
30
+ 'disabledBridges',
31
+ 'disabledExchanges',
32
32
  'enabledAutoRefuel',
33
- 'enabledBridges',
34
- 'enabledExchanges',
33
+ 'routePriority',
34
+ 'slippage',
35
35
  ]);
36
36
  const [fromTokenAmount] = useDebouncedWatch(320, 'fromAmount');
37
37
  const [
@@ -117,8 +117,9 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
117
117
  toContractCallData,
118
118
  toContractGasLimit,
119
119
  slippage,
120
- swapOnly ? [] : enabledBridges,
121
- enabledExchanges,
120
+ swapOnly,
121
+ disabledBridges,
122
+ disabledExchanges,
122
123
  routePriority,
123
124
  subvariant,
124
125
  sdkConfig?.routeOptions?.allowSwitchChain,
@@ -146,8 +147,9 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
146
147
  toContractCallData,
147
148
  toContractGasLimit,
148
149
  slippage = defaultSlippage,
149
- enabledBridges,
150
- enabledExchanges,
150
+ swapOnly,
151
+ disabledBridges,
152
+ disabledExchanges,
151
153
  routePriority,
152
154
  subvariant,
153
155
  allowSwitchChain,
@@ -165,21 +167,29 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
165
167
  ).toString();
166
168
  const formattedSlippage = parseFloat(slippage) / 100;
167
169
 
168
- const allowedBridges: string[] = insurableRoute
169
- ? insurableRoute.steps.flatMap((step) =>
170
- step.includedSteps
171
- .filter((includedStep) => includedStep.type === 'cross')
172
- .map((includedStep) => includedStep.toolDetails.key),
173
- )
174
- : enabledBridges;
170
+ const allowedBridges = swapOnly
171
+ ? []
172
+ : insurableRoute
173
+ ? insurableRoute.steps.flatMap((step) =>
174
+ step.includedSteps.reduce((toolKeys, includedStep) => {
175
+ if (includedStep.type === 'cross') {
176
+ toolKeys.push(includedStep.toolDetails.key);
177
+ }
178
+ return toolKeys;
179
+ }, [] as string[]),
180
+ )
181
+ : undefined;
175
182
 
176
- const allowedExchanges: string[] = insurableRoute
183
+ const allowedExchanges = insurableRoute
177
184
  ? insurableRoute.steps.flatMap((step) =>
178
- step.includedSteps
179
- .filter((includedStep) => includedStep.type === 'swap')
180
- .map((includedStep) => includedStep.toolDetails.key),
185
+ step.includedSteps.reduce((toolKeys, includedStep) => {
186
+ if (includedStep.type === 'swap') {
187
+ toolKeys.push(includedStep.toolDetails.key);
188
+ }
189
+ return toolKeys;
190
+ }, [] as string[]),
181
191
  )
182
- : enabledExchanges;
192
+ : undefined;
183
193
 
184
194
  if (subvariant === 'nft') {
185
195
  const contractCallQuote = await getContractCallQuote(
@@ -194,7 +204,12 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
194
204
  toContractAddress,
195
205
  toContractCallData,
196
206
  toContractGasLimit,
207
+ denyBridges: disabledBridges.length ? disabledBridges : undefined,
208
+ denyExchanges: disabledExchanges.length
209
+ ? disabledExchanges
210
+ : undefined,
197
211
  allowBridges: allowedBridges,
212
+ allowExchanges: allowedExchanges,
198
213
  toFallbackAddress: toWalletAddress,
199
214
  slippage: formattedSlippage,
200
215
  },
@@ -255,17 +270,29 @@ export const useRoutes = ({ insurableRoute }: RoutesProps = {}) => {
255
270
  ? gasRecommendationFromAmount
256
271
  : undefined,
257
272
  options: {
258
- slippage: formattedSlippage,
259
- bridges: {
260
- allow: allowedBridges,
261
- },
262
- exchanges: {
263
- allow: allowedExchanges,
264
- },
265
- order: routePriority,
266
273
  allowSwitchChain:
267
274
  subvariant === 'refuel' ? false : allowSwitchChain,
275
+ bridges:
276
+ allowedBridges?.length || disabledBridges.length
277
+ ? {
278
+ allow: allowedBridges,
279
+ deny: disabledBridges.length
280
+ ? disabledBridges
281
+ : undefined,
282
+ }
283
+ : undefined,
284
+ exchanges:
285
+ allowedExchanges?.length || disabledExchanges.length
286
+ ? {
287
+ allow: allowedExchanges,
288
+ deny: disabledExchanges.length
289
+ ? disabledExchanges
290
+ : undefined,
291
+ }
292
+ : undefined,
268
293
  insurance: insurance ? Boolean(insurableRoute) : undefined,
294
+ order: routePriority,
295
+ slippage: formattedSlippage,
269
296
  },
270
297
  },
271
298
  { signal },
@@ -1,24 +1,29 @@
1
+ import { shallow } from 'zustand/shallow';
2
+ import { useWidgetConfig } from '../providers';
1
3
  import {
2
4
  defaultConfigurableSettings,
3
5
  setDefaultSettings,
4
6
  useSettingsStore,
5
7
  } from '../stores';
6
- import { shallow } from 'zustand/shallow';
7
8
  import { useTools } from './useTools';
8
- import { useWidgetConfig } from '../providers';
9
9
 
10
10
  export const useSettingMonitor = () => {
11
- const [enabledBridges, enabledExchanges, routePriority, slippage, gasPrice] =
12
- useSettingsStore(
13
- (state) => [
14
- state.enabledBridges,
15
- state.enabledExchanges,
16
- state.routePriority,
17
- state.slippage,
18
- state.gasPrice,
19
- ],
20
- shallow,
21
- );
11
+ const [
12
+ disabledBridges,
13
+ disabledExchanges,
14
+ routePriority,
15
+ slippage,
16
+ gasPrice,
17
+ ] = useSettingsStore(
18
+ (state) => [
19
+ state.disabledBridges,
20
+ state.disabledExchanges,
21
+ state.routePriority,
22
+ state.slippage,
23
+ state.gasPrice,
24
+ ],
25
+ shallow,
26
+ );
22
27
  const { tools } = useTools();
23
28
  const resetSettings = useSettingsStore((state) => state.reset);
24
29
  const config = useWidgetConfig();
@@ -36,13 +41,8 @@ export const useSettingMonitor = () => {
36
41
 
37
42
  const isGasPriceChanged = gasPrice !== defaultConfigurableSettings.gasPrice;
38
43
 
39
- const isBridgesChanged = tools?.bridges
40
- ? tools?.bridges?.length !== enabledBridges?.length
41
- : false;
42
-
43
- const isExchangesChanged = tools?.exchanges
44
- ? tools?.exchanges?.length !== enabledExchanges?.length
45
- : false;
44
+ const isBridgesChanged = Boolean(disabledBridges.length);
45
+ const isExchangesChanged = Boolean(disabledExchanges.length);
46
46
 
47
47
  const isCustomRouteSettings =
48
48
  isBridgesChanged ||
@@ -56,7 +56,6 @@ export const useSettingMonitor = () => {
56
56
  const reset = () => {
57
57
  if (tools) {
58
58
  resetSettings(
59
- config,
60
59
  tools.bridges.map((tool) => tool.key),
61
60
  tools.exchanges.map((tool) => tool.key),
62
61
  );
@@ -1,4 +1,5 @@
1
1
  import { useMemo } from 'react';
2
+ import type { TokenAmount } from '../types';
2
3
  import { useTokenBalances } from './useTokenBalances';
3
4
 
4
5
  export const useTokenAddressBalance = (
@@ -13,7 +14,7 @@ export const useTokenAddressBalance = (
13
14
  const token = (tokensWithBalance ?? tokens)?.find(
14
15
  (token) => token.address === tokenAddress && token.chainId === chainId,
15
16
  );
16
- return token;
17
+ return token as TokenAmount;
17
18
  }
18
19
  }, [chainId, tokenAddress, tokens, tokensWithBalance]);
19
20
 
@@ -3,14 +3,13 @@ import { useQuery } from '@tanstack/react-query';
3
3
  import { formatUnits } from 'viem';
4
4
  import type { TokenAmount } from '../types';
5
5
  import { useAccount } from './useAccount';
6
- import { useFeaturedTokens } from './useFeaturedTokens';
7
6
  import { useTokens } from './useTokens';
8
7
 
9
8
  const defaultRefetchInterval = 32_000;
10
9
 
11
10
  export const useTokenBalances = (selectedChainId?: number) => {
12
- const featuredTokens = useFeaturedTokens(selectedChainId);
13
- const { tokens, chain, isLoading } = useTokens(selectedChainId);
11
+ const { tokens, featuredTokens, popularTokens, chain, isLoading } =
12
+ useTokens(selectedChainId);
14
13
  const { account } = useAccount({ chainType: chain?.chainType });
15
14
 
16
15
  const isBalanceLoadingEnabled =
@@ -30,44 +29,49 @@ export const useTokenBalances = (selectedChainId?: number) => {
30
29
  tokens?.length,
31
30
  ],
32
31
  queryFn: async ({ queryKey: [, accountAddress] }) => {
33
- const tokenBalances = await getTokenBalances(
32
+ const tokensWithBalance: TokenAmount[] = await getTokenBalances(
34
33
  accountAddress as string,
35
34
  tokens!,
36
35
  );
37
36
 
38
- const featuredTokenAddresses = new Set(
39
- featuredTokens?.map((token) => token.address),
40
- );
37
+ if (!tokensWithBalance?.length) {
38
+ return tokens as TokenAmount[];
39
+ }
41
40
 
42
41
  const sortFn = (a: TokenAmount, b: TokenAmount) =>
43
42
  parseFloat(formatUnits(b.amount ?? 0n, b.decimals)) *
44
43
  parseFloat(b.priceUSD ?? '0') -
45
44
  parseFloat(formatUnits(a.amount ?? 0n, a.decimals)) *
46
45
  parseFloat(a.priceUSD ?? '0');
47
- const formattedTokens = (
48
- tokenBalances.length === 0 ? tokens : tokenBalances
49
- ) as TokenAmount[];
46
+
47
+ const featuredTokens: TokenAmount[] = [];
48
+ const tokensWithAmount: TokenAmount[] = [];
49
+ const popularTokens: TokenAmount[] = [];
50
+ const allTokens: TokenAmount[] = [];
51
+
52
+ tokensWithBalance.forEach((token) => {
53
+ if (token.amount) {
54
+ token.featured = false;
55
+ token.popular = false;
56
+ }
57
+ if (token.featured) {
58
+ featuredTokens.push(token);
59
+ } else if (token.amount) {
60
+ tokensWithAmount.push(token);
61
+ } else if (token.popular) {
62
+ popularTokens.push(token);
63
+ } else {
64
+ allTokens.push(token);
65
+ }
66
+ });
67
+
68
+ tokensWithAmount.sort(sortFn);
50
69
 
51
70
  const result = [
52
- ...formattedTokens
53
- .filter(
54
- (token) =>
55
- token.amount && featuredTokenAddresses.has(token.address),
56
- )
57
- .sort(sortFn),
58
- ...formattedTokens.filter(
59
- (token) => !token.amount && featuredTokenAddresses.has(token.address),
60
- ),
61
- ...formattedTokens
62
- .filter(
63
- (token) =>
64
- token.amount && !featuredTokenAddresses.has(token.address),
65
- )
66
- .sort(sortFn),
67
- ...formattedTokens.filter(
68
- (token) =>
69
- !token.amount && !featuredTokenAddresses.has(token.address),
70
- ),
71
+ ...featuredTokens,
72
+ ...tokensWithAmount,
73
+ ...popularTokens,
74
+ ...allTokens,
71
75
  ];
72
76
  return result;
73
77
  },
@@ -80,6 +84,7 @@ export const useTokenBalances = (selectedChainId?: number) => {
80
84
  tokens,
81
85
  tokensWithBalance,
82
86
  featuredTokens,
87
+ popularTokens,
83
88
  chain,
84
89
  isLoading,
85
90
  isBalanceLoading: isBalanceLoading && isBalanceLoadingEnabled,