This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
2624/add cow to swap modal #2647
Closed
+68
−1
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/custom/components/SearchModal/CurrencySearch/sorting.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't this exist somewhere already? or did u just move this and mod it a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It exists, this is mod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it's worth making a const list of tokens we'd like to prioritise listed in order of importance
e.g
const PRIORITISED_TOKENS = [COW, GNO]
where lower index = higher prioThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of line 33
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated 👍