2

anyone can fix this??

i always got output "txHash: undefined"

var Tx = require('ethereumjs-tx')

const Web3 = require('web3')
const web3 = newWeb3('https://mainet.infura.io/')

const account1 = '0x9c56de05d7e27bc8.....................'

const account2 = '0xb2269135b0388b6..........................'

const account3 = '0x7717d106658c2................................'

const account4 = '0xb7060b6.............................'

const addresses = [account2, account3, account4];

var i
for (i =1; i < addresses.length; i++) {
  }

const privateKey1 = Buffer.from('4987f2720bcf5e57ad20c60241fcfe1fc1...........', 'hex')

web3.eth.getTransactionCount(account1, (err, txCount) => {

    // Build the transaction
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      to: addresses[i],
      value: web3.utils.toHex(web3.utils.toWei('1.2', 'ether')),
      gasLimit: web3.utils.toHex(21000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
    }


    // Sign the transaction
    const tx = new Tx(txObject)
    tx.sign(privateKey1)

    const serializedTx = tx.serialize()
    const raw = '0x' + serializedTx.toString('hex')

    // Broadcast the transaction
    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
      console.log('txHash:', txHash)
      // Now go check etherscan to see the transaction!
    })
  })
  • 1
    Welcome to Ethereum StackExchange! Did you check what the err possibly contains? – Lauri Peltonen Sep 12 '18 at 4:27
1

addresses[i], which is actually addresses[3], doesn't exist because your array only has tree addresses. Just specifically write a number between 0 and 2 and it will work.

Also, for consistency, make sure to also set a from parameter in the txObject and do log in the console the error you may get there.

Code:

var Tx = require('ethereumjs-tx')

const Web3 = require('web3')
const web3 = newWeb3('https://mainet.infura.io/')

const account1 = '0x9c56de05d7e27bc8.....................'

const account2 = '0xb2269135b0388b6..........................'

const account3 = '0x7717d106658c2................................'

const account4 = '0xb7060b6.............................'

const addresses = [account2, account3, account4];

var i
for (i =1; i < addresses.length; i++) {
  }

const privateKey1 = Buffer.from('4987f2720bcf5e57ad20c60241fcfe1fc1...........', 'hex')

web3.eth.getTransactionCount(account1, (err, txCount) => {

    // Build the transaction
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      from: account1,
      to: addresses[1],
      value: web3.utils.toHex(web3.utils.toWei('1.2', 'ether')),
      gasLimit: web3.utils.toHex(21000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
    }


    // Sign the transaction
    const tx = new Tx(txObject)
    tx.sign(privateKey1)

    const serializedTx = tx.serialize()
    const raw = '0x' + serializedTx.toString('hex')

    // Broadcast the transaction
    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
      console.log('err:', err)
      console.log('txHash:', txHash)
      // Now go check etherscan to see the transaction!
    })
  })
  • can you modify that script here, show me how to do it – slank-blues Sep 12 '18 at 13:25
  • You can recheck the answer now. – Paul Berg Sep 12 '18 at 15:24

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.