2

Is there any way to get ethereum balance in "ether" rather than wei from JSON-RPC?

0

There is no way to return result from JSON-RPC in Ether, I suggest you convert result, here is an example:

Accourding to this table:

  • Wei = 10^0 Wei
  • Ada = 10^3 Wei
  • Babbage = 10^6 Wei
  • Shannon = 10^9 Wei
  • Szabo = 10^12 Wei
  • Finney = 10^15 Wei
  • Ether = 10^18 Wei

1 Ether is 1^18 = 1000000000000000000 Wei.

1 Wei is 0.000000000000000001 Ether.

Multiply the number of ether by 10^18.

0

As defined in the Ethereum JSON-RPC documentation, the return value for balance will be in Wei.

QUANTITY - integer of the current balance in wei.

And the result would looks like'

{
  "id":1,
  "jsonrpc": "2.0",
  "result": "0x0234c8a3397aab58" // 158972490234375000
}

The returned balance will be in a hexadecimal value. So the option you have is write a callback, eventlistner to catch the response and convert the result value to decimal and then multiply it with 10^(-18).

The following function I used to send a XHR request with JavaScript to a network and get the balance

getEthBal = function(url,accNo){

var data = JSON.stringify({
  "jsonrpc": "2.0",
  "method": "eth_getBalance",
  "params": [
    accNo,
    "latest"
  ],
  "id": 1
});

var xhr = new XMLHttpRequest();



xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    //resp = JSON.stringify(this.responseText);
    res = JSON.parse(this.responseText);
    console.log(res);
    EthBal = parseInt(res["result"],16) ;
    EthBal = EthBal * Math.pow(10,(-18));
    console.log("Eth balance = " +EthBal);
  }
});

xhr.open("POST", url);
xhr.setRequestHeader("content-type", "text/plain;charset=utf-8");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data); 

}

I used the function to get account balance in a private network as follow,

getEthBal("http://localhost:8545", "0xfe72330843652592e3ec45e925e85e03835f5cae")

And the result in console looked like,

{jsonrpc: "2.0", id: 1, result: "0x0"}id: 1jsonrpc: "2.0"result: "0x0"}
Eth Balance = 0
0

This is modification to code as suggested by Acchala

EthBal = parseInt(res["result"],16) ;
EthBal = EthBal * Math.pow(10,(-18));
console.log("Eth balance = " +EthBal);

Here you are parsing your balance in wei to an int and storing it in variable "EthBal" so EthBal variable is now an integer type variable

So,when "EthBal * Math.pow(10,(-18));" operation is performed and result is in decimal starting with 0 (i.e 0.15245145) it will not consider decimal places and the result would turn out to be 0

solution is to store the Result in new variable ( i.e var result = EthBal * Math.pow(10,(-18)); ) and then print out that result value.

Hope it helps!

0

Use this package to convert ethunitconv

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.