1

Issue: Using an account on testrpc I get 0 as my balance from within a program but I get the correct amount when I query balance at web3 command prompt:

//here I'm calling a function (shown below) from command line

contractInstance.queryBalance("0xf24qf4b3ae667a23f296f48eb92fa619310c95fd") { [String: '0'] s: 1, e: 0, c: [ 0 ] }

//here's I'm listing my accounts, just showing the first one to save space

web3.eth.accounts [ '0xf24qf4b3ae667a23f296f48eb92fa619310c95fd', ...

//here I am querying the balance directly at the command prompt

web3.eth.getBalance('0xf24qf4b3ae667a23f296f48eb92fa619310c95fd') { [String: '99999999999984495002'] s: 1, e: 19, c: [ 999999, 99999984495002 ] }

Here is the function call I'm making in my program:

function queryBalance(address addr) public constant returns (uint balance) {
    return balances[addr];
    }

What else I've tried contractInstance.queryBalance("0xf24qf4b3ae667a23f296f48eb92fa619310c95fd", {from: web3.eth.accounts[0], gas: 4500000}) { [String: '0'] s: 1, e: 0, c: [ 0 ] } and

Environment: web3.js environment on Mac with testrpc running

Thanks

0

We have that web3.eth.getBalance returns the balance in ethers of your address.

But in your function

function queryBalance(address addr) public constant returns (uint balance) {
    return balances[addr];
}

You are returning the value you have stored in the balances mapping. If you didn't store anything the default value is zero.

To obtain the balance in ether of your address in solidity

function queryBalance(address addr) public constant returns (uint balance) {
    return addr.balance;
}
  • Thanks for your clear response. That's what I was looking for. – Jazzmine Oct 28 '17 at 21:09

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.