Tracking Netwroks - Avalanche error

Hi,
I followed Netwrok tracking video. I added avalanche but when i switch chain to avax in metamask website reloads as it should but i get back chianID 1 instead of 43114. It works for all other chains (i switch to polygon i get 137 back and for bsc i get 137 back) except avalanche where i get back 1 which is ethereums chain ID.

Code:

//NETWORK:
checkWeb3();

function displayMessage(messageType, message, color, textColor){
    messages = {
        "00":`<span class="bg-${color} text-${textColor} ms-2 rounded-pill pt-1 pb-1 ps-2 pe-2 d-inline-block mt-1">${message}</span>`,
        "01":`<div class= "alert alert-danger text-center fs-1"> ${message} </div>`
    }
    if(messageType == "01") {
        document.getElementById("resultSpace").innerHTML = messages[messageType];
        alert("Please install MetaMask");
    }else{
        document.getElementById("network").innerHTML = messages[messageType];
    }
}

async function checkWeb3(){
    const ethereum = window.ethereum;
    if(!ethereum || !ethereum.on) {
        displayMessage("01", "<strong>This App Requires MetaMask. Please Install MetaMask!</strong>");
    }else{
        setWeb3Environment()
    }
}

function setWeb3Environment(){
    web3 = new Web3(window.ethereum);
    getNetwork();
    monitorNetwork();
}

async function getNetwork(){
    chainID = await web3.eth.net.getId();
    currentChain = getNetworkSymbol(chainID);
    await getSupportedTokens();
    displayMessage("00","<strong>"+ getNetworkName(chainID) + "</strong>", getNetworkColor(chainID), getNetworkText(chainID));
}

function getNetworkName(chainID){
    networks = {
        1:"Ethereum",
        56:"Binance SC",
        137:"Polygon",
        43114:"Avalanche",
        //Testnets:
        3:"Ropsten",
        4:"Rinkeby",
        5:"Goerli",
        42:"Kovan",
        97:"BSC Testnet",
        80001:"Mumbai"
    }
    return networks[chainID];
}
function getNetworkColor(chainID){
    colors = {
        1:"primary",
        56:"warning",
        137:"info",
        43114:"danger",
        //Testnets:
        3:"black",
        4:"black",
        5:"black",
        42:"black",
        97:"black",
        80001:"black"
    }
    return colors[chainID];
}
function getNetworkText(chainID){
    textColors = {
        1:"white",
        56:"black",
        137:"black",
        43114:"white",
        //Testnets:
        3:"white",
        4:"white",
        5:"white",
        42:"white",
        97:"white",
        80001:"white"
    }
    return textColors[chainID];
}
function getNetworkSymbol(chainID){
    symbols = {
        1:"eth",
        56:"bsc",
        137:"polygon",
        43114:"avalanche",
        //Testnets:
        3:"ropsten",
        4:"rinkeby",
        5:"goerli",
        42:"kovan",
        97:"0x61",
        80001:"mumbai"
    }
    return symbols[chainID];
}
function monitorNetwork(){
    Moralis.onChainChanged(function(){
        window.location.reload()
    })
}

Regards,
Tijan

1 Like

You can use more functions from the Moralis SDK:

Take a look at these topics:
image
https://docs.moralis.io/moralis-server/web3/web3#ensureweb-3-isinstalled

There is no need to reload the page after chaniging the network now.

It still returns me 1 (with Moralis SDK), even though i change network/chain.

Could you share the full code or repo?

Full code has like 1500+ lines. But checkWeb3() is called on page reload.

Try this:

//NETWORK:
checkWeb3();

function displayMessage(messageType, message, color, textColor){
    messages = {
        "00":`<span class="bg-${color} text-${textColor} ms-2 rounded-pill pt-1 pb-1 ps-2 pe-2 d-inline-block mt-1">${message}</span>`,
        "01":`<div class= "alert alert-danger text-center fs-1"> ${message} </div>`
    }
    if(messageType == "01") {
        document.getElementById("resultSpace").innerHTML = messages[messageType];
        alert("Please install MetaMask");
    }else{
        document.getElementById("network").innerHTML = messages[messageType];
    }
}

