Skip to content

Commit

Permalink
feat: Add BNB Chain support (Uniswap#279)
Browse files Browse the repository at this point in the history
* add bsc

* add etherscan link

* fix: 'bsc' has been renamed BNB Chain (Uniswap#280)

* fix: timestamp issue (Uniswap#282)

* fix timestamp issue

* revert block issue

* update block numbers

* update chain name

* include bnb chain token icons

* use plasma list instead of coingecko

* use correct logos on all networks

---------

Co-authored-by: Jordan Frankfurt <[email protected]>
  • Loading branch information
ianlapham and JFrankfurt authored Mar 15, 2023
1 parent 87eb4d7 commit cd375aa
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 183 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"@data-ui/histogram": "^0.0.84",
"@types/numeral": "^2.0.1",
"@uniswap/default-token-list": "^2.0.0",
"@uniswap/sdk-core": "^3.0.1",
"@uniswap/sdk-core": "^3.2.2",
"@uniswap/v2-sdk": "^3.0.0-alpha.0",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
Expand Down
43 changes: 43 additions & 0 deletions src/apollo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ export const optimismClient = new ApolloClient({
},
})

export const bscClient = new ApolloClient({
uri: 'https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v3-bsc',
cache: new InMemoryCache({
typePolicies: {
Token: {
// Singleton types that have no identifying field can use an empty
// array for their keyFields.
keyFields: false,
},
Pool: {
// Singleton types that have no identifying field can use an empty
// array for their keyFields.
keyFields: false,
},
},
}),
queryDeduplication: true,
defaultOptions: {
watchQuery: {
fetchPolicy: 'no-cache',
},
query: {
fetchPolicy: 'no-cache',
errorPolicy: 'all',
},
},
})

export const bscBlockClient = new ApolloClient({
uri: 'https://api.thegraph.com/subgraphs/name/wombat-exchange/bnb-chain-block',
cache: new InMemoryCache(),
queryDeduplication: true,
defaultOptions: {
watchQuery: {
fetchPolicy: 'cache-first',
},
query: {
fetchPolicy: 'cache-first',
errorPolicy: 'all',
},
},
})

export const optimismBlockClient = new ApolloClient({
uri: 'https://api.thegraph.com/subgraphs/name/ianlapham/uni-testing-subgraph',
cache: new InMemoryCache(),
Expand Down
20 changes: 20 additions & 0 deletions src/assets/images/bnb-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 43 additions & 4 deletions src/components/CurrencyLogo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@ import useHttpLocations from 'hooks/useHttpLocations'
import { useActiveNetworkVersion } from 'state/application/hooks'
import { OptimismNetworkInfo } from 'constants/networks'
import EthereumLogo from '../../assets/images/ethereum-logo.png'
import { SupportedChainId } from '@uniswap/sdk-core'

export const getTokenLogoURL = (address: string) => {
return `https://raw.githubusercontent.com/uniswap/assets/master/blockchains/ethereum/assets/${address}/logo.png`
export function chainIdToNetworkName(networkId: SupportedChainId) {
switch (networkId) {
case SupportedChainId.MAINNET:
return 'ethereum'
case SupportedChainId.ARBITRUM_ONE:
return 'arbitrum'
case SupportedChainId.OPTIMISM:
return 'optimism'
case SupportedChainId.POLYGON:
return 'polygon'
case SupportedChainId.BNB:
return 'smartchain'
default:
return 'ethereum'
}
}

const getTokenLogoURL = ({ address, chainId }: { address: string; chainId: SupportedChainId }) => {
return `https://raw.githubusercontent.com/uniswap/assets/master/blockchains/${chainIdToNetworkName(
chainId
)}/assets/${address}/logo.png`
}

const StyledLogo = styled(Logo)<{ size: string }>`
Expand Down Expand Up @@ -43,6 +63,7 @@ export default function CurrencyLogo({
const arbitrumList = useCombinedActiveList()?.[42161]
const polygon = useCombinedActiveList()?.[137]
const celo = useCombinedActiveList()?.[42220]
const bnbList = useCombinedActiveList()?.[SupportedChainId.BNB]

const [activeNetwork] = useActiveNetworkVersion()

Expand All @@ -64,6 +85,14 @@ export default function CurrencyLogo({
}, [checkSummed, arbitrumList])
const uriLocationsArbitrum = useHttpLocations(arbitrumURI)

const BNBURI = useMemo(() => {
if (checkSummed && bnbList?.[checkSummed]) {
return bnbList?.[checkSummed].token.logoURI
}
return undefined
}, [checkSummed, bnbList])
const uriLocationsBNB = useHttpLocations(BNBURI)

const polygonURI = useMemo(() => {
if (checkSummed && polygon?.[checkSummed]) {
return polygon?.[checkSummed].token.logoURI
Expand Down Expand Up @@ -94,16 +123,26 @@ export default function CurrencyLogo({
if (checkSummed && address) {
const override = tempSources[address]
return [
getTokenLogoURL(checkSummed),
getTokenLogoURL({ address: checkSummed, chainId: activeNetwork.chainId }),
...uriLocationsOptimism,
...uriLocationsArbitrum,
...uriLocationsPolygon,
...uriLocationsCelo,
...uriLocationsBNB,
override,
]
}
return []
}, [address, tempSources, uriLocationsArbitrum, uriLocationsOptimism, uriLocationsPolygon, uriLocationsCelo])
}, [
address,
tempSources,
activeNetwork.chainId,
uriLocationsOptimism,
uriLocationsArbitrum,
uriLocationsPolygon,
uriLocationsCelo,
uriLocationsBNB,
])

if (activeNetwork === OptimismNetworkInfo && address === '0x4200000000000000000000000000000000000006') {
return <StyledEthereumLogo src={EthereumLogo} size={size} style={style} {...rest} />
Expand Down
8 changes: 7 additions & 1 deletion src/components/Header/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ const TopBar = () => {
<Polling />
<AutoRow gap="6px">
<RowFixed>
{activeNetwork.id === SupportedNetwork.CELO ? <Item>Celo Price:</Item> : <Item>Eth Price:</Item>}
{activeNetwork.id === SupportedNetwork.CELO ? (
<Item>Celo Price:</Item>
) : activeNetwork.id === SupportedNetwork.BNB ? (
<Item>BNB Price:</Item>
) : (
<Item>Eth Price:</Item>
)}
<Item fontWeight="700" ml="4px">
{formatDollarAmount(ethPrices?.current)}
</Item>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Menu/NetworkDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RowFixed, RowBetween } from 'components/Row'
import { CeloNetworkInfo, PolygonNetworkInfo, SUPPORTED_NETWORK_VERSIONS } from 'constants/networks'
import { BNBNetworkInfo, CeloNetworkInfo, PolygonNetworkInfo, SUPPORTED_NETWORK_VERSIONS } from 'constants/networks'
import useTheme from 'hooks/useTheme'
import React, { useState, useRef } from 'react'
import { ChevronDown } from 'react-feather'
Expand Down Expand Up @@ -98,7 +98,7 @@ export default function NetworkDropdown() {
<TYPE.main fontSize="14px" color={theme.white} ml="8px" mt="-2px" mr="2px" style={{ whiteSpace: 'nowrap' }}>
{activeNetwork.name}
</TYPE.main>
{[EthereumNetworkInfo, PolygonNetworkInfo, CeloNetworkInfo].includes(activeNetwork) ? null : (
{[EthereumNetworkInfo, PolygonNetworkInfo, CeloNetworkInfo, BNBNetworkInfo].includes(activeNetwork) ? null : (
<Badge bgColor={activeNetwork.primaryColor} style={{ margin: '0 4px' }}>
L2
</Badge>
Expand Down
12 changes: 12 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const TOKEN_HIDE: { [key: string]: string[] } = {
[SupportedNetwork.ARBITRUM]: [],
[SupportedNetwork.OPTIMISM]: [],
[SupportedNetwork.CELO]: [],
[SupportedNetwork.BNB]: [],
}

export const POOL_HIDE: { [key: string]: string[] } = {
Expand All @@ -41,7 +42,18 @@ export const POOL_HIDE: { [key: string]: string[] } = {
[SupportedNetwork.ARBITRUM]: [],
[SupportedNetwork.OPTIMISM]: [],
[SupportedNetwork.CELO]: [],
[SupportedNetwork.BNB]: [],
}

export const START_BLOCKS: { [key: string]: number } = {
[SupportedNetwork.ETHEREUM]: 14292820,
[SupportedNetwork.POLYGON]: 25459720,
[SupportedNetwork.ARBITRUM]: 175,
[SupportedNetwork.OPTIMISM]: 10028767,
[SupportedNetwork.CELO]: 13916355,
[SupportedNetwork.BNB]: 26324014,
}

export interface WalletInfo {
connector?: AbstractConnector
name: string
Expand Down
4 changes: 3 additions & 1 deletion src/constants/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ export const ARBITRUM_LIST = 'https://bridge.arbitrum.io/token-list-42161.json'
export const POLYGON_LIST =
'https://unpkg.com/[email protected]/build/quickswap-default.tokenlist.json'
export const CELO_LIST = 'https://celo-org.github.io/celo-token-list/celo.tokenlist.json'
export const BNB_LIST = 'https://raw.githubusercontent.com/plasmadlt/plasma-finance-token-list/master/bnb.json'

// lower index == higher priority for token import
export const DEFAULT_LIST_OF_LISTS: string[] = [
OPTIMISM_LIST,
ARBITRUM_LIST,
POLYGON_LIST,
CELO_LIST,
BNB_LIST,
...UNSUPPORTED_LIST_URLS, // need to load unsupported tokens as well
]

// default lists to be 'active' aka searched across
export const DEFAULT_ACTIVE_LIST_URLS: string[] = [OPTIMISM_LIST, ARBITRUM_LIST, POLYGON_LIST, CELO_LIST]
export const DEFAULT_ACTIVE_LIST_URLS: string[] = [OPTIMISM_LIST, ARBITRUM_LIST, POLYGON_LIST, CELO_LIST, BNB_LIST]
22 changes: 22 additions & 0 deletions src/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import ARBITRUM_LOGO_URL from '../assets/images/arbitrum.svg'
import ETHEREUM_LOGO_URL from '../assets/images/ethereum-logo.png'
import POLYGON_LOGO_URL from '../assets/images/polygon-logo.png'
import CELO_LOGO_URL from '../assets/images/celo-logo.svg'
import BNB_LOGO_URL from '../assets/images/bnb-logo.svg'
import { SupportedChainId } from '@uniswap/sdk-core'

export enum SupportedNetwork {
ETHEREUM,
ARBITRUM,
OPTIMISM,
POLYGON,
CELO,
BNB,
}

export type NetworkInfo = {
chainId: SupportedChainId
id: SupportedNetwork
route: string
name: string
Expand All @@ -24,6 +28,7 @@ export type NetworkInfo = {
}

export const EthereumNetworkInfo: NetworkInfo = {
chainId: SupportedChainId.MAINNET,
id: SupportedNetwork.ETHEREUM,
route: '',
name: 'Ethereum',
Expand All @@ -34,6 +39,7 @@ export const EthereumNetworkInfo: NetworkInfo = {
}

export const ArbitrumNetworkInfo: NetworkInfo = {
chainId: SupportedChainId.ARBITRUM_ONE,
id: SupportedNetwork.ARBITRUM,
route: 'arbitrum',
name: 'Arbitrum',
Expand All @@ -44,6 +50,7 @@ export const ArbitrumNetworkInfo: NetworkInfo = {
}

export const OptimismNetworkInfo: NetworkInfo = {
chainId: SupportedChainId.OPTIMISM,
id: SupportedNetwork.OPTIMISM,
route: 'optimism',
name: 'Optimism',
Expand All @@ -54,6 +61,7 @@ export const OptimismNetworkInfo: NetworkInfo = {
}

export const PolygonNetworkInfo: NetworkInfo = {
chainId: SupportedChainId.POLYGON,
id: SupportedNetwork.POLYGON,
route: 'polygon',
name: 'Polygon',
Expand All @@ -64,6 +72,7 @@ export const PolygonNetworkInfo: NetworkInfo = {
blurb: '',
}
export const CeloNetworkInfo: NetworkInfo = {
chainId: SupportedChainId.CELO,
id: SupportedNetwork.CELO,
route: 'celo',
name: 'Celo',
Expand All @@ -74,10 +83,23 @@ export const CeloNetworkInfo: NetworkInfo = {
blurb: '',
}

export const BNBNetworkInfo: NetworkInfo = {
chainId: SupportedChainId.BNB,
id: SupportedNetwork.BNB,
route: 'bnb',
name: 'BNB Chain',
bgColor: '#F0B90B',
primaryColor: '#F0B90B',
secondaryColor: '#F0B90B',
imageURL: BNB_LOGO_URL,
blurb: '',
}

export const SUPPORTED_NETWORK_VERSIONS: NetworkInfo[] = [
EthereumNetworkInfo,
PolygonNetworkInfo,
OptimismNetworkInfo,
ArbitrumNetworkInfo,
CeloNetworkInfo,
BNBNetworkInfo,
]
7 changes: 6 additions & 1 deletion src/hooks/useBlocksFromTimestamps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gql from 'graphql-tag'
import { useState, useEffect, useMemo } from 'react'
import { splitQuery } from 'utils/queries'
import { START_BLOCKS } from 'constants/index'
import { useActiveNetworkVersion, useClients } from 'state/application/hooks'
import { ApolloClient, NormalizedCacheObject } from '@apollo/client'

Expand Down Expand Up @@ -63,9 +64,13 @@ export function useBlocksFromTimestamps(
const formatted = []
for (const t in networkBlocks) {
if (networkBlocks[t].length > 0) {
const number = networkBlocks[t][0]['number']
const deploymentBlock = START_BLOCKS[activeNetwork.id]
const adjustedNumber = number > deploymentBlock ? number : deploymentBlock

formatted.push({
timestamp: t.split('t')[1],
number: networkBlocks[t][0]['number'],
number: adjustedNumber,
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/state/application/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
polygonClient,
celoClient,
celoBlockClient,
bscClient,
bscBlockClient,
} from 'apollo/client'
import { NetworkInfo, SupportedNetwork } from 'constants/networks'
import { useCallback, useMemo } from 'react'
Expand Down Expand Up @@ -138,6 +140,8 @@ export function useDataClient(): ApolloClient<NormalizedCacheObject> {
return polygonClient
case SupportedNetwork.CELO:
return celoClient
case SupportedNetwork.BNB:
return bscClient
default:
return client
}
Expand All @@ -157,6 +161,8 @@ export function useBlockClient(): ApolloClient<NormalizedCacheObject> {
return polygonBlockClient
case SupportedNetwork.CELO:
return celoBlockClient
case SupportedNetwork.BNB:
return bscBlockClient
default:
return blockClient
}
Expand Down
1 change: 1 addition & 0 deletions src/state/pools/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const initialState: PoolsState = {
[SupportedNetwork.OPTIMISM]: {},
[SupportedNetwork.POLYGON]: {},
[SupportedNetwork.CELO]: {},
[SupportedNetwork.BNB]: {},
},
}

Expand Down
6 changes: 6 additions & 0 deletions src/state/protocol/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export const initialState: ProtocolState = {
transactions: undefined,
lastUpdated: undefined,
},
[SupportedNetwork.BNB]: {
data: undefined,
chartData: undefined,
transactions: undefined,
lastUpdated: undefined,
},
}

export default createReducer(initialState, (builder) =>
Expand Down
Loading

0 comments on commit cd375aa

Please sign in to comment.