Connect wallet and send transaction - how to change blockchain to Bianance smart chain (BEP-20) network instead of Ethereum?

Hello, is simple source code in HTML and JS about button to connecting wallet as a web3 and loading ETH transaction in MetaMask.

const ethereumButton = document.querySelector('.enableEthereumButton');
const sendEthButton = document.querySelector('.sendEthButton');

let accounts = [];

//Sending Ethereum to an address
sendEthButton.addEventListener('click', () => {
  ethereum
    .request({
      method: 'eth_sendTransaction',
      params: [
        {
          from: accounts[0],
          to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
          value: '0x29a2241af62c0000',
          gasPrice: '0x09184e72a000',
          gas: '0x2710',
        },
      ],
    })
    .then((txHash) => console.log(txHash))
    .catch((error) => console.error);
});

ethereumButton.addEventListener('click', () => {
  getAccount();
});

async function getAccount() {
  accounts = await ethereum.request({ method: 'eth_requestAccounts' });
}
<button class="enableEthereumButton btn">Enable Ethereum</button>
<button class="sendEthButton btn">Send Eth</button>

I would like to ask, how to change code to determine loading of Binance smart chain network (BEP-20) instead of ETH. I tried to find and change ETH contract which would be changed to BEP-20 contract but I don´t see contract there.

Second problem there is that this code contains value of 3 ETH which are loaded for transaction. I would like to ask where is value set up to 3?

Thank you for reply.

5 Likes

@david21

Hello
You want to make an automatic detection of a network change or connect to a contract deployed on the BEP-20 network ?

1 Like

I want automatic load of BNB (BEP-20) for check and confirmation of transaction.

1 Like

Yes, so probably the first, if you mean same thing.
For example there is loaded amount of ETH. I would like to automatically work with BEP-20…
image

1 Like

You have to manually switch to the network on which you need to do the respective transaction.

3 Likes

For example there is button for that.

1 Like

Hey @david21. Try to see if this article could help you with what you’re trying to accomplish.

5 Likes

Thank you but I see lots of error messages there:
errors
First: ’Unmateched “{” ’
Second is: “Missing semicolon”.
What is wrong?

1 Like

@david21

Hi, u may use this library

import detectEthereumProvider from “@metamask/detect-provider”;

then you need to identify a provider

const setListener = provider => {
provider.on(“chainChanged”, _ => window.location.reload());
};

const provider = await detectEthereumProvider();
setListener(provider);

Are you using the web3js library? Which contains a useful function for you to determine the current network?

const chainId = await web3.eth.getChainId();

3 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.