1

I'm fairly new to programing and solidity, still on the learning curve of things here.... So right now I'm trying to check for the balance in Wei of the account I submit into the text field (Account). How do I achieve this functionality?

Check

My html code for this in index.html is;

<h1>Check</h1>
    <br><label for="acctAddress">Account:</label><input type="text" id="acctAddress" placeholder="e.g., 0x93e66d9baea28c17d9fc393b53e3fbdd76899dae"></input>
    <br><br><button id="check" onclick="checkBalance()">Check Balance</button>
    <br>
    <h3>You have <span class="green"><span id="acctBalance"></span> META</span>
    </h3>

The Metacoin Solidity in MetaCoin.sol function is;

function checkBalance(address _account) returns (uint balance) {
        return balances[_account];
    }   

The JavaScript function in app.js is

function checkBalance() {


    var acctAddress = document.getElementById("acctAddress").value;
    var check_element = document.getElementById("acctBalance");

    setStatus("Retreving Balance... (please wait)");

    web3.eth.getBalance(acctadd) 

      acctAddress = acctadd;


    }

};

2 Answers 2

1

I hope you are not mixing these 2 concepts:

  • The ether balance of your account, which you retrieve with web3.eth.getBalance(YOUR_ADDRESS).
  • The MetaCoin balance stored in the contract, which you retrieve with contractAddress.checkBalance(YOUR_ADDRESS).

So if your case, you want to:

function checkBalance() {
    var acctAddress = document.getElementById("acctAddress").value;
    var check_element = document.getElementById("acctBalance");

    setStatus("Retreving Balance... (please wait)");

    MetaCoin.deployed().checkBalance(acctAddress)
        .then(function(balance) {
            console.log(balance.toString(10));
            console.log(balance.toNumber());
        })
        .catch(function(error) {
            console.error(error);
        });
    }
};

Note that Truffle uses Ether-Pudding so your contract calls return promises.

2
  • Thanks! I managed to get it to display the balance although it seems to be wrong, I sent 5 to the address 0xcb439bd73060413b5b24227bd2bad7b4263039b6, or could it be a conversion thing?!. I also managed to add a field that returns the transaction hash but i get an error. See screen shot here -> ![SS_meta.png](postimg.org/image/gb1w2wpwp) and The code functions here -> ![bal_tran.png](postimg.org/image/zfxaplm5f)
    – Kizito
    Commented Oct 5, 2016 at 17:29
  • Well, of course, you are using web3.eth.getBalance(). Read my answer again. Commented Oct 6, 2016 at 8:47
1

Here is the way I'm doing it using Truffle and the Web3 API :

web3.eth.getBalance(address, 'latest', function(err, result) {
    if (err != null) {
        console.error("Error while retrieving the balance for address["+address+"]: "+err);
    }

    var balance = Number(web3.fromWei(result, "ether"));
    console.debug("Balance for address["+address+"]="+balance);
}); 

Please find below the full documentation :

web3.eth.getBalance

web3.eth.getBalance(addressHexString [, defaultBlock] [, callback]) Get the balance of an address at a given block.

Parameters

String - The address to get the balance of.

Number|String - (optional) If you pass this parameter it will not use the default block set with web3.eth.defaultBlock.

Function - (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.


EDIT: Full example

HTML

<label for="amount">Address:</label><input type="text" id="address" placeholder="0x***"></input>
<br><button id="send" onclick="checkBalance()">Check balance</button>
<label for="balance">Balance:</label><input type="text" id="balance" disabled></input>

Javascript

function checkBalance() {
    var address = document.getElementById("address").value;
    web3.eth.getBalance(address, 'latest', function(err, result) {
        if (err != null) {
            console.error("Error while retrieving the balance for address["+address+"]: "+err);
        }

        var balance = Number(web3.fromWei(result, "ether"));
        console.debug("Balance for address["+address+"]="+balance);

        var balance_element = document.getElementById("balance");
        balance_element.innerHTML = balance.valueOf();
    }); 
};
3
  • Thanks @gjeanmart Could you please show me the corresponding html block and solidity block as well?
    – Kizito
    Commented Oct 4, 2016 at 22:02
  • Account, balance and transaction are native functions in Ethereum, You don't need a smart contract (in Solidity) to implement a GetBalance(add) function For the html code. I added a full example which should work Commented Oct 4, 2016 at 22:16
  • Thanks! I managed to get it to display the balance although it seems to be wrong, I sent 5 to the address 0xcb439bd73060413b5b24227bd2bad7b4263039b6, or could it be a conversion thing?!. I also managed to add a field that returns the transaction hash but i get an error. See screen shot here -> ![SS_meta.png](postimg.org/image/gb1w2wpwp) and The code functions here -> ![bal_tran.png](postimg.org/image/zfxaplm5f)
    – Kizito
    Commented Oct 5, 2016 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.