How to send BNB in BSC mainnet using Web3

562 Views Asked by At

I am going to send BNB using web3 on BSC main net. BSC mainnet is as follow.

MAINNET=https://bsc-dataseed1.binance.org:443
const web3 = new Web3(process.env.MAINNET);
const BNBcontract = await new web3.eth.Contract(BNBabi, process.env.BNB_ADDRESS);
const BNBdecimals = await BNBcontract.methods.decimals().call();

But it happens error when I run the code

Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.

My idea is wrong? I can't send BNB on BSC main net? And what is BNB token address? I found the BNB token address in Etherscan.io. 0xB8c77482e45F1F44dE1745F52C74426C631bDD52 This is right? And I should use contract ABI on etherscan.io?

3

There are 3 best solutions below

0
On BEST ANSWER
const web3 = new Web3(req.body.network && req.body.network === "MAINNET" ? process.env.MAINNET : process.env.TESTNET);

try {
    // Sign transaction
    let signTransaction = await web3.eth.accounts.signTransaction({
        to: req.body.to,
        value: web3.utils.toWei(req.body.amount, 'ether'),
        gas: req.body.gas || 2000000
    }, req.body.from_private_key);

    // Transaction
    let tx = await web3.eth.sendSignedTransaction(
        signTransaction.rawTransaction
    );
    
    res.status(200).send({ status: true, hash: tx.transactionHash });
} catch (error) {
    res.status(500).send({ status: false, message: 'Transfer Failed' });
}
0
On
const ethers = require('ethers');
const privateKey = '<YOUR_PRIVATE_KEY>';
const provider = new ethers.JsonRpcProvider('https://bsc-dataseed.binance.org/');
const wallet = new ethers.Wallet(privateKey, provider);

const toAddress = '0xRecipientAddress'; // Replace with the recipient's BSC address.
const valueToBeSend = ethers.utils.parseUnits('0.1', '18'); // Replace with the amount of BNB you want to send (in BNB)

async function sendBNB() {
  const tx = await wallet.sendTransaction({
    to: toAddress,
    value: valueToBeSend
  });

  await tx.wait();
  console.log(`Transaction hash: ${tx.hash}`);
}

sendBNB();

You can also use ether.js for sending BNB to an BSC account. Also for sending BNB we dont need any ABI.

The script above can help you.

0
On
const signedTx = await  web3.eth.accounts.signTransaction({
  to: account.address,
  value: (process.env.BNB_SEND_AMOUNT* 10**decimals).toString(),
  gas: 2000000,
  common: {
    customChain: {
      name: 'custom-chain',
      chainId: 56,
      networkId: 56
    }
  }
}, process.env.MAIN_WALLET_PRIVATE);
        
// BNB send
let BNBtx = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);

// BNB hash
console.log("BNB Hash: ", BNBtx.transactionHash)

For sending BNB on BSC, we don't need ABI file of BNB.