From 4bb3bcf07a00d5b916c6c9ced4b1d6495a4bc65b Mon Sep 17 00:00:00 2001 From: Kristof Csillag Date: Thu, 6 Jul 2023 14:47:26 +0200 Subject: [PATCH] Make token sorting code more robust Don't die on suddenly appearing new tone types --- .../pages/AccountDetailsPage/AccountTokensCard.tsx | 2 +- src/app/pages/AccountDetailsPage/index.tsx | 4 ++-- src/oasis-nexus/api.ts | 14 ++++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/app/pages/AccountDetailsPage/AccountTokensCard.tsx b/src/app/pages/AccountDetailsPage/AccountTokensCard.tsx index 9dc71bbc83..d2fdfd880a 100644 --- a/src/app/pages/AccountDetailsPage/AccountTokensCard.tsx +++ b/src/app/pages/AccountDetailsPage/AccountTokensCard.tsx @@ -85,7 +85,7 @@ export const AccountTokensCard: FC = ({ type }) => { - {!isLoading && !account?.tokenBalances[type].length && ( + {!isLoading && !account?.tokenBalances[type]?.length && ( )} diff --git a/src/app/pages/AccountDetailsPage/index.tsx b/src/app/pages/AccountDetailsPage/index.tsx index 40a55d3790..938f442cc0 100644 --- a/src/app/pages/AccountDetailsPage/index.tsx +++ b/src/app/pages/AccountDetailsPage/index.tsx @@ -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('') diff --git a/src/oasis-nexus/api.ts b/src/oasis-nexus/api.ts index f3540a92d7..25c18af78d 100644 --- a/src/oasis-nexus/api.ts +++ b/src/oasis-nexus/api.ts @@ -44,7 +44,7 @@ declare module './generated/api' { layer: Layer address_eth?: string ticker: NativeTicker - tokenBalances: Record + tokenBalances: Partial> } export interface RuntimeEvent { network: Network @@ -73,12 +73,14 @@ export const isAccountEmpty = (account: RuntimeAccount) => { export const isAccountNonEmpty = (account: RuntimeAccount) => !isAccountEmpty(account) export const groupAccountTokenBalances = (account: Omit): RuntimeAccount => { - const tokenBalances: Record = { - ERC20: [], - ERC721: [], - } + const tokenBalances: Partial> = {} 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 {