[Ethereum] How to use latest web3 version from metamask

metamaskweb3js

Im new to web3 and Im using following code to connect my UI to web3.

// Modern dapp browsers...
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);
        try {
            // Request account access if needed
            console.log("window.ethereum");
            ethereum.enable();
        } catch (error) {
            // User denied account access...
        }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
        window.web3 = new Web3(web3.currentProvider);
        console.log("window.currentProvider");
        // Acccounts always exposed

    }
    // Non-dapp browsers...
    else {
        console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
    }

    console.log("version");
    var version = web3.version;
    console.log(version);

The output of console is this, which means it is using 0.20.3 version of web3.
enter image description here

who is actually injecting this version? If metamask is injecting then why it is not injecting the latest version which is 1.0.0-beta.

P.S: metamask version is 6.0.1

Best Answer

You can 'override' the version injected by Metamask, so that you get more cross-browser/cross-wallet compatibility.

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.min.js"></script>
<script>
    const web3 = new Web3(window.web3.currentProvider);
    web3.eth.getAccounts().then(...)
</script>

See for example the bounty given by Opera

Related Topic