繁体   English   中英

如何使用 web3.js 发送带有元掩码的 BNB

[英]How can I send BNB with metamask using web3.js

我想在我的网站上接收带有 metamask 的 bnb 付款。 我为此使用了 web3。 我找不到如何发送 bnb 而不是以太坊。 我的手臂是这样的,你好,我想在我的网站上收到带有 metamask 的 bnb 付款。 我为此使用了 web3。 我找不到如何发送 bnb 而不是以太坊。 我的手臂是这样的

 console.log(window.web3) 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) 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 = '0x192c96bfee59158441f26101b2db1af3b07feb40' const amountEth = 1 web3.eth.sendTransaction({ to: paymentAddress, value: web3.toWei(amountEth, 'ether') }, (err, transactionId) => { if (err) { console.log('Payment failed', err) $('#status').html('Payment failed') } else { console.log('Payment successful', transactionId) $('#status').html('Payment successful') } }) }) }
 console.log(window.web3) 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) 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 = '0x192c96bfee59158441f26101b2db1af3b07feb40' const amountEth = 1 web3.eth.sendTransaction({ to: paymentAddress, value: web3.toWei(amountEth, 'ether') }, (err, transactionId) => { if (err) { console.log('Payment failed', err) $('#status').html('Payment failed') } else { console.log('Payment successful', transactionId) $('#status').html('Payment successful') } }) }) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script> <div> <button class="pay-button">Pay</button> <div id="status"></div> </div>

BNB 不存储在以太坊区块链中。 它有自己的区块链,称为币安链(尽管之前它运行在以太坊区块链上)。 “Web3”对象需要的是一种称为 web3 提供程序的东西,用于连接某个区块链的节点。 所以假设支付地址是币安链主网,那么web provider就是“https://bsc-dataseed1.binance.org:443”,这样就可以连接到币安链了。 所以在代码中它将是:

window.web3 = new Web3('https://bsc-dataseed1.binance.org:443');

此外,由于您使用的是元掩码,因此不妨通过元掩码连接到币安链。 为此,请单击 Netowrk 下拉菜单 ->“自定义 RPC”。 然后添加币安网络并保存:

在此处输入图片说明

有关更多详细信息,您可以在此处查看

ede selamlar bir cozum bulabildin mi? eger bulabildi isen bana telegramdan: memoksc yazabilirsen cok mutlu olurum。 伊伊拜拉姆拉尔

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM