Ethereum: How to connect Metamask wallet address to web3 on bsc testnet on React

Here is a detailed guide how to connect the Metamask wallet with the BSC test using web3.js and respond:

assumptions

  • Install Metamask and“ Web3.js:NPM Install Metamask Web3

  • Set the Metamask wallet to Testnet (BSC)

  • Create a new React project:NPX Create-React-App Metamask-Testnet

Step 1: Allow BSC Testnet in Metamask

  • Open your Metamask wallet and go to
    Settings

    Ethereum: How to connect Metamask wallet address to web3 on bsc testnet on React

  • Go to
    Advanced Settings

    >
    Networks

  • Select
    Binance Smart Chain (BSC) Testnet from the list of available networks

  • Make sure the testnet network is allowed

Step 2: Set Web3.JS on React project **

  • InstallWeb3.JSPackage: NPM Install Web3

  • Create a new file calledWeb3.jsin the React Root Directory: Touch SRC/Web3.js

Javascript

Import * as Web3 from ‘Web3’;

Const Provider = New Web3.Providers.httpprovider (‘

`

Step 3: Connect your Metamask wallet to web3.Js

Create a new file called “MetamaskConnect.js” and add the following code:

`Javascript

Import * as Web3 from ‘Web3’;

Import {metamaskConnect}.

Const Accounts_ = Expect MetamaskConnect ();

`

This will connect your Metamask wallet to the testnet network using the “Metamask-Connector.js” file.

Step 4: Setting up account obtaining

Create a new file called Account.js and add the following code:

`Javascript

Import * as Web3 from ‘Web3’;

Import {accountyts_}.

Const Accounts = expect web3.eth.getacounts ();

Console.log (accounts);

`

This gets Metamask’s wallet addresses connected to your Testnet account.

Step 5: Use Accounts in your React app **

Update your app.js file and use the accounts obtained:

`Javascript

Import * as React, {UseState} from ‘React’;

Import Web3 from ‘./web3’;

APP () {) feature

Const [Accounts, SetacCunts] = UseState (Accounts_);

return (

Metamask Testnet Accounts

    {accounts.map ((account) => (

  • {foreign}
  • )))}

);

}

MetamasKTestnet () {) {)

return (

>>>>>>>>>>

{/ The content of React App TU /}

);

}

`

This will display Metamask’s wallet addresses connected to your Testnet account in the list.

That’s all! Now you should be able to connect your Metamask wallet with BSC test and load accounts using web3.js.

Bep2 Bep2 Movement