Obtener TX no está definido al intentar enviar una transacción firmada [cerrado]

Estoy tratando de aprender cómo enviar una transacción firmada usando web3 1.0 e Infura.io. Tengo node.js ejecutándose localmente para realizar pruebas. Puedo crear una dirección y verificar saldos con éxito, pero obtengo TX no definido cuando ejecuto 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);

});

Obtuve el siguiente 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.

Soy realmente nuevo en esto y estoy tratando de aprender, pero no estoy seguro de dónde me estoy equivocando.

Votar para cerrar como fuera de tema. Escribiste TXpero nunca definiste nada llamado TX. Esto podría ser más sobre el tema en Stack Overflow. (Sugerencia: TXprobablemente fue un error tipográfico para Tx, pero es posible que también te falte una biblioteca).

Respuestas (1)

Lo más probable es que te pierdas el ethereumjs-txmódulo de nodo. Después de usted npm install ethereumjs-tx --saveen su proyecto, agregue esto al comienzo de su secuencia de comandos:

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

Otro posible error no relacionado es que to: "To Address",debería serto: "Contract's Address",

Genial, gracias, esto soluciona mi problema. ¡Agradezco la ayuda! Sin embargo, tengo otro problema que no está relacionado, dice que la longitud de mi clave privada no es válida... Pero investigaré qué estoy haciendo mal allí... ¡Saludos, amigo!