Skip to content

Instantly share code, notes, and snippets.

@pi0neerpat
Created March 26, 2020 15:23
Show Gist options
  • Select an option

  • Save pi0neerpat/a6f5e3898468787f80710bc976e91ea6 to your computer and use it in GitHub Desktop.

Select an option

Save pi0neerpat/a6f5e3898468787f80710bc976e91ea6 to your computer and use it in GitHub Desktop.
Survey "How I use MetaMask"
import { getErrorResponse } from '../general';
import { ethers } from 'ethers';
export const isWeb3EnabledBrowser = () => {
return (
typeof window !== 'undefined' && typeof window.ethereum !== 'undefined'
);
};
export const unlockWallet = async () => {
if (!isWeb3EnabledBrowser()) {
return { hasWallet: false, isUnlocked: false };
}
let walletAddress = '';
try {
window.ethereum.autoRefreshOnNetworkChange = false;
walletAddress = await window.ethereum.enable();
localStorage.setItem('walletAddress', walletAddress[0]);
const walletProvider = new ethers.providers.Web3Provider(
window.web3.currentProvider
);
const network = await walletProvider.getNetwork();
return {
hasWallet: true,
isUnlocked: true,
walletAddress: walletAddress[0],
network,
walletProvider
};
} catch (error) {
if (typeof window.ethereum === 'undefined') {
return { hasWallet: false, isUnlocked: false };
}
return {
hasWallet: true,
isUnlocked: false,
error: getErrorResponse(error, 'unlockWallet')
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment