Skip to content

Commit

Permalink
add reducer for metamask connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemanthghs committed Sep 18, 2024
1 parent 236cfbc commit 52dad86
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 138 deletions.
41 changes: 18 additions & 23 deletions frontend/src/components/main-layout/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useAppDispatch, useAppSelector } from '@/custom-hooks/StateHooks';
import {
establishMetamaskConnection,
establishWalletConnection,
setConnectWalletOpen,
setIsLoading,
} from '@/store/features/wallet/walletSlice';
import { networks } from '@/utils/chainsInfo';
import { SUPPORTED_WALLETS } from '@/utils/constants';
import { NotSupportedMetamaskChainIds, SUPPORTED_WALLETS } from '@/utils/constants';
import { getLocalNetworks } from '@/utils/localStorage';
import {
connectSnap,
Expand Down Expand Up @@ -38,50 +39,44 @@ const ConnectWallet = () => {

const tryConnectWallet = async (walletName: string) => {
if (walletName === 'metamask') {
const notSupoortedMetamaskChainIds = ['agoric-3', 'evmos_9001-2', 'desmos-mainnet']
dispatch(setIsLoading());

try {
const snapInstalled = await getSnap();
if (!snapInstalled) {
await connectSnap(); // Initiates installation if not already present
}
} catch (error) {
console.log('Unable to install snap', error)
}

try {
for (let i = 0; i < networks.length; i++) {
console.time("Function execution time");

const chainId: string = networks[i].config.chainId;
if (notSupoortedMetamaskChainIds.indexOf(chainId) <= -1) {
if (NotSupportedMetamaskChainIds.indexOf(chainId) <= -1) {
try {
await experimentalSuggestChain(networks[i].config, {
await experimentalSuggestChain(networks[i].config, {
force: false,
});
dispatch(
establishMetamaskConnection({
walletName,
network: networks[i],
})
);
} catch (error) {
console.log('Error while connecting ', chainId);
}
}

console.timeEnd("Function execution time");
}


} catch (error) {
console.log('trying to connect wallet ', error);
}
} else {
dispatch(
establishWalletConnection({
walletName,
networks: [...networks, ...getLocalNetworks()],
})
);
}

dispatch(
establishWalletConnection({
walletName,
networks: [...networks, ...getLocalNetworks()],
})
);
};

return (
<Dialog
open={open}
Expand Down
28 changes: 16 additions & 12 deletions frontend/src/components/main-layout/FixedLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getSnap,
} from '@leapwallet/cosmos-snap-provider';
import {
establishMetamaskConnection,
establishWalletConnection,
setIsLoading,
unsetIsLoading,
Expand All @@ -33,7 +34,7 @@ import CustomLoader from '../common/CustomLoader';
import { TxStatus } from '@/types/enums';
import useGetShowAuthzAlert from '@/custom-hooks/useGetShowAuthzAlert';
import { initializeGA } from '@/utils/util';

import { NotSupportedMetamaskChainIds } from '@/utils/constants';

declare let window: WalletWindow;

Expand All @@ -44,7 +45,6 @@ const FixedLayout = ({ children }: { children: React.ReactNode }) => {
const isLoading = useAppSelector((state) => state.wallet.isLoading);

const walletState = useAppSelector((state) => state.wallet.status);
const notSupoortedMetamaskChainIds = ['agoric-3', 'evmos_9001-2', 'desmos-mainnet']

const tryConnectWallet = async (walletName: string) => {
if (walletName === 'metamask') {
Expand All @@ -57,29 +57,33 @@ const FixedLayout = ({ children }: { children: React.ReactNode }) => {

for (let i = 0; i < networks.length; i++) {
const chainId: string = networks[i].config.chainId;
if (notSupoortedMetamaskChainIds.indexOf(chainId) <= -1) {

if (NotSupportedMetamaskChainIds.indexOf(chainId) <= -1) {
try {
await experimentalSuggestChain(networks[i].config, {
force: false,
});
dispatch(
establishMetamaskConnection({
walletName,
network: networks[i],
})
);
} catch (error) {
console.log('Error while connecting ', chainId);
}
}

}
} catch (error) {
console.log('trying to connect wallet ', error);
}
} else {
dispatch(
establishWalletConnection({
walletName,
networks: [...networks, ...getLocalNetworks()],
})
);
}

dispatch(
establishWalletConnection({
walletName,
networks: [...networks, ...getLocalNetworks()],
})
);
};

useEffect(() => {
Expand Down
Loading

0 comments on commit 52dad86

Please sign in to comment.