How to inject Binance Wallet into Web3 instead of Metamask?

Viewed 1057

I started using Binance Chain Wallet in my dapp, so I can now use:

    import { BscConnector } from '@binance-chain/bsc-connector'

export const bsc = new BscConnector({
  supportedChainIds: [56, 97] // later on 1 ethereum mainnet and 3 ethereum ropsten will be supported
})

// invoke method on bsc e.g.
await bsc.activate();
await bsc.getAccount();
await bsc.getChainId();

but in API docs it says to do some chain operations I need to iject :

The biggest difference between Binance Chain Wallet and MetaMask is we inject BinanceChain rather than ethereum (or web3) to the web page. So user could keep two extensions at the same time.

BinanceChain.request({method: "eth_sign", params: ["address", "message"])

with metamsk I use

ethereum.request(...)

can you explain to me how to do this? BinanceChain obj is not declared :)

1 Answers

So I figured out that these steps are necessarily :

// create instance of bsc wallet

        const bsc = new BscConnector({
        supportedChainIds: [56, 97, 1] // later on 1 ethereum mainnet and 3 ethereum ropsten will be supported
        })

// get provider out of this instance:

        const bsc_provider = await bsc.getProvider()

        provider = new ethers.providers.Web3Provider(bsc_provider)

// from here u can use all binance wallet functions:

provider.provider.switchNetwork('eth-mainnet')

see docs: binance wallet docs

Related