2

I'm new using Ethereum. I set up a testchain to play around and have deployed some contracts there. My goal now would be to do something simple, like getting the balance of some of my testchain accounts in a website.

I found this code on the Ethereum documentation here (slightly adapted to get the scripts for web3 and bignumber):

<!doctype>
<html>

<head>
<script type="text/javascript" src="bignumber.js/bignumber.min.js"></script>
<script type="text/javascript" src="dist/web3-light.js"></script>
<script type="text/javascript">

    var Web3 = require('web3');
    var web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider());
    function watchBalance() {
        var coinbase = web3.eth.coinbase;
        var originalBalance = web3.eth.getBalance(coinbase).toNumber();
        document.getElementById('coinbase').innerText = 'coinbase: ' + coinbase;
        document.getElementById('original').innerText = ' original balance: ' + originalBalance + '    watching...';
        web3.eth.filter('latest').watch(function() {
            var currentBalance = web3.eth.getBalance(coinbase).toNumber();
            document.getElementById("current").innerText = 'current: ' + currentBalance;
            document.getElementById("diff").innerText = 'diff:    ' + (currentBalance - originalBalance);
        });
    }
</script>
</head>
<body>
    <h1>coinbase balance</h1>
    <button type="button" onClick="watchBalance();">watch balance</button>
    <div></div>
    <div id="coinbase"></div>
    <div id="original"></div>
    <div id="current"></div>
    <div id="diff"></div>
</body>
</html>

I have been running the following command to start geth:

geth --identity "TestNode" --genesis testchain/customGenesis.json --rpc --rpcport "8123" --rpccorsdomain "*" --datadir testchain --port "30303" --nodiscover --ipcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcapi "db,eth,net,web3" --autodag --networkid 1378 --nat "any" console

I thought that by adding

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

to the HTML file above it could work but if I navigate to http://localhost:8123 I get the following error:

{"id":-1,"jsonrpc":"2.0","error":{"code":-32600,"message":"Could not decode request"}}

I realize my question is probably related to some basic error but I have been reading and haven't managed to figure it out. Any answer or pointers to guides would be greatly appreciated!

1

The error code is explained as

-32600 Invalid Request The JSON sent is not a valid Request object.

One possible solution is to run geth in minimal commands e.g.:

geth --genesis testchain/customGenesis.json --rpc --rpcport "8123" --rpccorsdomain "*" --datadir testchain --nodiscover --networkid 1378  console

Connect with

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

If this doesn't work you can try --rpcaddr "127.0.0.1"

  • You put --rpcport "8123" and web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545');. Shouldn't they both be 8123? That is how I did it and it worked! – punddalinni Mar 28 '16 at 18:54
  • Any port will work, both needs to be same. Fixed the typo. – niksmac Mar 29 '16 at 1:48

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.