Sending token with Web3js: Returned error: invalid sender

I am trying to send a token with web3js, but i am getting that error " UnhandledPromiseRejectionWarning: Error: Returned error: invalid sender"

I’ve been browsing for hours, and can’t solve the solution. Some have said i need to add chainid to tx data(i did, but did not resolve the issue).

Could someone point me in the right direction?

const Web3 = require(‘web3’)
const Tx = require(‘ethereumjs-tx’).Transaction

const Web3js = new Web3(new Web3.providers.HttpProvider('

let tokenAddress = ‘0x’ // HST contract address
let toAddress = ‘0x’ // where to send it
let fromAddress = ‘0x’ // your wallet
let privateKey = Buffer.from(‘key’, ‘hex’)

let contractABI = [
// transfer
‘constant’: false,
‘inputs’: [
‘name’: ‘_to’,
‘type’: ‘address’
‘name’: ‘_value’,
‘type’: ‘uint256’
‘name’: ‘transfer’,
‘outputs’: [
‘name’: ‘’,
‘type’: ‘bool’
‘type’: ‘function’

let contract = new Web3js.eth.Contract(contractABI, tokenAddress, {from: fromAddress})

// 1e18 === 1 HST
let amount = Web3js.utils.toHex(1e18)

.then((count) => {
let rawTransaction = {
‘from’: fromAddress,
‘gasPrice’: Web3js.utils.toHex(20 * 1e9),
‘gasLimit’: Web3js.utils.toHex(210000),
‘to’: tokenAddress,
‘value’: 0x0,
‘data’: contract.methods.transfer(toAddress, amount).encodeABI(),
‘nonce’: Web3js.utils.toHex(count)
let transaction = new Tx(rawTransaction)
Web3js.eth.sendSignedTransaction(‘0x’ + transaction.serialize().toString(‘hex’))
.on(‘transactionHash’, console.log)

Hi @wenMassAdoption, and welcome to the Infura community!

I’m not super clear how you are formatting your addresses (tokenAddress, toAddress, fromAddress) - it looks like you have them all set as ‘0x’, so want to make sure those are actually the correct addresses with the ‘0x’ prefix added on. In addition, I would also check that your privateKey begins with the same ‘0x’.

If both those are set up properly and you’re still getting the error, please let us know and we will continue digging!

Those addresses are correct(i use the same for sending ETH transaction which works), i just hid them as to not expose my private keys.

I think the code itself might be faulty. I’ve been googling for a while and just can’t find a working example that would send an ERC-20 token using web3js.

That makes sense! I just wanted to double-check that those are set up correctly :slight_smile:

Have you tried removing the “value” field? That is specifically used for ether, so that may be what’s going wrong, as you’re trying to send the token vs ether. You can read more on this potential answer here.

This article also has a nice walkthrough with proof that the transaction went through, with a link out to another article on getting the ERC20 token balance with web3 if that’s something you’re interested in as well.