@lifi/widget 1.18.2 → 1.18.4
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/AppRoutes.js +7 -3
- package/cjs/AppRoutes.js +7 -3
- package/cjs/components/AppContainer.js +0 -1
- package/cjs/components/BottomSheet/BottomSheet.js +2 -0
- package/cjs/components/ChainSelect/ChainSelect.js +1 -1
- package/cjs/components/Header/WalletHeader.js +1 -3
- package/cjs/components/SendToWallet/SendToWallet.js +1 -1
- package/cjs/components/SendToWallet/SendToWalletButton.js +9 -1
- package/cjs/components/SwapInput/FormPriceHelperText.js +1 -1
- package/cjs/components/SwapInput/SwapInputAdornment.js +1 -1
- package/cjs/config/version.d.ts +1 -1
- package/cjs/config/version.js +1 -1
- package/cjs/hooks/index.d.ts +1 -1
- package/cjs/hooks/index.js +1 -1
- package/cjs/hooks/useGasSufficiency.js +10 -11
- package/cjs/hooks/useScrollableContainer.d.ts +0 -1
- package/cjs/hooks/useScrollableContainer.js +1 -15
- package/cjs/hooks/useTokenAddressBalance.d.ts +5 -0
- package/cjs/hooks/useTokenAddressBalance.js +19 -0
- package/cjs/hooks/useTokenBalance.d.ts +7 -3
- package/cjs/hooks/useTokenBalance.js +87 -9
- package/cjs/hooks/useTokenBalances.d.ts +1 -1
- package/cjs/hooks/useTokenBalances.js +1 -4
- package/cjs/i18n/en/translation.json +1 -1
- package/cjs/pages/SelectTokenPage/SelectTokenPage.js +1 -11
- package/cjs/pages/SwapPage/StatusBottomSheet.js +19 -6
- package/cjs/utils/index.d.ts +1 -0
- package/cjs/utils/index.js +1 -0
- package/cjs/utils/wallet.d.ts +1 -0
- package/cjs/utils/wallet.js +7 -0
- package/components/AppContainer.js +0 -1
- package/components/BottomSheet/BottomSheet.js +2 -0
- package/components/ChainSelect/ChainSelect.js +2 -2
- package/components/Header/WalletHeader.js +2 -4
- package/components/SendToWallet/SendToWallet.js +1 -1
- package/components/SendToWallet/SendToWalletButton.js +10 -2
- package/components/SwapInput/FormPriceHelperText.js +2 -2
- package/components/SwapInput/SwapInputAdornment.js +2 -2
- package/config/version.d.ts +1 -1
- package/config/version.js +1 -1
- package/hooks/index.d.ts +1 -1
- package/hooks/index.js +1 -1
- package/hooks/useGasSufficiency.js +11 -12
- package/hooks/useScrollableContainer.d.ts +0 -1
- package/hooks/useScrollableContainer.js +0 -13
- package/hooks/useTokenAddressBalance.d.ts +5 -0
- package/hooks/useTokenAddressBalance.js +15 -0
- package/hooks/useTokenBalance.d.ts +7 -3
- package/hooks/useTokenBalance.js +88 -10
- package/hooks/useTokenBalances.d.ts +1 -1
- package/hooks/useTokenBalances.js +1 -4
- package/i18n/en/translation.json +1 -1
- package/package.json +5 -5
- package/pages/SelectTokenPage/SelectTokenPage.js +2 -12
- package/pages/SwapPage/StatusBottomSheet.js +19 -6
- package/tsconfig.cjs.tsbuildinfo +1 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/wallet.d.ts +1 -0
- package/utils/wallet.js +3 -0
- package/cjs/components/SwapButton/ButtonTooltip.d.ts +0 -5
- package/cjs/components/SwapButton/ButtonTooltip.js +0 -7
- package/cjs/hooks/useContentHeight.d.ts +0 -1
- package/cjs/hooks/useContentHeight.js +0 -27
- package/components/SwapButton/ButtonTooltip.d.ts +0 -5
- package/components/SwapButton/ButtonTooltip.js +0 -3
- package/hooks/useContentHeight.d.ts +0 -1
- package/hooks/useContentHeight.js +0 -23
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { Avatar, Box, Skeleton, Typography } from '@mui/material';
|
|
2
|
+
import { Avatar, Box, Skeleton, Tooltip, Typography } from '@mui/material';
|
|
3
3
|
import { useWatch } from 'react-hook-form';
|
|
4
4
|
import { useNavigate } from 'react-router-dom';
|
|
5
5
|
import { SwapFormKeyHelper } from '../../providers';
|
|
@@ -22,7 +22,7 @@ export const ChainSelect = ({ formType }) => {
|
|
|
22
22
|
const chainsToHide = ((_a = chains === null || chains === void 0 ? void 0 : chains.length) !== null && _a !== void 0 ? _a : 0) - maxChainToOrder;
|
|
23
23
|
return (_jsxs(ChainContainer, { children: [isLoading
|
|
24
24
|
? Array.from({ length: maxChainToOrder + 1 }).map((_, index) => (_jsx(Skeleton, { variant: "rectangular", width: 56, height: 56, sx: { borderRadius: 1 } }, index)))
|
|
25
|
-
: getChains().map((chain) => (_jsx(ChainCard, Object.assign({ onClick: () => setCurrentChain(chain.id), variant: chainId === chain.id ? 'selected' : 'default' }, { children: _jsx(Avatar, Object.assign({ src: chain.logoURI, alt: chain.key, sx: { width: 40, height: 40 } }, { children: chain.name[0] })) }), chain.id))), chainsToHide > 0 ? (_jsx(ChainCard, Object.assign({ onClick: showAllChains }, { children: _jsx(Box, Object.assign({ sx: {
|
|
25
|
+
: getChains().map((chain) => (_jsx(Tooltip, Object.assign({ title: chain.name, placement: "top", enterDelay: 500, enterNextDelay: 500, arrow: true }, { children: _jsx(ChainCard, Object.assign({ onClick: () => setCurrentChain(chain.id), variant: chainId === chain.id ? 'selected' : 'default' }, { children: _jsx(Avatar, Object.assign({ src: chain.logoURI, alt: chain.key, sx: { width: 40, height: 40 } }, { children: chain.name[0] })) })) }), chain.id))), chainsToHide > 0 ? (_jsx(ChainCard, Object.assign({ onClick: showAllChains }, { children: _jsx(Box, Object.assign({ sx: {
|
|
26
26
|
width: 40,
|
|
27
27
|
height: 40,
|
|
28
28
|
display: 'grid',
|
|
@@ -13,14 +13,12 @@ import { Box, IconButton, Typography } from '@mui/material';
|
|
|
13
13
|
import { useTranslation } from 'react-i18next';
|
|
14
14
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
15
15
|
import { useWallet, useWidgetConfig } from '../../providers';
|
|
16
|
-
import { navigationRoutes } from '../../utils';
|
|
16
|
+
import { navigationRoutes, shortenWalletAddress } from '../../utils';
|
|
17
17
|
import { HeaderAppBar } from './Header.style';
|
|
18
18
|
export const WalletHeader = () => {
|
|
19
19
|
const { t } = useTranslation();
|
|
20
20
|
const { account, disconnect } = useWallet();
|
|
21
|
-
const walletAddress = account.address
|
|
22
|
-
? `${account.address.substring(0, 7)}...${account.address.substring(account.address.length - 7)}`
|
|
23
|
-
: null;
|
|
21
|
+
const walletAddress = shortenWalletAddress(account.address);
|
|
24
22
|
return (_jsx(HeaderAppBar, Object.assign({ elevation: 0 }, { children: walletAddress ? (_jsxs(_Fragment, { children: [_jsxs(Box, Object.assign({ sx: {
|
|
25
23
|
display: 'flex',
|
|
26
24
|
flex: 1,
|
|
@@ -42,5 +42,5 @@ export const SendToWallet = forwardRef((props, ref) => {
|
|
|
42
42
|
}),
|
|
43
43
|
onBlur: () => trigger(SwapFormKey.ToAddress),
|
|
44
44
|
});
|
|
45
|
-
return (_jsx(Collapse, Object.assign({ timeout: 225, in: showSendToWallet }, { children: _jsxs(Card, Object.assign({}, props, { ref: ref }, { children: [_jsx(CardTitle, { children: t('swap.sendToWallet') }), _jsxs(FormControl, Object.assign({ fullWidth: true, sx: { paddingTop: '6px', paddingBottom: '5px' } }, { children: [_jsx(Input, { ref: inputRef, size: "small", autoComplete: "off", autoCorrect: "off", autoCapitalize: "off", spellCheck: "false", onChange: onChange, onBlur: onBlur, name: name, placeholder: t('swap.walletAddressOrEns') }), _jsx(FormHelperText, Object.assign({ error: !!errors.toAddress }, { children: (_a = errors.toAddress) === null || _a === void 0 ? void 0 : _a.message }))] }))] })) })));
|
|
45
|
+
return (_jsx(Collapse, Object.assign({ timeout: 225, unmountOnExit: true, mountOnEnter: true, in: showSendToWallet }, { children: _jsxs(Card, Object.assign({}, props, { ref: ref }, { children: [_jsx(CardTitle, { children: t('swap.sendToWallet') }), _jsxs(FormControl, Object.assign({ fullWidth: true, sx: { paddingTop: '6px', paddingBottom: '5px' } }, { children: [_jsx(Input, { ref: inputRef, size: "small", autoComplete: "off", autoCorrect: "off", autoCapitalize: "off", spellCheck: "false", onChange: onChange, onBlur: onBlur, name: name, placeholder: t('swap.walletAddressOrEns'), autoFocus: true }), _jsx(FormHelperText, Object.assign({ error: !!errors.toAddress }, { children: (_a = errors.toAddress) === null || _a === void 0 ? void 0 : _a.message }))] }))] })) })));
|
|
46
46
|
});
|
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { WalletOutlined as WalletOutlinedIcon } from '@mui/icons-material';
|
|
3
3
|
import { Button, Tooltip } from '@mui/material';
|
|
4
|
+
import { useFormContext } from 'react-hook-form';
|
|
4
5
|
import { useTranslation } from 'react-i18next';
|
|
5
|
-
import { useWallet } from '../../providers';
|
|
6
|
+
import { SwapFormKey, useWallet } from '../../providers';
|
|
6
7
|
import { useSettings } from '../../stores';
|
|
7
8
|
import { useSendToWalletStore } from './store';
|
|
8
9
|
export const SendToWalletButton = () => {
|
|
9
10
|
const { t } = useTranslation();
|
|
10
11
|
const { account } = useWallet();
|
|
12
|
+
const { setValue } = useFormContext();
|
|
11
13
|
const { showDestinationWallet } = useSettings(['showDestinationWallet']);
|
|
12
14
|
const { showSendToWallet, toggleSendToWallet } = useSendToWalletStore();
|
|
13
15
|
if (!showDestinationWallet || !account.isActive) {
|
|
14
16
|
return null;
|
|
15
17
|
}
|
|
16
|
-
|
|
18
|
+
const handleClick = () => {
|
|
19
|
+
if (showSendToWallet) {
|
|
20
|
+
setValue(SwapFormKey.ToAddress, '');
|
|
21
|
+
}
|
|
22
|
+
toggleSendToWallet();
|
|
23
|
+
};
|
|
24
|
+
return (_jsx(Tooltip, Object.assign({ title: t('swap.sendToWallet'), placement: "bottom-end", enterDelay: 500, enterNextDelay: 500, arrow: true }, { children: _jsx(Button, Object.assign({ variant: showSendToWallet ? 'contained' : 'text', onClick: handleClick, sx: {
|
|
17
25
|
minWidth: 48,
|
|
18
26
|
marginLeft: 1,
|
|
19
27
|
} }, { children: _jsx(WalletOutlinedIcon, {}) })) })));
|
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { FormHelperText, Skeleton, Typography } from '@mui/material';
|
|
3
3
|
import { useWatch } from 'react-hook-form';
|
|
4
4
|
import { useTranslation } from 'react-i18next';
|
|
5
|
-
import {
|
|
5
|
+
import { useTokenAddressBalance } from '../../hooks';
|
|
6
6
|
import { SwapFormKeyHelper } from '../../providers';
|
|
7
7
|
import { formatTokenPrice } from '../../utils';
|
|
8
8
|
export const FormPriceHelperText = ({ formType, selected }) => {
|
|
@@ -14,7 +14,7 @@ export const FormPriceHelperText = ({ formType, selected }) => {
|
|
|
14
14
|
SwapFormKeyHelper.getTokenKey(formType),
|
|
15
15
|
],
|
|
16
16
|
});
|
|
17
|
-
const { token, isLoading } =
|
|
17
|
+
const { token, isLoading } = useTokenAddressBalance(chainId, tokenAddress);
|
|
18
18
|
const fromAmountTokenPrice = formatTokenPrice(amount, token === null || token === void 0 ? void 0 : token.priceUSD);
|
|
19
19
|
return (_jsxs(FormHelperText, Object.assign({ component: "div", sx: { display: 'flex', justifyContent: 'space-between', margin: 0 } }, { children: [_jsx(Typography, Object.assign({ color: fromAmountTokenPrice ? 'text.secondary' : 'grey.600', fontWeight: 400, fontSize: 12, marginLeft: selected ? 8 : 2, lineHeight: 1.3334, flex: 1, sx: {
|
|
20
20
|
wordBreak: 'break-word',
|
|
@@ -2,7 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { InputAdornment, Skeleton } from '@mui/material';
|
|
3
3
|
import { useFormContext, useWatch } from 'react-hook-form';
|
|
4
4
|
import { useTranslation } from 'react-i18next';
|
|
5
|
-
import {
|
|
5
|
+
import { useTokenAddressBalance } from '../../hooks';
|
|
6
6
|
import { SwapFormKeyHelper } from '../../providers';
|
|
7
7
|
import { Button } from './SwapInputAdornment.style';
|
|
8
8
|
export const SwapInputAdornment = ({ formType }) => {
|
|
@@ -14,7 +14,7 @@ export const SwapInputAdornment = ({ formType }) => {
|
|
|
14
14
|
SwapFormKeyHelper.getTokenKey(formType),
|
|
15
15
|
],
|
|
16
16
|
});
|
|
17
|
-
const { token, isLoading } =
|
|
17
|
+
const { token, isLoading } = useTokenAddressBalance(chainId, tokenAddress);
|
|
18
18
|
const handleMax = () => {
|
|
19
19
|
var _a;
|
|
20
20
|
setValue(SwapFormKeyHelper.getAmountKey(formType), (_a = token === null || token === void 0 ? void 0 : token.amount) !== null && _a !== void 0 ? _a : '');
|
package/config/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export declare const name = "@lifi/widget";
|
|
2
|
-
export declare const version = "1.18.
|
|
2
|
+
export declare const version = "1.18.4";
|
package/config/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export const name = '@lifi/widget';
|
|
2
|
-
export const version = '1.18.
|
|
2
|
+
export const version = '1.18.4';
|
package/hooks/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export * from './useChain';
|
|
2
2
|
export * from './useChains';
|
|
3
|
-
export * from './useContentHeight';
|
|
4
3
|
export * from './useDebouncedWatch';
|
|
5
4
|
export * from './useFeaturedTokens';
|
|
6
5
|
export * from './useGasSufficiency';
|
|
@@ -12,6 +11,7 @@ export * from './useScrollableContainer';
|
|
|
12
11
|
export * from './useSwapRoutes';
|
|
13
12
|
export * from './useTelemetry';
|
|
14
13
|
export * from './useToken';
|
|
14
|
+
export * from './useTokenAddressBalance';
|
|
15
15
|
export * from './useTokenBalance';
|
|
16
16
|
export * from './useTokenBalances';
|
|
17
17
|
export * from './useTokens';
|
package/hooks/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export * from './useChain';
|
|
2
2
|
export * from './useChains';
|
|
3
|
-
export * from './useContentHeight';
|
|
4
3
|
export * from './useDebouncedWatch';
|
|
5
4
|
export * from './useFeaturedTokens';
|
|
6
5
|
export * from './useGasSufficiency';
|
|
@@ -12,6 +11,7 @@ export * from './useScrollableContainer';
|
|
|
12
11
|
export * from './useSwapRoutes';
|
|
13
12
|
export * from './useTelemetry';
|
|
14
13
|
export * from './useToken';
|
|
14
|
+
export * from './useTokenAddressBalance';
|
|
15
15
|
export * from './useTokenBalance';
|
|
16
16
|
export * from './useTokenBalances';
|
|
17
17
|
export * from './useTokens';
|
|
@@ -10,17 +10,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import Big from 'big.js';
|
|
11
11
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
12
12
|
import { useChains } from '.';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
13
|
+
import { useWallet } from '../providers';
|
|
14
|
+
import { useTokenBalance } from './useTokenBalance';
|
|
15
15
|
export const useGasSufficiency = (route) => {
|
|
16
|
-
const lifi = useLiFi();
|
|
17
16
|
const { account } = useWallet();
|
|
18
17
|
const { getChainById } = useChains();
|
|
19
|
-
const {
|
|
18
|
+
const { token: fromToken, getTokenBalancesWithRetry } = useTokenBalance(route === null || route === void 0 ? void 0 : route.fromToken);
|
|
20
19
|
const [insufficientGas, setInsufficientGas] = useState();
|
|
21
20
|
const checkInsufficientGas = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
21
|
var _a, _b;
|
|
23
|
-
if (!account.
|
|
22
|
+
if (!account.address || !route) {
|
|
24
23
|
setInsufficientGas(undefined);
|
|
25
24
|
return;
|
|
26
25
|
}
|
|
@@ -47,7 +46,7 @@ export const useGasSufficiency = (route) => {
|
|
|
47
46
|
if (route.fromToken.address === ((_a = gasCosts[route.fromChainId]) === null || _a === void 0 ? void 0 : _a.token.address)) {
|
|
48
47
|
gasCosts[route.fromChainId].tokenAmount = (_b = gasCosts[route.fromChainId]) === null || _b === void 0 ? void 0 : _b.gasAmount.plus(Big(route.fromAmount).div(Math.pow(10, route.fromToken.decimals)));
|
|
49
48
|
}
|
|
50
|
-
const tokenBalances = yield
|
|
49
|
+
const tokenBalances = yield getTokenBalancesWithRetry(account.address, Object.values(gasCosts).map((item) => item.token));
|
|
51
50
|
if (!(tokenBalances === null || tokenBalances === void 0 ? void 0 : tokenBalances.length)) {
|
|
52
51
|
setInsufficientGas(undefined);
|
|
53
52
|
return;
|
|
@@ -63,22 +62,22 @@ export const useGasSufficiency = (route) => {
|
|
|
63
62
|
const insufficientFromChainGasAmount = insufficientFromChainGas
|
|
64
63
|
? (_f = (_e = gasCosts[chainId].tokenAmount) === null || _e === void 0 ? void 0 : _e.minus(gasTokenBalance)) !== null && _f !== void 0 ? _f : gasCosts[chainId].gasAmount.minus(gasTokenBalance)
|
|
65
64
|
: undefined;
|
|
66
|
-
gasCosts[chainId] = Object.assign(Object.assign({}, gasCosts[chainId]), { insufficient: insufficientFromChainGas, insufficientAmount: insufficientFromChainGasAmount });
|
|
65
|
+
gasCosts[chainId] = Object.assign(Object.assign({}, gasCosts[chainId]), { insufficient: insufficientFromChainGas, insufficientAmount: insufficientFromChainGasAmount === null || insufficientFromChainGasAmount === void 0 ? void 0 : insufficientFromChainGasAmount.round(5, Big.roundUp) });
|
|
67
66
|
}
|
|
68
67
|
});
|
|
69
68
|
const gasCostResult = Object.values(gasCosts).filter((gasCost) => gasCost.insufficient);
|
|
70
69
|
setInsufficientGas(gasCostResult);
|
|
71
|
-
}), [account.address,
|
|
70
|
+
}), [account.address, getChainById, getTokenBalancesWithRetry, route]);
|
|
72
71
|
const insufficientFunds = useMemo(() => {
|
|
73
|
-
var _a
|
|
74
|
-
if (!account.isActive || !
|
|
72
|
+
var _a;
|
|
73
|
+
if (!account.isActive || !fromToken || !route) {
|
|
75
74
|
return false;
|
|
76
75
|
}
|
|
77
|
-
const balance = Big((
|
|
76
|
+
const balance = Big((_a = fromToken === null || fromToken === void 0 ? void 0 : fromToken.amount) !== null && _a !== void 0 ? _a : 0);
|
|
78
77
|
return Big(route.fromAmount)
|
|
79
78
|
.div(Math.pow(10, route.fromToken.decimals))
|
|
80
79
|
.gt(balance);
|
|
81
|
-
}, [account.isActive,
|
|
80
|
+
}, [account.isActive, fromToken, route]);
|
|
82
81
|
useEffect(() => {
|
|
83
82
|
checkInsufficientGas();
|
|
84
83
|
}, [checkInsufficientGas]);
|
|
@@ -9,16 +9,3 @@ export const useScrollableContainer = () => {
|
|
|
9
9
|
}, [containerElement]);
|
|
10
10
|
return containerElement;
|
|
11
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,5 @@
|
|
|
1
|
+
export declare const useTokenAddressBalance: (chainId?: number, tokenAddress?: string) => {
|
|
2
|
+
token: import("..").Token | undefined;
|
|
3
|
+
isLoading: boolean;
|
|
4
|
+
refetch: <TPageData>(options?: (import("@tanstack/query-core").RefetchOptions & import("@tanstack/query-core").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/query-core").QueryObserverResult<import("..").Token[], unknown>>;
|
|
5
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useTokenBalances } from './useTokenBalances';
|
|
3
|
+
export const useTokenAddressBalance = (chainId, tokenAddress) => {
|
|
4
|
+
const { tokens, tokensWithBalance, isBalanceLoading, refetch } = useTokenBalances(chainId);
|
|
5
|
+
const token = useMemo(() => {
|
|
6
|
+
var _a;
|
|
7
|
+
const token = (_a = (tokensWithBalance !== null && tokensWithBalance !== void 0 ? tokensWithBalance : tokens)) === null || _a === void 0 ? void 0 : _a.find((token) => token.address === tokenAddress && token.chainId === chainId);
|
|
8
|
+
return token;
|
|
9
|
+
}, [chainId, tokenAddress, tokens, tokensWithBalance]);
|
|
10
|
+
return {
|
|
11
|
+
token,
|
|
12
|
+
isLoading: isBalanceLoading,
|
|
13
|
+
refetch,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type { Token, TokenAmount } from '@lifi/sdk';
|
|
2
|
+
export declare const useTokenBalance: (token?: Token, accountAddress?: string) => {
|
|
3
|
+
token: TokenAmount | undefined;
|
|
3
4
|
isLoading: boolean;
|
|
4
|
-
refetch: <TPageData>(options?: (import("@tanstack/query
|
|
5
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<TokenAmount, unknown>>;
|
|
6
|
+
refetchNewBalance: () => void;
|
|
7
|
+
refetchAllBalances: () => void;
|
|
8
|
+
getTokenBalancesWithRetry: (accountAddress: string, tokens: Token[], depth?: number) => Promise<TokenAmount[] | undefined>;
|
|
5
9
|
};
|
package/hooks/useTokenBalance.js
CHANGED
|
@@ -1,15 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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, useQueryClient } from '@tanstack/react-query';
|
|
11
|
+
import { useCallback, useMemo } from 'react';
|
|
12
|
+
import { useLiFi, useWallet } from '../providers';
|
|
13
|
+
import { formatTokenAmount } from '../utils';
|
|
14
|
+
const defaultRefetchInterval = 60000;
|
|
15
|
+
export const useTokenBalance = (token, accountAddress) => {
|
|
16
|
+
const lifi = useLiFi();
|
|
17
|
+
const { account } = useWallet();
|
|
18
|
+
const queryClient = useQueryClient();
|
|
19
|
+
const walletAddress = accountAddress !== null && accountAddress !== void 0 ? accountAddress : account.address;
|
|
20
|
+
const getTokenBalancesWithRetry = useCallback((accountAddress, tokens, depth = 0) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
const tokenBalances = yield lifi.getTokenBalances(accountAddress, tokens);
|
|
22
|
+
if (!tokenBalances.every((token) => token.blockNumber)) {
|
|
23
|
+
if (depth > 5) {
|
|
24
|
+
console.warn('Token balance backoff depth exceeded.');
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
yield new Promise((resolve) => {
|
|
28
|
+
setTimeout(resolve, depth * 100);
|
|
29
|
+
});
|
|
30
|
+
return getTokenBalancesWithRetry(accountAddress, tokens, depth + 1);
|
|
31
|
+
}
|
|
32
|
+
return tokenBalances;
|
|
33
|
+
}), [lifi]);
|
|
34
|
+
const tokenBalanceQueryKey = useMemo(() => ['token-balance', walletAddress, token === null || token === void 0 ? void 0 : token.chainId, token === null || token === void 0 ? void 0 : token.address], [token === null || token === void 0 ? void 0 : token.address, token === null || token === void 0 ? void 0 : token.chainId, walletAddress]);
|
|
35
|
+
const { data, isLoading, refetch } = useQuery(tokenBalanceQueryKey, ({ queryKey: [, accountAddress] }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
6
36
|
var _a;
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
37
|
+
const cachedToken = (_a = queryClient
|
|
38
|
+
.getQueryData([
|
|
39
|
+
'token-balances',
|
|
40
|
+
accountAddress,
|
|
41
|
+
token.chainId,
|
|
42
|
+
])) === null || _a === void 0 ? void 0 : _a.find((t) => t.address === token.address);
|
|
43
|
+
if (cachedToken) {
|
|
44
|
+
return cachedToken;
|
|
45
|
+
}
|
|
46
|
+
const tokenBalances = yield getTokenBalancesWithRetry(accountAddress, [token]);
|
|
47
|
+
if (!(tokenBalances === null || tokenBalances === void 0 ? void 0 : tokenBalances.length)) {
|
|
48
|
+
throw Error('Could not get tokens balance.');
|
|
49
|
+
}
|
|
50
|
+
const cachedTokenAmount = queryClient.getQueryData(tokenBalanceQueryKey);
|
|
51
|
+
const formattedAmount = formatTokenAmount(tokenBalances[0].amount);
|
|
52
|
+
if ((cachedTokenAmount === null || cachedTokenAmount === void 0 ? void 0 : cachedTokenAmount.amount) !== formattedAmount) {
|
|
53
|
+
queryClient.setQueryDefaults(tokenBalanceQueryKey, {
|
|
54
|
+
refetchInterval: defaultRefetchInterval,
|
|
55
|
+
staleTime: defaultRefetchInterval,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
queryClient.setQueryData(['token-balances', accountAddress, token.chainId], (data) => {
|
|
59
|
+
if (data) {
|
|
60
|
+
const index = data.findIndex((dataToken) => dataToken.address === token.address);
|
|
61
|
+
data[index] = Object.assign(Object.assign({}, data[index]), { amount: formattedAmount });
|
|
62
|
+
}
|
|
63
|
+
return data;
|
|
64
|
+
});
|
|
65
|
+
return Object.assign(Object.assign({}, tokenBalances[0]), { amount: formattedAmount });
|
|
66
|
+
}), {
|
|
67
|
+
enabled: Boolean(walletAddress && token),
|
|
68
|
+
refetchIntervalInBackground: true,
|
|
69
|
+
refetchInterval: defaultRefetchInterval,
|
|
70
|
+
staleTime: defaultRefetchInterval,
|
|
71
|
+
});
|
|
72
|
+
const refetchAllBalances = () => {
|
|
73
|
+
queryClient.refetchQueries([
|
|
74
|
+
'token-balances',
|
|
75
|
+
token === null || token === void 0 ? void 0 : token.chainId,
|
|
76
|
+
accountAddress,
|
|
77
|
+
]);
|
|
78
|
+
};
|
|
79
|
+
const refetchNewBalance = useCallback(() => {
|
|
80
|
+
queryClient.setQueryDefaults(tokenBalanceQueryKey, {
|
|
81
|
+
refetchInterval: 500,
|
|
82
|
+
staleTime: 500,
|
|
83
|
+
});
|
|
84
|
+
}, [queryClient, tokenBalanceQueryKey]);
|
|
10
85
|
return {
|
|
11
|
-
token,
|
|
12
|
-
isLoading
|
|
86
|
+
token: data,
|
|
87
|
+
isLoading,
|
|
13
88
|
refetch,
|
|
89
|
+
refetchNewBalance,
|
|
90
|
+
refetchAllBalances,
|
|
91
|
+
getTokenBalancesWithRetry,
|
|
14
92
|
};
|
|
15
93
|
};
|
|
@@ -5,5 +5,5 @@ export declare const useTokenBalances: (selectedChainId?: number) => {
|
|
|
5
5
|
featuredTokens: import("@lifi/types").Token[];
|
|
6
6
|
isLoading: boolean;
|
|
7
7
|
isBalanceLoading: boolean;
|
|
8
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<Token[]
|
|
8
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<Token[], unknown>>;
|
|
9
9
|
};
|
|
@@ -26,9 +26,6 @@ export const useTokenBalances = (selectedChainId) => {
|
|
|
26
26
|
Boolean(tokens === null || tokens === void 0 ? void 0 : tokens.length) &&
|
|
27
27
|
Boolean(selectedChainId);
|
|
28
28
|
const { data: tokensWithBalance, isLoading: isBalanceLoading, refetch, } = useQuery(['token-balances', account.address, selectedChainId, tokens === null || tokens === void 0 ? void 0 : tokens.length], ({ queryKey: [, accountAddress] }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
-
if (!accountAddress || !tokens) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
29
|
const tokenBalances = yield lifi.getTokenBalances(accountAddress, tokens);
|
|
33
30
|
if (!(tokenBalances === null || tokenBalances === void 0 ? void 0 : tokenBalances.length)) {
|
|
34
31
|
// Sometimes RPCs (e.g. Arbitrum) don't return balances on first call
|
|
@@ -36,7 +33,7 @@ export const useTokenBalances = (selectedChainId) => {
|
|
|
36
33
|
setRefetchInterval((interval) => interval === defaultRefetchInterval
|
|
37
34
|
? minRefetchInterval
|
|
38
35
|
: interval * 2);
|
|
39
|
-
|
|
36
|
+
throw Error('Could not get tokens balance.');
|
|
40
37
|
}
|
|
41
38
|
const featuredTokenAddresses = new Set(featuredTokens === null || featuredTokens === void 0 ? void 0 : featuredTokens.map((token) => token.address));
|
|
42
39
|
const sortFn = (a, b) => {
|
package/i18n/en/translation.json
CHANGED
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"fundsReceived": "Funds received"
|
|
79
79
|
},
|
|
80
80
|
"message": {
|
|
81
|
-
"fundsReceived": "
|
|
81
|
+
"fundsReceived": "There are now {{amount}} {{tokenSymbol}} in wallet {{walletAddress}} on {{chainName}} chain."
|
|
82
82
|
}
|
|
83
83
|
},
|
|
84
84
|
"info": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lifi/widget",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.4",
|
|
4
4
|
"description": "LI.FI Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.",
|
|
5
5
|
"main": "./cjs/index.js",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
"@lifi/sdk": "^1.6.0",
|
|
46
46
|
"@lifi/wallet-management": "^1.1.10",
|
|
47
47
|
"@mui/icons-material": "^5.10.6",
|
|
48
|
-
"@mui/lab": "^5.0.0-alpha.
|
|
49
|
-
"@mui/material": "^5.10.
|
|
48
|
+
"@mui/lab": "^5.0.0-alpha.101",
|
|
49
|
+
"@mui/material": "^5.10.7",
|
|
50
50
|
"@sentry/integrations": "^7.13.0",
|
|
51
51
|
"@sentry/react": "^7.13.0",
|
|
52
52
|
"@sentry/tracing": "^7.13.0",
|
|
53
|
-
"@tanstack/react-query": "^4.
|
|
53
|
+
"@tanstack/react-query": "^4.6.1",
|
|
54
54
|
"@tanstack/react-virtual": "^3.0.0-beta.18",
|
|
55
55
|
"big.js": "^6.2.1",
|
|
56
56
|
"i18next": "^21.9.2",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"mitt": "^3.0.0",
|
|
60
60
|
"react": "^18.2.0",
|
|
61
61
|
"react-dom": "^18.2.0",
|
|
62
|
-
"react-hook-form": "^7.36.
|
|
62
|
+
"react-hook-form": "^7.36.1",
|
|
63
63
|
"react-i18next": "^11.18.6",
|
|
64
64
|
"react-router-dom": "^6.4.1",
|
|
65
65
|
"react-timer-hook": "^3.0.5",
|
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Box, Container } from '@mui/material';
|
|
3
|
-
import { useLayoutEffect, useRef, useState } from 'react';
|
|
4
3
|
import { ChainSelect } from '../../components/ChainSelect';
|
|
5
4
|
import { TokenList } from '../../components/TokenList';
|
|
6
|
-
import {
|
|
5
|
+
import { useNavigateBack } from '../../hooks';
|
|
7
6
|
import { SearchTokenInput } from './SearchTokenInput';
|
|
8
|
-
const minTokenListHeight = 330;
|
|
9
7
|
export const SelectTokenPage = ({ formType }) => {
|
|
10
|
-
useScrollableOverflowHidden();
|
|
11
8
|
const { navigateBack } = useNavigateBack();
|
|
12
|
-
|
|
13
|
-
const contentHeight = useContentHeight();
|
|
14
|
-
const [tokenListHeight, setTokenListHeight] = useState(0);
|
|
15
|
-
useLayoutEffect(() => {
|
|
16
|
-
var _a, _b;
|
|
17
|
-
setTokenListHeight(Math.max(contentHeight - ((_b = (_a = headerRef.current) === null || _a === void 0 ? void 0 : _a.offsetHeight) !== null && _b !== void 0 ? _b : 0), minTokenListHeight));
|
|
18
|
-
}, [contentHeight]);
|
|
19
|
-
return (_jsxs(Container, Object.assign({ disableGutters: true }, { children: [_jsxs(Box, Object.assign({ pt: 1, pb: 2, px: 3, ref: headerRef }, { children: [_jsx(ChainSelect, { formType: formType }), _jsx(Box, Object.assign({ mt: 2 }, { children: _jsx(SearchTokenInput, {}) }))] })), _jsx(TokenList, { height: tokenListHeight, onClick: navigateBack, formType: formType })] })));
|
|
9
|
+
return (_jsxs(Container, Object.assign({ disableGutters: true }, { children: [_jsxs(Box, Object.assign({ pt: 1, pb: 2, px: 3 }, { children: [_jsx(ChainSelect, { formType: formType }), _jsx(Box, Object.assign({ mt: 2 }, { children: _jsx(SearchTokenInput, {}) }))] })), _jsx(TokenList, { height: 360, onClick: navigateBack, formType: formType })] })));
|
|
20
10
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/* eslint-disable consistent-return */
|
|
2
3
|
import { Done as DoneIcon, Info as InfoIcon, Warning as WarningIcon, } from '@mui/icons-material';
|
|
3
4
|
import { Box, Button, Typography } from '@mui/material';
|
|
4
5
|
import { useEffect, useRef } from 'react';
|
|
@@ -8,17 +9,18 @@ import { BottomSheet } from '../../components/BottomSheet';
|
|
|
8
9
|
import { Token } from '../../components/Token';
|
|
9
10
|
import { getProcessMessage, useChains, useNavigateBack, useTokenBalance, } from '../../hooks';
|
|
10
11
|
import { SwapFormKey } from '../../providers';
|
|
12
|
+
import { navigationRoutes, shortenWalletAddress } from '../../utils';
|
|
11
13
|
import { IconCircle, IconContainer, iconStyles, } from './StatusBottomSheet.style';
|
|
12
14
|
export const StatusBottomSheet = ({ status, route, }) => {
|
|
13
15
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
14
16
|
const { t } = useTranslation();
|
|
15
|
-
const { navigateBack } = useNavigateBack();
|
|
17
|
+
const { navigateBack, navigate } = useNavigateBack();
|
|
16
18
|
const ref = useRef(null);
|
|
17
19
|
const { getChainById } = useChains();
|
|
18
|
-
const { token, refetch
|
|
20
|
+
const { token, refetch, refetchNewBalance, refetchAllBalances } = useTokenBalance(route.toToken, route.toAddress);
|
|
19
21
|
const { setValue } = useFormContext();
|
|
20
22
|
const clearFromAmount = () => {
|
|
21
|
-
|
|
23
|
+
refetchAllBalances();
|
|
22
24
|
setValue(SwapFormKey.FromAmount, '');
|
|
23
25
|
};
|
|
24
26
|
const handleDone = () => {
|
|
@@ -30,6 +32,13 @@ export const StatusBottomSheet = ({ status, route, }) => {
|
|
|
30
32
|
clearFromAmount();
|
|
31
33
|
(_a = ref.current) === null || _a === void 0 ? void 0 : _a.closeDrawer();
|
|
32
34
|
};
|
|
35
|
+
const handleSeeDetails = () => {
|
|
36
|
+
handleClose();
|
|
37
|
+
navigate(navigationRoutes.swapDetails, {
|
|
38
|
+
state: { routeId: route.id },
|
|
39
|
+
replace: true,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
33
42
|
let title;
|
|
34
43
|
let message;
|
|
35
44
|
switch (status) {
|
|
@@ -39,6 +48,7 @@ export const StatusBottomSheet = ({ status, route, }) => {
|
|
|
39
48
|
amount: token === null || token === void 0 ? void 0 : token.amount,
|
|
40
49
|
tokenSymbol: token === null || token === void 0 ? void 0 : token.symbol,
|
|
41
50
|
chainName: (_a = getChainById(route.toChainId)) === null || _a === void 0 ? void 0 : _a.name,
|
|
51
|
+
walletAddress: shortenWalletAddress(route.toAddress),
|
|
42
52
|
});
|
|
43
53
|
break;
|
|
44
54
|
case 'error': {
|
|
@@ -59,9 +69,12 @@ export const StatusBottomSheet = ({ status, route, }) => {
|
|
|
59
69
|
var _a, _b;
|
|
60
70
|
if ((status === 'success' || status === 'error') &&
|
|
61
71
|
!((_a = ref.current) === null || _a === void 0 ? void 0 : _a.isOpen())) {
|
|
62
|
-
|
|
72
|
+
if (status === 'success') {
|
|
73
|
+
refetchNewBalance();
|
|
74
|
+
refetch();
|
|
75
|
+
}
|
|
63
76
|
(_b = ref.current) === null || _b === void 0 ? void 0 : _b.openDrawer();
|
|
64
77
|
}
|
|
65
|
-
}, [
|
|
66
|
-
return (_jsx(BottomSheet, Object.assign({ ref: ref }, { children: _jsxs(Box, Object.assign({ p: 3 }, { children: [_jsxs(IconContainer, { children: [_jsxs(IconCircle, Object.assign({ status: status, mb: 1 }, { children: [status === 'idle' ? (_jsx(InfoIcon, { color: "primary", sx: iconStyles })) : null, status === 'success' ? (_jsx(DoneIcon, { color: "success", sx: iconStyles })) : null, status === 'error' ? (_jsx(WarningIcon, { color: "error", sx: iconStyles })) : null] })), _jsx(Typography, Object.assign({ py: 1, fontSize: 18, fontWeight: 700 }, { children: title })), status === 'success' ? (_jsx(Token, { token: Object.assign(Object.assign({}, route.toToken), { amount: (_g = (_e = (_d = (_c = route.steps.at(-1)) === null || _c === void 0 ? void 0 : _c.execution) === null || _d === void 0 ? void 0 : _d.toAmount) !== null && _e !== void 0 ? _e : (_f = route.steps.at(-1)) === null || _f === void 0 ? void 0 : _f.estimate.toAmount) !== null && _g !== void 0 ? _g : route.toAmount }), py: 1 })) : null] }), _jsx(Typography, Object.assign({ pt: 2, pb: 1 }, { children: message })), _jsx(Box, Object.assign({ mt: 2 }, { children: _jsxs(Button, Object.assign({ variant: "contained", fullWidth: true, onClick: status === 'success' ? handleDone : handleClose }, { children: [status === 'idle' ? t('button.okay') : null, status === 'success' ? t('button.done') : null, status === 'error' ? t('button.tryAgain') : null] })) })), status === 'success' ? (_jsx(Box, Object.assign({ mt: 2 }, { children: _jsx(Button, Object.assign({ variant: "outlined", fullWidth: true, onClick:
|
|
78
|
+
}, [refetch, refetchNewBalance, status]);
|
|
79
|
+
return (_jsx(BottomSheet, Object.assign({ ref: ref }, { children: _jsxs(Box, Object.assign({ p: 3 }, { children: [_jsxs(IconContainer, { children: [_jsxs(IconCircle, Object.assign({ status: status, mb: 1 }, { children: [status === 'idle' ? (_jsx(InfoIcon, { color: "primary", sx: iconStyles })) : null, status === 'success' ? (_jsx(DoneIcon, { color: "success", sx: iconStyles })) : null, status === 'error' ? (_jsx(WarningIcon, { color: "error", sx: iconStyles })) : null] })), _jsx(Typography, Object.assign({ py: 1, fontSize: 18, fontWeight: 700 }, { children: title })), status === 'success' ? (_jsx(Token, { token: Object.assign(Object.assign({}, route.toToken), { amount: (_g = (_e = (_d = (_c = route.steps.at(-1)) === null || _c === void 0 ? void 0 : _c.execution) === null || _d === void 0 ? void 0 : _d.toAmount) !== null && _e !== void 0 ? _e : (_f = route.steps.at(-1)) === null || _f === void 0 ? void 0 : _f.estimate.toAmount) !== null && _g !== void 0 ? _g : route.toAmount }), py: 1 })) : null] }), _jsx(Typography, Object.assign({ pt: 2, pb: 1 }, { children: message })), _jsx(Box, Object.assign({ mt: 2 }, { children: _jsxs(Button, Object.assign({ variant: "contained", fullWidth: true, onClick: status === 'success' ? handleDone : handleClose }, { children: [status === 'idle' ? t('button.okay') : null, status === 'success' ? t('button.done') : null, status === 'error' ? t('button.tryAgain') : null] })) })), status === 'success' ? (_jsx(Box, Object.assign({ mt: 2 }, { children: _jsx(Button, Object.assign({ variant: "outlined", fullWidth: true, onClick: handleSeeDetails }, { children: t('button.seeDetails') })) }))) : null] })) })));
|
|
67
80
|
};
|