@lifi/widget 1.16.0 → 1.17.1

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 (179) hide show
  1. package/AppDrawer.d.ts +3 -3
  2. package/AppProvider.d.ts +0 -4
  3. package/AppProvider.js +2 -10
  4. package/README.md +1 -1
  5. package/cjs/AppDrawer.d.ts +3 -3
  6. package/cjs/AppProvider.d.ts +0 -4
  7. package/cjs/AppProvider.js +3 -12
  8. package/cjs/components/AppContainer.js +2 -2
  9. package/cjs/components/ChainSelect/ChainSelect.style.js +4 -4
  10. package/cjs/components/ChainSelect/useChainSelect.d.ts +2 -2
  11. package/cjs/components/GasSufficiencyMessage/GasSufficiencyMessage.js +3 -3
  12. package/cjs/components/Header/useHeaderActionStore.d.ts +0 -2
  13. package/cjs/components/Header/useHeaderActionStore.js +1 -5
  14. package/cjs/components/SelectChainAndToken.js +5 -5
  15. package/cjs/components/SwapButton/SwapButton.js +3 -23
  16. package/cjs/components/TokenList/TokenList.js +1 -1
  17. package/cjs/components/TokenList/TokenList.style.js +4 -3
  18. package/cjs/components/TokenList/TokenListItem.js +1 -1
  19. package/cjs/components/TokenList/TokenNotFound.d.ts +2 -1
  20. package/cjs/components/TokenList/TokenNotFound.js +12 -2
  21. package/cjs/components/TokenList/types.d.ts +2 -2
  22. package/cjs/config/version.d.ts +1 -1
  23. package/cjs/config/version.js +1 -1
  24. package/cjs/hooks/useChains.js +23 -10
  25. package/cjs/hooks/useFeaturedTokens.d.ts +1 -1
  26. package/cjs/hooks/useFeaturedTokens.js +6 -3
  27. package/cjs/hooks/useGasSufficiency.d.ts +1 -1
  28. package/cjs/hooks/useGasSufficiency.js +39 -28
  29. package/cjs/hooks/useInitializer.js +6 -0
  30. package/cjs/hooks/useRouteExecution.js +28 -12
  31. package/cjs/hooks/useSwapRoutes.js +10 -11
  32. package/cjs/hooks/useToken.d.ts +0 -1
  33. package/cjs/hooks/useToken.js +1 -2
  34. package/cjs/hooks/useTokenBalances.d.ts +1 -2
  35. package/cjs/hooks/useTokenBalances.js +5 -6
  36. package/cjs/hooks/useTokenSearch.js +3 -2
  37. package/cjs/hooks/useTokens.d.ts +1 -2
  38. package/cjs/hooks/useTokens.js +32 -9
  39. package/cjs/hooks/useTools.js +11 -5
  40. package/cjs/hooks/useWidgetEvents.d.ts +3 -3
  41. package/cjs/hooks/useWidgetEvents.js +2 -2
  42. package/cjs/i18n/en/translation.json +2 -3
  43. package/cjs/i18n/index.d.ts +1 -2
  44. package/cjs/index.d.ts +2 -1
  45. package/cjs/pages/MainPage/MainSwapButton.js +1 -1
  46. package/cjs/pages/SettingsPage/AdvancedPreferences.js +1 -1
  47. package/cjs/pages/SettingsPage/GasPriceSelect.js +1 -1
  48. package/cjs/pages/SettingsPage/RoutePrioritySelect.js +1 -1
  49. package/cjs/pages/SettingsPage/ShowDestinationWallet.js +1 -1
  50. package/cjs/pages/SettingsPage/SlippageInput.js +1 -1
  51. package/cjs/pages/SwapDetailsPage/SwapDetailsPage.js +2 -3
  52. package/cjs/pages/SwapHistoryPage/SwapHistoryPage.js +2 -3
  53. package/cjs/pages/SwapPage/SwapPage.js +11 -2
  54. package/cjs/pages/SwapRoutesPage/SwapRoutesPage.js +4 -3
  55. package/cjs/providers/SDKProvider/SDKProvider.d.ts +4 -0
  56. package/cjs/providers/SDKProvider/SDKProvider.js +27 -0
  57. package/cjs/providers/SDKProvider/index.d.ts +1 -0
  58. package/cjs/providers/SDKProvider/index.js +17 -0
  59. package/cjs/providers/SwapFormProvider/SwapFormProvider.js +4 -58
  60. package/cjs/providers/SwapFormProvider/types.d.ts +5 -5
  61. package/cjs/providers/TelemetryProvider/TelemetryProvider.d.ts +4 -0
  62. package/cjs/providers/TelemetryProvider/TelemetryProvider.js +11 -0
  63. package/cjs/providers/TelemetryProvider/index.d.ts +1 -0
  64. package/cjs/providers/TelemetryProvider/index.js +17 -0
  65. package/cjs/providers/WalletProvider/WalletProvider.d.ts +1 -4
  66. package/cjs/providers/WalletProvider/WalletProvider.js +59 -1
  67. package/cjs/providers/WidgetProvider/WidgetProvider.js +1 -12
  68. package/cjs/providers/WidgetProvider/index.d.ts +1 -0
  69. package/cjs/providers/WidgetProvider/index.js +1 -0
  70. package/cjs/providers/WidgetProvider/utils.d.ts +4 -0
  71. package/cjs/providers/WidgetProvider/utils.js +13 -0
  72. package/cjs/providers/index.d.ts +2 -0
  73. package/cjs/providers/index.js +2 -0
  74. package/cjs/stores/chains/index.d.ts +0 -1
  75. package/cjs/stores/chains/index.js +0 -1
  76. package/cjs/stores/chains/types.d.ts +1 -1
  77. package/cjs/stores/chains/useChainOrderStore.js +17 -14
  78. package/cjs/stores/route/utils.d.ts +2 -1
  79. package/cjs/stores/route/utils.js +17 -1
  80. package/cjs/stores/settings/index.d.ts +0 -1
  81. package/cjs/stores/settings/index.js +0 -1
  82. package/cjs/stores/settings/useAppearance.js +3 -1
  83. package/cjs/types/events.d.ts +15 -3
  84. package/cjs/types/events.js +4 -3
  85. package/cjs/types/widget.d.ts +29 -22
  86. package/cjs/utils/colors.d.ts +1 -1
  87. package/components/AppContainer.js +1 -1
  88. package/components/ChainSelect/ChainSelect.style.js +4 -4
  89. package/components/ChainSelect/useChainSelect.d.ts +2 -2
  90. package/components/GasSufficiencyMessage/GasSufficiencyMessage.js +3 -3
  91. package/components/Header/useHeaderActionStore.d.ts +0 -2
  92. package/components/Header/useHeaderActionStore.js +0 -3
  93. package/components/SelectChainAndToken.js +1 -1
  94. package/components/SwapButton/SwapButton.js +5 -25
  95. package/components/TokenList/TokenList.js +1 -1
  96. package/components/TokenList/TokenList.style.js +4 -3
  97. package/components/TokenList/TokenListItem.js +1 -1
  98. package/components/TokenList/TokenNotFound.d.ts +2 -1
  99. package/components/TokenList/TokenNotFound.js +12 -2
  100. package/components/TokenList/types.d.ts +2 -2
  101. package/config/version.d.ts +1 -1
  102. package/config/version.js +1 -1
  103. package/hooks/useChains.js +24 -11
  104. package/hooks/useFeaturedTokens.d.ts +1 -1
  105. package/hooks/useFeaturedTokens.js +6 -3
  106. package/hooks/useGasSufficiency.d.ts +1 -1
  107. package/hooks/useGasSufficiency.js +35 -24
  108. package/hooks/useInitializer.js +6 -0
  109. package/hooks/useRouteExecution.js +28 -12
  110. package/hooks/useSwapRoutes.js +3 -4
  111. package/hooks/useToken.d.ts +0 -1
  112. package/hooks/useToken.js +1 -2
  113. package/hooks/useTokenBalances.d.ts +1 -2
  114. package/hooks/useTokenBalances.js +4 -5
  115. package/hooks/useTokenSearch.js +3 -2
  116. package/hooks/useTokens.d.ts +1 -2
  117. package/hooks/useTokens.js +32 -9
  118. package/hooks/useTools.js +11 -5
  119. package/hooks/useWidgetEvents.d.ts +3 -3
  120. package/hooks/useWidgetEvents.js +2 -2
  121. package/i18n/en/translation.json +2 -3
  122. package/i18n/index.d.ts +1 -2
  123. package/index.d.ts +2 -1
  124. package/package.json +9 -8
  125. package/pages/MainPage/MainSwapButton.js +1 -1
  126. package/pages/SettingsPage/AdvancedPreferences.js +2 -2
  127. package/pages/SettingsPage/GasPriceSelect.js +2 -2
  128. package/pages/SettingsPage/RoutePrioritySelect.js +2 -2
  129. package/pages/SettingsPage/ShowDestinationWallet.js +2 -2
  130. package/pages/SettingsPage/SlippageInput.js +2 -2
  131. package/pages/SwapDetailsPage/SwapDetailsPage.js +3 -4
  132. package/pages/SwapHistoryPage/SwapHistoryPage.js +3 -4
  133. package/pages/SwapPage/SwapPage.js +11 -2
  134. package/pages/SwapRoutesPage/SwapRoutesPage.js +5 -4
  135. package/providers/SDKProvider/SDKProvider.d.ts +4 -0
  136. package/providers/SDKProvider/SDKProvider.js +19 -0
  137. package/providers/SDKProvider/index.d.ts +1 -0
  138. package/providers/SDKProvider/index.js +1 -0
  139. package/providers/SwapFormProvider/SwapFormProvider.js +4 -58
  140. package/providers/SwapFormProvider/types.d.ts +5 -5
  141. package/providers/TelemetryProvider/TelemetryProvider.d.ts +4 -0
  142. package/providers/TelemetryProvider/TelemetryProvider.js +7 -0
  143. package/providers/TelemetryProvider/index.d.ts +1 -0
  144. package/providers/TelemetryProvider/index.js +1 -0
  145. package/providers/WalletProvider/WalletProvider.d.ts +1 -4
  146. package/providers/WalletProvider/WalletProvider.js +59 -1
  147. package/providers/WidgetProvider/WidgetProvider.js +3 -14
  148. package/providers/WidgetProvider/index.d.ts +1 -0
  149. package/providers/WidgetProvider/index.js +1 -0
  150. package/providers/WidgetProvider/utils.d.ts +4 -0
  151. package/providers/WidgetProvider/utils.js +9 -0
  152. package/providers/index.d.ts +2 -0
  153. package/providers/index.js +2 -0
  154. package/stores/chains/index.d.ts +0 -1
  155. package/stores/chains/index.js +0 -1
  156. package/stores/chains/types.d.ts +1 -1
  157. package/stores/chains/useChainOrderStore.js +17 -14
  158. package/stores/route/utils.d.ts +2 -1
  159. package/stores/route/utils.js +12 -0
  160. package/stores/settings/index.d.ts +0 -1
  161. package/stores/settings/index.js +0 -1
  162. package/stores/settings/useAppearance.js +3 -1
  163. package/tsconfig.cjs.tsbuildinfo +1 -1
  164. package/types/events.d.ts +15 -3
  165. package/types/events.js +4 -3
  166. package/types/widget.d.ts +29 -22
  167. package/utils/colors.d.ts +1 -1
  168. package/cjs/config/lifi.d.ts +0 -4
  169. package/cjs/config/lifi.js +0 -14
  170. package/cjs/stores/chains/useInitializeChainOrder.d.ts +0 -1
  171. package/cjs/stores/chains/useInitializeChainOrder.js +0 -12
  172. package/cjs/stores/settings/useSetSettings.d.ts +0 -5
  173. package/cjs/stores/settings/useSetSettings.js +0 -12
  174. package/config/lifi.d.ts +0 -4
  175. package/config/lifi.js +0 -7
  176. package/stores/chains/useInitializeChainOrder.d.ts +0 -1
  177. package/stores/chains/useInitializeChainOrder.js +0 -5
  178. package/stores/settings/useSetSettings.d.ts +0 -5
  179. package/stores/settings/useSetSettings.js +0 -5
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect } from 'react';
3
3
  import { useLocation, useNavigate } from 'react-router-dom';
