Skip to content

Commit

Permalink
feat: add sufficient balance check
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Apr 1, 2022
1 parent 6dd287e commit f96b472
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 29 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^14.0.0",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^14.0.4",
"@types/big.js": "^6.1.3",
"@types/node": "^17.0.23",
"@types/react": "^17.0.43",
Expand Down
2 changes: 1 addition & 1 deletion packages/widget-embedded/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
]
},
"devDependencies": {
"@testing-library/user-event": "^14.0.0",
"@testing-library/user-event": "^14.0.4",
"@types/react": "^17.0.43",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/widget-embedded/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const root = createRoot(rootElement);

const config: WidgetConfig = {
enabledChains: process.env.LIFI_ENABLED_CHAINS_JSON!,
fromChain: 'pol',
fromChain: 'eth',
// toChain: 'okt',
useInternalWalletManagement: true,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"react-i18next": "^11.16.2",
"react-query": "^3.34.19",
"react-resize-detector": "^7.0.0",
"react-router-dom": "^6.2.2",
"react-router-dom": "^6.3.0",
"react-virtual": "^2.10.4"
},
"eslintConfig": {
Expand Down
6 changes: 6 additions & 0 deletions packages/widget/src/components/SwapButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { ChainId, getChainById } from '..';
import { useHasSufficientBalance } from '../hooks/useHasSufficientBalance';
import { useSwapRoutes } from '../hooks/useSwapRoutes';
import { SwapFormKeyHelper } from '../providers/SwapFormProvider';
import { useWalletInterface } from '../services/walletInterface';
Expand All @@ -23,6 +24,11 @@ export const SwapButton: React.FC<{
const navigate = useNavigate();
const { t } = useTranslation();
const { routes: swapRoutes } = useSwapRoutes();
const {
hasGasBalanceOnStartChain,
hasGasOnCrossChain,
hasSufficientBalance,
} = useHasSufficientBalance(swapRoutes?.[0]);
const { account, switchChain } = useWalletInterface();
const [chainId] = useWatch({
name: [SwapFormKeyHelper.getChainKey('from')],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const SwapInputAdornment: React.FC<SwapFormTypeProps> = ({
const handleMax = () => {
setValue(SwapFormKeyHelper.getAmountKey(formType), amount);
};
console.log(tokenWithBalance);

return (
<InputAdornment position="end">
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/components/SwapRoute/SwapRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const parsedStepTypes: {
export const SwapRoute: React.FC = () => {
const { t } = useTranslation();
const { routes, isFetching, isFetched } = useSwapRoutes();
console.log(routes);

const renderRoutesLoading = () => {
return (
<Box
Expand Down
78 changes: 78 additions & 0 deletions packages/widget/src/hooks/useHasSufficientBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { isSwapStep, Route } from '@lifinance/sdk';
import Big from 'big.js';
import { useMemo } from 'react';
import { useWatch } from 'react-hook-form';
import { SwapFormKeyHelper } from '../providers/SwapFormProvider';
import { useTokens } from './useTokens';

export const useHasSufficientBalance = (route?: Route) => {
const [fromChainId, toChainId] = useWatch({
name: [
SwapFormKeyHelper.getChainKey('from'),
SwapFormKeyHelper.getChainKey('to'),
],
});
const lastStep = route?.steps.at(-1);
const { tokensWithBalance: fromChainTokenBalances } = useTokens(fromChainId);
const { tokensWithBalance: toChainTokenBalances } = useTokens(
lastStep?.action.fromChainId ?? toChainId,
);

const hasGasBalanceOnStartChain = useMemo(() => {
const token = route?.steps[0].estimate.gasCosts?.[0].token;
if (!token) {
return true;
}
const balance = Big(
fromChainTokenBalances?.[route.fromChainId].find(
(t) => t.address === token.address,
)?.amount ?? 0,
);
const requiredAmount = route.steps
.filter((step) => step.action.fromChainId === route.fromChainId)
.map((step) => step.estimate.gasCosts?.[0].amount)
.map((amount) => Big(amount || 0))
.reduce((a, b) => a.plus(b), Big(0))
.div(10 ** token.decimals);
return balance.gt(0) && balance.gte(requiredAmount);
}, [fromChainTokenBalances, route]);

const hasGasOnCrossChain = useMemo(() => {
const token = lastStep?.estimate.gasCosts?.[0].token;
if (!token || !isSwapStep(lastStep)) {
return true;
}
const balance = Big(
toChainTokenBalances?.[lastStep.action.fromChainId].find(
(t) => t.address === token.address,
)?.amount ?? 0,
);
const gasEstimate = lastStep.estimate.gasCosts?.[0].amount;
const requiredAmount = Big(gasEstimate || 0).div(
10 ** (lastStep.estimate.gasCosts?.[0].token.decimals ?? 0),
);
return balance.gt(0) && balance.gte(requiredAmount);
}, [lastStep, toChainTokenBalances]);

const hasSufficientBalance = useMemo(() => {
if (!route) {
return true;
}

const balance = Big(
fromChainTokenBalances?.[route.fromChainId].find(
(t) => t.address === route.fromToken.address,
)?.amount ?? 0,
);

return Big(route.fromAmount)
.div(10 ** (route.fromToken.decimals ?? 0))
.lte(balance);
}, [fromChainTokenBalances, route]);

return {
hasGasBalanceOnStartChain,
hasGasOnCrossChain,
hasSufficientBalance,
};
};
1 change: 0 additions & 1 deletion packages/widget/src/hooks/useSwapRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SwapFormKey, SwapFormKeyHelper } from '../providers/SwapFormProvider';
import { useWalletInterface } from '../services/walletInterface';
import { formatTokenAmount } from '../utils/format';
import { useDebouncedWatch } from './useDebouncedWatch';
// import { usePriorityAccount } from './connectorHooks';
import { useToken } from './useToken';

export const useSwapRoutes = () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/widget/src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const useTokenBalance = (chainId: number, tokenAddress: string) => {
const { token } = useToken(chainId, tokenAddress);

const { data: tokenWithBalance, isLoading } = useQuery(
['token', token?.symbol, account.address],
async ({ queryKey: [_, tokenSymbol, address] }) => {
['token', tokenAddress, account.address],
async ({ queryKey: [_, __, address] }) => {
if (!address || !token) {
return null;
}
Expand Down
38 changes: 19 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3077,7 +3077,7 @@
"@svgr/plugin-jsx" "^6.2.1"
"@svgr/plugin-svgo" "^6.2.0"

"@testing-library/dom@^8.0.0":
"@testing-library/dom@^8.5.0":
version "8.12.0"
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.12.0.tgz#fef5e545533fb084175dda6509ee71d7d2f72e23"
integrity sha512-rBrJk5WjI02X1edtiUcZhgyhgBhiut96r5Jp8J5qktKdcvLcZpKDW8i2hkGMMItxrghjXuQ5AM6aE0imnFawaw==
Expand Down Expand Up @@ -3106,19 +3106,19 @@
lodash "^4.17.15"
redent "^3.0.0"

"@testing-library/react@^12.1.4":
version "12.1.4"
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-12.1.4.tgz#09674b117e550af713db3f4ec4c0942aa8bbf2c0"
integrity sha512-jiPKOm7vyUw311Hn/HlNQ9P8/lHNtArAx0PisXyFixDDvfl8DbD6EUdbshK5eqauvBSvzZd19itqQ9j3nferJA==
"@testing-library/react@^13.0.0":
version "13.0.0"
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-13.0.0.tgz#8cdaf4667c6c2b082eb0513731551e9db784e8bc"
integrity sha512-p0lYA1M7uoEmk2LnCbZLGmHJHyH59sAaZVXChTXlyhV/PRW9LoIh4mdf7tiXsO8BoNG+vN8UnFJff1hbZeXv+w==
dependencies:
"@babel/runtime" "^7.12.5"
"@testing-library/dom" "^8.0.0"
"@testing-library/dom" "^8.5.0"
"@types/react-dom" "*"

"@testing-library/user-event@^14.0.0":
version "14.0.0"
resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-14.0.0.tgz#3906aa6f0e56fd012d73559f5f05c02e63ba18dd"
integrity sha512-hZhjNrle/DMi1ziHwHy8LS0fYJtf+cID7fuG5+4h+Bk83xQaRDQT/DlqfL4hJYw3mxW6KTIxoODrhGnhqJebdQ==
"@testing-library/user-event@^14.0.4":
version "14.0.4"
resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-14.0.4.tgz#5b430a9c27f25078bff4471661b755115d0db9d4"
integrity sha512-VBZe5lcUsmrQyOwIFvqOxLBoaTw1/Qy4Ek+VgmFYs719bs2SxUp42vbsb7ATlQDkHdj4OIQlucfpwxe5WoG1jA==

"@tootallnate/once@1":
version "1.1.2"
Expand Down Expand Up @@ -10963,18 +10963,18 @@ react-resize-detector@^7.0.0:
"@types/resize-observer-browser" "^0.1.6"
lodash "^4.17.21"

react-router-dom@^6.2.2:
version "6.2.2"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-6.2.2.tgz#f1a2c88365593c76b9612ae80154a13fcb72e442"
integrity sha512-AtYEsAST7bDD4dLSQHDnk/qxWLJdad5t1HFa1qJyUrCeGgEuCSw0VB/27ARbF9Fi/W5598ujvJOm3ujUCVzuYQ==
react-router-dom@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-6.3.0.tgz#a0216da813454e521905b5fa55e0e5176123f43d"
integrity sha512-uaJj7LKytRxZNQV8+RbzJWnJ8K2nPsOOEuX7aQstlMZKQT0164C+X2w6bnkqU3sjtLvpd5ojrezAyfZ1+0sStw==
dependencies:
history "^5.2.0"
react-router "6.2.2"
react-router "6.3.0"

react-router@6.2.2:
version "6.2.2"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-6.2.2.tgz#495e683a0c04461eeb3d705fe445d6cf42f0c249"
integrity sha512-/MbxyLzd7Q7amp4gDOGaYvXwhEojkJD5BtExkuKmj39VEE0m3l/zipf6h2WIB2jyAO0lI6NGETh4RDcktRm4AQ==
react-router@6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/react-router/-/react-router-6.3.0.tgz#3970cc64b4cb4eae0c1ea5203a80334fdd175557"
integrity sha512-7Wh1DzVQ+tlFjkeo+ujvjSqSJmkt1+8JO+T5xklPlgrh70y7ogx75ODRW0ThWhY7S+6yEDks8TYrtQe/aoboBQ==
dependencies:
history "^5.2.0"

Expand Down

0 comments on commit f96b472

Please sign in to comment.