1

i have been using following code for transferring tokens from wallet to another with the use of web3js.

async Transfer(eth_address, amount){
  let tokenAddress = "0x6CbEDEc4F1ac9D874987D2769596544E0d9161ab";
  let sendAmount  = web3.utils.toWei(amount.toString(), 'ether')
  const consumerContract = new web3.eth.Contract(config.ERC20contractAbi, tokenAddress);
  const myData = consumerContract.methods.transfer(eth_address, sendAmount).encodeABI();
  const tx = {
    from,
    to:eth_address,
    value: '0',
    gasPrice: web3.utils.toWei('25', 'gwei'),
    gas: 60000,
    chainId: 1,
    nonce: await web3.eth.getTransactionCount(from,'pending'),
    data: myData
  }
  const signed = await web3.eth.accounts.signTransaction(tx, privateKey)
  const rawTx = signed.rawTransaction
  const sendRawTx = rawTx =>
  new Promise((resolve, reject) =>
    web3.eth
      .sendSignedTransaction(rawTx)
      .on('transactionHash', resolve)
      .on('error', reject)
  )

  const result = await sendRawTx(rawTx).catch((err) => {
      return err
  })

  if(result.toString().includes('error')){
    return result
  }
  else{
    return result
  }

},

But now it is suddenly not working for me. I do receive transaction hash and it is also marked as success on etherscan.io. Here is the link https://etherscan.io/tx/0xa92a7181275474cd6bd00e6e220385230c0c275e98d63bc35570ba6c0f9c4ab9

As it can be observed that there is no information about token being transferred or not. I have been using same method for a while, i don't understand what i am doing wrong now. kindly Guide regarding this. Thanks

1 Answer 1

1

From the transaction data you used the recipient address as it were the token address.

  const tx = {
    from,
    to: tokenAddress,    // <---- You should use tokenAddress instead of eth_address
    value: '0',
    gasPrice: web3.utils.toWei('25', 'gwei'),
    gas: 60000,

1
  • This is the answer I have been searching for for days. For the love of all that is holy help me bump it up...
    – eddyoc
    Apr 28, 2022 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.