I'm trying to transfer for BSC network (testnet) using web3 with NodeJS. However, I am getting an error that I can't find any source on the internet. Would you help me with this topic?
Thanks for help.
const Web3 = require('web3')
const Tx = require('ethereumjs-tx').Transaction
const Common = require('ethereumjs-common').default;
const Web3js = new Web3(new Web3.providers.HttpProvider('https://data-seed-prebsc-1-s1.binance.org:8545/'))
let tokenAddress = '0xd648d2d7a0b312c6464d1ea10723f670269f09d4'
let toAddress = '0x238BC31CeBd242fA4F8baC5e6Cc1fB272E79d221'
let fromAddress = '0x59eed5ACf8274263Db17190690ff6842Bd7b3593'
let privateKey = Buffer.from('PRIVATE-KEY', 'hex')
let contractABI = ['ABI CODE']
let contract = new Web3js.eth.Contract(contractABI, tokenAddress, { from: fromAddress })
let amount = 10;
var gasPriceGwei = 3;
var gasLimit = 3000000;
const BSC_MAIN = Common.forCustomChain(
'mainnet', {
name: 'bnb',
networkId: 56,
chainId: 56
},
'petersburg'
)
Web3js.eth.getTransactionCount(fromAddress)
.then((count) => {
let rawTransaction = {
'from': fromAddress,
'gasPrice': Web3js.utils.toHex(gasPriceGwei * 1e9),
'gasLimit': Web3js.utils.toHex(gasLimit),
'to': tokenAddress,
'value': 0x0,
'data': contract.methods.transfer(toAddress, amount).encodeABI(),
'nonce': Web3js.utils.toHex(count)
}
let transaction = new Tx(rawTransaction, BSC_MAIN)
transaction.sign(privateKey)
Web3js.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', console.log)
})
