0

I wrote the following script to send Ether. I can't figure out why the balance is still the same after I send out the Ether. I re run this script multiple times but nothing changes with the balance. Even when I check the blockchain network at https://ropsten.etherscan.io/address/0xbf14644246ebb0209bdb03be0cd1a3b27fc009af

I don't see either the test Ethers that I mined nor the transaction.

I am currently using the test network geth --testnet

What is wrong?

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)
    }
});

This is what I get in the console:

coinbase address 0xbf14644246ebb0209bdb03be0cd1a3b27fc009af
balacance 8930000000000000000000
recipient address 0x15fd7829542d2e851a9787d76f027992b89b2398
coinbase address 0xbf14644246ebb0209bdb03be0cd1a3b27fc009af
success transaction address is 0x093b9306e7b0517e105168b9cd4f42344eddcb7cfa10e9f6d9a095cf9cd1e66c
balance 8930000000000000000000
  • is it geth --testnet or geth --testnetwork? try the first one – Badr Bellaj Jun 27 '17 at 9:27
0

Maybe, balance is the same, because tx wasn't mined yet, when you checking the balance 2nd time. Try to add function, that will check, that transaction mined and check the balance in callback. Something like this:

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);
});
0

Check geth is fully synced with network. You can check it on your geth console,

> eth.blockNumber

you should have lastest block number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.