Skip to content

Commit

Permalink
add hasFlask check
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Dec 4, 2023
1 parent 5846532 commit a7d189c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@emotion/react": "latest",
"@emotion/styled": "latest",
"@metamask/detect-provider": "^2.0.0",
"@metamask/providers": "latest",
"@mui/icons-material": "latest",
"@mui/lab": "latest",
Expand Down
9 changes: 0 additions & 9 deletions packages/dapp/src/App.test.tsx

This file was deleted.

53 changes: 35 additions & 18 deletions packages/dapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getChain } from './util/getChain';
import { installPolkaMask } from './util/installPolkaMask';
import logo from './assets/logo.svg';
import SignMessage from './SignMessage';
import { hasFlask } from './util/hasFlask';

interface TabPanelProps {
children?: React.ReactNode;
Expand Down Expand Up @@ -59,6 +60,7 @@ export default function App() {
const [isPolkaMaskInstalled, setIsSnapInstalled] = useState<boolean>();
const [balances, setBalances] = useState<DeriveBalancesAll>();
const [token, setToken] = useState<string>();
const [hasFlaskDetected, setHasFlask] = useState<boolean>();

useEffect(() => {
if (!api) {
Expand Down Expand Up @@ -104,16 +106,25 @@ export default function App() {
}, [account]);

const handleInstallClick = useCallback(() => {
if (hasFlaskDetected === false) {
const FLASK_URL = 'https://chromewebstore.google.com/detail/metamask-flask-developmen/ljfoeinjpaedjfecbmggjgodbgkmjkjk';

window.open(FLASK_URL, '_blank');

return;
}

installPolkaMask().then((installedSnap) => {
installedSnap && setIsSnapInstalled(installedSnap[DEFAULT_SNAP_ORIGIN]?.enabled)
});
}, []);
}, [hasFlaskDetected]);

useEffect(() => {
handleInstallClick()
}, [handleInstallClick]);

useEffect(() => {
hasFlask().then(setHasFlask)
// getSnaps().then((snaps) => {
// if (snaps?.length) {
// const isDefaultSnapInstalled = !!Object.keys(snaps).find((id) => POLKAMASK_SNAP_IDS.includes(id));
Expand Down Expand Up @@ -154,29 +165,35 @@ export default function App() {
disabled={isPolkaMaskInstalled}
sx={{ fontSize: '16px', width: 'fit-content' }}
>
Install PolkaMask Snap
Install {hasFlaskDetected ? 'PolkaMask Snap' : 'Flask'}
</Button>
</Grid>
</Grid>
<Grid container
sx={{ flexGrow: 1, bgcolor: 'background.paper', display: 'flex', height: 224 }}
>
<Grid item sx={{ width: '20%' }}>
<Tabs
orientation="vertical"
variant="scrollable"
value={value}
onChange={handleChange}
aria-label="Vertical tabs example"
sx={{ borderRight: 1, borderColor: 'divider' }}
>
<Tab label="Transfer Fund" {...a11yProps(0)} />
<Tab label="Sign Message" {...a11yProps(1)} />
<Tab label="Other" {...a11yProps(2)} />
</Tabs>
</Grid>
{!isPolkaMaskInstalled
? <Grid container justifyContent="center" p='auto' m='auto' >
<Typography variant="h5" sx={{ fontWeight: '500' }}>
{hasFlaskDetected ? 'PolkaMask Snap' : 'Flask'} is not installed. Please install it using the button above.
</Typography>
</Grid>
: <Grid item sx={{ width: '20%' }}>
<Tabs
orientation="vertical"
variant="scrollable"
value={value}
onChange={handleChange}
aria-label="Vertical tabs example"
sx={{ borderRight: 1, borderColor: 'divider' }}
>
<Tab label="Transfer Fund" {...a11yProps(0)} />
<Tab label="Sign Message" {...a11yProps(1)} />
<Tab label="Other" {...a11yProps(2)} />
</Tabs>
</Grid>
}
<Grid item sx={{ width: '79%' }}>

<TabPanel value={value} index={0}>
<TransferFund
api={api}
Expand All @@ -189,7 +206,7 @@ export default function App() {
/>
</TabPanel>
<TabPanel value={value} index={1}>
<SignMessage
<SignMessage
api={api}
account={account}
isPolkaMaskInstalled={isPolkaMaskInstalled}
Expand Down
7 changes: 1 addition & 6 deletions packages/dapp/src/SignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function SignMessage({ api, account, balances, currentChainName, formatted, isPo
return (
<Grid container item justifyContent='center'>
{isPolkaMaskInstalled
? <>
&& <>
<Grid container justifyContent="center" py='5px'>
<Typography variant="h5" sx={{ fontWeight: '500' }}>
Sign a Message and Verify the Signature
Expand Down Expand Up @@ -112,11 +112,6 @@ function SignMessage({ api, account, balances, currentChainName, formatted, isPo
</LoadingButton>
</Grid>
</>
: <Grid container justifyContent="center" p='auto' m='auto' >
<Typography variant="h5" sx={{ fontWeight: '500' }}>
PolkaMask snap is not installed. Please install it using the button above.
</Typography>
</Grid>
}
{error && (
<Grid item container justifyContent='center' sx={{ textAlign: 'center', pt: '10px' }}>
Expand Down
7 changes: 1 addition & 6 deletions packages/dapp/src/TransferFund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function TransferFund({ api, account, balances, currentChainName, formatted, isP
return (
<Grid container item justifyContent='center'>
{isPolkaMaskInstalled
? <>
&& <>
<Grid container justifyContent="center" py='5px'>
<Typography variant="h5" sx={{ fontWeight: '500' }}>
A Simple Fund Transfer
Expand Down Expand Up @@ -182,11 +182,6 @@ function TransferFund({ api, account, balances, currentChainName, formatted, isP
</Grid>
</Grid>
</>
: <Grid container justifyContent="center" p='auto' m='auto' >
<Typography variant="h5" sx={{ fontWeight: '500' }}>
PolkaMask snap is not installed. Please install it using the button above.
</Typography>
</Grid>
}
{/* {state.error && (
<Grid item container>
Expand Down
File renamed without changes
25 changes: 25 additions & 0 deletions packages/dapp/src/util/hasFlask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import detectEthereumProvider from '@metamask/detect-provider';

/**
* to check if metamask Flask is installed of not
*/
export async function hasFlask() {
// This resolves to the value of window.ethereum or null
const provider = await detectEthereumProvider();

// web3_clientVersion returns the installed MetaMask version as a string
const isFlask = (
await (provider as any)?.request({ method: 'web3_clientVersion' })
)?.includes('flask');

if (provider && isFlask) {
console.log('MetaMask Flask successfully detected!');

// Now you can use Snaps!
return true;
} else {
console.error('Please install MetaMask Flask!');

return false;
}
}

0 comments on commit a7d189c

Please sign in to comment.