0

I'm trying to create transactions something like this:

var sender = meth.eth.accounts.privateKeyToAccount(data.fromPrivate)
meth.sendTransaction({
  sender,
  to: data.to,
  amount: data.value,
}, cb)

MEth.prototype.sendTransaction = function(data, cb, curCb, nonce_offset) {
    nonce_offset = nonce_offset || 0
    var meth = this
    this.eth.getTransactionCount(data.sender.address).then(nonce => {
        nonce = nonce + nonce_offset

        this.l.acquire("tx", function(done) {
            if (nonce <= meth.nonce) {
                nonce = meth.nonce + 1
            }
            var tx = {
                nonce: web3.utils.toHex(nonce),
                value: web3.utils.toHex(data.amount || 0),
                to: data.to,
                // from: data.sender.address,
                // gasPrice: web3.utils.toHex(data.gasPriceGwei * 1e9),
                gasLimit: web3.utils.toHex(data.gasLimit || 3000000),
                data: '0x0'
            }

            if(data.encodedData) {
                tx.data = data.encodedData;
            }

            data.sender.signTransaction(tx).then(raw => {
                meth.eth.sendSignedTransaction(raw.rawTransaction, err => {
                    if (!err)
                        meth.nonce = nonce
                    else {
                        if(err.message.startsWith('Returned error: known transaction') || 
                            err.message.startsWith('Returned error: already known')) {
                            var newtx = tx
                            meth.sendTransaction(data, cb, curCb, nonce_offset + 1)
                            done()
                            return
                        }
                    }
                    done()

                    if (!err) {
                        meth.eth.getTransaction(raw.transactionHash).then(function(nettx) {
                            if(!nettx)
                                var fee = undefined
                            else
                                var fee = parseInt(nettx.gasPrice) * nettx.gas
                            cb(err, raw.transactionHash, fee)
                        })
                    } else cb(err)
                })

                curCb && curCb(null)

            })
        
        })
    });
}

The problem is that periodically I do getTransaction and I manage to steal information, but at another moment I do not even have time. And in any case, the transaction does not appear on the network, that is, as if the node manages to accept the transaction and immediately deletes it for some reason. There are no error messages in the console, I tried to use .on('error'), it also returns nothing, I don’t understand anything and I don’t understand how I can understand what my problem is. I use infura ropsten network.

$ node eth.js
balance, 98276995500000000
null '0xc33771b09d8f9a922cb3daf5623cca2997b49b0fce503dcb55f1e97306cc2734' undefined // getTransaction -> fee = undefined

$ node eth.js
balance, 98276995500000000
null '0xc33771b09d8f9a922cb3daf5623cca2997b49b0fce503dcb55f1e97306cc2734' 9000000 // getTransaction -> fee = 9000000, the hash is repeated accordingly, there are no errors at the same time, there is no transaction in the explorer 

$ node eth.js
balance, 98276995500000000 undefined undefined
null '0xc33771b09d8f9a922cb3daf5623cca2997b49b0fce503dcb55f1e97306cc2734' undefined // again fee undefined, and again the same hash

  • 1
    What's meth and what's MEth? – goodvibration Jun 27 at 12:55
  • @goodvibration This is just my wrapper for an abbreviated call, in the first block there is an example of how this is called from the outside. This object contains the eth property which is web3.eth – mr blabla Jun 27 at 12:56
  • 1
    Please include ALL the relevant code and ONLY the relevant code (i.e., if your wrapper is not critical for us to understand and reproduce the problem, then please get rid of it). – goodvibration Jun 27 at 13:03
  • @Goodvibration You know, now I tried to exclude everything that in my opinion is not necessary here, and it began to work as expected, well, I'm now in the process of researching further – mr blabla Jun 27 at 13:18
  • @goodvibration thanks, i create answer on my question – mr blabla Jun 27 at 20:15
1

So I figured out my problem. It was hard to understand, but the point was that I tried to make nonce + 1 if it was equal to or less than the last time it was executed.

In general, if you do not go into details, your nonce should be clearly observed. In my situation, I tried to make nonce 1 when the network was expecting nonce 0, the node thought that there was another one before this transaction, but it was not yet known and just deleted the transaction. Be careful what nonce you send

| improve this answer | |

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.