-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add liquidity sources setting/panel in Settings #1130
Merged
viet-nv
merged 1 commit into
feature/modern-theme
from
feature/display-integrated-dexes
Jul 18, 2022
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React from 'react' | ||
import { Search } from 'react-feather' | ||
import styled from 'styled-components' | ||
import { t } from '@lingui/macro' | ||
|
||
type Props = { | ||
text: string | ||
setText: (txt: string) => void | ||
} | ||
|
||
const SearchBarWrapper = styled.div` | ||
width: 100%; | ||
height: 36px; | ||
position: relative; | ||
` | ||
|
||
const Input = styled.input` | ||
width: 100%; | ||
height: 100%; | ||
|
||
padding: 8px 36px 8px 12px; | ||
|
||
background: ${({ theme }) => theme.buttonBlack}; | ||
border: 0px; | ||
border-radius: 40px; | ||
color: inherit; | ||
outline: none; | ||
|
||
font-size: 14px; | ||
font-weight: 500; | ||
line-height: 20px; | ||
` | ||
|
||
const IconWrapper = styled.div` | ||
position: absolute; | ||
right: 12px; | ||
top: 0; | ||
|
||
width: 18px; | ||
height: 100%; | ||
|
||
display: flex; | ||
align-items: center; | ||
|
||
color: ${({ theme }) => theme.subText}; | ||
` | ||
|
||
const SearchBar: React.FC<Props> = ({ text, setText }) => { | ||
return ( | ||
<SearchBarWrapper> | ||
<Input value={text} onChange={e => setText(e.target.value)} placeholder={t`Search for a liquidity source`} /> | ||
<IconWrapper> | ||
<Search /> | ||
</IconWrapper> | ||
</SearchBarWrapper> | ||
) | ||
} | ||
|
||
export default SearchBar |
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,134 @@ | ||
import React, { useState } from 'react' | ||
import { Flex, Box } from 'rebass' | ||
import { ArrowLeft } from 'react-feather' | ||
import styled from 'styled-components' | ||
import { t } from '@lingui/macro' | ||
|
||
import { useActiveWeb3React } from 'hooks' | ||
import useDebounce from 'hooks/useDebounce' | ||
import useAggregatorStats from 'hooks/useAggregatorStats' | ||
import { dexListConfig } from 'constants/dexes' | ||
|
||
import SearchBar from './SearchBar' | ||
|
||
type Props = { | ||
onBack: () => void | ||
} | ||
|
||
const BackIconWrapper = styled(ArrowLeft)` | ||
height: 20px; | ||
width: 20px; | ||
margin-right: 10px; | ||
cursor: pointer; | ||
path { | ||
stroke: ${({ theme }) => theme.text} !important; | ||
} | ||
` | ||
|
||
const BackText = styled.span` | ||
font-size: 18px; | ||
font-weight: 500; | ||
color: ${({ theme }) => theme.text}; | ||
` | ||
|
||
const SourceList = styled.div` | ||
width: 100%; | ||
height: 300px; | ||
max-height: 300px; | ||
overflow: scroll; | ||
|
||
display: flex; | ||
flex-direction: column; | ||
row-gap: 24px; | ||
` | ||
|
||
const Source = styled.div` | ||
width: 100%; | ||
height: 32px; | ||
|
||
display: flex; | ||
align-items: center; | ||
column-gap: 16px; | ||
` | ||
|
||
const ImageWrapper = styled.div` | ||
width: 32px; | ||
height: 32px; | ||
|
||
display: flex; | ||
align-items: center; | ||
|
||
img { | ||
width: 100%; | ||
height: auto; | ||
} | ||
` | ||
|
||
const SourceName = styled.span` | ||
font-size: 14px; | ||
font-weight: 400; | ||
line-height: 20px; | ||
color: ${({ theme }) => theme.text}; | ||
` | ||
|
||
const LiquiditySourcesPanel: React.FC<Props> = ({ onBack }) => { | ||
const { chainId } = useActiveWeb3React() | ||
const [searchText, setSearchText] = useState('') | ||
const debouncedSearchText = useDebounce(searchText.toLowerCase(), 200) | ||
|
||
const { data, error } = useAggregatorStats(chainId) | ||
if (error || !data) { | ||
onBack() | ||
return null | ||
} | ||
|
||
const dexIDs = Object.keys(data.pools) | ||
if (dexIDs.length === 0) { | ||
onBack() | ||
return null | ||
} | ||
|
||
const visibleDEXes = dexIDs | ||
.map(id => dexListConfig[id]) | ||
.filter(Boolean) | ||
.filter(({ name }) => name.toLowerCase().includes(debouncedSearchText)) | ||
|
||
return ( | ||
<Box width="100%"> | ||
<Flex | ||
width={'100%'} | ||
flexDirection={'column'} | ||
sx={{ | ||
rowGap: '20px', | ||
}} | ||
> | ||
<Flex | ||
alignItems="center" | ||
sx={{ | ||
// this is to make the arrow stay exactly where it stays in Swap panel | ||
marginTop: '5px', | ||
}} | ||
> | ||
<BackIconWrapper onClick={onBack}></BackIconWrapper> | ||
<BackText>{t`Liquidity Sources`}</BackText> | ||
</Flex> | ||
|
||
<SearchBar text={searchText} setText={setSearchText} /> | ||
|
||
<SourceList> | ||
{visibleDEXes.map(({ name, icon }) => ( | ||
<Source key={name}> | ||
<ImageWrapper> | ||
<img src={icon} /> | ||
</ImageWrapper> | ||
|
||
<SourceName>{name}</SourceName> | ||
</Source> | ||
))} | ||
</SourceList> | ||
</Flex> | ||
</Box> | ||
) | ||
} | ||
|
||
export default LiquiditySourcesPanel |
73 changes: 73 additions & 0 deletions
73
src/components/swapv2/SwapSettingsPanel/LiquiditySourcesSetting.tsx
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,73 @@ | ||
import React from 'react' | ||
import styled from 'styled-components' | ||
import { isMobile } from 'react-device-detect' | ||
import { t, Trans } from '@lingui/macro' | ||
import { ChevronRight } from 'react-feather' | ||
import { Flex } from 'rebass' | ||
|
||
import QuestionHelper from 'components/QuestionHelper' | ||
import useAggregatorStats from 'hooks/useAggregatorStats' | ||
import { useActiveWeb3React } from 'hooks' | ||
|
||
type Props = { | ||
onClick: () => void | ||
} | ||
|
||
const SettingLabel = styled.span` | ||
font-size: ${isMobile ? '14px' : '12px'}; | ||
color: ${({ theme }) => theme.text}; | ||
font-weight: 400; | ||
line-height: 16px; | ||
` | ||
|
||
const Group = styled.div` | ||
display: flex; | ||
align-items: center; | ||
color: ${({ theme }) => theme.subText}; | ||
font-size: ${isMobile ? '14px' : '12px'}; | ||
` | ||
|
||
const NumberOfSources = styled.span` | ||
color: ${({ theme }) => theme.text}; | ||
font-weight: 400; | ||
line-height: 16px; | ||
` | ||
|
||
const LiquiditySourcesSetting: React.FC<Props> = ({ onClick }) => { | ||
const { chainId } = useActiveWeb3React() | ||
const { data, error } = useAggregatorStats(chainId) | ||
|
||
if (error || !data) { | ||
return null | ||
} | ||
|
||
const numberOfDEXes = Object.keys(data.pools).length | ||
if (numberOfDEXes === 0) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Flex | ||
justifyContent="space-between" | ||
alignItems="center" | ||
sx={{ | ||
cursor: 'pointer', | ||
}} | ||
onClick={onClick} | ||
> | ||
<Group> | ||
<SettingLabel> | ||
<Trans>Liquidity Sources</Trans> | ||
</SettingLabel> | ||
<QuestionHelper text={t`Your trade is routed through one or more of these liquidity sources`} /> | ||
</Group> | ||
|
||
<Group> | ||
<NumberOfSources>{numberOfDEXes}</NumberOfSources> | ||
<ChevronRight /> | ||
</Group> | ||
</Flex> | ||
) | ||
} | ||
|
||
export default LiquiditySourcesSetting |
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
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
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,51 @@ | ||
import useSWR from 'swr' | ||
import { ChainId } from '@kyberswap/ks-sdk-core' | ||
|
||
import { NETWORKS_INFO } from 'constants/networks' | ||
|
||
type Response = { | ||
pools: Record< | ||
string, | ||
{ | ||
poolSize: number | ||
tvl: number | ||
tokenSize: number | ||
} | ||
> | ||
} | ||
|
||
// It's recommended to use NETWORKS_INFO[chainId].route, | ||
// but very unfortunately that BE uses `bsc` instead of `bnb` | ||
const chainIdMapping: Partial<Record<ChainId, string>> = { | ||
[ChainId.BSCMAINNET]: 'bsc', | ||
} | ||
|
||
const useAggregatorStats = (chainId?: ChainId) => { | ||
const chainString = chainId ? chainIdMapping[chainId] || NETWORKS_INFO[chainId].route : '' | ||
|
||
return useSWR<Response>(`${process.env.REACT_APP_AGGREGATOR_API}/${chainString}/stats`, async (url: string) => { | ||
if (!chainId || !chainString) { | ||
const err = `chain (${chainId}) is not supported` | ||
console.error(err) | ||
throw err | ||
} | ||
|
||
const response = await fetch(url) | ||
if (response.ok) { | ||
const data = await response.json() | ||
if (data && data.pools) { | ||
return data | ||
} | ||
|
||
const err = `no pools found on ${chainString}` | ||
console.error(err) | ||
throw err | ||
} | ||
|
||
const err = `fetching stats on ${chainString} failed` | ||
console.error(err) | ||
throw err | ||
}) | ||
} | ||
|
||
export default useAggregatorStats |
Oops, something went wrong.
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.
you can use NETWORKS_INFO variable
NETWORKS_INFO[chainId].route
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.
Thanks. They are all the same, except for BSC. I need
bsc
instead ofbnb
(as in its route), so I decided to manually do mapping like this.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.
can we deal with backend update bsc to bnb =)) ?
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.
Ok let me ask
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.
BE refused this change request
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.
Ok this seems better
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.
thank @nguyenhoaidanh. as long as it's not changed frequently, it's OK
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.
I think it never changes :D
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's more than reusable. If you have a deep search on
NETWORK_INFOS
, there are fields only use once but still lie there.So why would we put it there? It's the answers for the situation that everytime we think about a piece of data that different accross networks, we first think it must stay in
NETWORK_INFOS
.Also, might you didn't care about maintainability, when we add a new chain, everything lie in one place is much more easily to find than many piece of data lie everywhere in the repository.
So in your case, what if we gonna add new chain like Celo for example. I fill up every infomation in
constants/networks/celo.ts
, but I missed this object, and this object dont shown up with error. So no one gonna known that I have missed this obj.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.
Valid point. It's bad 👍