refactored network and token selection

This commit is contained in:
hueso
2024-10-09 21:52:59 -03:00
parent 1f003f19bc
commit e483fd537c
8 changed files with 103 additions and 102 deletions

View File

@@ -6,11 +6,12 @@ import { updateWalletStatus } from "./wallet";
import {
getProviderUrl,
isPossibleNetwork,
possibleChains,
network2Chain,
getP2PixAddress,
} from "./addresses";
import type { NetworkEnum } from "@/model/NetworkEnum";
import { Networks } from "@/model/Networks";
import { ethers } from "ethers";
const getProvider = (
@@ -57,11 +58,11 @@ const listenToWalletChange = (connection: any): void => {
const listenToNetworkChange = (connection: any) => {
const etherStore = useEtherStore();
connection.on("chainChanged", (networkChain: string) => {
connection.on("chainChanged", (networkChain: NetworkEnum) => {
console.log("Changed network!");
if (isPossibleNetwork(networkChain)) {
etherStore.setNetworkName(possibleChains[networkChain]);
etherStore.setNetworkName(networkChain);
updateWalletStatus();
} else {
window.alert("Invalid chain!");
@@ -69,7 +70,7 @@ const listenToNetworkChange = (connection: any) => {
});
};
const requestNetworkChange = async (network: string): Promise<boolean> => {
const requestNetworkChange = async (network: NetworkEnum): Promise<boolean> => {
const etherStore = useEtherStore();
if (!etherStore.walletAddress) return true;
@@ -77,7 +78,7 @@ const requestNetworkChange = async (network: string): Promise<boolean> => {
const window_ = window as any;
await window_.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: network2Chain[network] }], // chainId must be in hexadecimal numbers
params: [{ chainId: Networks[network].chainId }], // chainId must be in hexadecimal numbers
});
} catch {
return false;