简体   繁体   中英

How can I send BNB with metamask using web3.js

I want to receive Payment as bnb with metamask on my website. I am using web3 for this. I couldn't find how to send bnb instead of Ethereum. my arm is like this hello, I want to receive Payment as bnb with metamask on my website. I am using web3 for this. I couldn't find how to send bnb instead of Ethereum. 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. 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. 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. 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". Then add the binance network and save:

在此处输入图片说明

For more details you can check here .

ede selamlar bir cozum bulabildin mi? eger bulabildi isen bana telegramdan: memoksc yazabilirsen cok mutlu olurum. iyi bayramlar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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