Skip to content

Commit

Permalink
feat: bnb chain support (#532)
Browse files Browse the repository at this point in the history
* feat: bnb chain changes

* update

* update

* fix: update suggested tokens

* fix: name of wbnb

* fix: rename bsc to bnb where possible
  • Loading branch information
just-toby authored Mar 7, 2023
1 parent ee28857 commit 27c5e7a
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
"@types/wcag-contrast": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"@uniswap/default-token-list": "^4.1.0",
"@uniswap/default-token-list": "^9.0.0",
"@uniswap/v2-core": "1.0.0",
"@uniswap/v3-core": "1.0.0",
"@uniswap/v3-periphery": "^1.1.1",
Expand Down
19 changes: 19 additions & 0 deletions src/assets/svg/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.
2 changes: 1 addition & 1 deletion src/components/Logo/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function useLogo(currency: LogoTableInput | undefined) {

const invalidateSrc = useCallback(() => {
const nextSrc = entry?.invalidateSrc()
nextSrc && setSrc(nextSrc.getUri())
setSrc(nextSrc?.getUri())
}, [entry])

return { src, invalidateSrc }
Expand Down
7 changes: 6 additions & 1 deletion src/components/Logo/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { SupportedChainId } from 'constants/chains'
import { isAddress } from 'utils'

import EthereumLogo from '../../assets/images/ethereum-logo.png'
import BnbLogo from '../../assets/svg/bnb-logo.svg'
import CeloLogo from '../../assets/svg/celo_logo.svg'
import MaticLogo from '../../assets/svg/matic-token-icon.svg'
import { LogoTableInput } from './LogoTable'

type Network = 'ethereum' | 'arbitrum' | 'optimism' | 'polygon' | 'celo'
type Network = 'ethereum' | 'arbitrum' | 'optimism' | 'polygon' | 'celo' | 'binance'

function chainIdToNetworkName(networkId: SupportedChainId): Network | undefined {
switch (networkId) {
Expand All @@ -20,6 +21,8 @@ function chainIdToNetworkName(networkId: SupportedChainId): Network | undefined
return 'polygon'
case SupportedChainId.CELO:
return 'celo'
case SupportedChainId.BNB:
return 'binance'
default:
return 'ethereum'
}
Expand All @@ -46,6 +49,8 @@ export function getNativeLogoURI(chainId: SupportedChainId = SupportedChainId.MA
case SupportedChainId.CELO:
case SupportedChainId.CELO_ALFAJORES:
return CeloLogo
case SupportedChainId.BNB:
return BnbLogo
default:
return EthereumLogo
}
Expand Down
14 changes: 14 additions & 0 deletions src/constants/chainInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ethereumLogoUrl from 'assets/images/ethereum-logo.png'
import arbitrumLogoUrl from 'assets/svg/arbitrum_logo.svg'
import bnbLogo from 'assets/svg/bnb-logo.svg'
import celoLogo from 'assets/svg/celo_logo.svg'
import optimismLogoUrl from 'assets/svg/optimism_logo.svg'
import polygonMaticLogo from 'assets/svg/polygon-matic-logo.svg'
Expand Down Expand Up @@ -223,6 +224,19 @@ const CHAIN_INFO: ChainInfoMap = {
color: '#35D07F',
backgroundColor: '#34d07f1f',
},
[SupportedChainId.BNB]: {
networkType: NetworkType.L1,
blockWaitMsBeforeWarning: ms`10m`,
bridge: 'https://cbridge.celer.network/1/56',
docs: 'https://docs.bnbchain.org/',
explorer: 'https://bscscan.com/',
infoLink: 'https://info.uniswap.org/#/bnb/',
label: 'BNB Chain',
logoUrl: bnbLogo,
nativeCurrency: { name: 'BNB', symbol: 'BNB', decimals: 18 },
color: '#F0B90B',
backgroundColor: '#F0B90B',
},
}

export function getChainInfo(chainId: SupportedL1ChainId): L1ChainInfo
Expand Down
5 changes: 5 additions & 0 deletions src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export enum SupportedChainId {

CELO = 42220,
CELO_ALFAJORES = 44787,

BNB = 56,
}

export enum ChainName {
Expand All @@ -35,6 +37,7 @@ export enum ChainName {
POLYGON_MUMBAI = 'polygon-mumbai',
CELO = 'celo',
CELO_ALFAJORES = 'celo-alfajores',
BNB = 'bnb',
}

export const CHAIN_NAMES_TO_IDS: { [chainName: string]: SupportedChainId } = {
Expand All @@ -51,6 +54,7 @@ export const CHAIN_NAMES_TO_IDS: { [chainName: string]: SupportedChainId } = {
[ChainName.OPTIMISM_GOERLI]: SupportedChainId.OPTIMISM_GOERLI,
[ChainName.CELO]: SupportedChainId.CELO,
[ChainName.CELO_ALFAJORES]: SupportedChainId.CELO_ALFAJORES,
[ChainName.BNB]: SupportedChainId.BNB,
}

/**
Expand All @@ -66,6 +70,7 @@ export const SUPPORTED_GAS_ESTIMATE_CHAIN_IDS = [
SupportedChainId.OPTIMISM,
SupportedChainId.ARBITRUM_ONE,
SupportedChainId.CELO,
SupportedChainId.BNB,
]

/**
Expand Down
12 changes: 12 additions & 0 deletions src/constants/jsonRpcEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ export const JSON_RPC_FALLBACK_ENDPOINTS: Record<SupportedChainId, string[]> = {
// "Safe" URLs
'https://alfajores-forno.celo-testnet.org',
],
[SupportedChainId.BNB]: [
// "Safe" URLs
'https://endpoints.omniatech.io/v1/bsc/mainnet/public',
'https://bsc-mainnet.gateway.pokt.network/v1/lb/6136201a7bad1500343e248d',
'https://1rpc.io/bnb',
'https://bsc-dataseed3.binance.org',
'https://bsc-dataseed2.defibit.io',
'https://bsc-dataseed1.ninicoin.io',
'https://binance.nodereal.io',
'https://bsc-dataseed4.defibit.io',
'https://rpc.ankr.com/bsc',
],
}
8 changes: 8 additions & 0 deletions src/constants/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import {
sETH2,
SWISE,
TRIBE,
USDC_BNB_CHAIN,
USDC_MAINNET,
USDC_POLYGON,
USDT,
USDT_ARBITRUM_ONE,
USDT_BNB_CHAIN,
USDT_OPTIMISM,
USDT_POLYGON,
WBTC,
Expand Down Expand Up @@ -84,6 +86,12 @@ export const BASES_TO_CHECK_TRADES_AGAINST: ChainTokenList = {
PORTAL_USDC_CELO,
PORTAL_ETH_CELO,
],
[SupportedChainId.BNB]: [
nativeOnChain(SupportedChainId.BNB),
USDC_BNB_CHAIN,
USDT_BNB_CHAIN,
...WRAPPED_NATIVE_CURRENCIES_ONLY[SupportedChainId.BNB],
],
}
export const ADDITIONAL_BASES: { [chainId: number]: { [tokenAddress: string]: Token[] } } = {
[SupportedChainId.MAINNET]: {
Expand Down
102 changes: 102 additions & 0 deletions src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ export const DAI_OPTIMISM = new Token(
'DAI',
'Dai stable coin'
)
export const USDC_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
18,
'USDC',
'USDC'
)
export const USDC: { [chainId in SupportedChainId]: Token } = {
[SupportedChainId.MAINNET]: USDC_MAINNET,
[SupportedChainId.ARBITRUM_ONE]: USDC_ARBITRUM,
Expand All @@ -137,6 +144,7 @@ export const USDC: { [chainId in SupportedChainId]: Token } = {
[SupportedChainId.RINKEBY]: USDC_RINKEBY,
[SupportedChainId.KOVAN]: USDC_KOVAN,
[SupportedChainId.ROPSTEN]: USDC_ROPSTEN,
[SupportedChainId.BNB]: USDC_BNB_CHAIN,
}
export const DAI_POLYGON = new Token(
SupportedChainId.POLYGON,
Expand Down Expand Up @@ -335,6 +343,91 @@ export const CEUR_CELO_ALFAJORES = new Token(
'CEUR',
'Celo Euro Stablecoin'
)
export const USDT_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x55d398326f99059fF775485246999027B3197955',
18,
'USDT',
'USDT'
)

export const ETH_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x2170Ed0880ac9A755fd29B2688956BD959F933F8',
18,
'ETH',
'Ethereum'
)

export const MATIC_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0xCC42724C6683B7E57334c4E856f4c9965ED682bD',
18,
'MATIC',
'Matic'
)

export const FRAX_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x90C97F71E18723b0Cf0dfa30ee176Ab653E89F40',
18,
'FRAX',
'FRAX'
)

export const BTC_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c',
18,
'BTCB',
'BTCB'
)

export const CAKE_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82',
18,
'CAKE',
'Cake'
)

export const BUSD_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56',
18,
'BUSD',
'BUSD'
)

export const DAI_BNB_CHAIN = new Token(
SupportedChainId.BNB,
'0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3',
18,
'DAI',
'DAI'
)

function isBnbChain(chainId: number): chainId is SupportedChainId.BNB {
return chainId === SupportedChainId.BNB
}

class BnbChainNativeCurrency extends NativeCurrency {
equals(other: Currency): boolean {
return other.isNative && other.chainId === this.chainId
}

get wrapped(): Token {
if (!isBnbChain(this.chainId)) throw new Error('Not BNB Chain')
const wrapped = WRAPPED_NATIVE_CURRENCY[this.chainId]
invariant(wrapped instanceof Token)
return wrapped
}

public constructor(chainId: number) {
if (!isBnbChain(chainId)) throw new Error('Not BNB Chain')
super(chainId, 18, 'BNB', 'BNB')
}
}

export const UNI: { [chainId: number]: Token } = {
[SupportedChainId.MAINNET]: new Token(SupportedChainId.MAINNET, UNI_ADDRESS[1], 18, 'UNI', 'Uniswap'),
Expand Down Expand Up @@ -390,6 +483,13 @@ export const WRAPPED_NATIVE_CURRENCY: { [chainId: number]: Token | undefined } =
'WMATIC',
'Wrapped MATIC'
),
[SupportedChainId.BNB]: new Token(
SupportedChainId.BNB,
'0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
18,
'WBNB',
'Wrapped BNB'
),
}

export function isCelo(chainId: number): chainId is SupportedChainId.CELO | SupportedChainId.CELO_ALFAJORES {
Expand Down Expand Up @@ -451,6 +551,8 @@ export function nativeOnChain(chainId: number): NativeCurrency | Token {
nativeCurrency = new MaticNativeCurrency(chainId)
} else if (isCelo(chainId)) {
nativeCurrency = getCeloNativeCurrency(chainId)
} else if (isBnbChain(chainId)) {
nativeCurrency = new BnbChainNativeCurrency(chainId)
} else {
nativeCurrency = ExtendedEther.onChain(chainId)
}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/web3/useJsonRpcUrlsMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function toJsonRpcMap<T>(getChainConnections: (chainId: SupportedChainId) => T):
[SupportedChainId.OPTIMISM_GOERLI]: getChainConnections(SupportedChainId.OPTIMISM_GOERLI),
[SupportedChainId.CELO]: getChainConnections(SupportedChainId.CELO),
[SupportedChainId.CELO_ALFAJORES]: getChainConnections(SupportedChainId.CELO_ALFAJORES),
[SupportedChainId.BNB]: getChainConnections(SupportedChainId.BNB),
}
}

Expand Down
1 change: 0 additions & 1 deletion src/state/routing/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export const routing = createApi({
protocols,
})
if (typeof quote === 'string') return { data: quote as TradeQuoteResult }

const tradeResult = transformQuoteToTradeResult(args, quote)
return { data: tradeResult }
} catch (error: any) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/getExplorerLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ describe('#getExplorerLink', () => {
it('enum', () => {
expect(getExplorerLink(4, 'abc', ExplorerDataType.ADDRESS)).toEqual('https://rinkeby.etherscan.io/address/abc')
})
it('bnb chain', () => {
expect(getExplorerLink(56, 'abc', ExplorerDataType.ADDRESS)).toEqual('https://bscscan.com/address/abc')
})
})
1 change: 1 addition & 0 deletions src/utils/getExplorerLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ETHERSCAN_PREFIXES: { [chainId: number]: string } = {
[SupportedChainId.POLYGON]: 'https://polygonscan.com',
[SupportedChainId.CELO]: 'https://celoscan.io',
[SupportedChainId.CELO_ALFAJORES]: 'https://alfajores.celoscan.io',
[SupportedChainId.BNB]: 'https://bscscan.com',
}

export enum ExplorerDataType {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3528,10 +3528,10 @@
resolved "https://registry.yarnpkg.com/@uniswap/default-token-list/-/default-token-list-2.2.0.tgz#d85a5c2520f57f4920bd989dfc9f01e1b701a567"
integrity sha512-vFPWoGzDjHP4i2l7yLaober/lZMmzOZXXirVF8XNyfNzRxgmYCWKO6SzKtfEUwxpd3/KUebgdK55II4Mnak62A==

"@uniswap/default-token-list@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@uniswap/default-token-list/-/default-token-list-4.1.0.tgz#bbda8b946096f248b92809f71b77b4d407768796"
integrity sha512-NxFW4OhYpnAunD37CKDRadG5ujp3r6cYnfBzTD1Eq4jwdr3ULt01xGqGljq0SuGAGhTsD+bmR46vPg3TdDwW0g==
"@uniswap/default-token-list@^9.0.0":
version "9.0.0"
resolved "https://registry.yarnpkg.com/@uniswap/default-token-list/-/default-token-list-9.0.0.tgz#35e070095ac24d24ef03877842c5d46807b59c87"
integrity sha512-a62uqv5ApULDVEXCOursXIaEoxP4MtlzdNSiQikYl1x9RfsUAZXl9dBtCo2d1Ne+iS1THic1oCJMshNtT9Z0EQ==

"@uniswap/lib@^4.0.1-alpha":
version "4.0.1-alpha"
Expand Down

1 comment on commit 27c5e7a

@vercel
Copy link

@vercel vercel bot commented on 27c5e7a Mar 7, 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:

widgets – ./

widgets-uniswap.vercel.app
widgets-git-main-uniswap.vercel.app
widgets-seven-tau.vercel.app

Please sign in to comment.