From 5552b696c8d18817247123760bb22d76427cc46a Mon Sep 17 00:00:00 2001 From: Alexandr Kazachenko Date: Mon, 16 Oct 2023 18:05:56 +0600 Subject: [PATCH] feat: add trust as a fallback to logo urls --- ...colTokenLists.ts => cowprotocolTokenUrl.ts} | 0 libs/common-const/src/index.ts | 2 +- .../common-const/src/nativeAndWrappedTokens.ts | 2 +- libs/common-const/src/tokens.ts | 2 +- libs/tokens/src/utils/getTokenLogoUrls.ts | 18 +++++++++++------- libs/tokens/src/utils/trustTokenLogoUrl.ts | 11 +++++++++++ 6 files changed, 25 insertions(+), 10 deletions(-) rename libs/common-const/src/{cowprotocolTokenLists.ts => cowprotocolTokenUrl.ts} (100%) create mode 100644 libs/tokens/src/utils/trustTokenLogoUrl.ts diff --git a/libs/common-const/src/cowprotocolTokenLists.ts b/libs/common-const/src/cowprotocolTokenUrl.ts similarity index 100% rename from libs/common-const/src/cowprotocolTokenLists.ts rename to libs/common-const/src/cowprotocolTokenUrl.ts diff --git a/libs/common-const/src/index.ts b/libs/common-const/src/index.ts index 972bafef50..756a984fdc 100644 --- a/libs/common-const/src/index.ts +++ b/libs/common-const/src/index.ts @@ -10,5 +10,5 @@ export * from './networks' export * from './routing' export * from './ipfs' export * from './gnosis_chain/hack' -export * from './cowprotocolTokenLists' +export * from './cowprotocolTokenUrl' export * from './nativeAndWrappedTokens' diff --git a/libs/common-const/src/nativeAndWrappedTokens.ts b/libs/common-const/src/nativeAndWrappedTokens.ts index 14a57beb6b..60f8c42c29 100644 --- a/libs/common-const/src/nativeAndWrappedTokens.ts +++ b/libs/common-const/src/nativeAndWrappedTokens.ts @@ -1,7 +1,7 @@ import { SupportedChainId, SupportedChainId as ChainId } from '@cowprotocol/cow-sdk' import { TokenWithLogo } from './types' import { WETH9 } from '@uniswap/sdk-core' -import { cowprotocolTokenUrl } from './cowprotocolTokenLists' +import { cowprotocolTokenUrl } from './cowprotocolTokenUrl' import wxDaiLogo from '@cowprotocol/assets/cow-swap/wxdai.png' diff --git a/libs/common-const/src/tokens.ts b/libs/common-const/src/tokens.ts index f4042c88b3..c77fbdf21f 100644 --- a/libs/common-const/src/tokens.ts +++ b/libs/common-const/src/tokens.ts @@ -5,7 +5,7 @@ import vCowLogo from '@cowprotocol/assets/cow-swap/vCOW.png' import { COW_CONTRACT_ADDRESS, V_COW_CONTRACT_ADDRESS } from './common' import { TokenWithLogo } from './types' -import { cowprotocolTokenUrl } from './cowprotocolTokenLists' +import { cowprotocolTokenUrl } from './cowprotocolTokenUrl' import { WETH_MAINNET } from './nativeAndWrappedTokens' // Mainnet diff --git a/libs/tokens/src/utils/getTokenLogoUrls.ts b/libs/tokens/src/utils/getTokenLogoUrls.ts index 226ce3c463..52e3874bc2 100644 --- a/libs/tokens/src/utils/getTokenLogoUrls.ts +++ b/libs/tokens/src/utils/getTokenLogoUrls.ts @@ -1,15 +1,10 @@ import { cowprotocolTokenUrl, TokenWithLogo } from '@cowprotocol/common-const' import { uriToHttp } from '@cowprotocol/common-utils' import { SupportedChainId } from '@cowprotocol/cow-sdk' +import { trustTokenLogoUrl } from './trustTokenLogoUrl' export function getTokenLogoUrls(token: TokenWithLogo | undefined): string[] { - const fallbackUrls = token?.address - ? [ - cowprotocolTokenUrl(token.address, token.chainId as SupportedChainId), - cowprotocolTokenUrl(token.address.toLowerCase(), token.chainId as SupportedChainId), - cowprotocolTokenUrl(token.address.toLowerCase(), SupportedChainId.MAINNET), - ] - : [] + const fallbackUrls = token?.address ? getTokenLogoFallbacks(token.address, token.chainId as SupportedChainId) : [] if (!token?.logoURI) { return fallbackUrls @@ -23,3 +18,12 @@ export function getTokenLogoUrls(token: TokenWithLogo | undefined): string[] { return urls } + +function getTokenLogoFallbacks(address: string, chainId: SupportedChainId): string[] { + return [ + cowprotocolTokenUrl(address, chainId), + cowprotocolTokenUrl(address.toLowerCase(), chainId), + cowprotocolTokenUrl(address.toLowerCase(), SupportedChainId.MAINNET), + trustTokenLogoUrl(address, chainId), + ] +} diff --git a/libs/tokens/src/utils/trustTokenLogoUrl.ts b/libs/tokens/src/utils/trustTokenLogoUrl.ts new file mode 100644 index 0000000000..643063f381 --- /dev/null +++ b/libs/tokens/src/utils/trustTokenLogoUrl.ts @@ -0,0 +1,11 @@ +import { SupportedChainId } from '@cowprotocol/cow-sdk' + +const chainIdToName: Record = { + [SupportedChainId.MAINNET]: 'ethereum', + [SupportedChainId.GNOSIS_CHAIN]: 'xdai', + [SupportedChainId.GOERLI]: 'ethereum', +} + +export function trustTokenLogoUrl(address: string, chainId: SupportedChainId): string { + return `https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/${chainIdToName[chainId]}/assets/${address}/logo.png` +}