1

I am trying to learn how to send a signed transaction using web3 1.0 and Infura.io. I have node.js running locally for testing. I can successfully create an address and check balances but am getting TX not defined when I run the sendSignedTransaction.

const Web3 = require('web3');

web3 = new Web3(new 
Web3.providers.HttpProvider("https://mainnet.infura.io/v3/My API Key"));

// Get Contract ABI
var abi = JSON.parse('[{"MY ABI"}]')

// Define Variable for Contract ABI
var AK = new web3.eth.Contract(abi);

// Buffer PK
var privateKey = new Buffer('Private Key')

// create transaction - to address, amount
var data = AK.methods.transfer("To Address", 10).encodeABI();

// object to hold the transaction data From Address
web3.eth.getTransactionCount('From Address').then(count => {

var txData = {

nonce: web3.utils.toHex(count),

gasLimit: web3.utils.toHex(25000),

gasPrice: web3.utils.toHex(web3.eth.gasPrice),

to: "To Address",

from: "From Address",

data: data

}

var transaction = new TX(txData);

transaction.sign(privateKey);

var serialisedTransaction = transaction.serialize().toString('hex');

web3.eth.sendSignedTransaction('0x' + serialisedTransaction);

});

I get the following error -

(node:16077) UnhandledPromiseRejectionWarning: ReferenceError: TX is not defined
at web3.eth.getTransactionCount.then.count 
(/Users/ryan/Documents/KapAction/public/send.js:39:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
(node:16077) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection. This error originated either by throwing inside of an async 
function without a catch block, or by rejecting a promise which was not 
handled with .catch(). (rejection id: 1)
(node:16077) [DEP0018] DeprecationWarning: Unhandled promise rejections 
are deprecated. In the future, promise rejections that are not handled 
will terminate the Node.js process with a non-zero exit code.

I am really new to this and am trying to learn but am not sure where I am going wrong.

closed as off-topic by smarx, Richard Horrocks, Achala Dissanayake, shane, flygoing Nov 14 '18 at 2:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – smarx, Richard Horrocks, Achala Dissanayake, shane, flygoing
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Voting to close as off topic. You typed TX but never defined anything called TX. This might be more on-topic on Stack Overflow. (Hint: TX was probably a typo for Tx, but you also may be missing a library.) – smarx Oct 28 '18 at 17:52
2

You most probably miss the ethereumjs-tx node module. After you npm install ethereumjs-tx --save in your project, add this to the beginning of your script:

const TX = require("ethereumjs-tx");

Another, unrelated possible mistake is that to: "To Address", should be to: "Contract's Address",

  • Great thank you, this fixes my issue. I appreciate the help! I do get another issue though which is unrelated, it says my Private key length is invalid.. But I will look into what I am doing wrong there.. Cheers mate! – Ryan D Oct 28 '18 at 18:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.