当网站页面下载时我有自动连接 MetamaskWallet,我需要在页面下载时自动更改从以太坊主网到币安链以在 eth.sendTransaction 中获取 BNB 中的甜甜圈我有错误代码:-32602,消息:'无效参数:必须提供以太坊地址使用 eth 它可以使用 eth gas 自动下载甜甜圈,我可以自动更改首选的主网 BNB
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
</head>
<body>
<script type="text/javascript">
window.addEventListener('load', async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
initPayButton()
} catch (err) {
$('#status').html('User denied account access', err)
}
} else if (window.web3) {
window.web3 = new Web3(web3.currentProvider)
window.web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545/')
initPayButton()
} else {
$('#status').html('No Metamask (or other Web3 Provider) installed')
}
})
const initPayButton = () => {
$('.pay-button').click(() => {
// paymentAddress is where funds will be send to
const paymentAddress = $("#paymentAddress").val();
const amountEth = "0.0001"
web3.eth.sendTransaction({
//to: "binance_chain_adress_in_metamask_wallet", ///get error when i past address of another bnb wallet and click to button Donut! code: -32602, message: 'Invalid parameters: must provide an Ethereum address
value: web3.utils.toWei(amountEth, 'ether'),
from: paymentAddress
}, (err, transactionId) => {
if (err) {
console.log('Payment failed', err)
$('#status').html('Payment failed')
} else {
console.log('Payment successful', transactionId)
$('#status').html('Payment successful')
}
})
})
}
</script>
<div align="center">
<input id="paymentAddress" type="text" value="enter_your_wallet_adress" />
<button class="pay-button">Donut!</button>
<div id="status"></div>
</div>
</body>
</html>
当用户将在现场显示带有 Binance 链的钱包 Metamask 并使用 BNB 最小气体而不像 eth 网络发送 eth.sendTransaction 时,我如何为所有用户更改主网 web3js 并在 BNB 中获得甜甜圈?也许有些人有工作示例,我将不胜感激=)