4
- import { useSetHeaderAction } from '../../components/Header';
4
+ import { useHeaderActionStore } from '../../components/Header';
5
5
  import { ProgressToNextUpdate } from '../../components/ProgressToNextUpdate';
6
6
  import { SwapRouteCard, SwapRouteCardSkeleton, SwapRouteNotFoundCard, } from '../../components/SwapRouteCard';
7
7
  import { useSwapRoutes } from '../../hooks';
@@ -11,7 +11,6 @@ import { Stack } from './SwapRoutesPage.style';
11
11
  export const SwapRoutesPage = () => {
12
12
  const navigate = useNavigate();
13
13
  const { pathname } = useLocation();
14
- const setHeaderAction = useSetHeaderAction();
15
14
  const { routes: swapRoutes, isLoading, isFetching, dataUpdatedAt, refetchTime, refetch, } = useSwapRoutes();
16
15
  const setExecutableRoute = useSetExecutableRoute();
17
16
  const handleRouteClick = (route) => {
@@ -28,8 +27,10 @@ export const SwapRoutesPage = () => {
28
27
  // eslint-disable-next-line react-hooks/exhaustive-deps
29
28
  }, []);
30
29
  useEffect(() => {
31
- return setHeaderAction(_jsx(ProgressToNextUpdate, { updatedAt: dataUpdatedAt || new Date().getTime(), timeToUpdate: refetchTime, isLoading: isFetching, onClick: () => refetch(), sx: { marginRight: -1 }, size: "medium", edge: "end" }));
32
- }, [dataUpdatedAt, isFetching, refetch, refetchTime, setHeaderAction]);
30
+ return useHeaderActionStore
31
+ .getState()
32
+ .setAction(_jsx(ProgressToNextUpdate, { updatedAt: dataUpdatedAt || new Date().getTime(), timeToUpdate: refetchTime, isLoading: isFetching, onClick: () => refetch(), sx: { marginRight: -1 }, size: "medium", edge: "end" }));
33
+ }, [dataUpdatedAt, isFetching, refetch, refetchTime]);
33
34
  const routeNotFound = !(swapRoutes === null || swapRoutes === void 0 ? void 0 : swapRoutes.length) && !isLoading && !isFetching;
