•   about 1 year ago

problem, executing web3-example send_signed_transaction.js

I'm executing the following program. slightly modified example. I used working addresses and keys.
txCount shows "0", which should be "1", and there is no transaction: "txHash: undefined".
Am I doing s.th. wrong ?
Thank you.

import fetch from 'isomorphic-fetch';
import {Transaction} from 'ethereumjs-tx';
import Web3 from 'web3';
const web3 = new Web3('https://eth-rpc-api-testnet.thetatoken.org/rpc');
const chainID = 365;

const account1 = '0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A' // Your account address 1
const account2 = '0x1563915e194D8CfBA1943570603F7606A3115508' // Your account address 2
const privateKey1 = Buffer.from('1111111111111111111111111111111111111111111111111111111111111111', 'hex')
const privateKey2 = Buffer.from('2222222222222222222222222222222222222222222222222222222222222222', 'hex')

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

// Build the transaction
const txObject = {
nonce: web3.utils.toHex(txCount),
to: account2,
value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('4000', 'gwei')),
chainId: chainID
}

// Sign the transaction
const tx = new Transaction(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!
})
})

// Output:
// 0
// txHash: undefined

  • 3 comments

  • Manager   •   about 1 year ago

    Thanks for the question. Basically `const tx = new Transaction(txObject)` may not work as you intended. `Transaction()` is expected a different data type than the txObject passed in. You can try the following:

    First run the following commands to initialize your project and install the dependencies:

    npm init
    npm i ethereumjs-tx@1.3.5
    npm i web3@1.5.0

    Next, create a new file index.js with the following content. Save it, and run it with `node index.js`. After the script execution, check this explorer page, and you should see a new tx just got confirmed: https://testnet-explorer.thetatoken.org/account/0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A

    var EthTx = require('ethereumjs-tx')
    const Web3 = require('web3')
    const web3 = new Web3('https://eth-rpc-api-testnet.thetatoken.org/rpc')
    const chainID = 365 // for the local privatenet

    const account1 = '0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A' // Your account address 1
    const account2 = '0x1563915e194D8CfBA1943570603F7606A3115508' // Your account address 2

    const privateKey1 = Buffer.from('1111111111111111111111111111111111111111111111111111111111111111', 'hex')
    const privateKey2 = Buffer.from('2222222222222222222222222222222222222222222222222222222222222222', 'hex')

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

    // Build the transaction
    const txObject = {
    nonce: web3.utils.toHex(txCount),
    to: account2,
    value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
    gasLimit: web3.utils.toHex(21000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('4000', 'gwei')),
    chainId: chainID
    }

    // Sign the transaction
    const tx = new EthTx(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!
    })
    })

  •   •   about 1 year ago

    Thank you very much. This is working!

  • Manager   •   about 1 year ago

    Awesome!

Comments are closed.