0

MetaMask announced that they will stop injecting web3.js here. I know previously MetaMask will add a global variable in browser window.web3 and this global lowercase web3 variable will be deprecated soon. But in my DApp, I use MetaMask as a provider to the original Web3.js library (i.e. captical Web3). Will my usage be affected by MetaMask stopping injecting web3.js?

I also posted another question here, where I find web3.js cannot listen to on("changed") events in smart contract using MetaMask offered provider ethereum. If I still want to use MetaMask and Web3.js in my DApp, how can I get notified when events emitted in smart contracts are removed due to chain reorganization?

Thanks in advance.

1

If MetaMask will stop injecting web3 this means web3.currentProvider is not gonna work anymore properly. My advice is to handle the change and not getting your app crashed by downloading your own web3 library and using ethereum provider. This variable was introduced in the latest versions. Example:

const Web3 = require('web3'); // import web3 v1.0 constructor from node_modules
if(window.ethereum) {
    const web3Instance = new Web3(window['ethereum']);

    var methods = {
        getAddressETHBalance: function(address)    {
            return new Promise(function(resolve, reject) {
                resolve(web3Instance.eth.getBalance(address));
            });
        }
    };

    methods.getAddressETHBalance(<ADDRESS>);
} else {
    alert('Missing MetaMask extention.');
}
| improve this answer | |
  • Thanks @MiroslavNedelchev. I noticed that web3.js requires ws provider to subscribe events in smart contracts and MetaMask uses http connection to ethereum node. Will that be affected if I get web3 in this way const web3Instance = new Web3(window['ethereum']);? – Aaron Feb 20 at 9:07
  • I'm using const web3Instance = new Web3(window['ethereum']); in my dApps and I have no problems in getting events or listening to them. – Miroslav Nedelchev Feb 21 at 4:20
  • I can listen to events and get notified when new events are emitted. But I cannot get notified when events are removed due to reorg. Are you also be able to listen to removed events? – Aaron Feb 23 at 10:56

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.