34
35
  return (_jsx(Stack, Object.assign({ direction: "column", spacing: 2, flex: 1 }, { children: routeNotFound ? (_jsx(SwapRouteNotFoundCard, {})) : isLoading || isFetching ? (Array.from({ length: 3 }).map((_, index) => (_jsx(SwapRouteCardSkeleton, {}, index)))) : (swapRoutes === null || swapRoutes === void 0 ? void 0 : swapRoutes.map((route) => (_jsx(SwapRouteCard, { route: route, onClick: () => handleRouteClick(route) }, route.id)))) })));
35
36
  };
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import LIFI from '@lifi/sdk';
3
+ export declare const useLiFi: () => LIFI;
4
+ export declare const SDKProvider: React.FC<React.PropsWithChildren>;
@@ -0,0 +1,19 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import LIFI from '@lifi/sdk';
3
+ import { createContext, useContext, useMemo } from 'react';
4
+ import { useWidgetConfig } from '../WidgetProvider';
5
+ let lifi;
6
+ const SDKContext = createContext(null);
7
+ export const useLiFi = () => useContext(SDKContext);
8
+ export const SDKProvider = ({ children, }) => {
9
+ const { sdkConfig, integrator } = useWidgetConfig();
10
+ const value = useMemo(() => {
11
+ const config = Object.assign(Object.assign({}, sdkConfig), { defaultRouteOptions: Object.assign(Object.assign({}, sdkConfig === null || sdkConfig === void 0 ? void 0 : sdkConfig.defaultRouteOptions), { integrator: integrator !== null && integrator !== void 0 ? integrator : window.location.hostname }) });
12
+ if (!lifi) {
13
+ lifi = new LIFI(Object.assign({ disableVersionCheck: true }, config));
14
+ }
15
+ lifi.setConfig(config);
16
+ return lifi;
17
+ }, [integrator, sdkConfig]);
18
+ return _jsx(SDKContext.Provider, Object.assign({ value: value }, { children: children }));
19
+ };
@@ -0,0 +1 @@
1
+ export * from './SDKProvider';
@@ -0,0 +1 @@
1
+ export * from './SDKProvider';
@@ -1,9 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { ChainId } from '@lifi/sdk';
3
- import { useEffect } from 'react';
4
2
  import { FormProvider, useForm } from 'react-hook-form';
