Ethereum: How to connect Metamask wallet address to web3 on bsc testnet on React
Here is a step -by -step guide on how to connect the Metamask purse to BSC Testnet using Web3 and react:
Prerequisites
- Install
Metamk
and ‘Web3.js’ packages:NPM Install Metamk Web3
- Set the Metamask Wallet to TestNet (BSC)
- Create a new REACT project:
NPX Create-React-App Metamk-Testnet
Step 1: Enable BSC TestNet Metamk
- Open Metamask’s wallet and go to
settings
- Go to
Advanced settings >
Networks
- Select
Binance Smart Chain (BSC) TestNet Available Network List
- Make sure the test network network is enabled
Step 2: Set Web3.You in the Response Project
- Install
Web3.js' Pack:
NPM Install Web3
- Create a new file namedWeb3.JS
in the root directory of your REACT project:
Touch SRC/Web3.JS
JavaScript
Import * as a Web3 of Web3;
Const Provider = New Web3.Providers.httpPprovider (‘
`
Step 3: Connect Metamk Wallet with Web3.js
Create a new file named Metamkconnect.js
and add the following code:
`JavaScript
Import * as a Web3 of Web3;
Import {metamconnect} from ‘.
Const accounts_ = wait for metamconnect ();
`
This will connect your Metamask purse to the test network using the “Metamk-Connector.js” file.
Step 4: Set to find your account
Create a new file named Account.js
and add the following code:
`JavaScript
Import * as a Web3 of Web3;
Import {accounts_} from ‘./Metamkconnect’;
Const accounts = wait for Web3.eth.getAccounts ();
Console.log (accounts);
`
This will recover the Metamask Wallet addresses connected to your test account.
Step 5: Use accounts in your React app
Update your file “App.JSto use the accounts obtained:
JavaScript
Import * How to React, {USESTATE} from ‘REACT’;
Import Web3 from ‘./Web3’;
Function app () {
Const [accounts, setaccounts] = usestate (accounts_);
return (
Metamk Test Accounts
- {Account}
{account.map ((account) => (
))}
);
}
Function Metamaktestnet () {
return (
{/ Your REACT app content here /}
web3provider>
);
}
`
It will show Metamask Wallet addresses connected to your test accounts list.
That’s it! Now you should be able to connect your Metamask Wallet to BSC TestNet and retrieve accounts using Web3.
Bir yanıt yazın