1

There is a code, the essence is this: I need to check the account balance for erc20 tokens, and if the balance is not zero, I need to send eth from another account to this one, after eth reaches the account, I need to send erc20 tokens to another account . How then can I get the status of a transaction? And if the status is successful, then do the token transaction?

 const balance = contractInstance.methods.balanceOf(walletAddress).call(function(err, result) {
       //console.log(err)
    if (result>0) {
    console.log("success")
    web3.eth.getTransactionCount(fromAddress)
  .then((count) => {
    let rawTransaction = {
      'from': fromAddress,
      'gasPrice': web3.utils.toHex(10 * 1e9),
      'gasLimit': web3.utils.toHex(21000),
      'to': walletAddress,
      'value': web3.utils.toHex(web3.utils.toWei("0.0002")),
      'nonce': web3.utils.toHex(count)
    }
    let transaction = new Tx(rawTransaction)
    transaction.sign(privateKey)
    web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
      .on('transactionHash', console.log)

  })


web3.eth.getTransactionCount(fromAddress)
  .then((count) => {
    let rawTransaction = {
      'from': walletAddress,
      'gasPrice': web3.utils.toHex(20 * 1e9),
      'gasLimit': web3.utils.toHex(41000),
      'to': tokenAddress,
      'value': '0x' + Number(result)/1000000000000000000,
      'data': contract.methods.transfer(fromAddress, amount).encodeABI(),
      'nonce': web3.utils.toHex(count)
    }
    let transaction = new Tx(rawTransaction)
    transaction.sign(privateKey1)
    web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
      .on('transactionHash', console.log)
  })    

    }
    })
2

Regarding how you can wait until the tx receipt is ready bellow I wrote an example of how:

const balance = contractInstance.methods.balanceOf(walletAddress).call(function(err, result) {
...
web3.eth.getTransactionCount(fromAddress)
  .then((count) => {
    let rawTransaction = {
      'from': fromAddress,
      'gasPrice': web3.utils.toHex(10 * 1e9),
      'gasLimit': web3.utils.toHex(21000),
      'to': walletAddress,
      'value': web3.utils.toHex(web3.utils.toWei("0.0002")),
      'nonce': web3.utils.toHex(count)
    }
    let transaction = new Tx(rawTransaction)
    transaction.sign(privateKey)

    return new Promise((resolve, reject) => {
       web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
      .on('transactionHash', console.log)
      .on('receipt', resolve)
      .on('error', reject)
    })
  }).then(receipt => {
    // Whatever logic when tx is succesfull
  })
...
  • Thank you very much! Could you tell me with one more question, when checking the account balance I have 3 erc20 tokens, but the value in the console is displayed as 3 * 1ะต18. How can I correctly transfer this value to "value" to send the entire balance to another account? I tried to do this: 'value': '0x' + Number (result) / 1000000000000000000, where result is the account balance, but I did not succeed – Ayurpwnz Dec 6 '19 at 21:31
  • @Ayurpwnz the balance value is being fetched in Wei units as a BN type. In case you want to convert that value back to erc20 tokens you could use web3js.readthedocs.io/en/v1.2.0/web3-utils.html#fromwei but you should do the tx in wei anyway, so IMO you should not have issues to just pass over the value from balance to transfer – Gabriel G. Dec 7 '19 at 8:33

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service, privacy policy and cookie policy

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