Dharmesh
Dharmesh

Reputation: 1

invalid sender while transfering BEP20 token

Hello I want to transfer bep20 token one address to another address,using ankr api and provider for web3, but got error like

Invalid Sender

Here is code which i used for transfer token, i have used chaind id for binanace like bnb, bsc binance smart chain mainnet

let tokenAddress = '0x41XXXXXXXXXXXXXXXXXXXXXXXXXXXXXbce' //  contract address
 
  let toAddress = req.params.toaddress // where to send it
  let fromAddress = req.params.fromaddress // your wallet
  let pKey = req.params.privatekey.split('0x');
  let privateKey = Buffer.from(pKey[1],'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 web32.eth.Contract(contractABI, tokenAddress, {from: fromAddress})

  // 1e18 === 1 HST
  let amount1 = req.params.amount;

  let amount = web32.utils.toBN(amount1*1000000000000000000)
  // console.log(amount);
  web32.eth.getTransactionCount(fromAddress)
    .then((count) => {
      let rawTransaction = {
        'from': fromAddress,
        'gasPrice': web32.utils.toHex(20 * 1e9),
        'gasLimit': web32.utils.toHex(210000),
        'to': tokenAddress,
        'value': 0x0,
        'data': contract.methods.transfer(toAddress, amount).encodeABI(),
        'nonce': web32.utils.toHex(count),
        "chainId": 56
      }
      let transaction = new EthereumTx(rawTransaction,{chain:'bsc'})
      transaction.sign(privateKey)
      web32.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'),(err,hash) => {
        console.log(err)
        var url = "https://etherscan.io/tx/"+hash
        res.json({hash: hash, status_url: url,success:'true',message : 'Transaction has been pushed.'});
      })
        
    })

Upvotes: 0

Views: 1365

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43571

transaction.sign(privateKey)
web32.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'),(err,hash) => {

You're passing the unsigned transaction, need to pass the signed one.

const signedTx = transaction.sign(privateKey)
web32.eth.sendSignedTransaction('0x' + signedTx.serialize().toString('hex'),(err,hash) => {

See the example in the readme (look for the const signedTx snippet)

Upvotes: 2

Related Questions