Go-Ethereum

為什麼我的交易沒有執行?

  • December 24, 2020

我編寫了以下腳本來發送 Ether。我無法弄清楚為什麼我發出乙太幣後餘額仍然相同。我多次重新執行此腳本,但餘額沒有任何變化。即使我在https://ropsten.etherscan.io/address/0xbf14644246ebb0209bdb03be0cd1a3b27fc009af檢查區塊鍊網路

我既沒有看到我開采的測試乙太幣,也沒有看到交易。

我目前正在使用測試網路geth --testnet

怎麼了?

var Web3 = require('web3');
var web3 = new Web3();

web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

var coinbase = web3.eth.coinbase;
console.log('coinbase address',coinbase);

var balance = web3.eth.getBalance(coinbase);
console.log('balacance', balance.toString(10));

var recipient_address =  web3.eth.accounts[1]
console.log('recipient address',recipient_address);

web3.personal.unlockAccount(coinbase, 'love2017');
console.log('coinbase address',coinbase);
web3.eth.sendTransaction({
   from: coinbase,
   to:recipient_address ,
   gas: 200000,
   value: web3.toWei(1, "ether"),

}, function (err, address) {
   if (!err) {
       console.log('success transaction address is', address); // "0x7f9fade1c0d57a7af66ab4ead7c2eb7b11a91385"
       var balance = web3.eth.getBalance(coinbase);
       console.log('balance', balance.toString(10));

   } else {
       console.log(err)
   }
});

這是我在控制台中得到的:

coinbase address 0xbf14644246ebb0209bdb03be0cd1a3b27fc009af
balacance 8930000000000000000000
recipient address 0x15fd7829542d2e851a9787d76f027992b89b2398
coinbase address 0xbf14644246ebb0209bdb03be0cd1a3b27fc009af
success transaction address is 0x093b9306e7b0517e105168b9cd4f42344eddcb7cfa10e9f6d9a095cf9cd1e66c
balance 8930000000000000000000

也許,餘額是一樣的,因為當您第二次檢查餘額時,還沒有開採 tx。嘗試添加功能,該功能將檢查該交易是否已開採並在回調中檢查餘額。像這樣的東西:

function getTxCallBack(txHash, cb) {
 web3.eth.getTransaction(txHash, function(err, txDetails) {
   if (err) console.log(err);

   if (!txDetails.blockNumber) {
     setTimeout(function() {
       getTxCallBack(txHash, cb);
     }, 2000)
   } else cb();
 });
};

web3.eth.sendTransaction({...}, function (err, address) {
   if (!err) {
       getTxCallBack(address, function() {
           var balance = web3.eth.getBalance(coinbase);
           console.log('balance', balance.toString(10));
       });
   } else console.log(err);
});

檢查 geth 是否與網路完全同步。你可以在你的 geth 控制台上查看它,

> eth.blockNumber

你應該有最新的塊號。

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