0

I'm new to Javascript, so bear with me. I've successfully gotten web3.js working in a HTML file, with getBalances working as it should. (I don't want to use Node.js for now)

However now I'm stuck at sending a raw transaction. The console tells me that a transaction hash is created, but when I search on Etherscan the transaction does not appear. Also no ETH is transferred between the 'from' and the 'to' address.

My code in the HTML file:

      const gasLimitInHex = web3.toHex(30000)

      const valueInWei = web3.toWei('0.05', 'ether')
      const valueWeiInHex = web3.toHex(valueInWei)

      const rawTx = {
        gasLimit: gasLimitInHex,
        to: receiveEthAddress,
        value: valueWeiInHex
      }

      const tx = new EthJS.Tx(rawTx)
      tx.sign(privateKey)

      var serializedTx = tx.serialize()

      console.log(serializedTx.toString('hex'))

      web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
        if (!err) {
          console.log(hash)
        } else {
          console.log(err)
        }
      })

The serialized tx: f866808082753094e1677d96bb82668bb188ec71498db5c0c0c4830e87b1a2bc2ec50000801ba0754d70a1dcbca6a7486fafeed8e63c8738b401af562697c2d97c990c4ac3d543a02a0664aeaa900c4d1d8ad94c3c69b76bcaa5f5c3e8307c4d63c86d60b70f4d7b

The transaction hash that web3.js returns: 0x9b89dad10e235d9bbd4b48f6a9768b557861281645b6d0702115e4a7141b0f41

FYI I'm using the myetherwallet API (https://api.myetherapi.com/eth)

  • 1
    It appear from your code that nonce is missing. You can obtain it by doing var nonce = web3.eth.getTransactionCount(0xaddress); – Ismael Sep 18 '17 at 4:27
  • Thanks. You're correct, the nonce was missing. I also realised that gasPrice is also needed for the rawTransaction to go through. – David T Sep 18 '17 at 6:46
1

Thanks to @Ismael for pointing me in the right direction.

Both the nonce and gasPrice were missing.

For nonce:

var nonce = web3.eth.getTransactionCount(0xaddress);

For gasPrice:

var gasPriceInWei = web3.toWei('10', 'gwei');

Both nonce and gasPrice need to be converted to hex.

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.