Skip to content

Commit

Permalink
Merge pull request #36 from dsrvlabs/feature/arbitrum-mainnet
Browse files Browse the repository at this point in the history
feat: edit switch logic
  • Loading branch information
markjung96 authored Sep 6, 2024
2 parents edd8136 + 463c1e5 commit b7893a8
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/components/arbitrum/MetamaskConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,46 +180,70 @@ export const MetamaskConnect: React.FunctionComponent<InterfaceProps> = ({
useEffect(() => {
const activateMetamask = async () => {
if (!network) return null;
await switchNetwork(network.value);
const currentChainId = await getChainId();
if (currentChainId !== network.value) await switchNetwork(network.value);
else {
const currentAccount = await getAccount();
setAccount(currentAccount);
setInfo(currentAccount, network);
}
};
if (active) activateMetamask();
}, [active]);
}, [active, network]);

useEffect(() => {
const init = async () => {
const chainId = await getChainId();
const targetNetwork = networks.find((net) => net.value === chainId);
if (!targetNetwork) await switchNetwork();
else {
const currentAccount = await getAccount();
setAccount(currentAccount);
setInfo(currentAccount, targetNetwork);
try {
const chainId = await getChainId();
const targetNetwork = networks.find((net) => net.value === chainId);
if (!targetNetwork) await switchNetwork();
else {
const currentAccount = await getAccount();
setAccount(currentAccount);
setInfo(currentAccount, targetNetwork);
}
} catch (error) {
log.error('Failed to initialize MetamaskConnect', error);
}
};

init();

if (!ethereum) return;
ethereum.on('chainChanged', async (_chainId: string) => {
const targetNetwork = networks.find((net) => net.value === _chainId);
if (!targetNetwork) window.location.reload();
else {
const currentAccount = await getAccount();
setAccount(currentAccount);
setInfo(currentAccount, targetNetwork);
try {
const targetNetwork = networks.find((net) => net.value === _chainId);
if (!targetNetwork) window.location.reload();
else {
const currentAccount = await getAccount();
setAccount(currentAccount);
setInfo(currentAccount, targetNetwork);
}
} catch (error) {
log.error('Failed to execute change chain logic', error);
}
});

ethereum.on('accountsChanged', async (accounts: string[]) => {
if (accounts.length === 0) {
setAccount('');
setBalance('');
} else {
const currentAccount = await getAccount();
setAccount(currentAccount);
setBalance('');
setActive(false);
try {
if (accounts.length === 0) {
setAccount('');
setBalance('');
} else {
const currentAccount = await getAccount();
setAccount(currentAccount);
if (network) setInfo(currentAccount, network);
else init();
}
} catch (error) {
log.error('Failed to execute change account logic', error);
}
});

return () => {
ethereum.removeAllListeners('chainChanged');
ethereum.removeAllListeners('accountsChanged');
};
}, []);

return (
Expand Down

0 comments on commit b7893a8

Please sign in to comment.