1

I want to transfer bep20 token one address to another address in bsc testnet, but got error "Invalid Sender"

Here is the code I used for transfer, I have used chain id for bsc testnet

var fromAddress = process.env.BIGADDRESS;
var toAddress = "0xb7f17658EAf84A63302....";
var transferAmount = 1;

const Web3 = require("web3");
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("https://data-seed-prebsc-1-s1.binance.org:8545/"));

/* NETWORK AND LIB VERSION CHECKS*/
web3.eth.net.isListening()
   .then(() => console.log('web3 is connected'))
   .catch(e => console.log('Wow. Something went wrong'));
var Tx = require('ethereumjs-tx');
web3.eth.net.getNetworkType()
.then(console.log);

var abi = require('./Abi.json'); 
const Web3EthContract = require('web3-eth-contract');

var count = web3.eth.getTransactionCount(fromAddress);
var abiArray = abi.abi;
var contractAddress = "0xf9f93cf501bfadb6494589cb4b4c15de49e85d0e" // Cake in bsc testnet
var contract = new web3.eth.Contract(abiArray, contractAddress);

var data = contract.methods.transfer(toAddress, transferAmount).encodeABI()
var gasPrice = 10;
var gasLimit = 90000;

var rawTransaction = {
  "from": fromAddress,
  "nonce": web3.utils.toHex(count),
  "gasPrice": web3.utils.toHex(gasPrice),
  "gasLimit": web3.utils.toHex(gasLimit),
  "to": toAddress,
  "value": "0x0",
  "data": data,
  "chainId": 0x38
};

console.log(rawTransaction)

const EthereumTx = require('ethereumjs-tx').Transaction
const privKey = Buffer.from(
   process.env.PRIVATE_KEY,
  'hex',
)

const tx = new EthereumTx(rawTransaction)

tx.sign(privKey);
var serializedTx = tx.serialize();  

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
  if (!err)
      console.log(hash);
  else
      console.log(err);
});
TylerH
  • 20,799
  • 66
  • 75
  • 101

0 Answers0