-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useToken and useTokenBalance hooks, update SwapInput
- Loading branch information
Showing
16 changed files
with
178 additions
and
98 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
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
23 changes: 11 additions & 12 deletions
23
packages/widget/src/components/SwapChainButton/SwapChainButton.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
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { SwapFormDirection } from '../../providers/SwapFormProvider'; | ||
import { | ||
SwapFormDirection, | ||
SwapFormTypeProps, | ||
} from '../../providers/SwapFormProvider'; | ||
|
||
export interface SwapChainButtonProps { | ||
onClick?(type: SwapFormDirection): void; | ||
type: SwapFormDirection; | ||
export interface SwapChainButtonProps extends SwapFormTypeProps { | ||
onClick?(formType: SwapFormDirection): void; | ||
} |
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
packages/widget/src/components/SwapInputAdornment/SwapInputAdornment.style.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,17 @@ | ||
import { Typography } from '@mui/material'; | ||
import { styled } from '@mui/material/styles'; | ||
|
||
export const SwapPriceTypography = styled(Typography)(({ theme }) => ({ | ||
borderLeft: '1px solid', | ||
borderColor: theme.palette.grey[300], | ||
paddingLeft: 8, | ||
marginLeft: 8, | ||
})); | ||
|
||
export const SwapMaxAmountTypography = styled(Typography)({ | ||
textDecoration: 'underline', | ||
marginRight: 8, | ||
'&:hover': { | ||
cursor: 'pointer', | ||
}, | ||
}); |
70 changes: 70 additions & 0 deletions
70
packages/widget/src/components/SwapInputAdornment/SwapInputAdornment.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,70 @@ | ||
import { InputAdornment, Skeleton, Typography } from '@mui/material'; | ||
import BigNumber from 'bignumber.js'; | ||
import { useMemo } from 'react'; | ||
import { useWatch } from 'react-hook-form'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useTokenBalance } from '../../hooks/useTokenBalance'; | ||
import { | ||
SwapFormKeyHelper, | ||
SwapFormTypeProps, | ||
} from '../../providers/SwapFormProvider'; | ||
import { formatTokenAmount } from '../../utils/format'; | ||
import { | ||
SwapMaxAmountTypography, | ||
SwapPriceTypography, | ||
} from './SwapInputAdornment.style'; | ||
|
||
export const SwapInputAdornment: React.FC<SwapFormTypeProps> = ({ | ||
formType, | ||
}) => { | ||
const { t } = useTranslation(); | ||
const [chainKey, tokenAddress] = useWatch({ | ||
name: [ | ||
SwapFormKeyHelper.getChainKey(formType), | ||
SwapFormKeyHelper.getTokenKey(formType), | ||
], | ||
}); | ||
const { token, tokenWithBalance, isFetching } = useTokenBalance( | ||
chainKey, | ||
tokenAddress, | ||
); | ||
|
||
const amount = useMemo( | ||
() => | ||
token | ||
? formatTokenAmount(token, new BigNumber(tokenWithBalance?.amount ?? 0)) | ||
: null, | ||
[token, tokenWithBalance?.amount], | ||
); | ||
|
||
return ( | ||
<InputAdornment position="end"> | ||
{isFetching ? ( | ||
<Skeleton | ||
variant="rectangular" | ||
width={formType === 'from' ? 160 : 96} | ||
height={20} | ||
sx={{ borderRadius: '6px' }} | ||
/> | ||
) : ( | ||
<> | ||
{formType === 'from' && token && amount ? ( | ||
<> | ||
<SwapMaxAmountTypography variant="body2" color="text.primary"> | ||
{t(`swap.max`)} | ||
</SwapMaxAmountTypography> | ||
<Typography variant="body2" color="text.secondary"> | ||
{t(`swap.maxAmount`, { | ||
amount, | ||
})} | ||
</Typography> | ||
</> | ||
) : null} | ||
<SwapPriceTypography variant="body2" color="text.secondary"> | ||
{t(`swap.price`, { price: tokenWithBalance?.priceUSD ?? 0 })} | ||
</SwapPriceTypography> | ||
</> | ||
)} | ||
</InputAdornment> | ||
); | ||
}; |
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 './SwapInputAdornment'; |
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,21 @@ | ||
import { ChainKey, getChainByKey } from '@lifinance/sdk'; | ||
import { useMemo } from 'react'; | ||
import { useSwapPossibilities } from './useSwapPossibilities'; | ||
|
||
export const useToken = (chainKey: ChainKey, tokenAddress: string) => { | ||
const { data: possibilities, isLoading } = useSwapPossibilities(); | ||
|
||
const [chain, token] = useMemo(() => { | ||
const chain = getChainByKey(chainKey); | ||
const token = possibilities?.tokens.find( | ||
(token) => token.address === tokenAddress && token.chainId === chain.id, | ||
); | ||
return [chain, token]; | ||
}, [chainKey, possibilities?.tokens, tokenAddress]); | ||
|
||
return { | ||
chain, | ||
token, | ||
isLoading, | ||
}; | ||
}; |
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,32 @@ | ||
import Lifi, { ChainKey } from '@lifinance/sdk'; | ||
import { useQuery } from 'react-query'; | ||
import { usePriorityAccount } from './connectorHooks'; | ||
import { useToken } from './useToken'; | ||
|
||
export const useTokenBalance = (chainKey: ChainKey, tokenAddress: string) => { | ||
const account = usePriorityAccount(); | ||
const { token } = useToken(chainKey, tokenAddress); | ||
|
||
const { data: tokenWithBalance, isFetching } = useQuery( | ||
['token', token?.symbol, account], | ||
async ({ queryKey: [_, tokenSymbol, account] }) => { | ||
if (!account || !token) { | ||
return null; | ||
} | ||
const tokenBalance = await Lifi.getTokenBalance(account, token); | ||
return tokenBalance; | ||
}, | ||
{ | ||
enabled: Boolean(account) && Boolean(token), | ||
refetchIntervalInBackground: true, | ||
refetchInterval: 60_000, | ||
staleTime: 60_000, | ||
}, | ||
); | ||
|
||
return { | ||
token, | ||
tokenWithBalance, | ||
isFetching, | ||
}; | ||
}; |
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
Oops, something went wrong.