1

I'm trying to use MetaMask and web3js to talk to my smart contract. Below are my test codes.

This is without defining a web3 js file and I get the result "0.20.3" at console.

<script>
window.addEventListener("load", function() {
    // Checking if Web3 has been injected by the browser (Mist/MetaMask)
    if (typeof web3 !== "undefined") {
      // Use Mist/MetaMask's provider
      web3 = new Web3(web3.currentProvider);
      var version = web3.version.api;
      console.log(version);
    } else {
      console.log("No web3? You should consider trying MetaMask!");
      // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
      window.web3 = new Web3(
        new Web3.providers.HttpProvider("https://localhost:8545")
      );
    });
</script>

And this is with defining a web3 1.0 beta-34 file. But I get "undefined" at console.

<script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.min.js"></script>
<script>
        window.addEventListener('load', function() {
        // Check if Web3 has been injected by the browser:
        if (typeof web3 !== 'undefined') {
            // You have a web3 browser! Continue below!
            web3 = new Web3(web3.currentProvider);

            var version = web3.version.api;
            console.log(version); // "0.2.0"

            startApp(web3);

        } else {
            alert("No hay web3");
            // Warn the user that they need to get a web3 browser
            // Or install MetaMask, maybe with a nice graphic.
        }
    });
</script>

2 Answers 2

1

Adding to Peter's explaination, If you have metamask extension installed on your chrome and logged in, open the console and type web3.version.apiyou will get "0.20.3" as this is the version of web3 injected by metamask. This is what happens when you try the first time => "This is without defining a web3 js file and I get the result "0.20.3" at console"

In your second attempt, you are checking the validity of the web3.

if (typeof web3 !== 'undefined')

This web3 is the one injected by metamask. Not the one from your script. Later you update this

web3 = new Web3(web3.currentProvider)

If you comment this line, you will get console output as "0.20.3". After this line is executed, web3 variable is now having the Web3 instance of version "1.0.0-beta.34". So you need to use web3.version

Also, If you add a script file, ideally you should use the api as per usage definitions of that version

0

They had changed lots of apis in beta version of web3.

If you want to know the version of web3.js, you can use web3.version or check the version from package.json:

import { version } from 'web3/package.json';

If you want to know version of api protocol, check this (source code from v0.2):

return [
        new Property({
            name: 'version.node',
            getter: 'web3_clientVersion'
        }),
        new Property({
            name: 'version.network',
            getter: 'net_version',
            inputFormatter: utils.toDecimal
        }),
        new Property({
            name: 'version.ethereum',
            getter: 'eth_protocolVersion',
            inputFormatter: utils.toDecimal
        }),
        new Property({
            name: 'version.whisper',
            getter: 'shh_version',
            inputFormatter: utils.toDecimal
        })
];

Each version of protocol is a JSONRPC call.

Your Answer

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

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