Web3js

乙太坊不在 Ropsten 網路上轉移

  • September 23, 2020

這是我在 Ropsten 測試網上簽名和發送交易的程式碼,它工作正常。但是,設置為 ‘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)
 })
})

在此處輸入圖像描述

Value將對像中的參數更改txObjectvalue(不帶大寫字母)。愚蠢的錯誤,但發生了很多:)

引用自:https://ethereum.stackexchange.com/questions/87691