Commit f3a80c62 authored by cartcrom's avatar cartcrom Committed by GitHub

feat: special case arb search (#6584)

* feat: special case arb search

* fix: check both current and existing token
parent b89ee364
...@@ -359,6 +359,14 @@ export const UNI: { [chainId: number]: Token } = { ...@@ -359,6 +359,14 @@ export const UNI: { [chainId: number]: Token } = {
[SupportedChainId.GOERLI]: new Token(SupportedChainId.GOERLI, UNI_ADDRESS[5], 18, 'UNI', 'Uniswap'), [SupportedChainId.GOERLI]: new Token(SupportedChainId.GOERLI, UNI_ADDRESS[5], 18, 'UNI', 'Uniswap'),
} }
export const ARB = new Token(
SupportedChainId.ARBITRUM_ONE,
'0x912CE59144191C1204E64559FE8253a0e49E6548',
18,
'ARB',
'Arbitrum'
)
export const WRAPPED_NATIVE_CURRENCY: { [chainId: number]: Token | undefined } = { export const WRAPPED_NATIVE_CURRENCY: { [chainId: number]: Token | undefined } = {
...(WETH9 as Record<SupportedChainId, Token>), ...(WETH9 as Record<SupportedChainId, Token>),
[SupportedChainId.OPTIMISM]: new Token( [SupportedChainId.OPTIMISM]: new Token(
......
import { WRAPPED_NATIVE_CURRENCY } from 'constants/tokens' import { ARB, WRAPPED_NATIVE_CURRENCY } from 'constants/tokens'
import gql from 'graphql-tag' import gql from 'graphql-tag'
import { useMemo } from 'react' import { useMemo } from 'react'
import invariant from 'tiny-invariant'
import { Chain, SearchTokensQuery, useSearchTokensQuery } from './__generated__/types-and-hooks' import { Chain, SearchTokensQuery, useSearchTokensQuery } from './__generated__/types-and-hooks'
import { chainIdToBackendName } from './util' import { chainIdToBackendName } from './util'
...@@ -41,19 +42,30 @@ gql` ...@@ -41,19 +42,30 @@ gql`
} }
` `
const ARB_ADDRESS = ARB.address.toLowerCase()
export type SearchToken = NonNullable<NonNullable<SearchTokensQuery['searchTokens']>[number]> export type SearchToken = NonNullable<NonNullable<SearchTokensQuery['searchTokens']>[number]>
function isMoreRevelantToken(current: SearchToken, existing: SearchToken | undefined, searchChain: Chain) { /* Returns the more relevant cross-chain token based on native status and search chain */
if (!existing) return true function dedupeCrosschainTokens(current: SearchToken, existing: SearchToken | undefined, searchChain: Chain) {
if (!existing) return current
invariant(current.project?.id === existing.project?.id, 'Cannot dedupe tokens within different tokenProjects')
// Special case: always prefer Arbitrum ARB over Mainnet ARB
if (current.address?.toLowerCase() === ARB_ADDRESS) return current
if (existing.address?.toLowerCase() === ARB_ADDRESS) return existing
// Always priotize natives, and if both tokens are native, prefer native on current chain (i.e. Matic on Polygon over Matic on Mainnet ) // Always prioritize natives, and if both tokens are native, prefer native on current chain (i.e. Matic on Polygon over Matic on Mainnet )
if (current.standard === 'NATIVE' && (existing.standard !== 'NATIVE' || current.chain === searchChain)) return true if (current.standard === 'NATIVE' && (existing.standard !== 'NATIVE' || current.chain === searchChain)) return current
// Prefer tokens on the searched chain, otherwise prefer mainnet tokens // Prefer tokens on the searched chain, otherwise prefer mainnet tokens
return current.chain === searchChain || (existing.chain !== searchChain && current.chain === Chain.Ethereum) if (current.chain === searchChain || (existing.chain !== searchChain && current.chain === Chain.Ethereum))
return current
return existing
} }
// Places natives first, wrapped native on current chain next, then sorts by volume /* Places natives first, wrapped native on current chain next, then sorts by volume */
function searchTokenSortFunction( function searchTokenSortFunction(
searchChain: Chain, searchChain: Chain,
wrappedNativeAddress: string | undefined, wrappedNativeAddress: string | undefined,
...@@ -87,7 +99,7 @@ export function useSearchTokens(searchQuery: string, chainId: number) { ...@@ -87,7 +99,7 @@ export function useSearchTokens(searchQuery: string, chainId: number) {
data?.searchTokens?.forEach((token) => { data?.searchTokens?.forEach((token) => {
if (token.project?.id) { if (token.project?.id) {
const existing = selectionMap[token.project.id] const existing = selectionMap[token.project.id]
if (isMoreRevelantToken(token, existing, searchChain)) selectionMap[token.project.id] = token selectionMap[token.project.id] = dedupeCrosschainTokens(token, existing, searchChain)
} }
}) })
return Object.values(selectionMap).sort( return Object.values(selectionMap).sort(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment