Skip to content

Commit

Permalink
Move token helpers to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Aug 19, 2024
1 parent 1887609 commit 26a237e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 57 deletions.
8 changes: 2 additions & 6 deletions src/app/components/Tokens/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import { TokenLink } from './TokenLink'
import { CopyToClipboard } from '../CopyToClipboard'
import { VerificationIcon, verificationIconBoxHeight } from '../ContractVerificationIcon'
import Box from '@mui/material/Box'
import {
getTokenTypeDescription,
getTokenTypeStrictName,
tokenBackgroundColor,
tokenBorderColor,
} from '../../../types/tokens'
import { tokenBackgroundColor, tokenBorderColor } from '../../../types/tokens'
import { getTokenTypeDescription, getTokenTypeStrictName } from '../../utils/tokens'
import { SearchScope } from '../../../types/searchScope'
import { FC } from 'react'
import Typography from '@mui/material/Typography'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getTokenTypePluralDescription,
getTokenTypePluralName,
getTokenTypeStrictName,
} from '../../../types/tokens'
} from '../../utils/tokens'
import { SearchScope } from '../../../types/searchScope'
import { RuntimeAccountDetailsContext } from './index'
import { getPreciseNumberFormat } from '../../../locales/getPreciseNumberFormat'
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/RuntimeAccountDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useRequiredScopeParam } from '../../hooks/useScopeParam'
import { contractCodeContainerId } from './ContractCodeCard'
import { useTokenInfo } from '../TokenDashboardPage/hook'
import { accountTokenTransfersContainerId } from './AccountTokenTransfersCard'
import { getTokenTypePluralName } from '../../../types/tokens'
import { getTokenTypePluralName } from '../../utils/tokens'
import { SearchScope } from '../../../types/searchScope'
import { RuntimeAccountDetailsCard } from './RuntimeAccountDetailsCard'
import { eventsContainerId } from './AccountEventsCard'
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/TokenDashboardPage/TokenTypeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SnapshotCard } from '../../components/Snapshots/SnapshotCard'
import { COLORS } from '../../../styles/theme/colors'
import { useTokenInfo } from './hook'
import Skeleton from '@mui/material/Skeleton'
import { getTokenTypeDescription, getTokenTypeStrictName } from '../../../types/tokens'
import { getTokenTypeDescription, getTokenTypeStrictName } from '../../utils/tokens'
import { SearchScope } from '../../../types/searchScope'

export const TokenTypeCard: FC<{ scope: SearchScope; address: string }> = ({ scope, address }) => {
Expand Down
50 changes: 50 additions & 0 deletions src/app/utils/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { TFunction } from 'i18next'
import { exhaustedTypeWarning } from '../../types/errors'
import { EvmTokenType } from 'oasis-nexus/api'

export const getTokenMarketCap = (
relativeTotalValue: number | undefined,
rosePriceInUsd: number | undefined,
Expand All @@ -10,3 +14,49 @@ export const getTokenMarketCap = (
const totalValueInUsd = totalValueInWRose * rosePriceInUsd
return totalValueInUsd
}

export const getTokenTypeDescription = (t: TFunction, tokenType: EvmTokenType | undefined): string => {
switch (tokenType ?? 'missing') {
case 'missing':
return t('common.missing')
case 'ERC20':
return t('common.token')
case 'ERC721':
return t('common.nft')
default:
exhaustedTypeWarning('Unknown token type', tokenType as any)
return '???'
}
}

export const getTokenTypePluralDescription = (t: TFunction, tokenType: EvmTokenType): string => {
switch (tokenType) {
case 'ERC20':
return t('common.tokens')
case 'ERC721':
return t('common.nfts')
default:
exhaustedTypeWarning('Unknown token type', tokenType)
return '???'
}
}

export const getTokenTypeStrictName = (t: TFunction, tokenType: EvmTokenType | 'missing'): string => {
switch (tokenType) {
case 'missing':
return t('common.missing')
case 'ERC20':
return t('account.ERC20')
case 'ERC721':
return t('account.ERC721')
default:
exhaustedTypeWarning('Unknown token type', tokenType)
return tokenType
}
}

export const getTokenTypePluralName = (t: TFunction, tokenType: EvmTokenType): string =>
t('tokens.typeDescription', {
spec: getTokenTypeStrictName(t, tokenType),
description: getTokenTypePluralDescription(t, tokenType),
})
48 changes: 0 additions & 48 deletions src/types/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { EvmTokenType } from '../oasis-nexus/api'
import { TFunction } from 'i18next'
import { exhaustedTypeWarning } from './errors'
import { COLORS } from '../styles/theme/colors'

export const getTokenTypeDescription = (t: TFunction, tokenType: EvmTokenType | undefined): string => {
switch (tokenType ?? 'missing') {
case 'missing':
return t('common.missing')
case 'ERC20':
return t('common.token')
case 'ERC721':
return t('common.nft')
default:
exhaustedTypeWarning('Unknown token type', tokenType as any)
return '???'
}
}

export const tokenBackgroundColor: Record<EvmTokenType | 'missing', string> = {
missing: COLORS.errorIndicatorBackground,
ERC20: COLORS.brandMedium15,
Expand All @@ -28,35 +12,3 @@ export const tokenBorderColor: Record<EvmTokenType | 'missing', string> = {
ERC20: COLORS.brandMedium,
ERC721: COLORS.pink,
}

export const getTokenTypePluralDescription = (t: TFunction, tokenType: EvmTokenType): string => {
switch (tokenType) {
case 'ERC20':
return t('common.tokens')
case 'ERC721':
return t('common.nfts')
default:
exhaustedTypeWarning('Unknown token type', tokenType)
return '???'
}
}

export const getTokenTypeStrictName = (t: TFunction, tokenType: EvmTokenType | 'missing'): string => {
switch (tokenType) {
case 'missing':
return t('common.missing')
case 'ERC20':
return t('account.ERC20')
case 'ERC721':
return t('account.ERC721')
default:
exhaustedTypeWarning('Unknown token type', tokenType)
return tokenType
}
}

export const getTokenTypePluralName = (t: TFunction, tokenType: EvmTokenType): string =>
t('tokens.typeDescription', {
spec: getTokenTypeStrictName(t, tokenType),
description: getTokenTypePluralDescription(t, tokenType),
})

0 comments on commit 26a237e

Please sign in to comment.