5
- import { useChainOrderStore, useSetChainOrder } from '../../stores/chains';
6
- import { useWallet } from '../WalletProvider';
7
3
  import { useWidgetConfig } from '../WidgetProvider';
8
4
  import { SwapFormKey } from './types';
9
5
  export const formDefaultValues = {
@@ -12,64 +8,14 @@ export const formDefaultValues = {
12
8
  [SwapFormKey.TokenSearchFilter]: '',
13
9
  };
14
10
  export const SwapFormProvider = ({ children, }) => {
15
- const [setChain] = useSetChainOrder();
16
- const { account } = useWallet();
17
11
  const { fromChain, fromToken, fromAmount, toChain, toToken, toAddress } = useWidgetConfig();
18
12
  const methods = useForm({
19
- defaultValues: Object.assign(Object.assign({}, formDefaultValues), { fromChain: fromChain !== null && fromChain !== void 0 ? fromChain : ChainId.ETH, fromToken, fromAmount: (typeof fromAmount === 'number'
13
+ defaultValues: Object.assign(Object.assign({}, formDefaultValues), { fromChain,
14
+ fromToken, fromAmount: (typeof fromAmount === 'number'
20
15
  ? fromAmount === null || fromAmount === void 0 ? void 0 : fromAmount.toPrecision()
21
- : fromAmount) || formDefaultValues.fromAmount, toChain: toChain !== null && toChain !== void 0 ? toChain : ChainId.ETH, toToken,
16
+ : fromAmount) || formDefaultValues.fromAmount, toChain,
17
+ toToken,
22
18
  toAddress }),
23
19
  });
24
- // Set wallet chain as default if no chains are provided by config and if they were not changed during widget usage
25
- useEffect(() => {
26
- if (!account.isActive || !account.chainId) {
27
- return;
28
- }
29
- const [fromChainValue, toChainValue] = methods.getValues([
30
- SwapFormKey.FromChain,
31
- SwapFormKey.ToChain,
32
- ]);
33
- const { isDirty: isFromChainDirty } = methods.getFieldState(SwapFormKey.FromChain, methods.formState);
34
- const { isDirty: isToChainDirty } = methods.getFieldState(SwapFormKey.ToChain, methods.formState);
35
- const { isDirty: isFromTokenDirty } = methods.getFieldState(SwapFormKey.FromToken, methods.formState);
36
- const { isDirty: isToTokenDirty } = methods.getFieldState(SwapFormKey.ToToken, methods.formState);
37
- // Users can switch chains in the wallet.
38
- // If we don't have a chain in the ordered chain list we should add it.
39
- setChain(account.chainId);
40
- // If we ran out of slots for the ordered chain list and the current chain isn't there
41
- // we should make a recently switched chain as current.
42
- const { chainOrder } = useChainOrderStore.getState();
43
- const hasFromChainInOrderedList = chainOrder.includes(fromChainValue);
44
- const hasToChainInOrderedList = chainOrder.includes(toChainValue);
45
- if ((!fromChain && !isFromChainDirty && !isFromTokenDirty) ||
46
- !hasFromChainInOrderedList) {
47
- methods.setValue(SwapFormKey.FromChain, account.chainId, {
48
- shouldDirty: false,
49
- });
50
- methods.setValue(SwapFormKey.FromToken, '', {
51
- shouldDirty: false,
52
- });
53
- methods.setValue(SwapFormKey.FromAmount, '', {
54
- shouldDirty: false,
55
- });
56
- }
57
- if ((!toChain && !isToChainDirty && !isToTokenDirty) ||
58
- !hasToChainInOrderedList) {
59
- methods.setValue(SwapFormKey.ToChain, account.chainId, {
60
- shouldDirty: false,
61
- });
62
- methods.setValue(SwapFormKey.ToToken, '', {
63
- shouldDirty: false,
64
- });
65
- }
66
- }, [
67
- account.chainId,
68
- account.isActive,
69
- fromChain,
70
- methods,
71
- setChain,
72
- toChain,
73
- ]);
74
20
  return _jsx(FormProvider, Object.assign({}, methods, { children: children }));
75
21
  };
@@ -16,12 +16,12 @@ export declare type SwapFormValues = {
16
16
  [SwapFormKey.ToToken]: string;
17
17
  [SwapFormKey.ToAddress]: string;
18
18
  };
19
- export declare type SwapFormDirection = 'from' | 'to';
19
+ export declare type SwapFormType = 'from' | 'to';
20
20
  export declare const SwapFormKeyHelper: {
21
- getChainKey: (formType: SwapFormDirection) => 'fromChain' | 'toChain';
22
- getTokenKey: (formType: SwapFormDirection) => 'fromToken' | 'toToken';
23
- getAmountKey: (formType: SwapFormDirection) => 'fromAmount' | 'toAmount';
21
+ getChainKey: (formType: SwapFormType) => 'fromChain' | 'toChain';
22
+ getTokenKey: (formType: SwapFormType) => 'fromToken' | 'toToken';
23
+ getAmountKey: (formType: SwapFormType) => 'fromAmount' | 'toAmount';
24
24
  };
25
25
  export interface SwapFormTypeProps {
26
- formType: SwapFormDirection;
26
+ formType: SwapFormType;
27
27
  }
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ export declare const TelemetryProvider: React.FC<{
3
+ children: React.ReactElement<any, any> | null;
4
+ }>;
@@ -0,0 +1,7 @@
1
+ import { useWidgetConfig } from '..';
2
+ import { useTelemetry } from '../../hooks';
3
+ export const TelemetryProvider = ({ children }) => {
4
+ const config = useWidgetConfig();
5
+ useTelemetry(config === null || config === void 0 ? void 0 : config.disableTelemetry);
6
+ return children;
7
+ };
@@ -0,0 +1 @@
1
+ export * from './TelemetryProvider';
@@ -0,0 +1 @@
1
+ export * from './TelemetryProvider';
@@ -1,11 +1,8 @@
1
1
  import type { Signer } from 'ethers';
2
2
  import type { FC, PropsWithChildren } from 'react';
3
- import type { WidgetWalletManagement } from '../../types';
4
3
  import type { WalletContextProps } from './types';
5
4
  export declare const useWallet: () => WalletContextProps;
6
- export declare const WalletProvider: FC<PropsWithChildren<{
7
- walletManagement?: WidgetWalletManagement;
8
- }>>;
5
+ export declare const WalletProvider: FC<PropsWithChildren>;
9
6
  export declare const extractAccountFromSigner: (signer?: Signer) => Promise<{
10
7
  address: string | undefined;
11
8
  isActive: boolean;
@@ -10,6 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { jsx as _jsx } from "react/jsx-runtime";
11
11
  import { addChain as walletAddChain, switchChain as walletSwitchChain, switchChainAndAddToken, useLiFiWalletManagement, } from '@lifi/wallet-management';
12
12
  import { createContext, useCallback, useContext, useEffect, useMemo, useState, } from 'react';
13
+ import { useFormContext } from 'react-hook-form';
14
+ import { useChainOrderStore } from '../../stores/chains';
15
+ import { SwapFormKey } from '../SwapFormProvider';
16
+ import { isItemAllowed, useWidgetConfig } from '../WidgetProvider';
13
17
  const stub = () => {
14
18
  throw new Error('You forgot to wrap your component in <WalletProvider>.');
15
19
  };
@@ -23,7 +27,9 @@ const initialContext = {
23
27
  };
24
28
  const WalletContext = createContext(initialContext);
25
29
  export const useWallet = () => useContext(WalletContext);
26
- export const WalletProvider = ({ children, walletManagement }) => {
30
+ export const WalletProvider = ({ children }) => {
31
+ const { walletManagement, fromChain, toChain, chains, disabledChains } = useWidgetConfig();
32
+ const methods = useFormContext();
27
33
  const { connect: walletManagementConnect, disconnect: walletManagementDisconnect, signer, provider, } = useLiFiWalletManagement();
28
34
  const [account, setAccount] = useState({});
29
35
  const connect = useCallback((wallet) => __awaiter(void 0, void 0, void 0, function* () {
@@ -88,6 +94,58 @@ export const WalletProvider = ({ children, walletManagement }) => {
88
94
  account,
89
95
  provider,
90
96
  }), [account, addChain, addToken, connect, disconnect, provider, switchChain]);
97
+ // Set wallet chain as default if no chains are provided by config and if they were not changed during widget usage
98
+ useEffect(() => {
99
+ const chainAllowed = account.chainId && isItemAllowed(account.chainId, chains, disabledChains);
100
+ if (!account.isActive || !account.chainId || !chainAllowed) {
101
+ return;
102
+ }
103
+ const [fromChainValue, toChainValue] = methods.getValues([
104
+ SwapFormKey.FromChain,
105
+ SwapFormKey.ToChain,
106
+ ]);
107
+ const { isDirty: isFromChainDirty } = methods.getFieldState(SwapFormKey.FromChain, methods.formState);
108
+ const { isDirty: isToChainDirty } = methods.getFieldState(SwapFormKey.ToChain, methods.formState);
109
+ const { isDirty: isFromTokenDirty } = methods.getFieldState(SwapFormKey.FromToken, methods.formState);
110
+ const { isDirty: isToTokenDirty } = methods.getFieldState(SwapFormKey.ToToken, methods.formState);
111
+ const { chainOrder, setChain } = useChainOrderStore.getState();
112
+ // Users can switch chains in the wallet.
113
+ // If we don't have a chain in the ordered chain list we should add it.
114
+ setChain(account.chainId);
115
+ // If we ran out of slots for the ordered chain list and the current chain isn't there
116
+ // we should make a recently switched chain as current.
117
+ const hasFromChainInOrderedList = chainOrder.includes(fromChainValue);
118
+ const hasToChainInOrderedList = chainOrder.includes(toChainValue);
119
+ if ((!fromChain && !isFromChainDirty && !isFromTokenDirty) ||
120
+ !hasFromChainInOrderedList) {
121
+ methods.setValue(SwapFormKey.FromChain, account.chainId, {
122
+ shouldDirty: false,
123
+ });
124
+ methods.setValue(SwapFormKey.FromToken, '', {
125
+ shouldDirty: false,
126
+ });
127
+ methods.setValue(SwapFormKey.FromAmount, '', {
128
+ shouldDirty: false,
129
+ });
130
+ }
131
+ if ((!toChain && !isToChainDirty && !isToTokenDirty) ||
132
+ !hasToChainInOrderedList) {
133
+ methods.setValue(SwapFormKey.ToChain, account.chainId, {
134
+ shouldDirty: false,
135
+ });
136
+ methods.setValue(SwapFormKey.ToToken, '', {
137
+ shouldDirty: false,
138
+ });
139
+ }
140
+ }, [
141
+ account.chainId,
142
+ account.isActive,
143
+ chains,
144
+ disabledChains,
145
+ fromChain,
146
+ methods,
147
+ toChain,
148
+ ]);
91
149
  return (_jsx(WalletContext.Provider, Object.assign({ value: value }, { children: children })));
92
150
  };
93
151
  export const extractAccountFromSigner = (signer) => __awaiter(void 0, void 0, void 0, function* () {
@@ -10,20 +10,15 @@ var __rest = (this && this.__rest) || function (s, e) {
10
10
  return t;
11
11
  };
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
- import { checkPackageUpdates, getChainByKey } from '@lifi/sdk';
14
- import { createContext, useContext, useEffect, useMemo } from 'react';
15
- import { updateLiFiConfig } from '../../config/lifi';
16
- import { name, version } from '../../config/version';
17
- const stub = () => {
18
- throw new Error('You forgot to wrap your component in <WidgetProvider>.');
19
- };
13
+ import { getChainByKey } from '@lifi/sdk';
14
+ import { createContext, useContext, useMemo } from 'react';
20
15
  const initialContext = {
21
16
  disabledChains: [],
22
17
  };
23
18
  const WidgetContext = createContext(initialContext);
24
19
  export const useWidgetConfig = () => useContext(WidgetContext);
25
20
  export const WidgetProvider = (_a) => {
26
- var { children } = _a, _b = _a.config, _c = _b === void 0 ? {} : _b, { fromChain, fromToken, toChain, toToken, fromAmount, integrator, sdkConfig } = _c, config = __rest(_c, ["fromChain", "fromToken", "toChain", "toToken", "fromAmount", "integrator", "sdkConfig"]);
21
+ var { children } = _a, _b = _a.config, _c = _b === void 0 ? {} : _b, { fromChain, fromToken, toChain, toToken, fromAmount } = _c, config = __rest(_c, ["fromChain", "fromToken", "toChain", "toToken", "fromAmount"]);
27
22
  const value = useMemo(() => {
28
23
  var _a, _b, _c, _d;
29
24
  try {
@@ -53,11 +48,5 @@ export const WidgetProvider = (_a) => {
53
48
  return config;
54
49
  }
55
50
  }, [config, fromAmount, fromChain, fromToken, toChain, toToken]);
56
- useEffect(() => {
57
- updateLiFiConfig(Object.assign(Object.assign({}, sdkConfig), { defaultRouteOptions: Object.assign(Object.assign({}, sdkConfig === null || sdkConfig === void 0 ? void 0 : sdkConfig.defaultRouteOptions), { integrator: integrator !== null && integrator !== void 0 ? integrator : window.location.hostname }) }));
58
- }, [integrator, sdkConfig]);
59
- useEffect(() => {
60
- checkPackageUpdates(name, version);
61
- }, []);
62
51
  return (_jsx(WidgetContext.Provider, Object.assign({ value: value }, { children: children })));
63
52
  };
@@ -1,2 +1,3 @@
1
1
  export * from './types';
2
+ export * from './utils';
2
3
  export * from './WidgetProvider';
@@ -1,2 +1,3 @@
1
1
  export * from './types';
2
+ export * from './utils';
2
3
  export * from './WidgetProvider';
@@ -0,0 +1,4 @@
1
+ export declare const isItemAllowed: <T>(chainId: T, chains?: {
2
+ allow?: T[] | undefined;
3
+ deny?: T[] | undefined;
4
+ } | undefined, disabledChains?: T[] | undefined) => boolean;
@@ -0,0 +1,9 @@
1
+ export const isItemAllowed = (chainId, chains,
2
+ /** @deprecated Remove in the next major release */
3
+ disabledChains) => {
4
+ var _a, _b;
5
+ if ((_a = chains === null || chains === void 0 ? void 0 : chains.allow) === null || _a === void 0 ? void 0 : _a.length) {
6
+ return chains.allow.includes(chainId);
7
+ }
8
+ return !((disabledChains === null || disabledChains === void 0 ? void 0 : disabledChains.includes(chainId)) || ((_b = chains === null || chains === void 0 ? void 0 : chains.deny) === null || _b === void 0 ? void 0 : _b.includes(chainId)));
9
+ };
@@ -1,4 +1,6 @@
1
+ export * from './SDKProvider';
1
2
  export * from './SwapFormProvider';
3
+ export * from './TelemetryProvider';
2
4
  export * from './ThemeProvider';
3
5
  export * from './WalletProvider';
4
6
  export * from './WidgetProvider';
@@ -1,4 +1,6 @@
1
+ export * from './SDKProvider';
1
2
  export * from './SwapFormProvider';
3
+ export * from './TelemetryProvider';
2
4
  export * from './ThemeProvider';
3
5
  export * from './WalletProvider';
4
6
  export * from './WidgetProvider';
@@ -1,4 +1,3 @@
1
1
  export * from './types';
2
2
  export * from './useChainOrder';
3
3
  export * from './useChainOrderStore';
4
- export * from './useInitializeChainOrder';
@@ -1,4 +1,3 @@
1
1
  export * from './types';
2
2
  export * from './useChainOrder';
3
3
  export * from './useChainOrderStore';
4
- export * from './useInitializeChainOrder';
@@ -3,6 +3,6 @@ export interface ChainOrderState {
3
3
  availableChains: number[];
4
4
  }
5
5
  export interface ChainOrderStore extends ChainOrderState {
6
- initializeChains(chainIds: number[]): void;
6
+ initializeChains(chainIds: number[]): number[];
7
7
  setChain(chainId: number): void;
8
8
  }
@@ -3,22 +3,25 @@ import create from 'zustand';
3
3
  import { persist } from 'zustand/middleware';
4
4
  import { immer } from 'zustand/middleware/immer';
5
5
  export const maxChainToOrder = 9;
6
- export const useChainOrderStore = create()(persist(immer((set) => ({
6
+ export const useChainOrderStore = create()(persist(immer((set, get) => ({
7
7
  chainOrder: [],
8
8
  availableChains: [],
9
- initializeChains: (chainIds) => set((state) => {
10
- state.availableChains = chainIds;
11
- state.chainOrder = state.chainOrder.filter((chainId) => chainIds.includes(chainId));
12
- const chainsToAdd = chainIds.filter((chainId) => !state.chainOrder.includes(chainId));
13
- if (state.chainOrder.length === maxChainToOrder ||
14
- !chainsToAdd.length) {
15
- return;
16
- }
17
- const chainsToAddLength = maxChainToOrder - state.chainOrder.length;
18
- for (let index = 0; index < chainsToAddLength; index++) {
19
- state.chainOrder.push(chainsToAdd[index]);
20
- }
21
- }),
9
+ initializeChains: (chainIds) => {
10
+ set((state) => {
11
+ state.availableChains = chainIds;
12
+ state.chainOrder = state.chainOrder.filter((chainId) => chainIds.includes(chainId));
13
+ const chainsToAdd = chainIds.filter((chainId) => !state.chainOrder.includes(chainId));
14
+ if (state.chainOrder.length === maxChainToOrder ||
15
+ !chainsToAdd.length) {
16
+ return;
17
+ }
18
+ const chainsToAddLength = maxChainToOrder - state.chainOrder.length;
19
+ for (let index = 0; index < chainsToAddLength; index++) {
20
+ state.chainOrder.push(chainsToAdd[index]);
21
+ }
22
+ });
23
+ return get().chainOrder;
24
+ },
22
25
  setChain: (chainId) => set((state) => {
23
26
  if (state.chainOrder.includes(chainId) ||
24
27
  !state.availableChains.includes(chainId)) {
@@ -1,4 +1,5 @@
1
- import type { Route } from '@lifi/sdk';
1
+ import type { Process, Route } from '@lifi/sdk';
2
2
  export declare const isRouteCompleted: (route: Route) => boolean;
3
3
  export declare const isRouteFailed: (route: Route) => boolean;
4
4
  export declare const isRouteActive: (route?: Route) => boolean;
5
+ export declare const getUpdatedProcess: (currentRoute: Route, updatedRoute: Route) => Process | undefined;
@@ -1,3 +1,4 @@
1
+ import microdiff from 'microdiff';
1
2
  export const isRouteCompleted = (route) => {
2
3
  return route.steps.every((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'DONE'; });
3
4
  };
@@ -13,3 +14,14 @@ export const isRouteActive = (route) => {
13
14
  const alreadyStarted = route.steps.some((step) => step.execution);
14
15
  return !isDone && !isFailed && alreadyStarted;
15
16
  };
17
+ export const getUpdatedProcess = (currentRoute, updatedRoute) => {
18
+ const processDiff = microdiff(currentRoute, updatedRoute).find((diff) => diff.path.includes('process'));
19
+ if (!processDiff) {
20
+ return undefined;
21
+ }
22
+ // e.g. ['steps', 0, 'execution', 'process', 0]
23
+ const process = processDiff.path
24
+ .slice(0, processDiff.path.findIndex((path) => path === 'process') + 2)
25
+ .reduce((obj, path) => obj[path], updatedRoute);
26
+ return process;
27
+ };
@@ -1,5 +1,4 @@
1
1
  export * from './types';
2
2
  export * from './useAppearance';
3
- export * from './useSetSettings';
4
3
  export * from './useSettings';
5
4
  export * from './useSettingsStore';
@@ -1,5 +1,4 @@
1
1
  export * from './types';
2
2
  export * from './useAppearance';
3
- export * from './useSetSettings';
4
3
  export * from './useSettings';
5
4
  export * from './useSettingsStore';
@@ -2,6 +2,8 @@ import shallow from 'zustand/shallow';
2
2
  import { useSettingsStore } from './useSettingsStore';
3
3
  export const useAppearance = () => {
4
4
  const [appearance, setValue] = useSettingsStore((state) => [state.appearance, state.setValue], shallow);
5
- const setAppearance = (appearance) => setValue('appearance', appearance);
5
+ const setAppearance = (appearance) => {
6
+ setValue('appearance', appearance);
7
+ };
6
8
  return [appearance, setAppearance];
7
9
  };