fix connect provider call and lint
Co-authored-by: brunoedcf <brest.dallacosta@outlook.com>
This commit is contained in:
@@ -4,71 +4,69 @@ import { NetworkEnum } from "@/model/NetworkEnum";
|
||||
import { updateWalletStatus } from "./wallet";
|
||||
|
||||
const getProviderUrl = (): string => {
|
||||
const etherStore = useEtherStore();
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const possibleProvidersUrls: {[key: string]: string} = {
|
||||
Ethereum: import.meta.env.VITE_GOERLI_API_URL,
|
||||
Polygon: import.meta.env.VITE_MUMBAI_API_URL,
|
||||
Localhost: import.meta.env.VITE_GOERLI_API_URL
|
||||
}
|
||||
const possibleProvidersUrls: { [key: string]: string } = {
|
||||
Ethereum: import.meta.env.VITE_GOERLI_API_URL,
|
||||
Polygon: import.meta.env.VITE_MUMBAI_API_URL,
|
||||
Localhost: import.meta.env.VITE_GOERLI_API_URL,
|
||||
};
|
||||
|
||||
return possibleProvidersUrls[etherStore.networkName]
|
||||
}
|
||||
return possibleProvidersUrls[etherStore.networkName];
|
||||
};
|
||||
|
||||
const getProvider = (): ethers.providers.Web3Provider | ethers.providers.JsonRpcProvider => {
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
|
||||
if (!connection)
|
||||
return new ethers.providers.JsonRpcProvider(getProviderUrl()); // alchemy provider
|
||||
const getProvider = ():
|
||||
| ethers.providers.Web3Provider
|
||||
| ethers.providers.JsonRpcProvider => {
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
|
||||
return new ethers.providers.Web3Provider(connection); // metamask provider
|
||||
if (!connection)
|
||||
return new ethers.providers.JsonRpcProvider(getProviderUrl()); // alchemy provider
|
||||
|
||||
return new ethers.providers.Web3Provider(connection); // metamask provider
|
||||
};
|
||||
|
||||
const connectProvider = async (): Promise<void | null> => {
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
const provider = getProvider();
|
||||
|
||||
if(!(provider instanceof ethers.providers.Web3Provider)){
|
||||
window.alert("Please, connect to metamask extension");
|
||||
return null;
|
||||
}
|
||||
|
||||
await updateWalletStatus();
|
||||
// await updateValidDeposits();
|
||||
// await updateDepositAddedEvents();
|
||||
// await updateLockAddedEvents();
|
||||
// await updateLockReleasedEvents();
|
||||
|
||||
listenToNetworkChange(connection);
|
||||
listenToWalletChange(connection);
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
const provider = getProvider();
|
||||
|
||||
if (!(provider instanceof ethers.providers.Web3Provider)) {
|
||||
window.alert("Please, connect to metamask extension");
|
||||
return null;
|
||||
}
|
||||
|
||||
await updateWalletStatus();
|
||||
// await updateValidDeposits();
|
||||
// await updateDepositAddedEvents();
|
||||
// await updateLockAddedEvents();
|
||||
// await updateLockReleasedEvents();
|
||||
|
||||
listenToNetworkChange(connection);
|
||||
listenToWalletChange(connection);
|
||||
};
|
||||
|
||||
const listenToWalletChange = (connection: any): void => {
|
||||
connection.on("accountsChanged", async () => {
|
||||
await updateWalletStatus();
|
||||
});
|
||||
}
|
||||
|
||||
connection.on("accountsChanged", async () => {
|
||||
await updateWalletStatus();
|
||||
});
|
||||
};
|
||||
|
||||
const listenToNetworkChange = (connection: any) => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const possibleNetworks: {[key: string]: NetworkEnum} = {
|
||||
"0x5": NetworkEnum.ethereum,
|
||||
"0x13881": NetworkEnum.polygon,
|
||||
"0x7a69": NetworkEnum.localhost,
|
||||
}
|
||||
|
||||
connection.on("chainChanged", (networkChain: string) => {
|
||||
if (Object.keys(possibleNetworks).includes(networkChain)){
|
||||
etherStore.setNetworkName(possibleNetworks[networkChain]);
|
||||
}
|
||||
});
|
||||
}
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
export {
|
||||
getProvider,
|
||||
listenToNetworkChange
|
||||
}
|
||||
const possibleNetworks: { [key: string]: NetworkEnum } = {
|
||||
"0x5": NetworkEnum.ethereum,
|
||||
"0x13881": NetworkEnum.polygon,
|
||||
"0x7a69": NetworkEnum.localhost,
|
||||
};
|
||||
|
||||
connection.on("chainChanged", (networkChain: string) => {
|
||||
if (Object.keys(possibleNetworks).includes(networkChain)) {
|
||||
etherStore.setNetworkName(possibleNetworks[networkChain]);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export { getProvider, connectProvider, listenToNetworkChange };
|
||||
|
||||
Reference in New Issue
Block a user