Hello, I’m trying to make a website where there’s a button to connect the metamask wallet and another 4 buttons to send different amounts of BNB to a specific wallet. Right now the user can login under any network, the problem is if the user logs under the ethereum network it sends ETH instead of BNB… I need to add a network verification for connection, if the user is not on the BSC network then ask the user to change the network first and then connect. But I can’t make it work.
Can someone please give me a hand with this?
I’m using the following code:
const connectbtn = document.getElementById('connectbtn');
connectbtn.addEventListener("click", ()=>{
if (typeof window.ethereum == 'undefined') {
alert('Please install metamask!');
}
else{
getaccount();
}
});
accounts = [];
async function getaccount() {
accounts = await ethereum.request({method : 'eth_requestAccounts'});
var address = accounts[0];
var front_add = address.substr(0,8);
var tail_add = address.substr(address.length -2,2);
connectbtn.innerHTML = front_add.toLowerCase() + '...' +tail_add.toLowerCase();
}
const send0_1btn = document.getElementById('0_1sendbtn');
const send0_3btn = document.getElementById('0_3sendbtn');
const send1btn = document.getElementById('1sendbtn');
const send16btn = document.getElementById('16sendbtn');
//Sending Ethereum to an address
send0_1btn.addEventListener('click', () => {
amount = '0x16345785D8A0000'
send_bnb(amount)
});
send0_3btn.addEventListener('click', () => {
amount = '0x429D069189E0000'
send_bnb(amount)
});
send1btn.addEventListener('click', () => {
amount = '0xDE0B6B3A7640000'
send_bnb(amount)
});
send16btn.addEventListener('click', () => {
amount = '0xDE0B6B3A76400000'
send_bnb(amount)
});
async function send_bnb(amount){
ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: '0xA743C05A64017e12C31D15117d6235bbE7c8aC90',
value: amount,
gasPrice: '0x2540BE400',
gas: '0x30D40',
chainId: '0x38',
},
],
})
.then((txHash) => console.log(txHash))
.catch((error) => console.error);
}