Skip to content

Commit

Permalink
feat: special case arb search (#6584)
Browse files Browse the repository at this point in the history
* feat: special case arb search

* fix: check both current and existing token
  • Loading branch information
cartcrom committed May 22, 2023
1 parent b89ee36 commit f3a80c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ export const UNI: { [chainId: number]: Token } = {
[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 } = {
...(WETH9 as Record<SupportedChainId, Token>),
[SupportedChainId.OPTIMISM]: new Token(
Expand Down
28 changes: 20 additions & 8 deletions src/graphql/data/SearchTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WRAPPED_NATIVE_CURRENCY } from 'constants/tokens'
import { ARB, WRAPPED_NATIVE_CURRENCY } from 'constants/tokens'
import gql from 'graphql-tag'
import { useMemo } from 'react'
import invariant from 'tiny-invariant'

import { Chain, SearchTokensQuery, useSearchTokensQuery } from './__generated__/types-and-hooks'
import { chainIdToBackendName } from './util'
Expand Down Expand Up @@ -41,19 +42,30 @@ gql`
}
`

const ARB_ADDRESS = ARB.address.toLowerCase()

export type SearchToken = NonNullable<NonNullable<SearchTokensQuery['searchTokens']>[number]>

function isMoreRevelantToken(current: SearchToken, existing: SearchToken | undefined, searchChain: Chain) {
if (!existing) return true
/* Returns the more relevant cross-chain token based on native status and search chain */
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 )
if (current.standard === 'NATIVE' && (existing.standard !== 'NATIVE' || current.chain === searchChain)) return true
// 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 current

// 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(
searchChain: Chain,
wrappedNativeAddress: string | undefined,
Expand Down Expand Up @@ -87,7 +99,7 @@ export function useSearchTokens(searchQuery: string, chainId: number) {
data?.searchTokens?.forEach((token) => {
if (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(
Expand Down

1 comment on commit f3a80c6

@vercel
Copy link

@vercel vercel bot commented on f3a80c6 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

interface – ./

interface-uniswap.vercel.app
interface-git-main-uniswap.vercel.app

Please sign in to comment.