2

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
1
  • is it geth --testnet or geth --testnetwork? try the first one
    – Badr Bellaj
    Jun 27, 2017 at 9:27

3 Answers 3

1

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

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

> eth.blockNumber

you should have lastest block number.

0
  1. you don't need to setup a fulled synced ETH node.
  2. go to infura and register
  3. make your signed transaction ( google it for how to do)
  4. especially, you must set a correct nonce parameter when signing transaction
  5. you got a transaction string ( maybe 255 byte length)
  6. broadcast it via infura interface.

it's done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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