12

I want to send transaction in Binance Smart Chain and I got an error: new Error("Chain with name " + chain + " not supported");

const Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');

const web3 = new Web3("https://mainnet.infura.io/v3/xxx");

const privKey = Buffer.from('xxx', 'hex'); const addressFrom = '0xXXX'; const addressTo = '0xXXX';

web3.eth.getTransactionCount(addressFrom, (err, txCount) => { const txObject = { nonce: web3.utils.toHex(txCount), to: addressTo, value: web3.utils.toHex(web3.utils.toWei('0.01', 'ether')), gasLimit: web3.utils.toHex(100000), gasPrice: web3.utils.toHex(web3.utils.toWei('100', 'gwei')) };

    const tx = new Tx(txObject,  {'chain':'smart chain'});
    //const tx = new Tx(txObject,  {'chain':'56'});
    //const tx = new Tx(txObject,  {'chain':'binance'});
    tx.sign(privKey);

    const serializedTrans = tx.serialize();
    const raw = '0x' + serializedTrans.toString('hex');

    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
        console.log('txHash:', txHash)
    });
}

);

And if using these libraries it is impossible to send a transaction, what is better for me to use to send a transaction in Smart Chain? I found a couple of sites https://docs.binance.org/smart-chain/wallet/wallet_api.html and https://github.com/drunken005/binance-utils but it seems to me they are for a regular Binance network (as I see wallets in examples start with bnb...) or am I wrong?

AVorobev
  • 123
  • 1
  • 1
  • 4

3 Answers3

12

Infura does not support BSC, instead try another provider like shown here

// mainnet 
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
// testnet
const web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545');

Or use providers like quikNode

Kristian
  • 296
  • 1
  • 8
5

Try this:

import Common from 'ethereumjs-common';
import transaction from 'ethereumjs-tx';

const common = Common.default.forCustomChain('mainnet', { name: 'bnb', networkId: 56, chainId: 56 }, 'petersburg');

const tx = new transaction.Transaction(data, { common });

Denis
  • 151
  • 2
  • if you want to use Binance Smart Chain testnet, do you know the configuration? using the same code with id: 97 and name 'Chapel' I get this error: 'Error: Chain with name Chapel not supported' – gopeca Apr 28 '21 at 10:44
  • for Binance smart chain testnet it worked for me: – cryptoKTM May 28 '21 at 10:08
0

For Binance smart chain testnet it worked fine:

         const createRawTransaction = require('ethereumjs-tx').Transaction;
         const common = require('ethereumjs-common');
      const chain = common.default.forCustomChain(
        'mainnet',{
          name: 'bnb',
          networkId: 97,
          chainId: 97
        },
        'petersburg'
      )
      var rawTx = new createRawTransaction(rawData, {common: chain});

cryptoKTM
  • 431
  • 1
  • 3
  • 15