feat: show real values for current network balance (#4565)

* init

* remove card when balance is zero

* remove commented code

* remove commented
This commit is contained in:
lynn 2022-09-06 11:50:02 -04:00 committed by GitHub
parent eb725f51ce
commit 622581ee0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 51 deletions

@ -1,12 +1,12 @@
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import { getChainInfoOrDefault } from 'constants/chainInfo' import { formatToDecimal } from 'components/AmplitudeAnalytics/utils'
import { useToken } from 'hooks/Tokens' import { useToken } from 'hooks/Tokens'
import { useNetworkTokenBalances } from 'hooks/useNetworkTokenBalances' import { useNetworkTokenBalances } from 'hooks/useNetworkTokenBalances'
import { useStablecoinValue } from 'hooks/useStablecoinPrice'
import { useTokenBalance } from 'lib/hooks/useCurrencyBalance'
import { AlertTriangle } from 'react-feather' import { AlertTriangle } from 'react-feather'
import styled, { useTheme } from 'styled-components/macro' import styled from 'styled-components/macro'
import NetworkBalance from './NetworkBalance'
const BalancesCard = styled.div` const BalancesCard = styled.div`
width: 100%; width: 100%;
@ -33,14 +33,9 @@ const ErrorText = styled.span`
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
` `
const NetworkBalancesSection = styled.div`
height: fit-content;
`
const TotalBalanceSection = styled.div` const TotalBalanceSection = styled.div`
height: fit-content; height: fit-content;
border-bottom: 1px solid ${({ theme }) => theme.backgroundOutline};
margin-bottom: 20px;
padding-bottom: 20px;
` `
const TotalBalance = styled.div` const TotalBalance = styled.div`
display: flex; display: flex;
@ -54,58 +49,35 @@ const TotalBalanceItem = styled.div`
display: flex; display: flex;
` `
export default function BalanceSummary({ export default function BalanceSummary({ address }: { address: string }) {
address, const token = useToken(address)
networkBalances, const { loading, error } = useNetworkTokenBalances({ address })
totalBalance,
}: {
address: string
networkBalances: (JSX.Element | null)[] | null
totalBalance: number
}) {
const theme = useTheme()
const tokenSymbol = useToken(address)?.symbol
const { loading, error, data } = useNetworkTokenBalances({ address })
const { chainId: connectedChainId } = useWeb3React() const { account } = useWeb3React()
const balance = useTokenBalance(account, token ?? undefined)
const balanceNumber = balance ? formatToDecimal(balance, Math.min(balance.currency.decimals, 6)) : undefined
const balanceUsd = useStablecoinValue(balance)?.toFixed(2)
const balanceUsdNumber = balanceUsd ? parseFloat(balanceUsd) : undefined
const { label: connectedLabel, logoUrl: connectedLogoUrl } = getChainInfoOrDefault(connectedChainId) if (loading || (!error && !balanceNumber && !balanceUsdNumber)) return null
const connectedFiatValue = 1
const multipleBalances = true // for testing purposes
if (loading) return null
return ( return (
<BalancesCard> <BalancesCard>
{error ? ( {error ? (
<ErrorState> <ErrorState>
<AlertTriangle size={24} /> <AlertTriangle size={24} />
<ErrorText> <ErrorText>
<Trans>There was an error loading your {tokenSymbol} balance</Trans> <Trans>There was an error loading your {token?.symbol} balance</Trans>
</ErrorText> </ErrorText>
</ErrorState> </ErrorState>
) : multipleBalances ? (
<>
<TotalBalanceSection>
Your balance across all networks
<TotalBalance>
<TotalBalanceItem>{`${totalBalance} ${tokenSymbol}`}</TotalBalanceItem>
<TotalBalanceItem>$4,210.12</TotalBalanceItem>
</TotalBalance>
</TotalBalanceSection>
<NetworkBalancesSection>Your balances by network</NetworkBalancesSection>
{data && networkBalances}
</>
) : ( ) : (
<> <>
Your balance on {connectedLabel} <TotalBalanceSection>
<NetworkBalance Your balance
logoUrl={connectedLogoUrl} <TotalBalance>
balance={'1'} <TotalBalanceItem>{`${balanceNumber} ${token?.symbol}`}</TotalBalanceItem>
tokenSymbol={tokenSymbol ?? 'XXX'} <TotalBalanceItem>{`$${balanceUsdNumber}`}</TotalBalanceItem>
fiatValue={connectedFiatValue} </TotalBalance>
label={connectedLabel} </TotalBalanceSection>
networkColor={theme.textPrimary}
/>
</> </>
)} )}
</BalancesCard> </BalancesCard>

@ -130,7 +130,7 @@ export default function TokenDetails() {
<RightPanel> <RightPanel>
<Widget defaultToken={token ?? undefined} /> <Widget defaultToken={token ?? undefined} />
{tokenWarning && <TokenSafetyMessage tokenAddress={token.address} warning={tokenWarning} />} {tokenWarning && <TokenSafetyMessage tokenAddress={token.address} warning={tokenWarning} />}
<BalanceSummary address={token.address} totalBalance={totalBalance} networkBalances={balancesByNetwork} /> <BalanceSummary address={token.address} />
</RightPanel> </RightPanel>
<Footer> <Footer>
<FooterBalanceSummary <FooterBalanceSummary