下面是我在Ropsten testnet上签名和发送事务的代码,它正在正常工作。但是,设置为'1‘eth的值不会传输到目标地址。
var Tx = require('ethereumjs-tx').Transaction
const Web3 = require ('web3')
const web3 = new Web3('MY URL including the key')
//Public keys for two accounts
const account1 = '0x8dAd70D461d01C2945A0a1b358eDF21544E03d07'
const account2 = '0x2bA9D4119A2F5c80812d273F3720347dd529FD0A'
const prKey1 = Buffer.from(process.env.PR_KEY1.substring(2,66), 'hex')
const prKey2 = Buffer.from(process.env.PR_KEY2.substring(2,66), 'hex')
// *********************Transfer from account 1 to account 2 *******************
web3.eth.getTransactionCount(account1, (err, txCount) => {
// Build the transaction
const txObject = {
nonce: web3.utils.toHex(txCount),
to: account2,
Value: web3.utils.toHex(web3.utils.toWei('1', 'ether')),
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('100' , 'gwei'))
}
// Sign the transaction - choose ropsten as the network
const tx = new Tx(txObject, {chain: 'ropsten'})
tx.sign(prKey1)
const serializedTransaction = tx.serialize()
const raw = '0x' + serializedTransaction.toString('hex')
// Broadcast the transaction
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', txHash)
})
})
发布于 2020-09-23 08:14:36
将object txObject中的参数D1更改为value (没有大写字母)。愚蠢的错误,但经常发生:)
https://ethereum.stackexchange.com/questions/87691
复制相似问题