connect to trustwallet and get bsc, eth, btc addresses and balances

2.8k Views Asked by At

right now I use this code: https://github.com/Web3Modal/web3modal-vanilla-js-example

but it connects only eth address from trust wallet

I tried to change provider options to this:

const providerOptions = {
      walletconnect: {
        package: WalletConnectProvider,
        options: {
            rpc: {
            56: "https://bsc-dataseed.binance.org/"}
        }
      },

But didn't work

In my trust wallet I have btc address, eth address, bnb address and I want to know all this addresses and balances. But right now I can only get eth address

I tried to do this:

const chainId = 56//await web3.eth.getChainId();
// Get list of accounts of the connected wallet
const accounts = await web3.eth.getAccounts();

But this gives me only eth accounts. So what do I do?

2

There are 2 best solutions below

0
On

In my case, it worked on Trust wallet, and did not work on metamask mobile app:

 walletconnect: {
    package: WalletConnectProvider, // required
    options: {
      rpc: {
        56: 'https://bsc-dataseed.binance.org/',
        97: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
      },
      network: "binance", // --> this will be use to determine chain id 56
    },
  },

I'm not sure the bellow code is required:

web3Modal = new Web3Modal({
    network: 'binance', // optional
    cacheProvider: true,
    providerOptions, // required
  })

You can find out how to configure by reading the source code directly: // Select BSC work on Trust wallet but dont work on metamask https://github.com/Web3Modal/web3modal/blob/72596699b97d231dfaa5ef04110b61b8dc77d57d/src/providers/connectors/walletconnect.ts#L30 https://github.com/Web3Modal/web3modal/blob/72596699b97d231dfaa5ef04110b61b8dc77d57d/src/helpers/utils.ts#L198

2
On

You need to ask the user to change the chain in the wallet itself to get access to accounts on different chains. Currently there is no API or way to force the user t change a chain.