首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: EthereumTransaction不是构造函数

TypeError: EthereumTransaction不是构造函数
EN

Ethereum用户
提问于 2020-06-18 10:51:20
回答 4查看 1.5K关注 0票数 6
代码语言:javascript
复制
var EthereumTransaction = require("ethereumjs-tx")

var Web3 = require('web3')

var web3 = new Web3('http://127.0.0.1:7545')

//Setting Receiving and Sending Address

var sendingAddress = acc1

var receivingAddress = acc2

//Checking the balance of each account in ether

web3.eth.getBalance(sendingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

web3.eth.getBalance(receivingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

//Creating a transaction

var rawTransaction ={
    nounce:0,
    to:receivingAddress,
    gasPrice:20000000,
    gasLimit:30000,
    value:100,
    data:""
}

//Sign the Transaction

var privateKey = 'private key goes here'

var senderPrivateKeyHex = new Buffer(privateKey,'hex')

var transaction = new EthereumTransaction(rawTransaction)

transaction.sign(senderPrivateKeyHex)

//Send transaction to the network

var serializedTransaction = transaction.serialize()

web3.eth.sendSignedTransaction(serializedTransaction)

发生错误是。

代码语言:javascript
复制
TypeError: EthereumTransaction is not a constructor
EN

回答 4

Ethereum用户

发布于 2021-01-23 20:15:01

之所以发生这种情况,是因为ethereumjs-tx库更改了它的语法

这是代码的工作版本:

代码语言:javascript
复制
const EthereumTx = require('ethereumjs-tx').Transaction

var Web3 = require('web3')

var web3 = new Web3('http://127.0.0.1:7545')

//Setting Receiving and Sending Address

var sendingAddress = 'ADD SENDING ADDRESS HERE'

var receivingAddress = 'ADD RECEIVING ADDRESS HERE'

//Checking the balance of each account in ether

web3.eth.getBalance(sendingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

web3.eth.getBalance(receivingAddress).then(console.log(web3.utils.fromWei('1', 'ether')))

//Creating a transaction

var rawTransaction ={
    nonce: web3.utils.toHex(0),
    to: receivingAddress,
    gasPrice: web3.utils.toHex(20000000),
    gasLimit: web3.utils.toHex(30000),
    value: web3.utils.toHex(100),
    data: web3.utils.toHex("")
}

//Sign the Transaction

var privateKey = 'private key goes here'

var senderPrivateKeyHex = new Buffer.from(privateKey,'hex')

var transaction = new EthereumTx(rawTransaction)

transaction.sign(senderPrivateKeyHex)

//Send transaction to the network

var serializedTransaction = transaction.serialize()

web3.eth.sendSignedTransaction(serializedTransaction)
票数 8
EN

Ethereum用户

发布于 2020-06-18 17:59:36

尝尝这个

代码语言:javascript
复制
   var transaction = new TX(rawTransaction); 

而不是这个

代码语言:javascript
复制
  var transaction = new EthereumTransaction(rawTransaction)
票数 0
EN

Ethereum用户

发布于 2020-06-19 07:04:01

var TX = require('ethereumjs-tx').Transaction将删除此错误。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/84357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档