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

2624/add cow to swap modal #359

Merged
merged 3 commits into from
Apr 4, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import CommonBases from 'components/SearchModal/CommonBases'
import CurrencyList from 'components/SearchModal/CurrencyList'
import { filterTokens, useSortedTokensByQuery } from 'components/SearchModal/filtering'
import ImportRow from 'components/SearchModal/ImportRow'
import { useTokenComparator } from 'components/SearchModal/sorting'
import { useTokenComparator } from './sorting'
import { PaddedColumn, SearchInput, Separator } from 'components/SearchModal/styleds'
import useNetworkName from 'hooks/useNetworkName'
import { ContentWrapper } from '.' //mod
Expand Down
63 changes: 63 additions & 0 deletions src/custom/components/SearchModal/CurrencySearch/sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Currency, CurrencyAmount, Token } from '@uniswap/sdk-core'
import { useMemo } from 'react'

import { useAllTokenBalances } from 'state/wallet/hooks'

const PRIORITISED_TOKENS = ['COW', 'GNO']

// compare two token amounts with highest one coming first
function balanceComparator(balanceA?: CurrencyAmount<Currency>, balanceB?: CurrencyAmount<Currency>) {
if (balanceA && balanceB) {
return balanceA.greaterThan(balanceB) ? -1 : balanceA.equalTo(balanceB) ? 0 : 1
} else if (balanceA && balanceA.greaterThan('0')) {
return -1
} else if (balanceB && balanceB.greaterThan('0')) {
return 1
}
return 0
}

function getTokenComparator(balances: {
[tokenAddress: string]: CurrencyAmount<Currency> | undefined
}): (tokenA: Token, tokenB: Token) => number {
return function sortTokens(tokenA: Token, tokenB: Token): number {
// -1 = a is first
// 1 = b is first

// sort by balances
const balanceA = balances[tokenA.address]
const balanceB = balances[tokenB.address]

const balanceComp = balanceComparator(balanceA, balanceB)
if (balanceComp !== 0) return balanceComp

// Mod: modify tokens list by prioritised list
const indexA = PRIORITISED_TOKENS.indexOf(tokenA.symbol || '')
const indexB = PRIORITISED_TOKENS.indexOf(tokenB.symbol || '')

if (indexA !== -1 && indexB !== -1) {
return indexB < indexA ? 1 : -1
} else if (indexA !== -1 || indexB !== -1) {
return indexB !== -1 ? 1 : -1
}

if (tokenA.symbol && tokenB.symbol) {
// sort by symbol
return tokenA.symbol.toLowerCase() < tokenB.symbol.toLowerCase() ? -1 : 1
} else {
return tokenA.symbol ? -1 : tokenB.symbol ? -1 : 0
}
}
}

export function useTokenComparator(inverted: boolean): (tokenA: Token, tokenB: Token) => number {
const balances = useAllTokenBalances()
const comparator = useMemo(() => getTokenComparator(balances ?? {}), [balances])
return useMemo(() => {
if (inverted) {
return (tokenA: Token, tokenB: Token) => comparator(tokenA, tokenB) * -1
} else {
return comparator
}
}, [inverted, comparator])
}
4 changes: 4 additions & 0 deletions src/custom/constants/routing/routingMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
// WBTC_ARBITRUM_ONE,
// WBTC_OPTIMISM,
WETH9_EXTENDED,
COW,
} from 'constants/tokens'

import { USDC_XDAI, /* USDT_XDAI, */ WBTC_XDAI, WETH_XDAI } from 'utils/xdai/constants'
Expand Down Expand Up @@ -80,6 +81,7 @@ export const COMMON_BASES: ChainCurrencyList = {
[SupportedChainId.MAINNET]: [
// ExtendedEther.onChain(SupportedChainId.MAINNET),
DAI,
COW[SupportedChainId.MAINNET],
USDC,
USDT,
WBTC,
Expand All @@ -92,6 +94,7 @@ export const COMMON_BASES: ChainCurrencyList = {
[SupportedChainId.RINKEBY]: [
// ExtendedEther.onChain(SupportedChainId.RINKEBY),
WETH9_EXTENDED[SupportedChainId.RINKEBY],
COW[SupportedChainId.RINKEBY],
DAI_RINKEBY,
USDC_RINKEBY,
USDT_RINKEBY,
Expand Down Expand Up @@ -120,6 +123,7 @@ export const COMMON_BASES: ChainCurrencyList = {
[SupportedChainId.XDAI]: [
// ExtendedEther.onChain(SupportedChainId.XDA),
USDC_XDAI,
COW[SupportedChainId.XDAI],
/*USDT_XDAI,*/ WBTC_XDAI,
WETH9_EXTENDED[100],
WETH_XDAI,
Expand Down