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