Skip to content

Commit

Permalink
Make token sorting code more robust
Browse files Browse the repository at this point in the history
Don't die on suddenly appearing new tone types
  • Loading branch information
csillag committed Jul 6, 2023
1 parent 06302b6 commit 4bb3bcf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/app/pages/AccountDetailsPage/AccountTokensCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const AccountTokensCard: FC<AccountTokensCardProps> = ({ type }) => {
<LinkableDiv id={accountTokenContainerId}>
<CardHeader disableTypography component="h3" title={tokenListLabel} />
<CardContent>
{!isLoading && !account?.tokenBalances[type].length && (
{!isLoading && !account?.tokenBalances[type]?.length && (
<CardEmptyState label={t('account.emptyTokenList', { token: tokenLabel })} />
)}

Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/AccountDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const AccountDetailsPage: FC = () => {

const showTokenTransfers = showEmptyAccountDetails || !!numberOfTokenTransfers
const tokenTransfersLink = useHref(`token-transfers#${accountTokenTransfersContainerId}`)
const showErc20 = showEmptyAccountDetails || !!account?.tokenBalances[EvmTokenType.ERC20].length
const showErc20 = showEmptyAccountDetails || !!account?.tokenBalances[EvmTokenType.ERC20]?.length
const erc20Link = useHref(`tokens/erc-20#${accountTokenContainerId}`)
const showErc721 = showEmptyAccountDetails || !!account?.tokenBalances[EvmTokenType.ERC721].length
const showErc721 = showEmptyAccountDetails || !!account?.tokenBalances[EvmTokenType.ERC721]?.length
const erc721Link = useHref(`tokens/erc-721#${accountTokenContainerId}`)
const showTxs = showEmptyAccountDetails || showErc20 || !!account?.stats.num_txns
const txLink = useHref('')
Expand Down
14 changes: 8 additions & 6 deletions src/oasis-nexus/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ declare module './generated/api' {
layer: Layer
address_eth?: string
ticker: NativeTicker
tokenBalances: Record<EvmTokenType, generated.RuntimeEvmBalance[]>
tokenBalances: Partial<Record<EvmTokenType, generated.RuntimeEvmBalance[]>>
}
export interface RuntimeEvent {
network: Network
Expand Down Expand Up @@ -73,12 +73,14 @@ export const isAccountEmpty = (account: RuntimeAccount) => {
export const isAccountNonEmpty = (account: RuntimeAccount) => !isAccountEmpty(account)

export const groupAccountTokenBalances = (account: Omit<RuntimeAccount, 'tokenBalances'>): RuntimeAccount => {
const tokenBalances: Record<generated.EvmTokenType, generated.RuntimeEvmBalance[]> = {
ERC20: [],
ERC721: [],
}
const tokenBalances: Partial<Record<generated.EvmTokenType, generated.RuntimeEvmBalance[]>> = {}
account.evm_balances.forEach(balance => {
if (balance.token_type) tokenBalances[balance.token_type].push(balance)
if (balance.token_type) {
if (!tokenBalances[balance.token_type]) {
tokenBalances[balance.token_type] = []
}
tokenBalances[balance.token_type]!.push(balance)
}
})

return {
Expand Down

0 comments on commit 4bb3bcf

Please sign in to comment.