@lifi/widget 1.24.0 → 1.25.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.
- package/AppDrawer.style.d.ts +1 -1
- package/AppProvider.js +1 -1
- package/cjs/AppDrawer.style.d.ts +1 -1
- package/cjs/AppProvider.js +1 -1
- package/cjs/components/ActiveSwaps/ActiveSwapItem.js +2 -1
- package/cjs/components/BottomSheet/types.d.ts +1 -1
- package/cjs/components/Card/Card.d.ts +2 -2
- package/cjs/components/Card/CardTitle.d.ts +1 -1
- package/cjs/components/ChainSelect/ChainSelect.js +6 -1
- package/cjs/components/ChainSelect/useChainSelect.d.ts +3 -1
- package/cjs/components/ChainSelect/useChainSelect.js +12 -3
- package/cjs/components/SendToWallet/SendToWallet.style.d.ts +1 -1
- package/cjs/components/SwapInput/FitInputText.d.ts +3 -0
- package/cjs/components/SwapInput/FitInputText.js +19 -0
- package/cjs/components/SwapInput/FormPriceHelperText.d.ts +1 -3
- package/cjs/components/SwapInput/FormPriceHelperText.js +1 -1
- package/cjs/components/SwapInput/SwapInput.js +12 -24
- package/cjs/components/SwapInput/SwapInput.style.d.ts +1 -1
- package/cjs/components/SwapInput/SwapInputEndAdornment.d.ts +3 -0
- package/cjs/components/SwapInput/{SwapInputAdornment.js → SwapInputEndAdornment.js} +3 -3
- package/cjs/components/SwapInput/SwapInputStartAdornment.d.ts +3 -0
- package/cjs/components/SwapInput/SwapInputStartAdornment.js +21 -0
- package/cjs/components/SwapRouteCard/SwapRouteCard.style.d.ts +1 -1
- package/cjs/components/SwapRoutes/SwapRoutes.style.d.ts +1 -1
- package/cjs/components/Token/Token.style.d.ts +1 -1
- package/cjs/components/TokenList/useTokenSelect.js +7 -4
- package/cjs/config/version.d.ts +1 -1
- package/cjs/config/version.js +1 -1
- package/cjs/hooks/useGasSufficiency.js +1 -1
- package/cjs/hooks/useRouteExecution.js +1 -1
- package/cjs/hooks/useSwapRoutes.js +12 -9
- package/cjs/hooks/useTools.d.ts +1 -1
- package/cjs/i18n/en.json +3 -1
- package/cjs/pages/SettingsPage/RoutePrioritySelect.js +2 -1
- package/cjs/pages/SettingsPage/SlippageInput.js +2 -1
- package/cjs/pages/SwapPage/StatusBottomSheet.js +61 -14
- package/cjs/pages/SwapPage/StatusBottomSheet.style.d.ts +4 -2
- package/cjs/pages/SwapPage/StatusBottomSheet.style.js +5 -2
- package/cjs/pages/SwapPage/SwapPage.js +8 -6
- package/cjs/pages/SwapRoutesPage/SwapRoutesPage.style.d.ts +1 -1
- package/cjs/providers/I18nProvider/types.d.ts +5 -5
- package/cjs/providers/SDKProvider/SDKProvider.js +4 -3
- package/cjs/providers/SwapFormProvider/FormUpdater.d.ts +5 -0
- package/cjs/providers/{WalletProvider/WalletFormUpdate.js → SwapFormProvider/FormUpdater.js} +24 -21
- package/cjs/providers/SwapFormProvider/SwapFormProvider.js +12 -7
- package/cjs/providers/SwapFormProvider/types.d.ts +2 -2
- package/cjs/providers/WalletProvider/WalletProvider.d.ts +1 -1
- package/cjs/providers/WalletProvider/WalletProvider.js +1 -2
- package/cjs/providers/WalletProvider/types.d.ts +3 -3
- package/cjs/providers/WidgetProvider/WidgetProvider.js +4 -0
- package/cjs/providers/WidgetProvider/types.d.ts +1 -1
- package/cjs/stores/routes/types.d.ts +8 -1
- package/cjs/stores/routes/types.js +10 -0
- package/cjs/stores/routes/useExecutingRoutesIds.js +3 -1
- package/cjs/stores/routes/useRouteExecutionStore.js +53 -18
- package/cjs/stores/routes/useSwapHistory.js +4 -1
- package/cjs/stores/routes/utils.d.ts +3 -1
- package/cjs/stores/routes/utils.js +12 -4
- package/cjs/stores/settings/types.d.ts +6 -6
- package/cjs/stores/settings/useSettingsStore.d.ts +12 -9
- package/cjs/stores/settings/useSettingsStore.js +22 -5
- package/cjs/types/events.d.ts +1 -1
- package/cjs/types/widget.d.ts +14 -8
- package/cjs/utils/enum.d.ts +1 -0
- package/cjs/utils/enum.js +5 -0
- package/cjs/utils/index.d.ts +1 -0
- package/cjs/utils/index.js +1 -0
- package/cjs/utils/navigationRoutes.d.ts +1 -1
- package/components/ActiveSwaps/ActiveSwapItem.js +2 -1
- package/components/BottomSheet/types.d.ts +1 -1
- package/components/Card/Card.d.ts +2 -2
- package/components/Card/CardTitle.d.ts +1 -1
- package/components/ChainSelect/ChainSelect.js +6 -1
- package/components/ChainSelect/useChainSelect.d.ts +3 -1
- package/components/ChainSelect/useChainSelect.js +13 -4
- package/components/SendToWallet/SendToWallet.style.d.ts +1 -1
- package/components/SwapInput/FitInputText.d.ts +3 -0
- package/components/SwapInput/FitInputText.js +16 -0
- package/components/SwapInput/FormPriceHelperText.d.ts +1 -3
- package/components/SwapInput/FormPriceHelperText.js +1 -1
- package/components/SwapInput/SwapInput.js +16 -28
- package/components/SwapInput/SwapInput.style.d.ts +1 -1
- package/components/SwapInput/SwapInputEndAdornment.d.ts +3 -0
- package/components/SwapInput/{SwapInputAdornment.js → SwapInputEndAdornment.js} +1 -1
- package/components/SwapInput/SwapInputStartAdornment.d.ts +3 -0
- package/components/SwapInput/SwapInputStartAdornment.js +17 -0
- package/components/SwapRouteCard/SwapRouteCard.style.d.ts +1 -1
- package/components/SwapRoutes/SwapRoutes.style.d.ts +1 -1
- package/components/Token/Token.style.d.ts +1 -1
- package/components/TokenList/useTokenSelect.js +8 -5
- package/config/version.d.ts +1 -1
- package/config/version.js +1 -1
- package/hooks/useGasSufficiency.js +2 -2
- package/hooks/useRouteExecution.js +2 -2
- package/hooks/useSwapRoutes.js +12 -9
- package/hooks/useTools.d.ts +1 -1
- package/i18n/en.json +3 -1
- package/package.json +13 -13
- package/pages/SettingsPage/RoutePrioritySelect.js +2 -1
- package/pages/SettingsPage/SlippageInput.js +2 -1
- package/pages/SwapPage/StatusBottomSheet.js +62 -15
- package/pages/SwapPage/StatusBottomSheet.style.d.ts +4 -2
- package/pages/SwapPage/StatusBottomSheet.style.js +5 -2
- package/pages/SwapPage/SwapPage.js +8 -6
- package/pages/SwapRoutesPage/SwapRoutesPage.style.d.ts +1 -1
- package/providers/I18nProvider/types.d.ts +5 -5
- package/providers/SDKProvider/SDKProvider.js +4 -3
- package/providers/SwapFormProvider/FormUpdater.d.ts +5 -0
- package/providers/{WalletProvider/WalletFormUpdate.js → SwapFormProvider/FormUpdater.js} +23 -20
- package/providers/SwapFormProvider/SwapFormProvider.js +12 -7
- package/providers/SwapFormProvider/types.d.ts +2 -2
- package/providers/WalletProvider/WalletProvider.d.ts +1 -1
- package/providers/WalletProvider/WalletProvider.js +2 -3
- package/providers/WalletProvider/types.d.ts +3 -3
- package/providers/WidgetProvider/WidgetProvider.js +5 -1
- package/providers/WidgetProvider/types.d.ts +1 -1
- package/stores/routes/types.d.ts +8 -1
- package/stores/routes/types.js +9 -1
- package/stores/routes/useExecutingRoutesIds.js +3 -1
- package/stores/routes/useRouteExecutionStore.js +51 -16
- package/stores/routes/useSwapHistory.js +4 -1
- package/stores/routes/utils.d.ts +3 -1
- package/stores/routes/utils.js +8 -2
- package/stores/settings/types.d.ts +6 -6
- package/stores/settings/useSettingsStore.d.ts +12 -9
- package/stores/settings/useSettingsStore.js +20 -4
- package/tsconfig.cjs.tsbuildinfo +1 -1
- package/types/events.d.ts +1 -1
- package/types/widget.d.ts +14 -8
- package/utils/enum.d.ts +1 -0
- package/utils/enum.js +1 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/navigationRoutes.d.ts +1 -1
- package/cjs/components/SwapInput/SwapInputAdornment.d.ts +0 -3
- package/cjs/providers/WalletProvider/WalletFormUpdate.d.ts +0 -5
- package/components/SwapInput/SwapInputAdornment.d.ts +0 -3
- package/providers/WalletProvider/WalletFormUpdate.d.ts +0 -5
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type * as languages from '../../i18n';
|
|
2
|
-
export
|
|
2
|
+
export type PartialResource<T> = T extends object ? {
|
|
3
3
|
[P in keyof T]?: PartialResource<T[P]>;
|
|
4
4
|
} : T;
|
|
5
|
-
export
|
|
6
|
-
export
|
|
5
|
+
export type LanguageKey = keyof typeof languages;
|
|
6
|
+
export type LanguageResources = {
|
|
7
7
|
[language in LanguageKey]?: PartialResource<typeof languages.en>;
|
|
8
8
|
} | {
|
|
9
9
|
[language: string]: PartialResource<typeof languages.en>;
|
|
10
10
|
};
|
|
11
|
-
export
|
|
11
|
+
export type LanguageTranslationResource = {
|
|
12
12
|
[namespace in 'translation']: PartialResource<typeof languages.en>;
|
|
13
13
|
};
|
|
14
|
-
export
|
|
14
|
+
export type LanguageTranslationResources = {
|
|
15
15
|
[language: string]: LanguageTranslationResource;
|
|
16
16
|
};
|
|
@@ -6,14 +6,15 @@ let lifi;
|
|
|
6
6
|
const SDKContext = createContext(null);
|
|
7
7
|
export const useLiFi = () => useContext(SDKContext);
|
|
8
8
|
export const SDKProvider = ({ children, }) => {
|
|
9
|
-
const { sdkConfig, integrator } = useWidgetConfig();
|
|
9
|
+
const { sdkConfig, fee, integrator, routePriority, slippage } = useWidgetConfig();
|
|
10
10
|
const value = useMemo(() => {
|
|
11
|
-
const config = Object.assign(Object.assign({}, sdkConfig), { defaultRouteOptions: Object.assign({ integrator: integrator !== null && integrator !== void 0 ? integrator : window.location.hostname
|
|
11
|
+
const config = Object.assign(Object.assign({}, sdkConfig), { defaultRouteOptions: Object.assign({ fee, integrator: integrator !== null && integrator !== void 0 ? integrator : window.location.hostname, routePriority,
|
|
12
|
+
slippage }, sdkConfig === null || sdkConfig === void 0 ? void 0 : sdkConfig.defaultRouteOptions) });
|
|
12
13
|
if (!lifi) {
|
|
13
14
|
lifi = new LIFI(Object.assign({ disableVersionCheck: true }, config));
|
|
14
15
|
}
|
|
15
16
|
lifi.setConfig(config);
|
|
16
17
|
return lifi;
|
|
17
|
-
}, [integrator, sdkConfig]);
|
|
18
|
+
}, [fee, integrator, routePriority, sdkConfig, slippage]);
|
|
18
19
|
return _jsx(SDKContext.Provider, Object.assign({ value: value }, { children: children }));
|
|
19
20
|
};
|
|
@@ -1,47 +1,35 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
import { useEffect } from 'react';
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
3
3
|
import { useFormContext } from 'react-hook-form';
|
|
4
|
-
import { useChainOrderStore } from '../../stores';
|
|
5
4
|
import { SwapFormKey } from '../SwapFormProvider';
|
|
5
|
+
import { useWallet } from '../WalletProvider';
|
|
6
6
|
import { isItemAllowed, useWidgetConfig } from '../WidgetProvider';
|
|
7
|
-
export const
|
|
7
|
+
export const FormUpdater = ({ defaultValues }) => {
|
|
8
8
|
const { fromChain, toChain, chains, disabledChains } = useWidgetConfig();
|
|
9
|
-
const {
|
|
9
|
+
const { account } = useWallet();
|
|
10
|
+
const { setValue, getValues, getFieldState, resetField,
|
|
10
11
|
// Subscription to touchedFields is required by getFieldState to work
|
|
11
12
|
formState: { touchedFields }, } = useFormContext();
|
|
13
|
+
const previousDefaultValues = useRef(defaultValues);
|
|
12
14
|
// Set wallet chain as default if no chains are provided by config and if they were not changed during widget usage
|
|
13
15
|
useEffect(() => {
|
|
14
16
|
const chainAllowed = account.chainId && isItemAllowed(account.chainId, chains, disabledChains);
|
|
15
17
|
if (!account.isActive || !account.chainId || !chainAllowed) {
|
|
16
18
|
return;
|
|
17
19
|
}
|
|
18
|
-
const [fromChainValue, toChainValue] = getValues([
|
|
19
|
-
SwapFormKey.FromChain,
|
|
20
|
-
SwapFormKey.ToChain,
|
|
21
|
-
]);
|
|
22
20
|
const { isTouched: isFromChainTouched } = getFieldState(SwapFormKey.FromChain);
|
|
23
21
|
const { isTouched: isToChainTouched } = getFieldState(SwapFormKey.ToChain);
|
|
24
22
|
const { isTouched: isFromTokenTouched } = getFieldState(SwapFormKey.FromToken);
|
|
25
23
|
const { isTouched: isToTokenTouched } = getFieldState(SwapFormKey.ToToken);
|
|
26
24
|
const { isTouched: isFromAmountTouched } = getFieldState(SwapFormKey.FromAmount);
|
|
27
|
-
|
|
28
|
-
// Users can switch chains in the wallet.
|
|
29
|
-
// If we don't have a chain in the ordered chain list we should add it.
|
|
30
|
-
setChain(account.chainId);
|
|
31
|
-
// If we ran out of slots for the ordered chain list and the current chain isn't there
|
|
32
|
-
// we should make a recently switched chain as current.
|
|
33
|
-
const hasFromChainInOrderedList = chainOrder.includes(fromChainValue);
|
|
34
|
-
const hasToChainInOrderedList = chainOrder.includes(toChainValue);
|
|
35
|
-
if ((!fromChain && !isFromChainTouched && !isFromTokenTouched) ||
|
|
36
|
-
!hasFromChainInOrderedList) {
|
|
25
|
+
if (!fromChain && !isFromChainTouched && !isFromTokenTouched) {
|
|
37
26
|
setValue(SwapFormKey.FromChain, account.chainId);
|
|
38
27
|
setValue(SwapFormKey.FromToken, '');
|
|
39
28
|
if (isFromAmountTouched) {
|
|
40
29
|
setValue(SwapFormKey.FromAmount, '');
|
|
41
30
|
}
|
|
42
31
|
}
|
|
43
|
-
if (
|
|
44
|
-
!hasToChainInOrderedList) {
|
|
32
|
+
if (!toChain && !isToChainTouched && !isToTokenTouched) {
|
|
45
33
|
setValue(SwapFormKey.ToChain, account.chainId);
|
|
46
34
|
setValue(SwapFormKey.ToToken, '');
|
|
47
35
|
}
|
|
@@ -56,5 +44,20 @@ export const WalletFormUpdate = ({ account, }) => {
|
|
|
56
44
|
setValue,
|
|
57
45
|
toChain,
|
|
58
46
|
]);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
Object.keys(defaultValues).forEach((key) => {
|
|
49
|
+
if (previousDefaultValues.current[key] !== defaultValues[key]) {
|
|
50
|
+
const value = defaultValues[key] ||
|
|
51
|
+
// set the chain to the current user one if it is not present in the config
|
|
52
|
+
(key === SwapFormKey.FromChain || key === SwapFormKey.ToChain
|
|
53
|
+
? account.chainId || ''
|
|
54
|
+
: '');
|
|
55
|
+
setValue(key, value);
|
|
56
|
+
resetField(key, { defaultValue: value });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
previousDefaultValues.current = defaultValues;
|
|
60
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
61
|
+
}, [defaultValues, getValues, resetField, setValue]);
|
|
59
62
|
return null;
|
|
60
63
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo } from 'react';
|
|
2
3
|
import { FormProvider, useForm } from 'react-hook-form';
|
|
3
4
|
import { useWidgetConfig } from '../WidgetProvider';
|
|
5
|
+
import { FormUpdater } from './FormUpdater';
|
|
4
6
|
import { SwapFormKey } from './types';
|
|
5
7
|
import { URLSearchParamsBuilder } from './URLSearchParamsBuilder';
|
|
6
8
|
export const formDefaultValues = {
|
|
@@ -10,13 +12,16 @@ export const formDefaultValues = {
|
|
|
10
12
|
};
|
|
11
13
|
export const SwapFormProvider = ({ children, }) => {
|
|
12
14
|
const { fromChain, fromToken, fromAmount, toChain, toToken, toAddress, buildSwapUrl, } = useWidgetConfig();
|
|
15
|
+
const defaultValues = useMemo(() => (Object.assign(Object.assign({}, formDefaultValues), { fromChain,
|
|
16
|
+
fromToken, fromAmount: (typeof fromAmount === 'number'
|
|
17
|
+
? fromAmount === null || fromAmount === void 0 ? void 0 : fromAmount.toPrecision()
|
|
18
|
+
: fromAmount) || formDefaultValues.fromAmount, toChain,
|
|
19
|
+
toToken,
|
|
20
|
+
toAddress })), [fromAmount, fromChain, fromToken, toAddress, toChain, toToken]);
|
|
13
21
|
const methods = useForm({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
: fromAmount) || formDefaultValues.fromAmount, toChain,
|
|
18
|
-
toToken,
|
|
19
|
-
toAddress }),
|
|
22
|
+
// TODO: revisit after RHF release values support
|
|
23
|
+
// values,
|
|
24
|
+
defaultValues,
|
|
20
25
|
});
|
|
21
|
-
return (_jsxs(FormProvider, Object.assign({}, methods, { children: [buildSwapUrl ? _jsx(URLSearchParamsBuilder, {}) : null, children] })));
|
|
26
|
+
return (_jsxs(FormProvider, Object.assign({}, methods, { children: [_jsx(FormUpdater, { defaultValues: defaultValues }), buildSwapUrl ? _jsx(URLSearchParamsBuilder, {}) : null, children] })));
|
|
22
27
|
};
|
|
@@ -7,7 +7,7 @@ export declare enum SwapFormKey {
|
|
|
7
7
|
ToToken = "toToken",
|
|
8
8
|
TokenSearchFilter = "tokenSearchFilter"
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export type SwapFormValues = {
|
|
11
11
|
[SwapFormKey.FromAmount]: string;
|
|
12
12
|
[SwapFormKey.FromChain]: number;
|
|
13
13
|
[SwapFormKey.FromToken]: string;
|
|
@@ -16,7 +16,7 @@ export declare type SwapFormValues = {
|
|
|
16
16
|
[SwapFormKey.ToToken]: string;
|
|
17
17
|
[SwapFormKey.TokenSearchFilter]: string;
|
|
18
18
|
};
|
|
19
|
-
export
|
|
19
|
+
export type SwapFormType = 'from' | 'to';
|
|
20
20
|
export declare const SwapFormKeyHelper: {
|
|
21
21
|
getChainKey: (formType: SwapFormType) => 'fromChain' | 'toChain';
|
|
22
22
|
getTokenKey: (formType: SwapFormType) => 'fromToken' | 'toToken';
|
|
@@ -7,11 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { jsx as _jsx
|
|
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
13
|
import { useWidgetConfig } from '../WidgetProvider';
|
|
14
|
-
import { WalletFormUpdate } from './WalletFormUpdate';
|
|
15
14
|
const stub = () => {
|
|
16
15
|
throw new Error('You forgot to wrap your component in <WalletProvider>.');
|
|
17
16
|
};
|
|
@@ -91,7 +90,7 @@ export const WalletProvider = ({ children }) => {
|
|
|
91
90
|
account,
|
|
92
91
|
provider,
|
|
93
92
|
}), [account, addChain, addToken, connect, disconnect, provider, switchChain]);
|
|
94
|
-
return (
|
|
93
|
+
return (_jsx(WalletContext.Provider, Object.assign({ value: value }, { children: children })));
|
|
95
94
|
};
|
|
96
95
|
export const extractAccountFromSigner = (signer) => __awaiter(void 0, void 0, void 0, function* () {
|
|
97
96
|
try {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Signer } from '@ethersproject/abstract-signer';
|
|
2
|
+
import type { Provider } from '@ethersproject/providers';
|
|
2
3
|
import type { Token } from '@lifi/sdk';
|
|
3
4
|
import type { Wallet } from '@lifi/wallet-management';
|
|
4
|
-
import type { Signer } from 'ethers';
|
|
5
5
|
export interface WalletContextProps {
|
|
6
6
|
account: WalletAccount;
|
|
7
|
-
provider?:
|
|
7
|
+
provider?: Provider;
|
|
8
8
|
addChain(chainId: number): Promise<boolean>;
|
|
9
9
|
addToken(chainId: number, token: Token): Promise<void>;
|
|
10
10
|
disconnect(): void;
|
|
@@ -11,7 +11,8 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
};
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
13
|
import { getChainByKey } from '@lifi/sdk';
|
|
14
|
-
import { createContext, useContext, useMemo } from 'react';
|
|
14
|
+
import { createContext, useContext, useEffect, useMemo } from 'react';
|
|
15
|
+
import { setDefaultSettings } from '../../stores';
|
|
15
16
|
import { formatAmount } from '../../utils';
|
|
16
17
|
const initialContext = {
|
|
17
18
|
disabledChains: [],
|
|
@@ -55,5 +56,8 @@ export const WidgetProvider = (_a) => {
|
|
|
55
56
|
return config;
|
|
56
57
|
}
|
|
57
58
|
}, [config, fromAmount, fromChain, fromToken, toChain, toToken]);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
setDefaultSettings(value);
|
|
61
|
+
}, [value]);
|
|
58
62
|
return (_jsx(WidgetContext.Provider, Object.assign({ value: value }, { children: children })));
|
|
59
63
|
};
|
package/stores/routes/types.d.ts
CHANGED
|
@@ -7,7 +7,14 @@ export interface RouteExecutionStore {
|
|
|
7
7
|
deleteRoute: (routeId: string) => void;
|
|
8
8
|
deleteRoutes: (type: 'completed' | 'active') => void;
|
|
9
9
|
}
|
|
10
|
-
export declare
|
|
10
|
+
export declare enum RouteExecutionStatus {
|
|
11
|
+
Idle = 0,
|
|
12
|
+
Pending = 1,
|
|
13
|
+
Done = 2,
|
|
14
|
+
Failed = 4,
|
|
15
|
+
Partial = 8,
|
|
16
|
+
Refunded = 16
|
|
17
|
+
}
|
|
11
18
|
export interface RouteExecution {
|
|
12
19
|
route: Route;
|
|
13
20
|
status: RouteExecutionStatus;
|
package/stores/routes/types.js
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
export
|
|
1
|
+
export var RouteExecutionStatus;
|
|
2
|
+
(function (RouteExecutionStatus) {
|
|
3
|
+
RouteExecutionStatus[RouteExecutionStatus["Idle"] = 0] = "Idle";
|
|
4
|
+
RouteExecutionStatus[RouteExecutionStatus["Pending"] = 1] = "Pending";
|
|
5
|
+
RouteExecutionStatus[RouteExecutionStatus["Done"] = 2] = "Done";
|
|
6
|
+
RouteExecutionStatus[RouteExecutionStatus["Failed"] = 4] = "Failed";
|
|
7
|
+
RouteExecutionStatus[RouteExecutionStatus["Partial"] = 8] = "Partial";
|
|
8
|
+
RouteExecutionStatus[RouteExecutionStatus["Refunded"] = 16] = "Refunded";
|
|
9
|
+
})(RouteExecutionStatus || (RouteExecutionStatus = {}));
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import shallow from 'zustand/shallow';
|
|
2
|
+
import { RouteExecutionStatus } from './types';
|
|
2
3
|
import { useRouteExecutionStore } from './useRouteExecutionStore';
|
|
3
4
|
export const useExecutingRoutesIds = (address) => {
|
|
4
5
|
return useRouteExecutionStore((state) => Object.values(state.routes)
|
|
5
6
|
.filter((item) => item.route.fromAddress === address &&
|
|
6
|
-
(item.status ===
|
|
7
|
+
(item.status === RouteExecutionStatus.Pending ||
|
|
8
|
+
item.status === RouteExecutionStatus.Failed))
|
|
7
9
|
.sort((a, b) => {
|
|
8
10
|
var _a, _b, _c, _d;
|
|
9
11
|
return ((_b = (_a = b === null || b === void 0 ? void 0 : b.route.steps[0].execution) === null || _a === void 0 ? void 0 : _a.process[0].startedAt) !== null && _b !== void 0 ? _b : 0) -
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import create from 'zustand';
|
|
2
2
|
import { persist } from 'zustand/middleware';
|
|
3
3
|
import { immer } from 'zustand/middleware/immer';
|
|
4
|
-
import {
|
|
4
|
+
import { hasEnumFlag } from '../../utils';
|
|
5
|
+
import { RouteExecutionStatus } from './types';
|
|
6
|
+
import { isRouteDone, isRouteFailed, isRoutePartiallyDone, isRouteRefunded, } from './utils';
|
|
5
7
|
export const useRouteExecutionStore = create()(persist(immer((set) => ({
|
|
6
8
|
routes: {},
|
|
7
9
|
setExecutableRoute: (route) => set((state) => {
|
|
8
10
|
if (!state.routes[route.id]) {
|
|
9
11
|
// clean previous idle routes that were not executed
|
|
10
12
|
Object.keys(state.routes)
|
|
11
|
-
.filter((routeId) => { var _a; return ((_a = state.routes[routeId]) === null || _a === void 0 ? void 0 : _a.status) ===
|
|
13
|
+
.filter((routeId) => { var _a; return ((_a = state.routes[routeId]) === null || _a === void 0 ? void 0 : _a.status) === RouteExecutionStatus.Idle; })
|
|
12
14
|
.forEach((routeId) => delete state.routes[routeId]);
|
|
13
15
|
state.routes[route.id] = {
|
|
14
16
|
route,
|
|
15
|
-
status:
|
|
17
|
+
status: RouteExecutionStatus.Idle,
|
|
16
18
|
};
|
|
17
19
|
}
|
|
18
20
|
}),
|
|
@@ -21,22 +23,28 @@ export const useRouteExecutionStore = create()(persist(immer((set) => ({
|
|
|
21
23
|
state.routes[route.id].route = route;
|
|
22
24
|
const isFailed = isRouteFailed(route);
|
|
23
25
|
if (isFailed) {
|
|
24
|
-
state.routes[route.id].status =
|
|
26
|
+
state.routes[route.id].status = RouteExecutionStatus.Failed;
|
|
25
27
|
return;
|
|
26
28
|
}
|
|
27
|
-
const isDone =
|
|
29
|
+
const isDone = isRouteDone(route);
|
|
28
30
|
if (isDone) {
|
|
29
|
-
state.routes[route.id].status =
|
|
31
|
+
state.routes[route.id].status = RouteExecutionStatus.Done;
|
|
32
|
+
if (isRoutePartiallyDone(route)) {
|
|
33
|
+
state.routes[route.id].status |= RouteExecutionStatus.Partial;
|
|
34
|
+
}
|
|
35
|
+
else if (isRouteRefunded(route)) {
|
|
36
|
+
state.routes[route.id].status |= RouteExecutionStatus.Refunded;
|
|
37
|
+
}
|
|
30
38
|
return;
|
|
31
39
|
}
|
|
32
40
|
const isLoading = route.steps.some((step) => step.execution);
|
|
33
41
|
if (isLoading) {
|
|
34
|
-
state.routes[route.id].status =
|
|
42
|
+
state.routes[route.id].status = RouteExecutionStatus.Pending;
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
45
|
}),
|
|
38
46
|
restartRoute: (routeId) => set((state) => {
|
|
39
|
-
state.routes[routeId].status =
|
|
47
|
+
state.routes[routeId].status = RouteExecutionStatus.Pending;
|
|
40
48
|
}),
|
|
41
49
|
deleteRoute: (routeId) => set((state) => {
|
|
42
50
|
if (state.routes[routeId]) {
|
|
@@ -46,15 +54,16 @@ export const useRouteExecutionStore = create()(persist(immer((set) => ({
|
|
|
46
54
|
deleteRoutes: (type) => set((state) => {
|
|
47
55
|
Object.keys(state.routes)
|
|
48
56
|
.filter((routeId) => {
|
|
49
|
-
var _a, _b;
|
|
57
|
+
var _a, _b, _c, _d;
|
|
50
58
|
return type === 'completed'
|
|
51
|
-
? ((_a = state.routes[routeId]) === null || _a === void 0 ? void 0 : _a.status)
|
|
52
|
-
: ((
|
|
59
|
+
? hasEnumFlag((_b = (_a = state.routes[routeId]) === null || _a === void 0 ? void 0 : _a.status) !== null && _b !== void 0 ? _b : 0, RouteExecutionStatus.Done)
|
|
60
|
+
: !hasEnumFlag((_d = (_c = state.routes[routeId]) === null || _c === void 0 ? void 0 : _c.status) !== null && _d !== void 0 ? _d : 0, RouteExecutionStatus.Done);
|
|
53
61
|
})
|
|
54
62
|
.forEach((routeId) => delete state.routes[routeId]);
|
|
55
63
|
}),
|
|
56
64
|
})), {
|
|
57
65
|
name: 'li.fi-widget-routes',
|
|
66
|
+
version: 1,
|
|
58
67
|
partialize: (state) => ({ routes: state.routes }),
|
|
59
68
|
merge: (persistedState, currentState) => {
|
|
60
69
|
const state = Object.assign(Object.assign({}, currentState), persistedState);
|
|
@@ -68,21 +77,21 @@ export const useRouteExecutionStore = create()(persist(immer((set) => ({
|
|
|
68
77
|
}
|
|
69
78
|
state.routes[route.id] = {
|
|
70
79
|
route,
|
|
71
|
-
status:
|
|
80
|
+
status: RouteExecutionStatus.Idle,
|
|
72
81
|
};
|
|
73
82
|
const isFailed = isRouteFailed(route);
|
|
74
83
|
if (isFailed) {
|
|
75
|
-
state.routes[route.id].status =
|
|
84
|
+
state.routes[route.id].status = RouteExecutionStatus.Failed;
|
|
76
85
|
return;
|
|
77
86
|
}
|
|
78
|
-
const isDone =
|
|
87
|
+
const isDone = isRouteDone(route);
|
|
79
88
|
if (isDone) {
|
|
80
|
-
state.routes[route.id].status =
|
|
89
|
+
state.routes[route.id].status = RouteExecutionStatus.Done;
|
|
81
90
|
return;
|
|
82
91
|
}
|
|
83
92
|
const isLoading = route.steps.some((step) => step.execution);
|
|
84
93
|
if (isLoading) {
|
|
85
|
-
state.routes[route.id].status =
|
|
94
|
+
state.routes[route.id].status = RouteExecutionStatus.Pending;
|
|
86
95
|
}
|
|
87
96
|
});
|
|
88
97
|
}
|
|
@@ -93,4 +102,30 @@ export const useRouteExecutionStore = create()(persist(immer((set) => ({
|
|
|
93
102
|
}
|
|
94
103
|
return state;
|
|
95
104
|
},
|
|
105
|
+
migrate: (persistedState, version) => {
|
|
106
|
+
if (version === 0) {
|
|
107
|
+
Object.values(persistedState.routes).forEach((route) => {
|
|
108
|
+
if (route) {
|
|
109
|
+
switch (route.status) {
|
|
110
|
+
case 'idle':
|
|
111
|
+
route.status = RouteExecutionStatus.Idle;
|
|
112
|
+
break;
|
|
113
|
+
case 'loading':
|
|
114
|
+
route.status = RouteExecutionStatus.Pending;
|
|
115
|
+
break;
|
|
116
|
+
case 'success':
|
|
117
|
+
case 'warning':
|
|
118
|
+
route.status = RouteExecutionStatus.Done;
|
|
119
|
+
break;
|
|
120
|
+
case 'error':
|
|
121
|
+
route.status = RouteExecutionStatus.Failed;
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return persistedState;
|
|
130
|
+
},
|
|
96
131
|
}));
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import shallow from 'zustand/shallow';
|
|
2
|
+
import { hasEnumFlag } from '../../utils';
|
|
3
|
+
import { RouteExecutionStatus } from './types';
|
|
2
4
|
import { useRouteExecutionStore } from './useRouteExecutionStore';
|
|
3
5
|
export const useSwapHistory = (address) => {
|
|
4
6
|
return useRouteExecutionStore((state) => Object.values(state.routes)
|
|
5
|
-
.filter((item) => (item === null || item === void 0 ? void 0 : item.route.fromAddress) === address &&
|
|
7
|
+
.filter((item) => (item === null || item === void 0 ? void 0 : item.route.fromAddress) === address &&
|
|
8
|
+
hasEnumFlag(item.status, RouteExecutionStatus.Done))
|
|
6
9
|
.sort((a, b) => {
|
|
7
10
|
var _a, _b, _c, _d;
|
|
8
11
|
return ((_b = (_a = b === null || b === void 0 ? void 0 : b.route.steps[0].execution) === null || _a === void 0 ? void 0 : _a.process[0].startedAt) !== null && _b !== void 0 ? _b : 0) -
|
package/stores/routes/utils.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Process, Route } from '@lifi/sdk';
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const isRouteDone: (route: Route) => boolean;
|
|
3
|
+
export declare const isRoutePartiallyDone: (route: Route) => boolean;
|
|
4
|
+
export declare const isRouteRefunded: (route: Route) => boolean;
|
|
3
5
|
export declare const isRouteFailed: (route: Route) => boolean;
|
|
4
6
|
export declare const isRouteActive: (route?: Route) => boolean;
|
|
5
7
|
export declare const getUpdatedProcess: (currentRoute: Route, updatedRoute: Route) => Process | undefined;
|
package/stores/routes/utils.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import microdiff from 'microdiff';
|
|
2
|
-
export const
|
|
2
|
+
export const isRouteDone = (route) => {
|
|
3
3
|
return route.steps.every((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'DONE'; });
|
|
4
4
|
};
|
|
5
|
+
export const isRoutePartiallyDone = (route) => {
|
|
6
|
+
return route.steps.some((step) => { var _a; return (_a = step.execution) === null || _a === void 0 ? void 0 : _a.process.some((process) => process.substatus === 'PARTIAL'); });
|
|
7
|
+
};
|
|
8
|
+
export const isRouteRefunded = (route) => {
|
|
9
|
+
return route.steps.some((step) => { var _a; return (_a = step.execution) === null || _a === void 0 ? void 0 : _a.process.some((process) => process.substatus === 'REFUNDED'); });
|
|
10
|
+
};
|
|
5
11
|
export const isRouteFailed = (route) => {
|
|
6
12
|
return route.steps.some((step) => { var _a; return ((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'FAILED'; });
|
|
7
13
|
};
|
|
@@ -9,7 +15,7 @@ export const isRouteActive = (route) => {
|
|
|
9
15
|
if (!route) {
|
|
10
16
|
return false;
|
|
11
17
|
}
|
|
12
|
-
const isDone =
|
|
18
|
+
const isDone = isRouteDone(route);
|
|
13
19
|
const isFailed = isRouteFailed(route);
|
|
14
20
|
const alreadyStarted = route.steps.some((step) => step.execution);
|
|
15
21
|
return !isDone && !isFailed && alreadyStarted;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import type { Bridge, Exchange, Order } from '@lifi/sdk';
|
|
2
2
|
import type { Appearance } from '../../types';
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
3
|
+
export type ValueSetter<S> = <K extends keyof S>(key: K, value: S[Extract<K, string>]) => void;
|
|
4
|
+
export type ValuesSetter<S> = <K extends keyof S>(values: Record<K, S[Extract<K, string>]>) => void;
|
|
5
|
+
export type SettingsToolType = 'Bridges' | 'Exchanges';
|
|
6
6
|
export declare const SettingsToolTypes: SettingsToolType[];
|
|
7
7
|
export interface SettingsState {
|
|
8
8
|
advancedPreferences: boolean;
|
|
9
9
|
appearance: Appearance;
|
|
10
|
-
gasPrice
|
|
10
|
+
gasPrice?: string;
|
|
11
11
|
language?: string;
|
|
12
|
-
routePriority
|
|
12
|
+
routePriority?: Order;
|
|
13
13
|
showDestinationWallet: boolean;
|
|
14
|
-
slippage
|
|
14
|
+
slippage?: string;
|
|
15
15
|
enabledBridges?: string[];
|
|
16
16
|
_enabledBridges?: Record<string, boolean>;
|
|
17
17
|
enabledExchanges?: string[];
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import type { WidgetConfig } from '../../types';
|
|
1
2
|
import type { SettingsState, SettingsStore } from './types';
|
|
2
|
-
export declare const
|
|
3
|
+
export declare const defaultConfigurableSettings: Pick<SettingsState, 'routePriority' | 'slippage'>;
|
|
4
|
+
export declare const defaultSettings: SettingsState;
|
|
3
5
|
export declare const useSettingsStore: import("zustand").UseBoundStore<Omit<Omit<import("zustand").StoreApi<SettingsStore>, "persist"> & {
|
|
4
6
|
persist: {
|
|
5
7
|
setOptions: (options: Partial<import("zustand/middleware").PersistOptions<SettingsStore, {
|
|
@@ -8,12 +10,12 @@ export declare const useSettingsStore: import("zustand").UseBoundStore<Omit<Omit
|
|
|
8
10
|
initializeTools(toolType: import("./types").SettingsToolType, tools: string[]): void;
|
|
9
11
|
setTools(toolType: import("./types").SettingsToolType, tools: string[], availableTools: (Pick<import("@lifi/types").Bridge, "key"> | Pick<import("@lifi/types").Exchange, "key">)[]): void;
|
|
10
12
|
advancedPreferences: boolean;
|
|
11
|
-
appearance: import("
|
|
12
|
-
gasPrice
|
|
13
|
+
appearance: import("../../types").Appearance;
|
|
14
|
+
gasPrice?: string | undefined;
|
|
13
15
|
language?: string | undefined;
|
|
14
|
-
routePriority
|
|
16
|
+
routePriority?: "RECOMMENDED" | "FASTEST" | "CHEAPEST" | "SAFEST" | undefined;
|
|
15
17
|
showDestinationWallet: boolean;
|
|
16
|
-
slippage
|
|
18
|
+
slippage?: string | undefined;
|
|
17
19
|
_enabledBridges?: Record<string, boolean> | undefined;
|
|
18
20
|
_enabledExchanges?: Record<string, boolean> | undefined;
|
|
19
21
|
}>>) => void;
|
|
@@ -28,12 +30,12 @@ export declare const useSettingsStore: import("zustand").UseBoundStore<Omit<Omit
|
|
|
28
30
|
initializeTools(toolType: import("./types").SettingsToolType, tools: string[]): void;
|
|
29
31
|
setTools(toolType: import("./types").SettingsToolType, tools: string[], availableTools: (Pick<import("@lifi/types").Bridge, "key"> | Pick<import("@lifi/types").Exchange, "key">)[]): void;
|
|
30
32
|
advancedPreferences: boolean;
|
|
31
|
-
appearance: import("
|
|
32
|
-
gasPrice
|
|
33
|
+
appearance: import("../../types").Appearance;
|
|
34
|
+
gasPrice?: string | undefined;
|
|
33
35
|
language?: string | undefined;
|
|
34
|
-
routePriority
|
|
36
|
+
routePriority?: "RECOMMENDED" | "FASTEST" | "CHEAPEST" | "SAFEST" | undefined;
|
|
35
37
|
showDestinationWallet: boolean;
|
|
36
|
-
slippage
|
|
38
|
+
slippage?: string | undefined;
|
|
37
39
|
_enabledBridges?: Record<string, boolean> | undefined;
|
|
38
40
|
_enabledExchanges?: Record<string, boolean> | undefined;
|
|
39
41
|
}>>;
|
|
@@ -41,3 +43,4 @@ export declare const useSettingsStore: import("zustand").UseBoundStore<Omit<Omit
|
|
|
41
43
|
}, "setState"> & {
|
|
42
44
|
setState(nextStateOrUpdater: SettingsStore | Partial<SettingsStore> | ((state: import("immer/dist/internal").WritableDraft<SettingsStore>) => void), shouldReplace?: boolean | undefined): void;
|
|
43
45
|
}>;
|
|
46
|
+
export declare const setDefaultSettings: (config?: WidgetConfig) => void;
|
|
@@ -14,11 +14,13 @@ import create from 'zustand';
|
|
|
14
14
|
import { persist } from 'zustand/middleware';
|
|
15
15
|
import { immer } from 'zustand/middleware/immer';
|
|
16
16
|
import { SettingsToolTypes } from './types';
|
|
17
|
+
export const defaultConfigurableSettings = {
|
|
18
|
+
routePriority: 'RECOMMENDED',
|
|
19
|
+
slippage: '0.5',
|
|
20
|
+
};
|
|
17
21
|
export const defaultSettings = {
|
|
18
22
|
appearance: 'auto',
|
|
19
23
|
gasPrice: 'normal',
|
|
20
|
-
routePriority: 'RECOMMENDED',
|
|
21
|
-
slippage: '0.5',
|
|
22
24
|
advancedPreferences: false,
|
|
23
25
|
showDestinationWallet: true,
|
|
24
26
|
};
|
|
@@ -78,11 +80,25 @@ export const useSettingsStore = create()(persist(immer((set) => (Object.assign(O
|
|
|
78
80
|
},
|
|
79
81
|
migrate: (persistedState, version) => {
|
|
80
82
|
if (version === 0 && persistedState.appearance === 'system') {
|
|
81
|
-
persistedState.appearance =
|
|
83
|
+
persistedState.appearance = defaultSettings.appearance;
|
|
82
84
|
}
|
|
83
85
|
if (version === 1) {
|
|
84
|
-
persistedState.slippage =
|
|
86
|
+
persistedState.slippage = defaultConfigurableSettings.slippage;
|
|
85
87
|
}
|
|
86
88
|
return persistedState;
|
|
87
89
|
},
|
|
88
90
|
}));
|
|
91
|
+
export const setDefaultSettings = (config) => {
|
|
92
|
+
var _a, _b, _c, _d, _e;
|
|
93
|
+
const { slippage, routePriority, setValue } = useSettingsStore.getState();
|
|
94
|
+
const defaultSlippage = ((config === null || config === void 0 ? void 0 : config.slippage) ||
|
|
95
|
+
((_b = (_a = config === null || config === void 0 ? void 0 : config.sdkConfig) === null || _a === void 0 ? void 0 : _a.defaultRouteOptions) === null || _b === void 0 ? void 0 : _b.slippage) ||
|
|
96
|
+
0) * 100;
|
|
97
|
+
const defaultRoutePriority = (config === null || config === void 0 ? void 0 : config.routePriority) || ((_d = (_c = config === null || config === void 0 ? void 0 : config.sdkConfig) === null || _c === void 0 ? void 0 : _c.defaultRouteOptions) === null || _d === void 0 ? void 0 : _d.order);
|
|
98
|
+
if (!slippage) {
|
|
99
|
+
setValue('slippage', (_e = (defaultSlippage || defaultConfigurableSettings.slippage)) === null || _e === void 0 ? void 0 : _e.toString());
|
|
100
|
+
}
|
|
101
|
+
if (!routePriority) {
|
|
102
|
+
setValue('routePriority', defaultRoutePriority || defaultConfigurableSettings.routePriority);
|
|
103
|
+
}
|
|
104
|
+
};
|