0

I need to send eth to accounts that have erc20 tokens:

foreth.forEach(function(item,i){
    const balance = contractInstance.methods.balanceOf(item).call(function(err, result) {
       console.log(result)
    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': item,
      'value': web3.utils.toHex(web3.utils.toWei("0.0002")),
      'nonce': web3.utils.toHex(count)+i
    }
    let transaction = new Tx(rawTransaction)
    transaction.sign(privateKey)
    web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
      .on('transactionHash', console.log)

  })




    }
    })
    })

But when executing the code, 2 caches are generated for me, which are not on etherscan. Account transactions are also not visible, what's the problem? Some later i get error:

Transaction was not mined w
ithin 750 seconds, please make sure your transaction was properly sent. Be aware
 that it might still be mined!

any1 can help me?

0

1 Answer 1

0

You need to clearly identify what you are trying to achieve.

  • A proper indentation will help people here to help you.
  • function(item,i), perhaps function(recipient,i) would be more informative.
  • It is clunky to mix callbacks and promises. If you want to use only promises, my advice, then do:

    const balance = await contractInstance.methods.balanceOf(item).call();
    

    Of course this means you have to make async function(item,i).

  • Why do you limit the gas: 'gasLimit': web3.utils.toHex(21000)? Are you sure that none of the recipients are contracts that need a bit more?
  • Are you sure that your hard-coded gas price 'gasPrice': web3.utils.toHex(10 * 1e9), is consistent with the going rate?
  • What do you think web3.utils.toHex(count)+i achieves? Would web3.utils.toHex(count+i) not make more sense?
  • And, most importantly, you are doing web3.eth.getTransactionCount(fromAddress) inside the loop. You do understand that this number can change if a previous transaction is mined while the loop is running. When this happens, there will be a nonce at which there is no transaction. The transaction with nonces above the missing one will not be mined as long as a tx with the missing nonce is emitted. You have to fetch this number before you enter the loop.
2
  • Thank you very much for your reply! My mistakes are related to my inexperience in this direction, I will try to correct it. Could you tell me what values should be in the variables gasprice and gaslimit in my case?
    – Ayurpwnz
    Commented Dec 6, 2019 at 11:41
  • You can ask the gas price from web3, and you can estimateGas needed. Commented Dec 7, 2019 at 12:53

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.