Created
March 26, 2020 15:23
-
-
Save pi0neerpat/a6f5e3898468787f80710bc976e91ea6 to your computer and use it in GitHub Desktop.
Survey "How I use MetaMask"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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