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('https://polygon-mainnet.infura.io/v3/key))
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)
Web3js.eth.getTransactionCount(fromAddress)
.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)
transaction.sign(privateKey)
Web3js.eth.sendSignedTransaction(‘0x’ + transaction.serialize().toString(‘hex’))
.on(‘transactionHash’, console.log)
})