Skip to content

Commit

Permalink
[Gateway] fix: loading screen and connect accounts on reload (#1703)
Browse files Browse the repository at this point in the history
* fix:
- no MM loading screen
- fetch connected accounts on reload
  • Loading branch information
Jennievon authored Dec 15, 2023
1 parent c7aee80 commit b528fcb
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,26 @@ export const WalletConnectionProvider = ({
const [accounts, setAccounts] = useState<Account[] | null>(null);
const [provider, setProvider] = useState({} as ethers.providers.Web3Provider);

const initialize = async () => {
if (!provider) {
const initialize = async (
providerInstance: ethers.providers.Web3Provider
) => {
if (!providerInstance) {
return showToast(
ToastType.INFO,
"Provider is required to initialize wallet connection."
);
}

try {
await ethService.checkIfMetamaskIsLoaded(provider);
await ethService.checkIfMetamaskIsLoaded(providerInstance);

const fetchedToken = await getToken(provider);
const fetchedToken = await getToken(providerInstance);
setToken(fetchedToken);

const status = await ethService.isUserConnectedToTenChain(fetchedToken);
setWalletConnected(status);

const accounts = await ethService.getAccounts(provider);
const accounts = await ethService.getAccounts(providerInstance);
setAccounts(accounts || null);
setVersion(await fetchVersion());
} catch (error) {
Expand Down Expand Up @@ -185,18 +187,16 @@ export const WalletConnectionProvider = ({
if (ethereum && ethereum.isMetaMask) {
const providerInstance = new ethers.providers.Web3Provider(ethereum);
setProvider(providerInstance);
initialize();
initialize(providerInstance);

ethereum.on("accountsChanged", () => {
fetchUserAccounts();
});
ethereum.on("accountsChanged", fetchUserAccounts);
} else {
setLoading(false);
}

return () => {
if (ethereum && ethereum.removeListener) {
ethereum.removeListener("accountsChanged", () => {
fetchUserAccounts();
});
ethereum.removeListener("accountsChanged", fetchUserAccounts);
}
};
}, []);
Expand Down

0 comments on commit b528fcb

Please sign in to comment.