我编写了这个java脚本代码来将测试醚发送到我的另一个帐户:
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)})
})现在,我在命令提示符中运行以下代码:
C:\Users\HP\path>
node testEther这一产出如下:
txHash: undefinedtxHash:未定义
这一切为什么要发生?我试着连接到不同的网络,但问题仍然存在。我已经检查了我的测试以太平衡,这表明我有足够的ether.The事务根本没有发生。我和罗丝汀的联系方式错了吗?
发布于 2019-11-23 00:53:40
你拥有的地方
var tx = new Tx(txObject)你需要
const tx = new Tx(txObject, {chain:'ropsten', hardfork: 'petersburg'})h/t
https://community.infura.io/t/sendsignedtransaction-invalid-sender-error/832
此代码在本地工作。更换你的私钥和私钥。
npm install ethereumjs-tx)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
https://ethereum.stackexchange.com/questions/77737
复制相似问题