@lifi/widget 1.0.0
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.
- package/App.d.ts +4 -0
- package/App.js +18 -0
- package/AppDrawer.d.ts +14 -0
- package/AppDrawer.js +34 -0
- package/AppDrawer.style.d.ts +49 -0
- package/AppDrawer.style.js +52 -0
- package/AppProvider.d.ts +6 -0
- package/AppProvider.js +12 -0
- package/LICENSE.md +21 -0
- package/components/AppContainer.d.ts +5 -0
- package/components/AppContainer.js +53 -0
- package/components/BottomSheet/BottomSheet.d.ts +3 -0
- package/components/BottomSheet/BottomSheet.js +42 -0
- package/components/BottomSheet/index.d.ts +2 -0
- package/components/BottomSheet/index.js +2 -0
- package/components/BottomSheet/types.d.ts +10 -0
- package/components/BottomSheet/types.js +1 -0
- package/components/Card/Card.d.ts +8 -0
- package/components/Card/Card.js +8 -0
- package/components/Card/CardContainer.d.ts +11 -0
- package/components/Card/CardContainer.js +16 -0
- package/components/Card/CardHeader.d.ts +18 -0
- package/components/Card/CardHeader.js +20 -0
- package/components/Card/CardTitle.d.ts +14 -0
- package/components/Card/CardTitle.js +9 -0
- package/components/Card/index.d.ts +4 -0
- package/components/Card/index.js +4 -0
- package/components/Header/Header.d.ts +2 -0
- package/components/Header/Header.js +17 -0
- package/components/Header/Header.style.d.ts +22 -0
- package/components/Header/Header.style.js +21 -0
- package/components/Header/NavigationHeader.d.ts +2 -0
- package/components/Header/NavigationHeader.js +46 -0
- package/components/Header/WalletHeader.d.ts +2 -0
- package/components/Header/WalletHeader.js +28 -0
- package/components/Header/index.d.ts +1 -0
- package/components/Header/index.js +1 -0
- package/components/Input.d.ts +1 -0
- package/components/Input.js +14 -0
- package/components/LiFiLogo.d.ts +5 -0
- package/components/LiFiLogo.js +7 -0
- package/components/PoweredBy.d.ts +2 -0
- package/components/PoweredBy.js +14 -0
- package/components/ProgressToNextUpdate/ProgressToNextUpdate.d.ts +7 -0
- package/components/ProgressToNextUpdate/ProgressToNextUpdate.js +49 -0
- package/components/ProgressToNextUpdate/index.d.ts +1 -0
- package/components/ProgressToNextUpdate/index.js +1 -0
- package/components/ReverseTokensButton/ReverseTokensButton.d.ts +4 -0
- package/components/ReverseTokensButton/ReverseTokensButton.js +24 -0
- package/components/ReverseTokensButton/ReverseTokensButton.style.d.ts +29 -0
- package/components/ReverseTokensButton/ReverseTokensButton.style.js +14 -0
- package/components/ReverseTokensButton/index.d.ts +1 -0
- package/components/ReverseTokensButton/index.js +1 -0
- package/components/Select.d.ts +3 -0
- package/components/Select.js +27 -0
- package/components/SelectChainAndToken.d.ts +3 -0
- package/components/SelectChainAndToken.js +19 -0
- package/components/SelectTokenButton/SelectTokenButton.d.ts +5 -0
- package/components/SelectTokenButton/SelectTokenButton.js +29 -0
- package/components/SelectTokenButton/SelectTokenButton.style.d.ts +28 -0
- package/components/SelectTokenButton/SelectTokenButton.style.js +31 -0
- package/components/SelectTokenButton/index.d.ts +1 -0
- package/components/SelectTokenButton/index.js +1 -0
- package/components/StepActions/StepActions.d.ts +13 -0
- package/components/StepActions/StepActions.js +49 -0
- package/components/StepActions/StepActions.style.d.ts +20 -0
- package/components/StepActions/StepActions.style.js +56 -0
- package/components/StepActions/index.d.ts +1 -0
- package/components/StepActions/index.js +1 -0
- package/components/StepActions/types.d.ts +6 -0
- package/components/StepActions/types.js +1 -0
- package/components/StepToken.d.ts +6 -0
- package/components/StepToken.js +21 -0
- package/components/SwapButton/ButtonTooltip.d.ts +4 -0
- package/components/SwapButton/ButtonTooltip.js +3 -0
- package/components/SwapButton/SwapButton.d.ts +2 -0
- package/components/SwapButton/SwapButton.js +77 -0
- package/components/SwapButton/SwapButton.style.d.ts +65 -0
- package/components/SwapButton/SwapButton.style.js +20 -0
- package/components/SwapButton/index.d.ts +1 -0
- package/components/SwapButton/index.js +1 -0
- package/components/SwapInProgress/SwapInProgress.d.ts +3 -0
- package/components/SwapInProgress/SwapInProgress.js +22 -0
- package/components/SwapInProgress/SwapInProgress.style.d.ts +43 -0
- package/components/SwapInProgress/SwapInProgress.style.js +15 -0
- package/components/SwapInProgress/index.d.ts +1 -0
- package/components/SwapInProgress/index.js +1 -0
- package/components/SwapInput/FormPriceHelperText.d.ts +5 -0
- package/components/SwapInput/FormPriceHelperText.js +27 -0
- package/components/SwapInput/SwapInput.d.ts +3 -0
- package/components/SwapInput/SwapInput.js +43 -0
- package/components/SwapInput/SwapInput.style.d.ts +21 -0
- package/components/SwapInput/SwapInput.style.js +30 -0
- package/components/SwapInput/SwapInputAdornment.d.ts +2 -0
- package/components/SwapInput/SwapInputAdornment.js +23 -0
- package/components/SwapInput/SwapInputAdornment.style.d.ts +34 -0
- package/components/SwapInput/SwapInputAdornment.style.js +22 -0
- package/components/SwapInput/index.d.ts +1 -0
- package/components/SwapInput/index.js +1 -0
- package/components/SwapRouteCard/SwapRouteCard.d.ts +4 -0
- package/components/SwapRouteCard/SwapRouteCard.js +34 -0
- package/components/SwapRouteCard/SwapRouteCard.style.d.ts +36 -0
- package/components/SwapRouteCard/SwapRouteCard.style.js +71 -0
- package/components/SwapRouteCard/index.d.ts +1 -0
- package/components/SwapRouteCard/index.js +1 -0
- package/components/SwapRouteCard/types.d.ts +7 -0
- package/components/SwapRouteCard/types.js +1 -0
- package/components/SwapRoutes/SwapRoutes.d.ts +3 -0
- package/components/SwapRoutes/SwapRoutes.js +33 -0
- package/components/SwapRoutes/SwapRoutes.style.d.ts +11 -0
- package/components/SwapRoutes/SwapRoutes.style.js +9 -0
- package/components/SwapRoutes/SwapRoutesUpdateProgress.d.ts +3 -0
- package/components/SwapRoutes/SwapRoutesUpdateProgress.js +24 -0
- package/components/SwapRoutes/index.d.ts +1 -0
- package/components/SwapRoutes/index.js +1 -0
- package/components/Switch.d.ts +1 -0
- package/components/Switch.js +53 -0
- package/components/TextFitter/TextFitter.d.ts +3 -0
- package/components/TextFitter/TextFitter.js +44 -0
- package/components/TextFitter/index.d.ts +1 -0
- package/components/TextFitter/index.js +1 -0
- package/components/TextFitter/types.d.ts +12 -0
- package/components/TextFitter/types.js +1 -0
- package/components/TokenList/TokenList.d.ts +3 -0
- package/components/TokenList/TokenList.js +58 -0
- package/components/TokenList/TokenList.style.d.ts +32 -0
- package/components/TokenList/TokenList.style.js +22 -0
- package/components/TokenList/TokenListItem.d.ts +4 -0
- package/components/TokenList/TokenListItem.js +24 -0
- package/components/TokenList/index.d.ts +1 -0
- package/components/TokenList/index.js +1 -0
- package/components/TokenList/types.d.ts +16 -0
- package/components/TokenList/types.js +1 -0
- package/components/TokenList/utils.d.ts +15 -0
- package/components/TokenList/utils.js +10 -0
- package/config/env.d.ts +12 -0
- package/config/env.js +19 -0
- package/config/queryClient.d.ts +2 -0
- package/config/queryClient.js +25 -0
- package/config/rpcs.d.ts +1 -0
- package/config/rpcs.js +20 -0
- package/config/theme.d.ts +27 -0
- package/config/theme.js +126 -0
- package/hooks/index.d.ts +15 -0
- package/hooks/index.js +15 -0
- package/hooks/useBridges.d.ts +1 -0
- package/hooks/useBridges.js +12 -0
- package/hooks/useChain.d.ts +4 -0
- package/hooks/useChain.js +13 -0
- package/hooks/useChains.d.ts +101 -0
- package/hooks/useChains.js +39 -0
- package/hooks/useContentHeight.d.ts +1 -0
- package/hooks/useContentHeight.js +15 -0
- package/hooks/useDebouncedWatch.d.ts +1 -0
- package/hooks/useDebouncedWatch.js +27 -0
- package/hooks/useExchanges.d.ts +1 -0
- package/hooks/useExchanges.js +12 -0
- package/hooks/useHasSufficientBalance.d.ts +5 -0
- package/hooks/useHasSufficientBalance.js +59 -0
- package/hooks/useRouteExecution/index.d.ts +3 -0
- package/hooks/useRouteExecution/index.js +3 -0
- package/hooks/useRouteExecution/types.d.ts +15 -0
- package/hooks/useRouteExecution/types.js +1 -0
- package/hooks/useRouteExecution/useRouteExecution.d.ts +8 -0
- package/hooks/useRouteExecution/useRouteExecution.js +132 -0
- package/hooks/useRouteExecution/useRouteStore.d.ts +17 -0
- package/hooks/useRouteExecution/useRouteStore.js +71 -0
- package/hooks/useScrollableContainer.d.ts +2 -0
- package/hooks/useScrollableContainer.js +24 -0
- package/hooks/useSettings/index.d.ts +2 -0
- package/hooks/useSettings/index.js +2 -0
- package/hooks/useSettings/types.d.ts +5 -0
- package/hooks/useSettings/types.js +1 -0
- package/hooks/useSettings/useSettingsStore.d.ts +13 -0
- package/hooks/useSettings/useSettingsStore.js +22 -0
- package/hooks/useSwapRoutes.d.ts +8 -0
- package/hooks/useSwapRoutes.js +101 -0
- package/hooks/useToken.d.ts +4 -0
- package/hooks/useToken.js +13 -0
- package/hooks/useTokenBalance.d.ts +7 -0
- package/hooks/useTokenBalance.js +46 -0
- package/hooks/useTokenBalances.d.ts +7 -0
- package/hooks/useTokenBalances.js +54 -0
- package/hooks/useTokens.d.ts +5 -0
- package/hooks/useTokens.js +24 -0
- package/i18n/en/translation.json +138 -0
- package/i18n/index.d.ts +144 -0
- package/i18n/index.js +20 -0
- package/i18n/react-i18next.d.ts +9 -0
- package/index.d.ts +5 -0
- package/index.js +9 -0
- package/lifi.d.ts +2 -0
- package/lifi.js +10 -0
- package/package.json +55 -0
- package/pages/MainPage/MainPage.d.ts +2 -0
- package/pages/MainPage/MainPage.js +11 -0
- package/pages/MainPage/MainPage.style.d.ts +11 -0
- package/pages/MainPage/MainPage.style.js +8 -0
- package/pages/MainPage/SendToRecipientForm.d.ts +2 -0
- package/pages/MainPage/SendToRecipientForm.js +20 -0
- package/pages/MainPage/index.d.ts +1 -0
- package/pages/MainPage/index.js +1 -0
- package/pages/SelectTokenPage/ChainSelect.d.ts +2 -0
- package/pages/SelectTokenPage/ChainSelect.js +25 -0
- package/pages/SelectTokenPage/SearchTokenInput.d.ts +1 -0
- package/pages/SelectTokenPage/SearchTokenInput.js +17 -0
- package/pages/SelectTokenPage/SearchTokenInput.style.d.ts +1 -0
- package/pages/SelectTokenPage/SearchTokenInput.style.js +5 -0
- package/pages/SelectTokenPage/SelectTokenPage.d.ts +5 -0
- package/pages/SelectTokenPage/SelectTokenPage.js +16 -0
- package/pages/SelectTokenPage/index.d.ts +2 -0
- package/pages/SelectTokenPage/index.js +2 -0
- package/pages/SelectTokenPage/types.d.ts +4 -0
- package/pages/SelectTokenPage/types.js +5 -0
- package/pages/SelectWalletPage/SelectWalletPage.d.ts +1 -0
- package/pages/SelectWalletPage/SelectWalletPage.js +68 -0
- package/pages/SelectWalletPage/SelectWalletPage.style.d.ts +46 -0
- package/pages/SelectWalletPage/SelectWalletPage.style.js +27 -0
- package/pages/SelectWalletPage/index.d.ts +1 -0
- package/pages/SelectWalletPage/index.js +1 -0
- package/pages/SettingsPage/AdvancedPreferences.d.ts +1 -0
- package/pages/SettingsPage/AdvancedPreferences.js +19 -0
- package/pages/SettingsPage/ColorSchemeButtonGroup.d.ts +2 -0
- package/pages/SettingsPage/ColorSchemeButtonGroup.js +20 -0
- package/pages/SettingsPage/ColorSchemeButtonGroup.style.d.ts +33 -0
- package/pages/SettingsPage/ColorSchemeButtonGroup.style.js +16 -0
- package/pages/SettingsPage/EnabledBridgesSelect.d.ts +2 -0
- package/pages/SettingsPage/EnabledBridgesSelect.js +15 -0
- package/pages/SettingsPage/EnabledExchangesSelect.d.ts +2 -0
- package/pages/SettingsPage/EnabledExchangesSelect.js +15 -0
- package/pages/SettingsPage/GasPriceSelect.d.ts +1 -0
- package/pages/SettingsPage/GasPriceSelect.js +13 -0
- package/pages/SettingsPage/RoutePrioritySelect.d.ts +2 -0
- package/pages/SettingsPage/RoutePrioritySelect.js +13 -0
- package/pages/SettingsPage/SettingsPage.d.ts +1 -0
- package/pages/SettingsPage/SettingsPage.js +10 -0
- package/pages/SettingsPage/SlippageInput.d.ts +1 -0
- package/pages/SettingsPage/SlippageInput.js +28 -0
- package/pages/SettingsPage/index.d.ts +1 -0
- package/pages/SettingsPage/index.js +1 -0
- package/pages/SwapPage/CircularProgress.d.ts +4 -0
- package/pages/SwapPage/CircularProgress.js +20 -0
- package/pages/SwapPage/CircularProgress.style.d.ts +6 -0
- package/pages/SwapPage/CircularProgress.style.js +51 -0
- package/pages/SwapPage/ExecutionItem.d.ts +6 -0
- package/pages/SwapPage/ExecutionItem.js +21 -0
- package/pages/SwapPage/ExecutionItem.style.d.ts +39 -0
- package/pages/SwapPage/ExecutionItem.style.js +5 -0
- package/pages/SwapPage/StatusBottomSheet.d.ts +3 -0
- package/pages/SwapPage/StatusBottomSheet.js +79 -0
- package/pages/SwapPage/StatusBottomSheet.style.d.ts +25 -0
- package/pages/SwapPage/StatusBottomSheet.style.js +32 -0
- package/pages/SwapPage/StepDivider.d.ts +2 -0
- package/pages/SwapPage/StepDivider.js +6 -0
- package/pages/SwapPage/StepDivider.style.d.ts +11 -0
- package/pages/SwapPage/StepDivider.style.js +8 -0
- package/pages/SwapPage/StepItem.d.ts +7 -0
- package/pages/SwapPage/StepItem.js +29 -0
- package/pages/SwapPage/StepTimer.d.ts +5 -0
- package/pages/SwapPage/StepTimer.js +59 -0
- package/pages/SwapPage/SwapPage.d.ts +2 -0
- package/pages/SwapPage/SwapPage.js +22 -0
- package/pages/SwapPage/SwapPage.style.d.ts +44 -0
- package/pages/SwapPage/SwapPage.style.js +12 -0
- package/pages/SwapPage/index.d.ts +1 -0
- package/pages/SwapPage/index.js +1 -0
- package/pages/SwapPage/utils.d.ts +6 -0
- package/pages/SwapPage/utils.js +85 -0
- package/pages/SwapRoutesPage/SwapRoutesPage.d.ts +3 -0
- package/pages/SwapRoutesPage/SwapRoutesPage.js +24 -0
- package/pages/SwapRoutesPage/SwapRoutesPage.style.d.ts +11 -0
- package/pages/SwapRoutesPage/SwapRoutesPage.style.js +6 -0
- package/pages/SwapRoutesPage/index.d.ts +1 -0
- package/pages/SwapRoutesPage/index.js +1 -0
- package/providers/SwapFormProvider/SwapFormProvider.d.ts +2 -0
- package/providers/SwapFormProvider/SwapFormProvider.js +20 -0
- package/providers/SwapFormProvider/index.d.ts +2 -0
- package/providers/SwapFormProvider/index.js +2 -0
- package/providers/SwapFormProvider/types.d.ts +44 -0
- package/providers/SwapFormProvider/types.js +24 -0
- package/providers/ThemeProvider/ThemeProvider.d.ts +2 -0
- package/providers/ThemeProvider/ThemeProvider.js +33 -0
- package/providers/ThemeProvider/index.d.ts +1 -0
- package/providers/ThemeProvider/index.js +1 -0
- package/providers/WalletProvider/WalletProvider.d.ts +4 -0
- package/providers/WalletProvider/WalletProvider.js +89 -0
- package/providers/WalletProvider/index.d.ts +2 -0
- package/providers/WalletProvider/index.js +2 -0
- package/providers/WalletProvider/types.d.ts +17 -0
- package/providers/WalletProvider/types.js +1 -0
- package/providers/WidgetProvider/WidgetProvider.d.ts +4 -0
- package/providers/WidgetProvider/WidgetProvider.js +44 -0
- package/providers/WidgetProvider/index.d.ts +2 -0
- package/providers/WidgetProvider/index.js +2 -0
- package/providers/WidgetProvider/types.d.ts +8 -0
- package/providers/WidgetProvider/types.js +1 -0
- package/react-app-env.d.ts +1 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/types/widget.d.ts +41 -0
- package/types/widget.js +1 -0
- package/utils/colors.d.ts +2 -0
- package/utils/colors.js +3 -0
- package/utils/deepClone.d.ts +1 -0
- package/utils/deepClone.js +3 -0
- package/utils/elements.d.ts +4 -0
- package/utils/elements.js +5 -0
- package/utils/format.d.ts +9 -0
- package/utils/format.js +76 -0
- package/utils/input.d.ts +1 -0
- package/utils/input.js +24 -0
- package/utils/routes.d.ts +10 -0
- package/utils/routes.js +9 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Route } from '@lifinance/sdk';
|
|
2
|
+
export interface RouteExecutionStore {
|
|
3
|
+
currentRoute?: Route;
|
|
4
|
+
routes: Record<string, RouteExecution>;
|
|
5
|
+
setCurrentRoute: (route?: Route) => void;
|
|
6
|
+
setExecutableRoute: (route: Route) => void;
|
|
7
|
+
updateRoute: (route: Route) => void;
|
|
8
|
+
restartRoute: (routeId: string) => void;
|
|
9
|
+
removeRoute: (routeId: string) => void;
|
|
10
|
+
}
|
|
11
|
+
export declare type RouteExecutionStatus = 'error' | 'idle' | 'loading' | 'success';
|
|
12
|
+
export interface RouteExecution {
|
|
13
|
+
route: Route;
|
|
14
|
+
status: RouteExecutionStatus;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useCallback, useEffect } from 'react';
|
|
11
|
+
import { useMutation } from 'react-query';
|
|
12
|
+
import shallow from 'zustand/shallow';
|
|
13
|
+
import { LiFi } from '../../lifi';
|
|
14
|
+
import { useWallet } from '../../providers/WalletProvider';
|
|
15
|
+
import { deepClone } from '../../utils/deepClone';
|
|
16
|
+
import { useRouteStore } from './useRouteStore';
|
|
17
|
+
export const useRouteExecution = (routeId) => {
|
|
18
|
+
const { route, status } = useRouteStore((state) => state.routes[routeId]);
|
|
19
|
+
const [updateRoute, restartRoute, removeRoute] = useRouteStore((state) => [state.updateRoute, state.restartRoute, state.removeRoute], shallow);
|
|
20
|
+
const { account, switchChain } = useWallet();
|
|
21
|
+
const updateCallback = (updatedRoute) => {
|
|
22
|
+
console.log('Route updated.', updatedRoute);
|
|
23
|
+
updateRoute(deepClone(updatedRoute));
|
|
24
|
+
};
|
|
25
|
+
const switchChainHook = useCallback((requiredChainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
if (!account.isActive || !account.signer) {
|
|
27
|
+
return account.signer;
|
|
28
|
+
}
|
|
29
|
+
if ((yield account.signer.getChainId()) !== requiredChainId) {
|
|
30
|
+
const switched = yield switchChain(requiredChainId);
|
|
31
|
+
if (!switched) {
|
|
32
|
+
throw new Error('Chain was not switched.');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return account.signer;
|
|
36
|
+
}), [account.isActive, account.signer, switchChain]);
|
|
37
|
+
const executeRouteMutation = useMutation(() => {
|
|
38
|
+
if (!account.signer) {
|
|
39
|
+
throw Error('Account signer not found.');
|
|
40
|
+
}
|
|
41
|
+
if (!route) {
|
|
42
|
+
throw Error('Execution route not found.');
|
|
43
|
+
}
|
|
44
|
+
return LiFi.executeRoute(account.signer, route, {
|
|
45
|
+
updateCallback,
|
|
46
|
+
switchChainHook,
|
|
47
|
+
infiniteApproval: false,
|
|
48
|
+
});
|
|
49
|
+
}, {
|
|
50
|
+
onMutate: () => {
|
|
51
|
+
console.log('Execution started.', routeId);
|
|
52
|
+
},
|
|
53
|
+
onError: () => {
|
|
54
|
+
console.warn('Execution failed.', routeId);
|
|
55
|
+
},
|
|
56
|
+
onSuccess: (route) => {
|
|
57
|
+
console.log('Executed successfully.', routeId);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const resumeRouteMutation = useMutation((resumedRoute) => {
|
|
61
|
+
if (!account.signer) {
|
|
62
|
+
throw Error('Account signer not found.');
|
|
63
|
+
}
|
|
64
|
+
if (!route) {
|
|
65
|
+
throw Error('Execution route not found.');
|
|
66
|
+
}
|
|
67
|
+
return LiFi.resumeRoute(account.signer, resumedRoute !== null && resumedRoute !== void 0 ? resumedRoute : route, {
|
|
68
|
+
updateCallback,
|
|
69
|
+
switchChainHook,
|
|
70
|
+
infiniteApproval: false,
|
|
71
|
+
});
|
|
72
|
+
}, {
|
|
73
|
+
onMutate: () => {
|
|
74
|
+
console.log('Resumed to execution.', routeId);
|
|
75
|
+
},
|
|
76
|
+
onError: () => {
|
|
77
|
+
console.warn('Resumed execution failed.', routeId);
|
|
78
|
+
},
|
|
79
|
+
onSuccess: (route) => {
|
|
80
|
+
console.log('Resumed execution successful.', route);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
const executeRoute = useCallback(() => {
|
|
84
|
+
executeRouteMutation.mutateAsync(undefined, {
|
|
85
|
+
onError: () => {
|
|
86
|
+
console.warn('Real execution failed!', routeId);
|
|
87
|
+
// Notification.showNotification(NotificationType.SwapExecution_ERROR);
|
|
88
|
+
},
|
|
89
|
+
onSuccess: (route) => {
|
|
90
|
+
console.log('Real execution successfully!', route);
|
|
91
|
+
// Notification.showNotification(NotificationType.TRANSACTION_SUCCESSFULL);
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}, [executeRouteMutation, routeId]);
|
|
95
|
+
const resumeRoute = useCallback((route) => {
|
|
96
|
+
resumeRouteMutation.mutateAsync(route, {
|
|
97
|
+
onError: () => {
|
|
98
|
+
console.warn('Real resumed execution failed.', routeId);
|
|
99
|
+
},
|
|
100
|
+
onSuccess: (route) => {
|
|
101
|
+
console.log('Real resumed execution successful.', route);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
}, [resumeRouteMutation, routeId]);
|
|
105
|
+
const restartRouteMutation = useCallback(() => {
|
|
106
|
+
restartRoute(routeId);
|
|
107
|
+
resumeRoute(useRouteStore.getState().routes[routeId].route);
|
|
108
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
109
|
+
}, [resumeRoute, routeId]);
|
|
110
|
+
const removeRouteMutation = useCallback(() => {
|
|
111
|
+
removeRoute(routeId);
|
|
112
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
113
|
+
}, [routeId]);
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
// check if route is eligible for automatic resuming
|
|
116
|
+
const isDone = route.steps.every((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'DONE'; });
|
|
117
|
+
const isFailed = route.steps.some((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED'; });
|
|
118
|
+
const alreadyStarted = route.steps.some((step) => step.execution);
|
|
119
|
+
if (!isDone && !isFailed && alreadyStarted) {
|
|
120
|
+
resumeRoute();
|
|
121
|
+
}
|
|
122
|
+
return () => LiFi.moveExecutionToBackground(route);
|
|
123
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
124
|
+
}, []);
|
|
125
|
+
return {
|
|
126
|
+
executeRoute,
|
|
127
|
+
restartRoute: restartRouteMutation,
|
|
128
|
+
removeRoute: removeRouteMutation,
|
|
129
|
+
route,
|
|
130
|
+
status,
|
|
131
|
+
};
|
|
132
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Route } from '@lifinance/sdk';
|
|
2
|
+
import type { RouteExecutionStore } from './types';
|
|
3
|
+
export declare const useRouteStore: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<RouteExecutionStore>, "persist"> & {
|
|
4
|
+
persist: {
|
|
5
|
+
setOptions: (options: Partial<import("zustand/middleware").PersistOptions<RouteExecutionStore, {
|
|
6
|
+
routes: Record<string, import("./types").RouteExecution>;
|
|
7
|
+
}>>) => void;
|
|
8
|
+
clearStorage: () => void;
|
|
9
|
+
rehydrate: () => Promise<void>;
|
|
10
|
+
hasHydrated: () => boolean;
|
|
11
|
+
onHydrate: (fn: (state: RouteExecutionStore) => void) => () => void;
|
|
12
|
+
onFinishHydration: (fn: (state: RouteExecutionStore) => void) => () => void;
|
|
13
|
+
};
|
|
14
|
+
}>;
|
|
15
|
+
export declare const useSetExecutableRoute: () => (route: Route) => void;
|
|
16
|
+
export declare const useExecutingRoutes: () => import("./types").RouteExecution[];
|
|
17
|
+
export declare const useCurrentRoute: () => [Route | undefined, (route?: Route) => void];
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import produce from 'immer';
|
|
2
|
+
import create from 'zustand';
|
|
3
|
+
import { persist } from 'zustand/middleware';
|
|
4
|
+
import shallow from 'zustand/shallow';
|
|
5
|
+
export const useRouteStore = create()(persist((set) => ({
|
|
6
|
+
routes: {},
|
|
7
|
+
setCurrentRoute: (route) => set(produce((state) => {
|
|
8
|
+
state.currentRoute = route;
|
|
9
|
+
})),
|
|
10
|
+
setExecutableRoute: (route) => set(produce((state) => {
|
|
11
|
+
if (!state.routes[route.id]) {
|
|
12
|
+
// clean previous idle routes that were not executed
|
|
13
|
+
Object.keys(state.routes)
|
|
14
|
+
.filter((routeId) => state.routes[routeId].status === 'idle')
|
|
15
|
+
.forEach((routeId) => delete state.routes[routeId]);
|
|
16
|
+
state.routes[route.id] = {
|
|
17
|
+
route,
|
|
18
|
+
status: 'idle',
|
|
19
|
+
};
|
|
20
|
+
state.currentRoute = route;
|
|
21
|
+
}
|
|
22
|
+
})),
|
|
23
|
+
updateRoute: (route) => set(produce((state) => {
|
|
24
|
+
state.routes[route.id].route = route;
|
|
25
|
+
const isFailed = route.steps.some((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED'; });
|
|
26
|
+
if (isFailed) {
|
|
27
|
+
state.routes[route.id].status = 'error';
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const isDone = route.steps.every((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'DONE'; });
|
|
31
|
+
if (isDone) {
|
|
32
|
+
state.routes[route.id].status = 'success';
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const isLoading = route.steps.some((step) => step.execution);
|
|
36
|
+
if (isLoading) {
|
|
37
|
+
state.routes[route.id].status = 'loading';
|
|
38
|
+
}
|
|
39
|
+
})),
|
|
40
|
+
restartRoute: (routeId) => set(produce((state) => {
|
|
41
|
+
state.routes[routeId].route.steps.forEach((step) => {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
const stepHasFailed = ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED';
|
|
44
|
+
// check if the step has been cancelled which is a "failed" state
|
|
45
|
+
const stepHasBeenCancelled = (_b = step.execution) === null || _b === void 0 ? void 0 : _b.process.some((process) => process.status === 'CANCELLED');
|
|
46
|
+
if (step.execution && (stepHasFailed || stepHasBeenCancelled)) {
|
|
47
|
+
step.execution.status = 'RESUME';
|
|
48
|
+
// remove last (failed) process
|
|
49
|
+
step.execution.process.pop();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
state.routes[routeId].status = 'loading';
|
|
53
|
+
})),
|
|
54
|
+
removeRoute: (routeId) => set(produce((state) => {
|
|
55
|
+
if (state.routes[routeId]) {
|
|
56
|
+
delete state.routes[routeId];
|
|
57
|
+
}
|
|
58
|
+
})),
|
|
59
|
+
}), {
|
|
60
|
+
name: 'li.fi-widget-executable-routes',
|
|
61
|
+
partialize: (state) => ({ routes: state.routes }),
|
|
62
|
+
}));
|
|
63
|
+
export const useSetExecutableRoute = () => {
|
|
64
|
+
return useRouteStore((state) => state.setExecutableRoute);
|
|
65
|
+
};
|
|
66
|
+
export const useExecutingRoutes = () => {
|
|
67
|
+
return useRouteStore((state) => Object.values(state.routes).filter((route) => route.status === 'loading' || route.status === 'error'));
|
|
68
|
+
};
|
|
69
|
+
export const useCurrentRoute = () => {
|
|
70
|
+
return useRouteStore((state) => [state.currentRoute, state.setCurrentRoute], shallow);
|
|
71
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useLayoutEffect, useState } from 'react';
|
|
2
|
+
import { ElementId } from '../utils/elements';
|
|
3
|
+
export const useScrollableContainer = () => {
|
|
4
|
+
const [containerElement, setContainerElement] = useState(() => document.getElementById(ElementId.ScrollableContainer));
|
|
5
|
+
useLayoutEffect(() => {
|
|
6
|
+
if (!containerElement) {
|
|
7
|
+
setContainerElement(document.getElementById(ElementId.ScrollableContainer));
|
|
8
|
+
}
|
|
9
|
+
}, [containerElement]);
|
|
10
|
+
return containerElement;
|
|
11
|
+
};
|
|
12
|
+
export const useScrollableOverflowHidden = () => {
|
|
13
|
+
useLayoutEffect(() => {
|
|
14
|
+
const element = document.getElementById(ElementId.ScrollableContainer);
|
|
15
|
+
if (element) {
|
|
16
|
+
element.style.overflowY = 'hidden';
|
|
17
|
+
}
|
|
18
|
+
return () => {
|
|
19
|
+
if (element) {
|
|
20
|
+
element.style.overflowY = 'auto';
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}, []);
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Appearance } from '../../types';
|
|
2
|
+
import { SettingsStore } from './types';
|
|
3
|
+
export declare const useSettingsStore: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<SettingsStore>, "persist"> & {
|
|
4
|
+
persist: {
|
|
5
|
+
setOptions: (options: Partial<import("zustand/middleware").PersistOptions<SettingsStore, Partial<SettingsStore>>>) => void;
|
|
6
|
+
clearStorage: () => void;
|
|
7
|
+
rehydrate: () => Promise<void>;
|
|
8
|
+
hasHydrated: () => boolean;
|
|
9
|
+
onHydrate: (fn: (state: SettingsStore) => void) => () => void;
|
|
10
|
+
onFinishHydration: (fn: (state: SettingsStore) => void) => () => void;
|
|
11
|
+
};
|
|
12
|
+
}>;
|
|
13
|
+
export declare const useAppearance: () => [Appearance, (appearance: Appearance) => void];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import produce from 'immer';
|
|
2
|
+
import create from 'zustand';
|
|
3
|
+
import { persist } from 'zustand/middleware';
|
|
4
|
+
import shallow from 'zustand/shallow';
|
|
5
|
+
export const useSettingsStore = create()(persist((set) => ({
|
|
6
|
+
appearance: 'auto',
|
|
7
|
+
setAppearance: (appearance) => set(produce((state) => {
|
|
8
|
+
state.appearance = appearance;
|
|
9
|
+
})),
|
|
10
|
+
}), {
|
|
11
|
+
name: 'li.fi-widget-settings',
|
|
12
|
+
version: 1,
|
|
13
|
+
migrate: (persistedState, version) => {
|
|
14
|
+
if (version === 0 && persistedState.appearance === 'system') {
|
|
15
|
+
persistedState.appearance = 'auto';
|
|
16
|
+
}
|
|
17
|
+
return persistedState;
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
export const useAppearance = () => {
|
|
21
|
+
return useSettingsStore((state) => [state.appearance, state.setAppearance], shallow);
|
|
22
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const useSwapRoutes: () => {
|
|
2
|
+
routes: import("@lifinance/types").Route[] | undefined;
|
|
3
|
+
isLoading: boolean;
|
|
4
|
+
isFetching: boolean;
|
|
5
|
+
dataUpdatedAt: number;
|
|
6
|
+
refetchTime: number;
|
|
7
|
+
refetch: <TPageData>(options?: (import("react-query").RefetchOptions & import("react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("react-query").QueryObserverResult<import("@lifinance/types").RoutesResponse, unknown>>;
|
|
8
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useEffect } from 'react';
|
|
11
|
+
import { useWatch } from 'react-hook-form';
|
|
12
|
+
import { useQuery } from 'react-query';
|
|
13
|
+
import { useDebouncedWatch, useToken } from '.';
|
|
14
|
+
import { LiFi } from '../lifi';
|
|
15
|
+
import { SwapFormKey } from '../providers/SwapFormProvider';
|
|
16
|
+
import { useWallet } from '../providers/WalletProvider';
|
|
17
|
+
import { useCurrentRoute } from './useRouteExecution';
|
|
18
|
+
const refetchTime = 60000;
|
|
19
|
+
export const useSwapRoutes = () => {
|
|
20
|
+
const { account } = useWallet();
|
|
21
|
+
const [currentRoute, setCurrentRoute] = useCurrentRoute();
|
|
22
|
+
const [fromChainId, fromTokenAddress, toChainId, toTokenAddress, slippage, enabledBridges, enabledExchanges,] = useWatch({
|
|
23
|
+
name: [
|
|
24
|
+
SwapFormKey.FromChain,
|
|
25
|
+
SwapFormKey.FromToken,
|
|
26
|
+
SwapFormKey.ToChain,
|
|
27
|
+
SwapFormKey.ToToken,
|
|
28
|
+
SwapFormKey.Slippage,
|
|
29
|
+
SwapFormKey.EnabledBridges,
|
|
30
|
+
SwapFormKey.EnabledExchanges,
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
const [fromTokenAmount] = useDebouncedWatch([SwapFormKey.FromAmount], 500);
|
|
34
|
+
const { token } = useToken(fromChainId, fromTokenAddress);
|
|
35
|
+
const isEnabled = Boolean(account.address) &&
|
|
36
|
+
!isNaN(fromChainId) &&
|
|
37
|
+
!isNaN(toChainId) &&
|
|
38
|
+
Boolean(fromTokenAddress) &&
|
|
39
|
+
Boolean(toTokenAddress) &&
|
|
40
|
+
Boolean(fromTokenAmount) &&
|
|
41
|
+
!isNaN(fromTokenAmount) &&
|
|
42
|
+
!isNaN(slippage);
|
|
43
|
+
const { data, isLoading, isFetching, dataUpdatedAt, refetch } = useQuery([
|
|
44
|
+
'routes',
|
|
45
|
+
account.address,
|
|
46
|
+
fromChainId,
|
|
47
|
+
fromTokenAddress,
|
|
48
|
+
fromTokenAmount,
|
|
49
|
+
toChainId,
|
|
50
|
+
toTokenAddress,
|
|
51
|
+
slippage,
|
|
52
|
+
enabledBridges,
|
|
53
|
+
enabledExchanges,
|
|
54
|
+
], ({ queryKey: [_, address, fromChainId, fromTokenAddress, fromTokenAmount, toChainId, toTokenAddress, slippage, enabledBridges, enabledExchanges,], signal, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
+
var _a;
|
|
56
|
+
return LiFi.getRoutes({
|
|
57
|
+
fromChainId,
|
|
58
|
+
// TODO: simplify
|
|
59
|
+
fromAmount: (Number(fromTokenAmount) *
|
|
60
|
+
Math.pow(10, ((_a = token === null || token === void 0 ? void 0 : token.decimals) !== null && _a !== void 0 ? _a : 0))).toFixed(0),
|
|
61
|
+
fromTokenAddress,
|
|
62
|
+
toChainId,
|
|
63
|
+
toTokenAddress,
|
|
64
|
+
fromAddress: address,
|
|
65
|
+
toAddress: address,
|
|
66
|
+
options: {
|
|
67
|
+
slippage: slippage / 100,
|
|
68
|
+
bridges: {
|
|
69
|
+
allow: enabledBridges,
|
|
70
|
+
},
|
|
71
|
+
exchanges: {
|
|
72
|
+
allow: enabledExchanges,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
}, { signal });
|
|
76
|
+
}), {
|
|
77
|
+
enabled: isEnabled,
|
|
78
|
+
refetchIntervalInBackground: true,
|
|
79
|
+
refetchInterval: refetchTime,
|
|
80
|
+
staleTime: refetchTime,
|
|
81
|
+
// TODO: probably should be removed
|
|
82
|
+
cacheTime: refetchTime,
|
|
83
|
+
});
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
// check that the current route is selected from existing routes
|
|
86
|
+
const isCurrentRouteInSet = data === null || data === void 0 ? void 0 : data.routes.some((route) => route.id === (currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.id));
|
|
87
|
+
const recommendedRoute = data === null || data === void 0 ? void 0 : data.routes[0];
|
|
88
|
+
// we don't want to set the current route again if it's already selected from existing routes
|
|
89
|
+
if (!isCurrentRouteInSet && recommendedRoute) {
|
|
90
|
+
setCurrentRoute(recommendedRoute);
|
|
91
|
+
}
|
|
92
|
+
}, [currentRoute === null || currentRoute === void 0 ? void 0 : currentRoute.id, data === null || data === void 0 ? void 0 : data.routes, setCurrentRoute]);
|
|
93
|
+
return {
|
|
94
|
+
routes: data === null || data === void 0 ? void 0 : data.routes,
|
|
95
|
+
isLoading: isEnabled && isLoading,
|
|
96
|
+
isFetching,
|
|
97
|
+
dataUpdatedAt,
|
|
98
|
+
refetchTime,
|
|
99
|
+
refetch,
|
|
100
|
+
};
|
|
101
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useTokens } from './useTokens';
|
|
3
|
+
export const useToken = (chainId, tokenAddress) => {
|
|
4
|
+
const { tokens, isLoading, isFetching } = useTokens(chainId);
|
|
5
|
+
const token = useMemo(() => {
|
|
6
|
+
const token = tokens === null || tokens === void 0 ? void 0 : tokens.find((token) => token.address === tokenAddress && token.chainId === chainId);
|
|
7
|
+
return token;
|
|
8
|
+
}, [chainId, tokenAddress, tokens]);
|
|
9
|
+
return {
|
|
10
|
+
token,
|
|
11
|
+
isLoading: isLoading && isFetching,
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TokenAmount } from '@lifinance/sdk';
|
|
2
|
+
export declare const useTokenBalance: (chainId: number, tokenAddress: string) => {
|
|
3
|
+
token: TokenAmount | undefined;
|
|
4
|
+
isLoading: boolean;
|
|
5
|
+
isFetching: boolean;
|
|
6
|
+
refetchBalance: (chainId?: number, tokenAddress?: string) => Promise<void>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useCallback } from 'react';
|
|
11
|
+
import { useQuery, useQueryClient } from 'react-query';
|
|
12
|
+
import { LiFi } from '../lifi';
|
|
13
|
+
import { useWallet } from '../providers/WalletProvider';
|
|
14
|
+
import { formatTokenAmount } from '../utils/format';
|
|
15
|
+
import { useToken } from './useToken';
|
|
16
|
+
export const useTokenBalance = (chainId, tokenAddress) => {
|
|
17
|
+
const { account } = useWallet();
|
|
18
|
+
const queryClient = useQueryClient();
|
|
19
|
+
const { token } = useToken(chainId, tokenAddress);
|
|
20
|
+
const { data: tokenWithBalance, isLoading, isFetching, refetch, } = useQuery(['token', account.address, chainId, tokenAddress], ({ queryKey: [, address] }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
if (!address || !token) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const tokenBalance = yield LiFi.getTokenBalance(address, token);
|
|
25
|
+
return Object.assign(Object.assign(Object.assign({}, token), tokenBalance), { amount: formatTokenAmount(tokenBalance === null || tokenBalance === void 0 ? void 0 : tokenBalance.amount) });
|
|
26
|
+
}), {
|
|
27
|
+
enabled: Boolean(account.address) && Boolean(token),
|
|
28
|
+
refetchIntervalInBackground: true,
|
|
29
|
+
refetchInterval: 60000,
|
|
30
|
+
staleTime: 60000,
|
|
31
|
+
cacheTime: 60000,
|
|
32
|
+
});
|
|
33
|
+
const refetchBalance = useCallback((chainId, tokenAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
if (!chainId && !tokenAddress) {
|
|
35
|
+
refetch();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
yield queryClient.invalidateQueries(['token', account.address, chainId, tokenAddress], { type: 'all', exact: true });
|
|
39
|
+
}), [account.address, queryClient, refetch]);
|
|
40
|
+
return {
|
|
41
|
+
token: tokenWithBalance !== null && tokenWithBalance !== void 0 ? tokenWithBalance : token,
|
|
42
|
+
isLoading,
|
|
43
|
+
isFetching,
|
|
44
|
+
refetchBalance,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TokenAmount } from '@lifinance/sdk';
|
|
2
|
+
export declare const useTokenBalances: (selectedChainId: number) => {
|
|
3
|
+
tokens: TokenAmount[] | undefined;
|
|
4
|
+
isLoading: boolean;
|
|
5
|
+
isFetching: boolean;
|
|
6
|
+
updateBalances: <TPageData>(options?: (import("react-query").RefetchOptions & import("react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("react-query").QueryObserverResult<TokenAmount[], unknown>>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useQuery } from 'react-query';
|
|
11
|
+
import { LiFi } from '../lifi';
|
|
12
|
+
import { useWallet } from '../providers/WalletProvider';
|
|
13
|
+
import { formatTokenAmount } from '../utils/format';
|
|
14
|
+
import { useChains } from './useChains';
|
|
15
|
+
import { useTokens } from './useTokens';
|
|
16
|
+
export const useTokenBalances = (selectedChainId) => {
|
|
17
|
+
const { account } = useWallet();
|
|
18
|
+
const { chains, isLoading: isChainsLoading } = useChains();
|
|
19
|
+
const { tokens, isLoading, isFetching } = useTokens(selectedChainId);
|
|
20
|
+
const isBalancesLoadingEnabled = Boolean(account.address) && Boolean(tokens) && Boolean(chains);
|
|
21
|
+
const { data: tokensWithBalance, isLoading: isBalancesLoading, isFetching: isBalancesFetching, refetch, } = useQuery(['token-balances', selectedChainId, account.address], ({ queryKey: [_, chainId, account] }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
+
if (!account || !tokens) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
const tokenBalances = yield LiFi.getTokenBalances(account, tokens);
|
|
26
|
+
const formatedTokens = (tokenBalances.length === 0 ? tokens : tokenBalances).map((token) => {
|
|
27
|
+
token.amount = formatTokenAmount(token.amount);
|
|
28
|
+
return token;
|
|
29
|
+
});
|
|
30
|
+
return [
|
|
31
|
+
...formatedTokens
|
|
32
|
+
.filter((token) => token.amount !== '0')
|
|
33
|
+
.sort((a, b) => {
|
|
34
|
+
var _a, _b, _c, _d;
|
|
35
|
+
return parseFloat((_a = b.amount) !== null && _a !== void 0 ? _a : '0') * parseFloat((_b = b.priceUSD) !== null && _b !== void 0 ? _b : '0') -
|
|
36
|
+
parseFloat((_c = a.amount) !== null && _c !== void 0 ? _c : '0') * parseFloat((_d = a.priceUSD) !== null && _d !== void 0 ? _d : '0');
|
|
37
|
+
}),
|
|
38
|
+
...formatedTokens.filter((token) => token.amount === '0'),
|
|
39
|
+
];
|
|
40
|
+
}), {
|
|
41
|
+
enabled: isBalancesLoadingEnabled,
|
|
42
|
+
refetchIntervalInBackground: true,
|
|
43
|
+
refetchInterval: 60000,
|
|
44
|
+
staleTime: 60000,
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
tokens: tokensWithBalance !== null && tokensWithBalance !== void 0 ? tokensWithBalance : tokens,
|
|
48
|
+
isLoading: (isBalancesLoading && isBalancesLoadingEnabled) ||
|
|
49
|
+
(isLoading && isFetching) ||
|
|
50
|
+
isChainsLoading,
|
|
51
|
+
isFetching: isBalancesFetching,
|
|
52
|
+
updateBalances: refetch,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { useQuery } from 'react-query';
|
|
11
|
+
import { LiFi } from '../lifi';
|
|
12
|
+
export const useTokens = (selectedChainId) => {
|
|
13
|
+
const { data: tokens, isLoading, isFetching, } = useQuery(['tokens', selectedChainId], () => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
var _a;
|
|
15
|
+
const data = yield LiFi.getTokens({ chains: [selectedChainId] });
|
|
16
|
+
return (_a = data.tokens) === null || _a === void 0 ? void 0 : _a[selectedChainId];
|
|
17
|
+
// .sort((a, b) => (a.symbol > b.symbol ? 1 : -1));
|
|
18
|
+
}));
|
|
19
|
+
return {
|
|
20
|
+
tokens,
|
|
21
|
+
isLoading,
|
|
22
|
+
isFetching,
|
|
23
|
+
};
|
|
24
|
+
};
|