Web3js

web3.eth.sendSignedTransaction 成功執行,但實際上沒有任何 ETH 轉移

  • May 29, 2019

正如您在下面看到的,我的原始交易已由氣體簡化值設置,返回值是已實現的交易雜湊碼,但從未假設此交易的結果弄清楚

function sendTransaction(_privateKey,_from,_to,_value) 
{
   return new Promise(function(resolve,reject){
   try {
       web3.eth.getBlock("latest", false, (error, result) => {
           var _gasLimit = result.gasLimit;

           web3.eth.getGasPrice(function(error,result){ 
               var _gasPrice = result;

               const Tx = require('ethereumjs-tx');
               const privateKey = Buffer.from(_privateKey, 'hex')

               var _hex_gasLimit = web3.utils.toHex(_gasLimit.toString());
               var _hex_gasPrice = web3.utils.toHex(_gasPrice.toString());
               var _hex_value = web3.utils.toHex(web3.utils.toWei(_value,'ether'));
               var _trx_count = web3.eth.getTransactionCount(_from);
               var _hex_Gas = web3.utils.toHex('50000');
               const rawTx = {
                   nonce: web3.utils.toHex(web3.eth.getTransactionCount(_from)),
                   to: _to,
                   from:_from,
                   gasLimit:_hex_gasLimit,
                   gas:50000,
                   gasPrice:_hex_gasPrice,

                   value: _hex_value,
                   data: '0x00'
               } 

               const tx = new Tx(rawTx);
               tx.sign(privateKey);

               var serializedTx = '0x'+tx.serialize().toString('hex');  
               web3.eth.sendSignedTransaction(serializedTx.toString('hex'),function(err,hash){
                   if(err)
                   { 
                       resolve(err);
                   }
                   else
                   {
                       resolve('Txn Sent and hash is '+hash);

                   }
               }); 
           });
       });
   } catch (error) {
       resolve(error);
   }
})}

這是返回的交易雜湊碼

0xa1338065afcfa20e4062a25892d79ad7137af2b631fc15c4a37977938562396c

var _trx_count = web3.eth.getTransactionCount(_from);這是非同步的,您應該等待結果,否則您將獲得無效的隨機數,您的交易將不會被探勘

交易已送出,您得到了它的雜湊值,但您需要等待交易被處理 -等待交易被探勘並獲得結果的正確方法是什麼?

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