2

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

Tom Hale
  • 3,107
  • 4
  • 20
  • 37
Phreak
  • 21
  • 1
  • 2
  • Use the search before, then do the question, this maybe can help you: https://ethereum.stackexchange.com/questions/29113/convert-ether-to-wei-without-web3 or this other https://ethereum.stackexchange.com/questions/6888/getting-contract-balance-over-json-rpc/6959#6959 – Gawey Oct 26 '17 at 07:58

4 Answers4

1

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.

Roman Kiselenko
  • 925
  • 5
  • 13
1

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
Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
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!

Shwet
  • 308
  • 1
  • 5
0

Use this package to convert ethunitconv