MetaMask / metamask-extension

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites
https://metamask.io
Other
11.65k stars 4.77k forks source link

Will "await web3.eth.net.getId();" return only the network ID selected when I connected to the DApp, even if I change the network after connecting MetaMask? #25042

Open Number000001 opened 3 weeks ago

Number000001 commented 3 weeks ago

Describe the bug

Can someone help me? I'm having trouble with "await web3.eth.net.getId();". If I connect MetaMask to a DApps, even if I change the network, will "await web3.eth.net.getId();" only return the network ID selected when I connected to the DApps?

If I have not connected my MetaMask account to the DApps, when I change the network, "await web3.eth.net.getId();" returns the changed network ID, but if I connect MetaMask to the DApps, "await web3.eth.net.getId();" returns only the network ID selected when connecting to the DApps.

Is there a way to get the changed network ID even after connecting MetaMask to the DApps, other than "window.ethereum.on('chainChanged', async (chainId) =>"?

import Web3 from 'web3';

useEffect(() => {           

    if (window.ethereum) {
        checkNetworkAndLoadData();

        window.ethereum.on('chainChanged', async (chainId) => {
            console.log("chainId", chainId);
            window.location.reload();
        });
    }

}, []);

const checkNetworkAndLoadData = async () => {
    if (window.ethereum) {
        try {
            const web3 = new Web3(window.ethereum);
            const currentNetworkId = await web3.eth.net.getId();
            console.log("currentNetworkId", currentNetworkId);                
        } catch (error) {
            console.error("An error occurred:", error);
        }
    } 
};

Expected behavior

Is there a way, even after logging into MetaMask, to actively retrieve the updated network ID, similar to await web3.eth.net.getId();, when the network is changed?

Screenshots/Recordings

No response

Steps to reproduce

1.Log in to MetaMask in the Dapp. 2.Change the network. 3.Unable to retrieve the updated network ID using const currentNetworkId = await web3.eth.net.getId();, only returns the network ID at the time of login.

Error messages or log output

No response

Version

11.16.5

Build type

None

Browser

Chrome

Operating system

Windows

Hardware wallet

No response

Additional context

No response

Severity

No response

adonesky1 commented 3 weeks ago

Hey @Number000001 thanks for your report here. We have a fix for this issue that is rolling out with version 11.16.7

Number000001 commented 3 weeks ago

Hey @Number000001 thanks for your report here. We have a fix for this issue that is rolling out with version 11.16.7

Thank you for your reply.

However, for users who have not updated to version 11.16.7, if they change the network after connecting to DApps, the user still cannot obtain the network ID, so this is not a complete solution.

After investigating, I found that even if a user has not updated to version 11.16.7, after connecting MetaMask to DApps, only “window.ethereum.networkVersion” can get the changed network ID.

However, there is a warning that “window.ethereum.networkVersion” is scheduled to be deleted, so please let us know if there is an alternative.

Please do not delete “window.ethereum.networkVersion” if possible.

Thank you.