Skip to content

Commit

Permalink
refactor: fetch safe balances individually when displaying a safe lis…
Browse files Browse the repository at this point in the history
…t item
  • Loading branch information
jmealy committed Feb 6, 2024
1 parent 885b9f6 commit fe7871b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 50 deletions.
9 changes: 1 addition & 8 deletions src/components/common/OwnedSafeList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ const OwnedSafeList = ({ closeDrawer, isWelcomePage }: { closeDrawer?: () => voi
const [lastChainId, setLastChainId] = useState<string | undefined>()
const [isListExpanded, setIsListExpanded] = useState<boolean>(false)
const router = useRouter()
const connectWallet = useConnectWallet()

// use watched safes list here for now. Change later to owned safes
const [safes, error, isLoading] = useWatchedSafes()
const safes = useWatchedSafes()

const isSingleTxPage = router.pathname === AppRoutes.transactions.tx

Expand Down Expand Up @@ -52,12 +51,6 @@ const OwnedSafeList = ({ closeDrawer, isWelcomePage }: { closeDrawer?: () => voi
</Typography>
</div>

{isLoading && (
<Box py={4} textAlign="center">
<CircularProgress size={20} />
</Box>
)}

{!safes.length && (
<Box display="flex" flexDirection="column" py={4} sx={{ maxWidth: '250px', margin: 'auto' }}>
<Typography variant="body2" color="primary.light" textAlign="center" mt={2} mb={2}>
Expand Down
8 changes: 1 addition & 7 deletions src/components/common/Watchlist/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Watchlist = ({
const [isListExpanded, setIsListExpanded] = useState<boolean>(false)
const router = useRouter()

const [safes, error, isLoading] = useWatchedSafes()
const safes = useWatchedSafes()

const isSingleTxPage = router.pathname === AppRoutes.transactions.tx

Expand Down Expand Up @@ -85,12 +85,6 @@ const Watchlist = ({
</Track>
</div>

{isLoading && (
<Box py={4} textAlign="center">
<CircularProgress size={20} />
</Box>
)}

{!safes.length && (
<Box display="flex" flexDirection="column" alignItems="center" sx={{ paddingY: '26px' }}>
<Typography variant="body2" color="primary.light" textAlign="center">
Expand Down
20 changes: 15 additions & 5 deletions src/components/sidebar/SafeListItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { OPEN_SAFE_LABELS, OVERVIEW_EVENTS } from '@/services/analytics'
import ChainIndicator from '@/components/common/ChainIndicator'
import useMediaQuery from '@mui/material/useMediaQuery'
import { useTheme } from '@mui/material/styles'
import { getBalances } from '@safe-global/safe-gateway-typescript-sdk'
import useAsync from '@/hooks/useAsync'

const SafeListItem = ({
address,
Expand Down Expand Up @@ -63,6 +65,12 @@ const SafeListItem = ({
const theme = useTheme()
const isMobile = useMediaQuery(theme.breakpoints.down('sm'))

const currency = 'USD'
const [balance] = useAsync(
() => getBalances(chainId, address, currency).then((result) => result.fiatTotal),
[chainId, address, currency],
)

// Scroll to the current Safe
useEffect(() => {
if (isCurrentSafe && shouldScrollToSafe) {
Expand Down Expand Up @@ -121,11 +129,13 @@ const SafeListItem = ({
/>
}
/>
<ListItemText
className={css.accountBalance}
primaryTypographyProps={{ variant: 'body2' }}
primary={<b>${fiatBalance}</b>}
/>
{balance && (
<ListItemText
className={css.accountBalance}
primaryTypographyProps={{ variant: 'body2' }}
primary={<b>${balance}</b>}
/>
)}
</div>
<ChainIndicator chainId={chainId} inline showName={isWelcomePage && !isMobile} />
</div>
Expand Down
42 changes: 12 additions & 30 deletions src/hooks/useSafes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,21 @@ const sortChainsByCurrentChain = (chains: ChainInfo[], currentChainId: string):
return currentChain ? [currentChain, ...otherChains] : chains
}

export const useWatchedSafes = (): [SafeListItemDetails[], Error | undefined, boolean] => {
export const useWatchedSafes = (): SafeListItemDetails[] => {
const currentChainId = useChainId()
const { configs } = useChains()
const watchedSafes = useAppSelector(selectAllAddedSafes)
const currency = useAppSelector(selectCurrency)
const chains = useMemo(() => sortChainsByCurrentChain(configs, currentChainId), [configs, currentChainId])

const [allWatchedSafesWithBalances, error, loading] = useAsync<SafeListItemDetails[]>(
() => {
let watchedSafesOnAllChains: SafeListItemDetails[] = []
for (const chain of chains) {
const watchedSafesOnChain = watchedSafes[chain.chainId] ?? {}
const watchedSafesAdressesOnChain = Object.keys(watchedSafesOnChain)
const watchedSafesWithChain = watchedSafesAdressesOnChain.map((safeAddress) => {
const { threshold, owners } = watchedSafesOnChain[safeAddress]
return { safeAddress, chain, threshold, owners }
})
watchedSafesOnAllChains = [...watchedSafesOnAllChains, ...watchedSafesWithChain]
}
const promises = watchedSafesOnAllChains.map(async ({ safeAddress, chain, threshold, owners }) => {
const fiatBalance = await getBalances(chain.chainId, safeAddress, currency).then((result) => result.fiatTotal)
return {
safeAddress,
chain,
fiatBalance,
threshold,
owners,
}
})
return Promise.all(promises)
},
[watchedSafes, chains],
false,
)
return [allWatchedSafesWithBalances ?? [], error, loading]
let watchedSafesOnAllChains: SafeListItemDetails[] = []
for (const chain of chains) {
const watchedSafesOnChain = watchedSafes[chain.chainId] ?? {}
const watchedSafesAdressesOnChain = Object.keys(watchedSafesOnChain)
const watchedSafesWithChain = watchedSafesAdressesOnChain.map((safeAddress) => {
const { threshold, owners } = watchedSafesOnChain[safeAddress]
return { safeAddress, chain, threshold, owners }
})
watchedSafesOnAllChains = [...watchedSafesOnAllChains, ...watchedSafesWithChain]
}
return watchedSafesOnAllChains
}

0 comments on commit fe7871b

Please sign in to comment.