-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve gas sufficiency handling
- Loading branch information
Showing
12 changed files
with
211 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/widget/src/components/GasSufficiencyMessage/GasSufficiencyMessage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Warning as WarningIcon } from '@mui/icons-material'; | ||
import { Box, BoxProps, Typography } from '@mui/material'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useGasSufficiency } from '../../hooks'; | ||
import { CardTitle } from '../Card'; | ||
import { MessageCard } from './GasSufficiencyMessage.style'; | ||
|
||
export const GasSufficiencyMessage: React.FC<BoxProps> = (props) => { | ||
const { t } = useTranslation(); | ||
const { insufficientFunds, insufficientGas } = useGasSufficiency(); | ||
|
||
if (!insufficientFunds || !insufficientGas.length) { | ||
return null; | ||
} | ||
|
||
let title; | ||
let message; | ||
if (insufficientFunds) { | ||
message = t(`swap.warning.message.insufficientFunds`); | ||
} | ||
if (insufficientGas.length) { | ||
title = t(`swap.warning.title.insufficientGas`); | ||
message = t(`swap.warning.message.insufficientGas`); | ||
} | ||
return ( | ||
<MessageCard {...props}> | ||
<WarningIcon | ||
sx={{ | ||
marginTop: 2, | ||
marginLeft: 2, | ||
}} | ||
/> | ||
<Box> | ||
{title ? <CardTitle>{title}</CardTitle> : null} | ||
<Typography | ||
variant="body2" | ||
px={2} | ||
pb={insufficientGas.length ? 0 : 2} | ||
pt={title ? 1 : 2} | ||
> | ||
{message} | ||
</Typography> | ||
{insufficientGas.length | ||
? insufficientGas.map((item, index) => ( | ||
<Typography | ||
variant="body2" | ||
px={2} | ||
pb={insufficientGas.length - 1 === index ? 2 : 0} | ||
pt={0.5} | ||
> | ||
{t(`swap.gasAmount`, { | ||
amount: item.insufficientAmount?.toString(), | ||
tokenSymbol: item.token.symbol, | ||
chainName: item.chain?.name, | ||
})} | ||
</Typography> | ||
)) | ||
: null} | ||
</Box> | ||
</MessageCard> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './GasSufficiencyMessage'; |
46 changes: 0 additions & 46 deletions
46
...ges/widget/src/components/InsufficientGasOrFundsMessage/InsufficientGasOrFundsMessage.tsx
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/widget/src/components/InsufficientGasOrFundsMessage/index.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { EVMChain, Token } from '@lifi/sdk'; | ||
import Big from 'big.js'; | ||
import { useMemo } from 'react'; | ||
import { useWatch } from 'react-hook-form'; | ||
import { useChains, useDebouncedWatch } from '.'; | ||
import { SwapFormKey, SwapFormKeyHelper } from '../providers/SwapFormProvider'; | ||
import { useWallet } from '../providers/WalletProvider'; | ||
import { useCurrentRoute } from '../stores'; | ||
import { useTokenBalances } from './useTokenBalances'; | ||
|
||
interface GasSufficiency { | ||
gasAmount: Big; | ||
tokenAmount?: Big; | ||
insufficientAmount?: Big; | ||
insufficient?: boolean; | ||
token: Token; | ||
chain?: EVMChain; | ||
} | ||
|
||
export const useGasSufficiency = () => { | ||
const { account } = useWallet(); | ||
const [route] = useCurrentRoute(); | ||
const [fromChainId, toChainId, fromToken]: [number, number, string] = | ||
useWatch({ | ||
name: [ | ||
SwapFormKeyHelper.getChainKey('from'), | ||
SwapFormKeyHelper.getChainKey('to'), | ||
SwapFormKey.FromToken, | ||
], | ||
}); | ||
const fromAmount = useDebouncedWatch(SwapFormKey.FromAmount, 250); | ||
const { tokens: fromChainTokenBalances } = useTokenBalances(fromChainId); | ||
const { tokens: toChainTokenBalances } = useTokenBalances(toChainId); | ||
const { getChainById } = useChains(); | ||
|
||
const insufficientGas = useMemo(() => { | ||
if (!account.isActive || !route || !fromAmount) { | ||
return []; | ||
} | ||
|
||
const tokenBalancesByChain = { | ||
[fromChainId]: fromChainTokenBalances, | ||
[toChainId]: toChainTokenBalances, | ||
}; | ||
|
||
const gasCosts = route.steps.reduce((groupedGasCosts, step) => { | ||
if (step.estimate.gasCosts) { | ||
const { token } = step.estimate.gasCosts[0]; | ||
const gasCostAmount = step.estimate.gasCosts | ||
.reduce( | ||
(amount, gasCost) => amount.plus(Big(gasCost.amount || 0)), | ||
Big(0), | ||
) | ||
.div(10 ** token.decimals); | ||
const groupedGasCost = groupedGasCosts[token.chainId]; | ||
const gasAmount = groupedGasCost | ||
? groupedGasCost.gasAmount.plus(gasCostAmount) | ||
: gasCostAmount; | ||
groupedGasCosts[token.chainId] = { | ||
gasAmount, | ||
tokenAmount: gasAmount, | ||
token, | ||
chain: getChainById(token.chainId), | ||
}; | ||
return groupedGasCosts; | ||
} | ||
return groupedGasCosts; | ||
}, {} as Record<number, GasSufficiency>); | ||
|
||
if ( | ||
gasCosts[fromChainId] && | ||
route.fromToken.address === gasCosts[fromChainId].token.address | ||
) { | ||
gasCosts[fromChainId].tokenAmount = gasCosts[fromChainId]?.gasAmount.plus( | ||
Big(fromAmount), | ||
); | ||
} | ||
|
||
[fromChainId, toChainId].forEach((chainId) => { | ||
if (gasCosts[chainId]) { | ||
const gasTokenBalance = Big( | ||
tokenBalancesByChain[chainId]?.find( | ||
(t) => t.address === gasCosts[chainId].token.address, | ||
)?.amount ?? 0, | ||
); | ||
|
||
const insufficientFromChainGas = | ||
gasTokenBalance.lte(0) || | ||
gasTokenBalance.lt(gasCosts[chainId].gasAmount ?? Big(0)) || | ||
gasTokenBalance.lt(gasCosts[chainId].tokenAmount ?? Big(0)); | ||
|
||
const insufficientFromChainGasAmount = insufficientFromChainGas | ||
? gasCosts[chainId].tokenAmount?.minus(gasTokenBalance) ?? | ||
gasCosts[chainId].gasAmount.minus(gasTokenBalance) | ||
: undefined; | ||
|
||
gasCosts[chainId] = { | ||
...gasCosts[chainId], | ||
insufficient: insufficientFromChainGas, | ||
insufficientAmount: insufficientFromChainGasAmount, | ||
}; | ||
} | ||
}); | ||
|
||
const gasCostResult = Object.values(gasCosts).filter( | ||
(gasCost) => gasCost.insufficient, | ||
); | ||
|
||
return gasCostResult; | ||
}, [ | ||
account.isActive, | ||
fromAmount, | ||
fromChainId, | ||
fromChainTokenBalances, | ||
getChainById, | ||
route, | ||
toChainId, | ||
toChainTokenBalances, | ||
]); | ||
|
||
const insufficientFunds = useMemo(() => { | ||
if (!account.isActive || !fromToken || !fromAmount) { | ||
return true; | ||
} | ||
const balance = Big( | ||
fromChainTokenBalances?.find((t) => t.address === fromToken)?.amount ?? 0, | ||
); | ||
return Big(fromAmount).lte(balance); | ||
}, [account.isActive, fromAmount, fromChainTokenBalances, fromToken]); | ||
|
||
return { | ||
insufficientGas, | ||
insufficientFunds, | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.