async function checkWeb3(){
    const isWeb3Active = Moralis.ensureWeb3IsInstalled()
    if (isWeb3Active) {
        setWeb3Environment();
    } else {
       displayMessage("01", "<strong>This App Requires MetaMask. Please Install MetaMask!</strong>")
    }
}

async function setWeb3Environment(){
    web3 = await Moralis.enable();
    await getNetwork();
    monitorNetwork();
}

async function getNetwork(){
    chainID = await Moralis.getChainId();
    currentChain = getNetworkSymbol(chainID);
    await getSupportedTokens();
    displayMessage("00","<strong>"+ getNetworkName(chainID) + "</strong>", getNetworkColor(chainID), getNetworkText(chainID));
}

function getNetworkName(chainID){
    networks = {
        1:"Ethereum",
        56:"Binance SC",
        137:"Polygon",
        43114:"Avalanche",
        //Testnets:
        3:"Ropsten",
        4:"Rinkeby",
        5:"Goerli",
        42:"Kovan",
        97:"BSC Testnet",
        80001:"Mumbai"
    }
    return networks[chainID];
}
function getNetworkColor(chainID){
    colors = {
        1:"primary",
        56:"warning",
        137:"info",
        43114:"danger",
        //Testnets:
        3:"black",
        4:"black",
        5:"black",
        42:"black",
        97:"black",
        80001:"black"
    }
    return colors[chainID];
}
function getNetworkText(chainID){
    textColors = {
        1:"white",
        56:"black",
        137:"black",
        43114:"white",
        //Testnets:
        3:"white",
        4:"white",
        5:"white",
        42:"white",
        97:"white",
        80001:"white"
    }
    return textColors[chainID];
}
function getNetworkSymbol(chainID){
    symbols = {
        1:"eth",
        56:"bsc",
        137:"polygon",
        43114:"avalanche",
        //Testnets:
        3:"ropsten",
        4:"rinkeby",
        5:"goerli",
        42:"kovan",
        97:"0x61",
        80001:"mumbai"
    }
    return symbols[chainID];
}
function monitorNetwork(){
    Moralis.onChainChanged(function(chain){
        console.log(chain)
    })
}

And please share your logs/messages.

Also check your chain settings in the metamask

Now it is telling me my wallet is not installed, but i get right chain id now (when i change network)
Do i share logs from console or dashboard ?

Sorry, my bad. In your case you need to use await Moralis.isMetaMaskInstalled();
Instead of const isWeb3Active = Moralis.ensureWeb3IsInstalled()

1 Like

Now it seems it is working thanks for help :star_struck: !

1 Like

You are welcome! Happy BUIDLing :man_mechanic:

1 Like

One question.

1. What do you get back if you connect to a moralis website with avax chain and write in console chainID = await Moralis.getChainId(); . I get back 1. This is very problematic, because when user logs in I get and compute everything for eth chain (cus 1 is returned before). For all other chains work perfectly.

  1. And also why does this
Moralis.onChainChanged(function(chain){
        currentChain = chian;
})

return completly different chian id than chainID = await Moralis.getChainId(); (it returns same chain but with different naming -> 1 and 0x1) ?

1 is decimal format
0x1 is hex format

1 Like

If i use Moralis.getChainId() on Avalance I get 1 too. I don’t know why :sweat_smile:

Okay, I also found the difficulty that if you want to use .getChainId you need to enable web3.
I suggest you to use the classic way in your case:

if (window.ethereum) {
   const chainId = window.ethereum.chainId;
}

The method will return you chainId in hex format 0xa86a

1 Like

Now it is fully working. Thanks again !

1 Like

Happy to help :man_factory_worker:

1 Like

the function switchNetwork() eth to avax doesn’t work for me .but the other chains work.

Hey @ihxx

Please for each question open a new topic :raised_hands:
But I guess we can easy solve your problem:

Moralis.switchNetwork("0xa86a") works nice, I just tested it. If your Metamask doesn’t have avalanche it will not switch to it. You need to add it manually.

Take a look at https://docs.moralis.io/moralis-server/web3/web3#addnetwork

1 Like