From 769a7ab9b559d5cc323bf0c3bfa3e9b6cf17d2d9 Mon Sep 17 00:00:00 2001 From: Kristie Huang Date: Mon, 6 Nov 2023 16:15:41 -0500 Subject: [PATCH] fix: rename WrappedTokenInfo class (#7495) * fix: rename WrappedTokenInfo class * delete old wrappedTokenInfo file * rename to TokenFromList * move back to state/lists * appease linter * fix nftlistrow --- .../SearchModal/CurrencyList/index.tsx | 6 +++--- src/components/Tokens/TokenDetails/index.tsx | 5 ++--- src/graphql/data/Token.ts | 20 ------------------- src/hooks/Tokens.ts | 8 ++++---- src/hooks/useColor.ts | 4 ++-- src/hooks/useTokenInfoFromActiveList.ts | 2 +- src/lib/hooks/useTokenList/utils.ts | 6 +++--- .../{wrappedTokenInfo.ts => tokenFromList.ts} | 2 +- 8 files changed, 16 insertions(+), 37 deletions(-) rename src/state/lists/{wrappedTokenInfo.ts => tokenFromList.ts} (97%) diff --git a/src/components/SearchModal/CurrencyList/index.tsx b/src/components/SearchModal/CurrencyList/index.tsx index d262b4394d..47e368af66 100644 --- a/src/components/SearchModal/CurrencyList/index.tsx +++ b/src/components/SearchModal/CurrencyList/index.tsx @@ -17,7 +17,7 @@ import { ThemedText } from 'theme/components' import { NumberType, useFormatter } from 'utils/formatNumbers' import { useIsUserAddedToken } from '../../../hooks/Tokens' -import { WrappedTokenInfo } from '../../../state/lists/wrappedTokenInfo' +import { TokenFromList } from '../../../state/lists/tokenFromList' import Column, { AutoColumn } from '../../Column' import CurrencyLogo from '../../Logo/CurrencyLogo' import Row, { RowFixed } from '../../Row' @@ -86,7 +86,7 @@ const TagContainer = styled.div` ` function TokenTags({ currency }: { currency: Currency }) { - if (!(currency instanceof WrappedTokenInfo)) { + if (!(currency instanceof TokenFromList)) { return null } @@ -247,7 +247,7 @@ export default function CurrencyList({ }: { height: number currencies: Currency[] - otherListTokens?: WrappedTokenInfo[] + otherListTokens?: TokenFromList[] selectedCurrency?: Currency | null onCurrencySelect: (currency: Currency, hasWarning?: boolean) => void otherCurrency?: Currency | null diff --git a/src/components/Tokens/TokenDetails/index.tsx b/src/components/Tokens/TokenDetails/index.tsx index 39fb52b301..6f40952d85 100644 --- a/src/components/Tokens/TokenDetails/index.tsx +++ b/src/components/Tokens/TokenDetails/index.tsx @@ -27,8 +27,7 @@ import { useInfoExplorePageEnabled } from 'featureFlags/flags/infoExplore' import { useInfoTDPEnabled } from 'featureFlags/flags/infoTDP' import { TokenPriceQuery } from 'graphql/data/__generated__/types-and-hooks' import { Chain, TokenQuery, TokenQueryData } from 'graphql/data/Token' -import { QueryToken } from 'graphql/data/Token' -import { getTokenDetailsURL, InterfaceGqlChain, supportedChainIdFromGQLChain } from 'graphql/data/util' +import { getTokenDetailsURL, gqlToCurrency, InterfaceGqlChain, supportedChainIdFromGQLChain } from 'graphql/data/util' import { useOnGlobalChainSwitch } from 'hooks/useGlobalChainSwitch' import { UNKNOWN_TOKEN_SYMBOL, useTokenFromActiveNetwork } from 'lib/hooks/useCurrency' import { Swap } from 'pages/Swap' @@ -83,7 +82,7 @@ function useRelevantToken( const queryToken = useMemo(() => { if (!address) return undefined if (address === NATIVE_CHAIN_ID) return nativeOnChain(pageChainId) - if (tokenQueryData) return new QueryToken(address, tokenQueryData) + if (tokenQueryData) return gqlToCurrency(tokenQueryData) return undefined }, [pageChainId, address, tokenQueryData]) // fetches on-chain token if query data is missing and page chain matches global chain (else fetch won't work) diff --git a/src/graphql/data/Token.ts b/src/graphql/data/Token.ts index f89ae592fb..0a58ba346d 100644 --- a/src/graphql/data/Token.ts +++ b/src/graphql/data/Token.ts @@ -1,9 +1,6 @@ -import { DEFAULT_ERC20_DECIMALS } from 'constants/tokens' import gql from 'graphql-tag' -import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo' import { TokenQuery } from './__generated__/types-and-hooks' -import { supportedChainIdFromGQLChain } from './util' // The difference between Token and TokenProject: // Token: an on-chain entity referring to a contract (e.g. uni token on ethereum 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984) @@ -104,20 +101,3 @@ gql` export type { Chain, TokenQuery } from './__generated__/types-and-hooks' export type TokenQueryData = TokenQuery['token'] - -// TODO: Return a QueryToken from useTokenQuery instead of TokenQueryData to make it more usable in Currency-centric interfaces. -export class QueryToken extends WrappedTokenInfo { - constructor(address: string, data: NonNullable, logoSrc?: string) { - const chainId = supportedChainIdFromGQLChain(data.chain) - if (chainId) { - super({ - chainId, - address, - decimals: data.decimals ?? DEFAULT_ERC20_DECIMALS, - symbol: data.symbol ?? '', - name: data.name ?? '', - logoURI: logoSrc ?? data.project?.logoUrl ?? undefined, - }) - } - } -} diff --git a/src/hooks/Tokens.ts b/src/hooks/Tokens.ts index 4d7687b07c..942b0da47f 100644 --- a/src/hooks/Tokens.ts +++ b/src/hooks/Tokens.ts @@ -10,7 +10,7 @@ import { useAppSelector } from 'state/hooks' import { isL2ChainId } from 'utils/chains' import { useAllLists, useCombinedActiveList, useCombinedTokenMapFromUrls } from '../state/lists/hooks' -import { WrappedTokenInfo } from '../state/lists/wrappedTokenInfo' +import { TokenFromList } from '../state/lists/tokenFromList' import { deserializeToken, useUserAddedTokens } from '../state/user/hooks' import { useUnsupportedTokenList } from './../state/lists/hooks' @@ -138,7 +138,7 @@ export function useUnsupportedTokens(): { [address: string]: Token } { return { ...unsupportedTokens, ...l2InferredBlockedTokens } } -export function useSearchInactiveTokenLists(search: string | undefined, minResults = 10): WrappedTokenInfo[] { +export function useSearchInactiveTokenLists(search: string | undefined, minResults = 10): TokenFromList[] { const lists = useAllLists() const inactiveUrls = DEFAULT_INACTIVE_LIST_URLS const { chainId } = useWeb3React() @@ -146,7 +146,7 @@ export function useSearchInactiveTokenLists(search: string | undefined, minResul return useMemo(() => { if (!search || search.trim().length === 0) return [] const tokenFilter = getTokenFilter(search) - const result: WrappedTokenInfo[] = [] + const result: TokenFromList[] = [] const addressSet: { [address: string]: true } = {} for (const url of inactiveUrls) { const list = lists[url]?.current @@ -154,7 +154,7 @@ export function useSearchInactiveTokenLists(search: string | undefined, minResul for (const tokenInfo of list.tokens) { if (tokenInfo.chainId === chainId && tokenFilter(tokenInfo)) { try { - const wrapped: WrappedTokenInfo = new WrappedTokenInfo(tokenInfo, list) + const wrapped: TokenFromList = new TokenFromList(tokenInfo, list) if (!(wrapped.address in activeTokens) && !addressSet[wrapped.address]) { addressSet[wrapped.address] = true result.push(wrapped) diff --git a/src/hooks/useColor.ts b/src/hooks/useColor.ts index 4fb20ef1e5..6c7ac57059 100644 --- a/src/hooks/useColor.ts +++ b/src/hooks/useColor.ts @@ -3,7 +3,7 @@ import { DEFAULT_COLOR } from 'constants/tokenColors' import useTokenLogoSource from 'hooks/useAssetLogoSource' import { darken, lighten, rgb } from 'polished' import { useEffect, useState } from 'react' -import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo' +import { TokenFromList } from 'state/lists/tokenFromList' import { useTheme } from 'styled-components' import { getColor } from 'utils/getColor' import { hex } from 'wcag-contrast' @@ -35,7 +35,7 @@ function URIForEthToken(address: string) { * @returns {Promise< | null>} A promise that resolves to a color string or null if color cannot be determined. */ async function getColorFromToken(token: Token, primarySrc?: string): Promise { - const wrappedToken = token as WrappedTokenInfo + const wrappedToken = token as TokenFromList let color: string | null = null try { diff --git a/src/hooks/useTokenInfoFromActiveList.ts b/src/hooks/useTokenInfoFromActiveList.ts index 027f84da33..7dc203d08d 100644 --- a/src/hooks/useTokenInfoFromActiveList.ts +++ b/src/hooks/useTokenInfoFromActiveList.ts @@ -3,7 +3,7 @@ import { useWeb3React } from '@web3-react/core' import { useMemo } from 'react' import { useCombinedActiveList } from 'state/lists/hooks' -/** Returns a WrappedTokenInfo from the active token lists when possible, or the passed token otherwise. */ +/** Returns a TokenFromList from the active token lists when possible, or the passed token otherwise. */ export function useTokenInfoFromActiveList(currency: Currency) { const { chainId } = useWeb3React() const activeList = useCombinedActiveList() diff --git a/src/lib/hooks/useTokenList/utils.ts b/src/lib/hooks/useTokenList/utils.ts index da7ed06dc4..71b50d23a6 100644 --- a/src/lib/hooks/useTokenList/utils.ts +++ b/src/lib/hooks/useTokenList/utils.ts @@ -1,7 +1,7 @@ import { TokenInfo, TokenList } from '@uniswap/token-lists' -import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo' +import { TokenFromList } from 'state/lists/tokenFromList' -type TokenMap = Readonly<{ [tokenAddress: string]: { token: WrappedTokenInfo; list?: TokenList } }> +type TokenMap = Readonly<{ [tokenAddress: string]: { token: TokenFromList; list?: TokenList } }> // TODO(WEB-2347): replace usage of the misnomered TokenAddressMap w/ ChainTokenMap from src/hooks/Tokens.ts export type TokenAddressMap = Readonly<{ [chainId: number]: TokenMap }> @@ -18,7 +18,7 @@ export function tokensToChainTokenMap(tokens: TokenList | TokenInfo[]): TokenAdd const [list, infos] = Array.isArray(tokens) ? [undefined, tokens] : [tokens, tokens.tokens] const map = infos.reduce>((map, info) => { try { - const token = new WrappedTokenInfo(info, list) + const token = new TokenFromList(info, list) if (map[token.chainId]?.[token.address] !== undefined) { console.warn(`Duplicate token skipped: ${token.address}`) return map diff --git a/src/state/lists/wrappedTokenInfo.ts b/src/state/lists/tokenFromList.ts similarity index 97% rename from src/state/lists/wrappedTokenInfo.ts rename to src/state/lists/tokenFromList.ts index 3d5a1125d7..679052724c 100644 --- a/src/state/lists/wrappedTokenInfo.ts +++ b/src/state/lists/tokenFromList.ts @@ -10,7 +10,7 @@ interface TagInfo extends TagDetails { /** * Token instances created from token info on a token list. */ -export class WrappedTokenInfo implements Token { +export class TokenFromList implements Token { public readonly isNative = false as const public readonly isToken = true as const public readonly list?: TokenList