Skip to content

Commit

Permalink
Fix listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Akim Mamedov committed Feb 27, 2024
1 parent ffd06a8 commit 32601e0
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions web/src/context/web3Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "react";
import WalletConnectProvider from "@walletconnect/web3-provider";
import Web3Modal from "web3modal";
import { providers } from "ethers";
import { ethers, providers } from "ethers";
import supportedChains from "../constants/chains";
import Web3 from "web3";
import { toast } from "react-toastify";
Expand Down Expand Up @@ -59,10 +59,6 @@ export const Web3ContextProvider = ({ children }) => {
return;
}

modal.on("chainChanged", () => {
console.log('modal.on("chainChanged", handleChainChanged);');
});

const web3Provider = new providers.Web3Provider(
modal,
{
Expand All @@ -71,18 +67,6 @@ export const Web3ContextProvider = ({ children }) => {
},
);

provider.on("network", () => {
console.log('provider.on("network", handleChainChanged); ');
});

provider.provider.on("chainChanged", () => {
console.log('provider.on("chainChanged", handleChainChanged); ');
});

web3Provider.on('network', () => {
console.log('adas');
});

const signer = web3Provider.getSigner();
const address = await signer.getAddress();
setProvider(web3Provider);
Expand All @@ -105,12 +89,13 @@ export const Web3ContextProvider = ({ children }) => {
}, []);

useEffect(() => {
if (provider?.on) {
if (provider instanceof providers.Web3Provider) {
const handleAccountsChanged = (accounts) => {
console.log("accountsChanged", accounts);
};

const handleChainChanged = (_hexChainId) => {
console.log("chainChanged", accounts);
let chainSupported = false;

supportedChains.forEach((chain) => {
Expand All @@ -131,16 +116,16 @@ export const Web3ContextProvider = ({ children }) => {
disconnect();
};

provider.on("accountsChanged", handleAccountsChanged);
provider.on("chainChanged", handleChainChanged);
provider.on("disconnect", handleDisconnect);
provider.provider.on("accountsChanged", handleAccountsChanged);
provider.provider.on("chainChanged", handleChainChanged);
provider.provider.on("disconnect", handleDisconnect);

// Subscription Cleanup
return () => {
if (provider.removeListener) {
provider.removeListener("accountsChanged", handleAccountsChanged);
provider.removeListener("network", handleChainChanged);
provider.removeListener("disconnect", handleDisconnect);
if (provider instanceof providers.Web3Provider) {
provider.provider.off("accountsChanged", handleAccountsChanged);
provider.provider.off("network", handleChainChanged);
provider.provider.off("disconnect", handleDisconnect);
}
};
}
Expand Down

0 comments on commit 32601e0

Please sign in to comment.