Skip to content

Commit

Permalink
Merge branch 'refactor/tokens-lib' of https://github.com/cowprotocol/…
Browse files Browse the repository at this point in the history
…cowswap into refactor/tokens-lib
  • Loading branch information
shoom3301 committed Oct 23, 2023
2 parents 24628e8 + ce75acf commit c913b78
Show file tree
Hide file tree
Showing 36 changed files with 263 additions and 266 deletions.
7 changes: 6 additions & 1 deletion apps/cowswap-frontend/src/modules/tokensList/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Token } from '@uniswap/sdk-core'
import { TokenInfo } from '@uniswap/token-lists'

export class TokenWithLogo extends Token {
static fromToken(token: Token | TokenInfo, logoURI?: string): TokenWithLogo {
return new TokenWithLogo(logoURI, token.chainId, token.address, token.decimals, token.symbol, token.name)
}

constructor(
public logoURI: string | undefined,
public logoURI: string | undefined, // <--- this is the only difference
chainId: number,
address: string,
decimals: number,
Expand Down
5 changes: 5 additions & 0 deletions libs/common-const/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Token } from '@uniswap/sdk-core'
import { TokenInfo } from '@uniswap/token-lists'

export class TokenWithLogo extends Token {
static fromToken(token: Token | TokenInfo, logoURI?: string): TokenWithLogo {
return new TokenWithLogo(logoURI, token.chainId, token.address, token.decimals, token.symbol, token.name)
}

constructor(
public logoURI: string | undefined, // <--- this is the only difference
chainId: number,
Expand Down
48 changes: 16 additions & 32 deletions libs/tokens/src/const/tokensList.json
Original file line number Diff line number Diff line change
@@ -1,93 +1,77 @@
{
"1": [
{
"id": "hc_zMahDEXc_WMKgPAUpK",
"priority": 1,
"enabledByDefault": true,
"url": "https://files.cow.fi/tokens/CowSwap.json"
"source": "https://files.cow.fi/tokens/CowSwap.json"
},
{
"id": "yi4K5DrkWkF3oiWweRjt3",
"priority": 2,
"enabledByDefault": true,
"url": "https://files.cow.fi/tokens/CoinGecko.json"
"source": "https://files.cow.fi/tokens/CoinGecko.json"
},
{
"id": "uJcgDR4A3MLQTrDsO34bA",
"priority": 3,
"url": "https://raw.githubusercontent.com/compound-finance/token-list/master/compound.tokenlist.json"
"source": "https://raw.githubusercontent.com/compound-finance/token-list/master/compound.tokenlist.json"
},
{
"id": "ePv0T2u5t0omW7HjPxGCL",
"priority": 4,
"ensName": "tokenlist.aave.eth"
"source": "tokenlist.aave.eth"
},
{
"id": "vcO-K1VshU97uUeao5p_i",
"priority": 5,
"ensName": "synths.snx.eth"
"source": "synths.snx.eth"
},
{
"id": "pBJ4ZL5jeZ0DcLw4S-_85",
"priority": 6,
"ensName": "wrapped.tokensoft.eth"
"source": "wrapped.tokensoft.eth"
},
{
"id": "4nKiWPc0hM6NgkZgTUnsy",
"priority": 7,
"url": "https://raw.githubusercontent.com/SetProtocol/uniswap-tokenlist/main/set.tokenlist.json"
"source": "https://raw.githubusercontent.com/SetProtocol/uniswap-tokenlist/main/set.tokenlist.json"
},
{
"id": "5anQfdMTp5WyI2jklNvFB",
"priority": 8,
"url": "https://raw.githubusercontent.com/opynfinance/opyn-tokenlist/master/opyn-squeeth-tokenlist.json"
"source": "https://raw.githubusercontent.com/opynfinance/opyn-tokenlist/master/opyn-squeeth-tokenlist.json"
},
{
"id": "YBalvtWm1YVzQoe8J2AJa",
"priority": 9,
"url": "https://app.tryroll.com/tokens.json"
"source": "https://app.tryroll.com/tokens.json"
},
{
"id": "Pl-AdOGqGki43XDf7EoGV",
"priority": 10,
"ensName": "defi.cmc.eth"
"source": "defi.cmc.eth"
},
{
"id": "JXGSqoOJDDY9hsY0Zn-Ue",
"priority": 11,
"ensName": "stablecoin.cmc.eth"
"source": "stablecoin.cmc.eth"
},
{
"id": "bffPi82n3c4zBzAbUQiV7",
"priority": 12,
"ensName": "t2crtokens.eth"
"source": "t2crtokens.eth"
}
],
"5": [
{
"id": "VZ--hetNymtoeZ5QUVhRT",
"priority": 1,
"enabledByDefault": true,
"url": "https://raw.githubusercontent.com/cowprotocol/token-lists/main/src/public/CowSwapGoerli.json"
"source": "https://raw.githubusercontent.com/cowprotocol/token-lists/main/src/public/CowSwapGoerli.json"
},
{
"id": "uJcgDR4A3MLQTrDsO34bA",
"priority": 2,
"url": "https://raw.githubusercontent.com/compound-finance/token-list/master/compound.tokenlist.json"
"source": "https://raw.githubusercontent.com/compound-finance/token-list/master/compound.tokenlist.json"
}
],
"100": [
{
"id": "hc_zMahDEXc_WMKgPAUpK",
"priority": 1,
"enabledByDefault": true,
"url": "https://files.cow.fi/tokens/CowSwap.json"
"source": "https://files.cow.fi/tokens/CowSwap.json"
},
{
"id": "hMVOQpr3d-b-lCpYDDyIM",
"priority": 2,
"enabledByDefault": true,
"url": "https://tokens.honeyswap.org"
"source": "https://tokens.honeyswap.org"
}
]
}
5 changes: 2 additions & 3 deletions libs/tokens/src/hooks/lists/useRemoveList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { removeListAnalytics } from '@cowprotocol/analytics'

import { removeListAtom } from '../../state/tokenLists/tokenListsActionsAtom'
import { ListState } from '../../types'
import { getTokenListSource } from '../../utils/getTokenListSource'

export function useRemoveList() {
const removeList = useSetAtom(removeListAtom)

return (list: ListState) => {
removeList(list.id)
removeListAnalytics('Confirm', getTokenListSource(list.source))
removeList(list.source)
removeListAnalytics('Confirm', list.source)
}
}
23 changes: 5 additions & 18 deletions libs/tokens/src/hooks/lists/useSearchList.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import useSWR, { SWRResponse } from 'swr'
import { fetchTokenList } from '../../services/fetchTokenList'
import { parseENSAddress } from '@cowprotocol/common-utils'
import { useAtomValue } from 'jotai'
import { allListsSourcesAtom, listsStatesMapAtom } from '../../state/tokenLists/tokenListsStateAtom'
import { useMemo } from 'react'
import { getIsTokenListWithUrl } from '../../utils/getIsTokenListWithUrl'
import { ListState } from '../../types'

export type ListSearchResponse =
Expand All @@ -21,31 +19,20 @@ export function useSearchList(input: string | null): ListSearchResponse {
const allTokensLists = useAtomValue(allListsSourcesAtom)
const listsStatesMap = useAtomValue(listsStatesMapAtom)

const listSource = useMemo(() => {
if (!input) return null

const id = 'search'
const isENS = !!parseENSAddress(input)

return isENS ? { id, ensName: input } : { id, url: input }
}, [input])

const existingList = useMemo(() => {
const inputLowerCase = input?.toLowerCase()

const list = allTokensLists.find((list) => {
return getIsTokenListWithUrl(list) ? list.url === inputLowerCase : list.ensName === inputLowerCase
})
const list = allTokensLists.find((list) => list.source === inputLowerCase)

return list ? listsStatesMap[list.id] : undefined
return list ? listsStatesMap[list.source] : undefined
}, [allTokensLists, listsStatesMap, input])

const response = useSWR<ListState | null>(
['useSearchList', listSource, existingList],
['useSearchList', input, existingList],
() => {
if (!listSource || existingList) return null
if (!input || existingList) return null

return fetchTokenList(listSource)
return fetchTokenList({ source: input })
},
{}
)
Expand Down
5 changes: 2 additions & 3 deletions libs/tokens/src/hooks/lists/useToggleList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { useSetAtom } from 'jotai'
import { toggleListAtom } from '../../state/tokenLists/tokenListsActionsAtom'
import { toggleListAnalytics } from '@cowprotocol/analytics'
import { ListState } from '../../types'
import { getTokenListSource } from '../../utils/getTokenListSource'

export function useToggleList() {
const toggleList = useSetAtom(toggleListAtom)

return (list: ListState, enabled: boolean) => {
toggleList(list.id)
toggleListAnalytics(enabled, getTokenListSource(list.source))
toggleList(list.source)
toggleListAnalytics(enabled, list.source)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useCallback } from 'react'
import { useIsUnsupportedToken } from './useIsUnsupportedToken'

type NullishAddress = string | null | undefined

export function useAreUnsupportedTokens() {
const isUnsupportedToken = useIsUnsupportedToken()

return useCallback(
({ sellToken, buyToken }: { sellToken: NullishAddress; buyToken: NullishAddress }) => {
return isUnsupportedToken(sellToken) || isUnsupportedToken(buyToken)
},
[isUnsupportedToken]
)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Currency } from '@uniswap/sdk-core'
import { useIsUnsupportedToken } from './useIsUnsupportedToken'
import { getAddress } from '@cowprotocol/common-utils'
import { useAreUnsupportedTokens } from './useAreUnsupportedTokens'

export function useIsTradeUnsupported(
inputCurrency: Currency | null | undefined,
outputCurrency: Currency | null | undefined
): boolean {
const isUnsupportedToken = useIsUnsupportedToken()
const isInputCurrencyUnsupported = inputCurrency?.isNative ? false : !!isUnsupportedToken(inputCurrency?.address)
const isOutputCurrencyUnsupported = outputCurrency?.isNative ? false : !!isUnsupportedToken(outputCurrency?.address)
type NullishCurrency = Currency | null | undefined

return isInputCurrencyUnsupported || isOutputCurrencyUnsupported
export function useIsTradeUnsupported(inputCurrency: NullishCurrency, outputCurrency: NullishCurrency): boolean {
const areUnsupportedTokens = useAreUnsupportedTokens()

return areUnsupportedTokens({ sellToken: getAddress(inputCurrency), buyToken: getAddress(outputCurrency) })
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { useUnsupportedTokens } from './useUnsupportedTokens'
import { useCallback } from 'react'

export function useIsUnsupportedToken() {
type NullishAddress = string | null | undefined

export function useIsUnsupportedToken(): (address: NullishAddress) => boolean {
const unsupportedTokens = useUnsupportedTokens()

return useCallback(
(address?: string) => {
(address: NullishAddress) => {
const state = address && unsupportedTokens[address.toLowerCase()]

if (state) {
return {
...state,
address,
}
}
return false
return !!state
},
[unsupportedTokens]
)
Expand Down
18 changes: 0 additions & 18 deletions libs/tokens/src/hooks/tokens/unsupported/useIsUnsupportedTokens.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useSetAtom } from 'jotai'
import { removeUnsupportedTokenAtom } from '../../../state/tokens/unsupportedTokensAtom'
import { removeUnsupportedTokensAtom } from '../../../state/tokens/unsupportedTokensAtom'

export function useRemoveUnsupportedToken() {
return useSetAtom(removeUnsupportedTokenAtom)
const removeUnsupportedToken = useSetAtom(removeUnsupportedTokensAtom)

return (tokenAddress: string) => removeUnsupportedToken([tokenAddress])
}
13 changes: 5 additions & 8 deletions libs/tokens/src/hooks/tokens/useSearchNonExistentToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ import { useAtomValue } from 'jotai'
import { useMemo } from 'react'

import { TokenWithLogo } from '@cowprotocol/common-const'
import { isAddress, isTruthy } from '@cowprotocol/common-utils'
import { isTruthy } from '@cowprotocol/common-utils'

import { tokenListsUpdatingAtom } from '../../state/tokenLists/tokenListsStateAtom'
import { useTokensByAddressMap } from './useTokensByAddressMap'
import { useSearchToken } from './useSearchToken'
import { useTokenBySymbolOrAddress } from './useTokenBySymbolOrAddress'

export function useSearchNonExistentToken(tokenAddress: string | null): TokenWithLogo | null {
export function useSearchNonExistentToken(tokenId: string | null): TokenWithLogo | null {
const tokenListsUpdating = useAtomValue(tokenListsUpdatingAtom)
const allTokens = useTokensByAddressMap()

const isNotAddress = !isAddress(tokenAddress)
const existingToken = tokenAddress ? allTokens[tokenAddress.toLowerCase()] : null
const existingToken = useTokenBySymbolOrAddress(tokenId)

const inputTokenToSearch = tokenListsUpdating || existingToken || !tokenAddress || isNotAddress ? null : tokenAddress
const inputTokenToSearch = tokenListsUpdating || existingToken ? null : tokenId

const foundToken = useSearchToken(inputTokenToSearch)

Expand All @@ -24,7 +22,6 @@ export function useSearchNonExistentToken(tokenAddress: string | null): TokenWit

return (
[foundToken.inactiveListsResult, foundToken.externalApiResult, foundToken.blockchainResult]
.filter(isTruthy)
.flat()
.filter(isTruthy)[0] || null
)
Expand Down
Loading

0 comments on commit c913b78

Please sign in to comment.