I am a bit of a javascript noob and have some questions about using web3js and Metamask.
My objective (for now) is to display the ether balance as displayed on my Metamask (where I am logged in).
From the Metamask Developers FAQ I took a snipped of code and made some changes. I am able to get the correct address, but the balance is null.
Here is the complete code:
<html>
<body>
<button id="buttonBalance">Click to get balance</button>
// Bundling my version of web3
<script type="text/javascript" src="scripts/web3.min.js"></script>
<script type="text/javascript">
window.addEventListener("load", function() {
if (typeof web3 === "undefined") {
console.log("No web3 provider found");
} else {
web3 = new Web3(web3.currentProvider);
console.log("Using web wallet");
console.log("address = " + web3.eth.coinbase);
button = document.getElementById("buttonBalance")
button.onclick = function tmp()
{
web3.eth.getBalance(
web3.eth.coinbase,
function tmp2 (balance)
{
console.log("balance=" + balance);
});
};
}
}) </script>
</body>
</html>
The above code shows in the console (after clicking the button):
Using web wallet
address = 0x4b6efab615a5e2c4002d23318b891d7bf673e1d34
balance=null
Question 1: Why does the above code not work? I get no syntax or "async" errors and Metamask shows my balance correctly ... ?
Question 2: From the best practices it is stated to "bundle the version of web3 that is used during development". So I downloaded web3.min.js and "bundled" it (see the comment in the code above). Now I am unsure if that implementation is actually used under the hood. Can I be sure, can I print out the web3 version or so?