0

So lets start of with some code, let me start with explaining why I have two web3 instances. This is because one is just looking for events on the WS protocol and the other is using the HTTP protocol to send and call transactions. If I do the everything on the WS port it closes on me(any alternatives for this would be great).

let nonce;
            web3NoEvents.eth.getTransactionCount(account).then(_nonce => {
              nonce = _nonce + 1;
              const encoded_tx = contractNoEvents.methods.myMethod(n) + 1).encodeABI();
              const rawTx = {
                  nonce: nonce,
                  gasLimit: 3000000,
                  gasPrice: web3NoEvents.utils.toHex(utils.toWei('20', 'gwei')),
                  data: encoded_tx,
                  from: account,
                  to: contractAddress
              }

              let tx = new Tx(rawTx);
              tx.sign(pvtKey);
              let serializedTx = tx.serialize();
              web3NoEvents.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, res){
                if(err) {
                  console.log(err);
                } 
                console.log(res);
              })
              .on('receipt', function(res){
                console.log('signedTransaction res', res);
              })
              .on('error', function(err){
                console.log('signedTransactionErr', err);
              });
            }).catch((error) => {
              console.log(error);
            });;
          }

So the res gets logged in the sendSignedTransaction function and returns a txId like for example:

0x72501de41d3914e5fc78d4351ec24c0466182e533fa206656b440c7b2309ba52

The problem is that the .on receipt or .on error never get called, it seems like transaction gets initialzed but never actually gets broadcasted. If I search for the txId in the Ropsten block explorer it never finds it.

I just started working on the testnet and suspect that I might have to broadcast the transaction manually but am not sure if this is the case or otherwise how..

1

The line where I add 1 to the nonce was the issue. I did this to fix an old bug but it created a new one once I fixed-up the other code.

Code change to make it work, change:

              nonce = _nonce + 1;

to:

              nonce = _nonce;

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.