1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import { UserStore } from 'store/modules/user' export function useWeb3() { async function getAccount(): Promise<any> { return new Promise((async (resolve, reject) => { await isMetamask() .then((res: string) => { console.log(res) window.ethereum .request({method: 'eth_requestAccounts'}) .then((res: string[]) => { onEvents() resolve(res) }) .catch((res: string[]) => { reject(res) }) }) .catch((res: string) => { reject(res) }) })) } function isMetamask(): Promise<string> { return new Promise((resolve, reject) => { if (typeof window.ethereum !== 'undefined') { resolve('MetaMask is installed!') } else { reject('MetaMask is uninstalled!') } }) } async function onEvents () { const user = UserStore() window.ethereum.on("accountsChanged", function(accounts: string[]) { if (accounts.length > 0) { user.setWalletAddress(accounts[0]) } }) } return { getAccount } }
|