Skip to content

Commit

Permalink
feat: Prompt to add BSC network to user wallet (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
RabbitDoge committed Mar 9, 2021
1 parent 88d0485 commit 35e83a5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useCallback } from 'react'
import { useWeb3React } from '@web3-react/core'
import { useWeb3React, UnsupportedChainIdError } from '@web3-react/core'
import { ConnectorNames } from '@pancakeswap-libs/uikit'
import { useToast } from 'state/hooks'
import { connectorsByName } from 'utils/web3React'
import { setupNetwork } from 'utils/wallet'

const useAuth = () => {
const { activate, deactivate } = useWeb3React()
Expand All @@ -11,7 +12,16 @@ const useAuth = () => {
const login = useCallback((connectorID: ConnectorNames) => {
const connector = connectorsByName[connectorID]
if (connector) {
activate(connector, (error: Error) => toastError(error.name, error.message))
activate(connector, async (error: Error) => {
if (error instanceof UnsupportedChainIdError) {
const hasSetup = await setupNetwork()
if (hasSetup) {
activate(connector)
}
} else {
toastError(error.name, error.message)
}
})
} else {
toastError("Can't find connector", 'The connector config is wrong')
}
Expand Down
5 changes: 0 additions & 5 deletions src/utils/getFarmConfig.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/getRpcUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random from 'lodash/random'

// Array of available nodes to connect to
const nodes = [process.env.REACT_APP_NODE_1, process.env.REACT_APP_NODE_2, process.env.REACT_APP_NODE_3]
export const nodes = [process.env.REACT_APP_NODE_1, process.env.REACT_APP_NODE_2, process.env.REACT_APP_NODE_3]

const getNodeUrl = () => {
const randomIndex = random(0, nodes.length - 1)
Expand Down
37 changes: 37 additions & 0 deletions src/utils/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { nodes } from './getRpcUrl'

export const setupNetwork = async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const provider = window.ethereum
if (provider) {
const chainId = parseInt(process.env.REACT_APP_CHAIN_ID, 10)
try {
await provider.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: `0x${chainId.toString(16)}`,
chainName: 'Binance Smart Chain',
nativeCurrency: {
name: 'BNB',
symbol: 'BNB',
decimals: 18,
},
rpcUrls: nodes,
blockExplorerUrls: ['https://bscscan.com/'],
},
],
})
return true
} catch (error) {
console.error(error)
return false
}
} else {
console.error("Can't setup the BSC network on metamask because window.ethereum is undefined")
return false
}
}

export default null

0 comments on commit 35e83a5

Please sign in to comment.