Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token logo update #259

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/BarChart/alt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const Chart = ({
const theme = useTheme()
const parsedValue = value

const now = dayjs()

return (
<Wrapper minHeight={minHeight} {...rest}>
<RowBetween style={{ alignItems: 'flex-start' }}>
Expand Down Expand Up @@ -121,10 +123,10 @@ const Chart = ({

if (setLabel && label !== formattedTime) {
if (activeWindow === VolumeWindow.weekly) {
const isCurrent = formattedTimePlusWeek.isAfter(dayjs())
const isCurrent = formattedTimePlusWeek.isAfter(now)
setLabel(formattedTime + '-' + (isCurrent ? 'current' : formattedTimePlusWeek.format('MMM D, YYYY')))
} else if (activeWindow === VolumeWindow.monthly) {
const isCurrent = formattedTimePlusMonth.isAfter(dayjs())
const isCurrent = formattedTimePlusMonth.isAfter(now)
setLabel(formattedTime + '-' + (isCurrent ? 'current' : formattedTimePlusMonth.format('MMM D, YYYY')))
} else {
setLabel(formattedTimeDaily)
Expand Down
4 changes: 3 additions & 1 deletion src/components/CurrencyLogo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default function CurrencyLogo({

const [activeNetwork] = useActiveNetworkVersion()

const checkSummed = isAddress(address)
const checkSummed = useMemo(() => {
return isAddress(address)
}, [address])

const optimismURI = useMemo(() => {
if (checkSummed && optimismList?.[checkSummed]) {
Expand Down
4 changes: 3 additions & 1 deletion src/constants/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
export const UNSUPPORTED_LIST_URLS: string[] = []
export const OPTIMISM_LIST = 'https://static.optimism.io/optimism.tokenlist.json'
export const ARBITRUM_LIST = 'https://bridge.arbitrum.io/token-list-42161.json'
export const COINGECKO_LIST = 'https://tokens.coingecko.com/uniswap/all.json'

// lower index == higher priority for token import
export const DEFAULT_LIST_OF_LISTS: string[] = [
OPTIMISM_LIST,
ARBITRUM_LIST,
COINGECKO_LIST,
...UNSUPPORTED_LIST_URLS, // need to load unsupported tokens as well
]

// default lists to be 'active' aka searched across
export const DEFAULT_ACTIVE_LIST_URLS: string[] = [OPTIMISM_LIST, ARBITRUM_LIST]
export const DEFAULT_ACTIVE_LIST_URLS: string[] = [OPTIMISM_LIST, ARBITRUM_LIST, COINGECKO_LIST]
24 changes: 23 additions & 1 deletion src/data/pools/poolData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { useBlocksFromTimestamps } from 'hooks/useBlocksFromTimestamps'
import { PoolData } from 'state/pools/reducer'
import { get2DayChange } from 'utils/data'
import { formatTokenName, formatTokenSymbol } from 'utils/tokens'
import { useClients } from 'state/application/hooks'
import { useActiveNetworkVersion, useClients } from 'state/application/hooks'
import { useCombinedActiveList } from 'state/lists/hooks'
import { EthereumNetworkInfo } from 'constants/networks'
import { isAddress } from 'utils'

export const POOLS_BULK = (block: number | undefined, pools: string[]) => {
let poolString = `[`
Expand Down Expand Up @@ -98,6 +101,10 @@ export function usePoolDatas(
}
| undefined
} {
// token logo URIs
const tokensFromLists = useCombinedActiveList()
const [activeNetwork] = useActiveNetworkVersion()

// get client
const { dataClient } = useClients()

Expand Down Expand Up @@ -195,6 +202,21 @@ export function usePoolDatas(

const feeTier = current ? parseInt(current.feeTier) : 0

// check token lists for token uri
const checkSummed = isAddress(address)
let logoURI
switch (activeNetwork) {
case EthereumNetworkInfo:
if (checkSummed) {
logoURI = tokensFromLists?.[1]?.[checkSummed]?.token?.logoURI
}
break
default:
break
}

console.log(logoURI)

if (current) {
accum[address] = {
address,
Expand Down
52 changes: 33 additions & 19 deletions src/state/lists/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ export class WrappedTokenInfo extends Token {
}
}

export type TokenAddressMap = Readonly<
{ [chainId in ChainId | number]: Readonly<{ [tokenAddress: string]: { token: WrappedTokenInfo; list: TokenList } }> }
>
export type TokenAddressMap = Readonly<{
[chainId: number]: Readonly<{ [tokenAddress: string]: { token: WrappedTokenInfo; list: TokenList } }>
}>

type Mutable<T> = {
-readonly [P in keyof T]: Mutable<T[P]>
}
/**
* An empty result, useful as a default.
*/
Expand Down Expand Up @@ -97,23 +100,36 @@ export function useAllLists(): {
return useSelector<AppState, AppState['lists']['byUrl']>((state) => state.lists.byUrl)
}

function combineMaps(map1: TokenAddressMap, map2: TokenAddressMap): TokenAddressMap {
return {
[ChainId.MAINNET]: { ...map1[ChainId.MAINNET], ...map2[ChainId.MAINNET] },
[ChainId.RINKEBY]: { ...map1[ChainId.RINKEBY], ...map2[ChainId.RINKEBY] },
[ChainId.ROPSTEN]: { ...map1[ChainId.ROPSTEN], ...map2[ChainId.ROPSTEN] },
[ChainId.KOVAN]: { ...map1[ChainId.KOVAN], ...map2[ChainId.KOVAN] },
[ChainId.GÖRLI]: { ...map1[ChainId.GÖRLI], ...map2[ChainId.GÖRLI] },
[10]: { ...map1[10], ...map2[10] },
[42161]: { ...map1[42161], ...map2[42161] },
}
/**
* Combine the tokens in map2 with the tokens on map1, where tokens on map1 take precedence
* @param map1 the base token map
* @param map2 the map of additioanl tokens to add to the base map
*/
export function combineMaps(map1: TokenAddressMap, map2: TokenAddressMap): TokenAddressMap {
const chainIds = Object.keys(
Object.keys(map1)
.concat(Object.keys(map2))
.reduce<{ [chainId: string]: true }>((memo, value) => {
memo[value] = true
return memo
}, {})
).map((id) => parseInt(id))

return chainIds.reduce<Mutable<TokenAddressMap>>((memo, chainId) => {
memo[chainId] = {
...map2[chainId],
// map1 takes precedence
...map1[chainId],
}
return memo
}, {}) as TokenAddressMap
}

// merge tokens contained within lists from urls
function useCombinedTokenMapFromUrls(urls: string[] | undefined): TokenAddressMap {
const lists = useAllLists()
return useMemo(() => {
if (!urls) return EMPTY_LIST
if (!urls) return {}
return (
urls
.slice()
Expand All @@ -123,17 +139,15 @@ function useCombinedTokenMapFromUrls(urls: string[] | undefined): TokenAddressMa
const current = lists[currentUrl]?.current
if (!current) return allTokens
try {
const newTokens = Object.assign(listToTokenMap(current))
return combineMaps(allTokens, newTokens)
return combineMaps(allTokens, listToTokenMap(current))
} catch (error) {
console.error('Could not show token list due to error', error)
return allTokens
}
}, EMPTY_LIST)
}, {})
)
}, [lists, urls])
}

// filter out unsupported lists
export function useActiveListUrls(): string[] | undefined {
return useSelector<AppState, AppState['lists']['activeListUrls']>((state) => state.lists.activeListUrls)?.filter(
Expand All @@ -151,7 +165,7 @@ export function useInactiveListUrls(): string[] {
export function useCombinedActiveList(): TokenAddressMap {
const activeListUrls = useActiveListUrls()
const activeTokens = useCombinedTokenMapFromUrls(activeListUrls)
return combineMaps(activeTokens, TRANSFORMED_DEFAULT_TOKEN_LIST)
return useMemo(() => combineMaps(activeTokens, TRANSFORMED_DEFAULT_TOKEN_LIST), [activeTokens])
}

// all tokens from inactive lists
Expand Down