简体   繁体   English

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

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

I want to receive Payment as bnb with metamask on my website.我想在我的网站上接收带有 metamask 的 bnb 付款。 I am using web3 for this.我为此使用了 web3。 I couldn't find how to send bnb instead of Ethereum.我找不到如何发送 bnb 而不是以太坊。 my arm is like this hello, I want to receive Payment as bnb with metamask on my website.我的手臂是这样的,你好,我想在我的网站上收到带有 metamask 的 bnb 付款。 I am using web3 for this.我为此使用了 web3。 I couldn't find how to send bnb instead of Ethereum.我找不到如何发送 bnb 而不是以太坊。 my arm is like this我的手臂是这样的

 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 is not stored in the ethereum blockchain. BNB 不存储在以太坊区块链中。 It has its own blockchain called Binance chain (although previously it ran on ethereum blockchain).它有自己的区块链,称为币安链(尽管之前它运行在以太坊区块链上)。 What the "Web3" object require, is something called the web3 provider to connect the a node of a certain blockchain. “Web3”对象需要的是一种称为 web3 提供程序的东西,用于连接某个区块链的节点。 So assuming the payment address is of Binance chain mainnet, the web provider will be "https://bsc-dataseed1.binance.org:443", so that you can connect to Binance chain.所以假设支付地址是币安链主网,那么web provider就是“https://bsc-dataseed1.binance.org:443”,这样就可以连接到币安链了。 So in the code it will be:所以在代码中它将是:

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

Also, since you're using metamask might as well connect to the binance chain through metamask.此外,由于您使用的是元掩码,因此不妨通过元掩码连接到币安链。 To do so click the Netowrk dropdown -> "Custom RPC".为此,请单击 Netowrk 下拉菜单 ->“自定义 RPC”。 Then add the binance network and save:然后添加币安网络并保存:

在此处输入图片说明

For more details you can check here .有关更多详细信息,您可以在此处查看

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

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

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