Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth.getBalance() returns nonsensical value when called without parameters #197

Closed
tgerring opened this issue May 10, 2015 · 9 comments
Closed

Comments

@tgerring
Copy link
Contributor

According to https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgetbalance, eth.getBalance() requires the account as first parameter, however, when calling without any, it returns a very large number that doesn't appear to correspond to the actual account balances.

> checkAllBalances();
  eth.accounts[0]: 0xd1ade25ccd3d550a7eb532ac759cac7be09c2719   balance: 63.11848 ether
  eth.accounts[1]: 0xda65665fc30803cb1fb7e6d86691e20b1826dee0   balance: 0 ether
  eth.accounts[2]: 0xe470b1a7d2c9c5c6f03bbaa8fa20db6d404a0c32   balance: 1 ether
  eth.accounts[3]: 0xf4dd5c3794f1fd0cdc0327a83aa472609c806e99   balance: 6 ether
> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
'63.11848'
> web3.fromWei(eth.getBalance(), "ether")
'1000000000000000000000441.3628860000000142'

I would expect to return an error indicating that the first parameter is required when missing or make it print total of all account balances.

@frozeman
Copy link
Contributor

Thanks for submitting this. I just checked and when you call eth.getBalance() we are sending:

{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x0000000000000000000000000000000000000000","latest"],"id":5148}

Interestingly GO is returning:

{
    "id": 5148,
    "jsonrpc": "2.0",
    "result": "0x0b7abc627050305adf2c90fdda604f3a9908"
}

So i guess the problem here is on the go side, which should return 0x0

@tgerring
Copy link
Contributor Author

Even if there is an issue with the underlying implementation, it's not good that web3.js assumes to fill in "0x0000000000000000000000000000000000000000" when a required parameter is missing

In the scenario you described, the user would execute an invalid eth.getBalance() and receive an invalid return value of 0, when really there should be an error to let them know the parameter was missing.

@obscuren
Copy link
Contributor

So i guess the problem here is on the go side, which should return 0x0

Nope

Fun fact, someone send that amount to the zero'd address :-)

It's correct that a nothing address defaults to the zero'd address (on the go implementation). This doesn't mean that web3.js should allow it.

@frozeman
Copy link
Contributor

so its not a bug?
But we probably shouldn't default to '0x000..' thats right, w'll fix it.
@debris any comment on that? Is there a reason why our toAddress function can't return null or undefined if it got undefined?

@obscuren
Copy link
Contributor

No, not a bug. See ethereum/go-ethereum#919

@tgerring
Copy link
Contributor Author

@frozeman What are your thoughts on having this method return the balance of all accounts when called without a parameter? It would be nice to have a slightly easier way than defining the checkAllBalances() function each time you start geth

@alexvandesande
Copy link

@tgerring that's not a bad idea actually. I agree that undefined should be treated differently than 0, but this would even be better..

@obscuren
Copy link
Contributor

@frozeman @tgerring @alexvandesande that's something that we can easily add to geth. We already have startup js files. Feel free to send a PR on go-ethereum that includes this method.

@frozeman
Copy link
Contributor

I agree, get all balances etc, are not part of the API, this a simple scripts one can write, which we can add to the geth console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants