首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当从命令提示符运行web3代码时,事务哈希是“未定义的”

当从命令提示符运行web3代码时,事务哈希是“未定义的”
EN

Ethereum用户
提问于 2019-11-22 19:46:44
回答 1查看 1K关注 0票数 0

我编写了这个java脚本代码来将测试醚发送到我的另一个帐户:

代码语言:javascript
复制
var Tx = require('ethereumjs-tx').Transaction
var Web3 = require('web3')
web3 = new Web3('https://ropsten.infura.io/hqRzEqFKv6IsjRxfVUMH')
const account1 = '0x0539B9c9C886e93778542B553C64cb5EAfB902b1'
const account2 = '0xca7F442c44b079dd07324C9c7eDfe348b92f46c9'
const privatekey1 = Buffer.from('cxxxxxxxxxxxxx5xd0804xxxxxxx238ca1aed1xxx' , 'hex')
web3.eth.getTransactionCount(account1 , (err, transCount) => {
const txObject = {
nonce: web3.utils.toHex(transCount) ,
to: account2 ,
value: web3.utils.toHex(web3.utils.toWei('1' , 'ether')) ,
gasLimit: web3.utils.toHex(30000) ,
gasPrice: web3.utils.toHex(web3.utils.toWei('10' , 'gwei'))
}
var tx = new Tx(txObject)
tx.sign(privatekey1)
const serializedTransaction = tx.serialize()
const raw = '0x' + serializedTransaction.toString('hex')
web3.eth.sendSignedTransaction(raw , (err, txHash) => 
  {console.log('txHash: ' , txHash)})
})

现在,我在命令提示符中运行以下代码:

代码语言:javascript
复制
C:\Users\HP\path>
node testEther

这一产出如下:

代码语言:javascript
复制
txHash: undefined

txHash:未定义

这一切为什么要发生?我试着连接到不同的网络,但问题仍然存在。我已经检查了我的测试以太平衡,这表明我有足够的ether.The事务根本没有发生。我和罗丝汀的联系方式错了吗?

EN

回答 1

Ethereum用户

发布于 2019-11-23 00:53:40

你拥有的地方

代码语言:javascript
复制
var tx = new Tx(txObject)

你需要

代码语言:javascript
复制
const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})

h/t

https://community.infura.io/t/sendsignedtransaction-invalid-sender-error/832

全码

此代码在本地工作。更换你的私钥和私钥。

  • web3 1.2.4
  • Etalumjs-TX2.1.1(安装有单独的npm install ethereumjs-tx)
代码语言:javascript
复制
var Tx = require('ethereumjs-tx').Transaction
var Web3 = require('web3');
var web3 = new Web3('https://ropsten.infura.io/v3/INFURA_PROJECT');
const account1 = '0x926f8E72CE94491Cafba874b4affD40C3b05a5e3'
const account2 = '0xca7F442c44b079dd07324C9c7eDfe348b92f46c9'
const privatekey1 = Buffer.from('PRIVATE_KEY' , 'hex')
web3.eth.getTransactionCount(account1 , (err, transCount) => {
  const txObject = {
    nonce: web3.utils.toHex(transCount) ,
    from: account1,
    to: account2 ,
    value: web3.utils.toHex(web3.utils.toWei('.01' , 'ether')) ,
    gasLimit: web3.utils.toHex(30000) ,
    gasPrice: web3.utils.toHex(web3.utils.toWei('10' , 'gwei'))
  }
  const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})
  tx.sign(privatekey1)
  const serializedTransaction = tx.serialize()
  const raw = '0x' + serializedTransaction.toString('hex')
  web3.eth.sendSignedTransaction(raw , (err, txHash) => {
    console.log('txHash: ' , txHash)
  });
});

由此产生的交易:

https://ropsten.etherscan.io/tx/0x2b2f2a09d7d9d7cf8ec7243c6975b14d1db04ebaa2cff530700cf686d88095e9

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

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

复制
相关文章

相似问题

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