Here is an example using the Linux utility curl
to retrieve the balance of the contract over JSON-RPC:
curl -s -X POST --data '{"jsonrpc":"2.0", "method":"eth_getBalance", "params":["0xc5910bcb2442e84845aa98b20ca51e8f5d2bee23", "latest"], "id":1}' http://localhost:8545
You will have to start geth
with the --rpc
parameter to run the curl
program above, e.g.,
geth --testnet --rpc console
The results you receive will be the number of weis in hexadecimal format:
{"jsonrpc":"2.0","id":1,"result":"0x58d15e17628000f"}
Converting this number using a Hexadecimal to Decimal Converter results in a decimal number of 400000000000000015
weis.
To calculate the number of ethers, divide the decimal number by 1e18 and your result is 0.400000000000000015
ethers.
And checking this balance using the geth
JavaScript console:
> web3.fromWei(eth.getBalance("0xc5910bcb2442e84845aa98b20ca51e8f5d2bee23"), "ether")
> 0.400000000000000015
Here is my Perl code to convert the hex wei number to a double ether number:
sub hexToDouble{
my $param = shift;
$param =~ s/^0x//;
my $fullnum = 0.0;
while ($param =~ /(.)/g) {
my $num = hex($1);
$fullnum = $fullnum * 16 + $num;
}
$fullnum *= 1e-18;
return $fullnum;
}