Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update wallet-ui unsupported MM version management #424

Merged
merged 16 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/wallet-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ function App() {
>
<ConnectModal />
</PopIn>
<PopIn isOpen={infoModalVisible} showClose={false}>
<PopIn
isOpen={infoModalVisible && !minVersionModalVisible}
showClose={false}
>
<ConnectInfoModal address={address} />
</PopIn>
<PopIn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import styled from 'styled-components';
import starknetSrc from 'assets/images/starknet-logo.svg';
import metamaskSrc from 'assets/images/metamask-fox-icon.svg';

export const Wrapper = styled.div`
display: flex;
Expand All @@ -23,6 +24,13 @@ export const StarknetLogo = styled.img.attrs(() => ({
margin-bottom: 32px;
`;

export const MetaMaskLogo = styled.img.attrs(() => ({
src: metamaskSrc,
}))`
width: 32px;
height: 32px;
`;

export const Title = styled.div`
text-align: center;
font-size: ${(props) => props.theme.typography.h3.fontSize};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import { MIN_METAMASK_VERSION } from 'utils/constants';
import {
Description,
MetaMaskLogo,
StarknetLogo,
Title,
Wrapper,
} from './MinVersionModal.style';
import { useHasMetamask } from 'hooks/useHasMetamask';
import { ConnectButton } from '../ConnectModal/ConnectModal.style';

export const MinVersionModalView = () => {
const { metaMaskUpgradeRequired } = useHasMetamask();
return (
<Wrapper>
<StarknetLogo />
<Title>A new version of the Starknet Snap is available</Title>
<Description>
To use this dapp, please install the latest version by following those
steps:
<ul>
<li>
Delete the current version in MetaMask by going in Settings {'>'}{' '}
Snaps {'>'} @consensys/starknet-snap {'>'} See details {'>'} Remove
Snap
</li>
<li>Refresh the page</li>
<li>
Click on connect, the new version will be proposed for installation.
</li>
</ul>
Note: Your account will be automatically recovered. Future upgrades will
be managed automatically
</Description>
{metaMaskUpgradeRequired ? (
<>
<Title>An upgrade of MetaMask is needed to use this dApp</Title>
<br />
<Description>
Please update to MetaMask Version {MIN_METAMASK_VERSION} or higher.
</Description>
<br />
<a href="https://metamask.io" target="_blank" rel="noreferrer">
<ConnectButton customIconLeft={<MetaMaskLogo />} onClick={() => {}}>
Go to MetaMask Website
khanti42 marked this conversation as resolved.
Show resolved Hide resolved
</ConnectButton>
</a>
</>
) : (
<>
<Title>A new version of the Starknet Snap is available</Title>
<Description>
To use this dapp, please install the latest version by following
those steps:
<ul>
<li>
Delete the current version in MetaMask by going in Settings{' '}
{'>'} Snaps {'>'} @consensys/starknet-snap {'>'} See details{' '}
{'>'} Remove Snap
</li>
<li>Refresh the page</li>
<li>
Click on connect, the new version will be proposed for
installation.
</li>
</ul>
Note: Your account will be automatically recovered. Future upgrades
will be managed automatically
</Description>
</>
)}
</Wrapper>
);
};
22 changes: 21 additions & 1 deletion packages/wallet-ui/src/hooks/useHasMetamask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useEffect, useState } from 'react';
import { useAppDispatch } from 'hooks/redux';
import { setProvider } from 'slices/walletSlice';
import { setMinVersionModalVisible } from 'slices/modalSlice';
import { enableLoadingWithMessage, disableLoading } from 'slices/UISlice';
import { MIN_METAMASK_VERSION } from 'utils/constants';
import semver from 'semver/preload';

interface MetaMaskProvider {
isMetaMask: boolean;
Expand Down Expand Up @@ -89,17 +92,23 @@ async function detectMetamaskSupport(windowObject: Window & typeof globalThis) {
export const useHasMetamask = () => {
const dispatch = useAppDispatch();
const [hasMetamask, setHasMetamask] = useState<boolean | null>(null);
const [metaMaskUpgradeRequired, setMetaMaskUpgradeRequired] = useState<
boolean | null
>(null);

useEffect(() => {
const init = async () => {
try {
dispatch(enableLoadingWithMessage('Detecting Metamask...'));
const provider = await detectMetamaskSupport(window);
// Use the new detection method

if (provider && (await isSupportSnap(provider))) {
dispatch(setProvider(provider));
setHasMetamask(provider != null);
if (await isMetaMaskUpgradeRequired(provider)) {
dispatch(setMinVersionModalVisible(true));
setMetaMaskUpgradeRequired(true);
}
} else {
dispatch(setProvider(null));
setHasMetamask(false);
Expand All @@ -116,9 +125,20 @@ export const useHasMetamask = () => {

return {
hasMetamask,
metaMaskUpgradeRequired,
};
};

const isMetaMaskUpgradeRequired = async (provider: any) => {
const clientVersion = await provider.request({
method: 'web3_clientVersion',
params: [],
});
const versionMatch = clientVersion.match(/MetaMask\/v(\d+\.\d+\.\d+)/);
const currentVersion = versionMatch[1];
return semver.lt(currentVersion, MIN_METAMASK_VERSION);
};

const isSupportSnap = async (provider: any) => {
try {
await provider.request({
Expand Down
2 changes: 2 additions & 0 deletions packages/wallet-ui/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ export const MIN_ACC_CONTRACT_VERSION = [0, 3, 0];
export const DUMMY_ADDRESS = '0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

export const DEFAULT_FEE_TOKEN = FeeToken.ETH;

export const MIN_METAMASK_VERSION = '12.5.0';
Loading