1

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?

1

Question 1:

The callback function you pass to getBalance takes two arguments. First one is the error that occured and second one the result. So for now since you only have one parameter for your function you actually print the error (which is null).

Try this:

web3.eth.getBalance(
     web3.eth.coinbase,
     function tmp2 (error, balance)
     {
         if (error) console.log("error" + error);
         console.log("balance=" + balance);
     });

Question 2:

I'd say if you bundled the code it will use the one that you just pass there, because where else should it fetch the dependency from... But to make sure you can call web3.version.api and make sure.

Hope this answers your questions!

  • Spot on about question 1, thanks! About question 2 however: The "bundling" above does not seem to take effect. Because when I download a different web3js version and "bundle" that one explicitely, then the web3.version.api still reports the previous version when I log it to the console ... So the question remains: How do I bundle my own version? Maybe (hopefully) I'm just missing something very simple ... – Ytsen de Boer May 17 '18 at 8:23
  • Actually, my second question merits a thread of its own, so I created that here: ethereum.stackexchange.com/questions/48729/… I'll leave this thread as is to keep consistency with the question and the comments. Moderators please feel free to redirect. – Ytsen de Boer May 17 '18 at 9:24
  • good to hear that I could answer the first question. About the second one I am still a little puzzled because I am quite sure, that your bundling is correct, but probably the problem there is because you are inside adding the eventListener to window - can you try to add another call outside of it and check version there? Unfortunately I can not test myself right now... feel free to accept the answer for this thread if you created another one :) – Kai Kälberer May 17 '18 at 9:33

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.