I tried to call Metamask and call trasnaction send but nothing had happened after I clicked on both buttons. Do you see anything bad there. I had this code from past And I really think it was working.
At first I need to enable ethereum, then is added BNB nework if was not before.
Then I can load transaction by click to Send Eth.
Thank you for reply
<button class="enableEthereumButton btn">Start Metamask</button>
<button class="sendEthButton btn">Send BNB</button>
<h2>Account: <span class="showAccount"></span></h2>
const ethereumButton = document.querySelector('.enableEthereumButton');
const sendEthButton = document.querySelector('.sendEthButton');
const showAccount = document.querySelector('.showAccount');
let accounts = [];
//Sending Ethereum to an address
console.log(window.ethereum.networkVersion, 'window.ethereum.networkVersion') ////
sendEthButton.addEventListener('click', () => {
window.ethereum.request({
method: "wallet_addEthereumChain",
params: [{
chainId: "0x38",
rpcUrls: ["https://bsc-dataseed.binance.org/"],
chainName: "Binance Smart Chain Mainnet",
nativeCurrency: {
name: "BNB",
symbol: "BNB",
decimals: 18
},
blockExplorerUrls: ["https://bscscan.com"]
}]
});
console.log(window.ethereum.networkVersion, 'window.ethereum.networkVersion') ////
if (ethereum.networkVersion == '56'){
ethereum.request({
method: 'eth_sendTransaction',
params: [
{
from: accounts[0],
to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
value: 'DE0B6B3A7640000', //0x29a2241af62c0000
gasPrice: '0x09184e72a000',
gas: '0x2710',
},
],
})
.then((txHash) => console.log(txHash))
.catch((error) => console.error);
}
});
ethereumButton.addEventListener('click', () => {
getAccount();
});
async function getAccount() {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
showAccount.innerHTML = account;
}