From 88d0776e76aed52054e5bb581de3a7d7580b94c5 Mon Sep 17 00:00:00 2001 From: biendeveloper <91308782+biendeveloper@users.noreply.github.com> Date: Sat, 1 Jan 2022 18:28:37 +0530 Subject: [PATCH 1/2] Web3 Modal --- .gitignore | 1 + web/app/index.html | 6 ++++++ web/lib/utils.js | 50 +++++++++++++++++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index a707c77..3739a40 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ !LICENSE web/lib/config.js web/lib/rollup-config.js +node_modules diff --git a/web/app/index.html b/web/app/index.html index d006605..fbe56b6 100644 --- a/web/app/index.html +++ b/web/app/index.html @@ -15,6 +15,12 @@ Habitat - Communities + + + + + + diff --git a/web/lib/utils.js b/web/lib/utils.js index 8e7d514..c9f01a7 100644 --- a/web/lib/utils.js +++ b/web/lib/utils.js @@ -33,6 +33,43 @@ const PERMIT_STRUCT_DAI = [ { name: 'allowed', type: 'bool' }, ]; +const Web3Modal = window.Web3Modal.default; +const WalletConnectProvider = window.WalletConnectProvider.default; +const Fortmatic = window.Fortmatic; +const evmChains = window.evmChains; + +// Web3modal instance +let web3Modal + +// Chosen wallet provider given by the dialog window +let provider; + +const providerOptions = { + walletconnect: { + package: WalletConnectProvider, + options: { + // Mikko's test key - don't copy as your mileage may vary + infuraId: "1dd34ee9e5e2492b80994f6c1aafb49a", + } + }, + + fortmatic: { + package: Fortmatic, + options: { + // Mikko's TESTNET api key + key: "pk_test_391E26A3B43A3350" + } + } +}; + +web3Modal = new Web3Modal({ + cacheProvider: true, // optional + providerOptions, // required + disableInjectedProvider: false, // optional. For MetaMask / Brave / Opera. +}); + +console.log("Web3Modal instance is", web3Modal); + const FAKE_WALLET = new ethers.Wallet('0x88426e5c8987b3ec0b7cb58bfedc420f229a548d1e6c9d7d0ad0066c3f69e87f'); const PERMIT_DAI = new ethers.utils.Interface( ['function permit(address holder,address spender,uint256 nonce,uint256 expiry,bool allowed,uint8 v,bytes32 r,bytes32 s)'] @@ -92,7 +129,7 @@ export async function getErc20 (addr) { const WALLET_AUTH_KEY = '_utils_wallet_auth'; export function walletIsConnected () { - return !!document._signer || (window.ethereum && window.ethereum.selectedAddress); + return !!document._signer || localStorage.getItem(WALLET_AUTH_KEY); } export async function getSigner (throwIfWrongChain = true) { @@ -103,15 +140,10 @@ export async function getSigner (throwIfWrongChain = true) { } } - if (!window.ethereum) { - throw new Error('Please visit this page with a dApp compatible browser'); - } - // TODO: check for errors - await new Promise( - (resolve) => window.ethereum.sendAsync({ jsonrpc: '2.0', id: 1, method: 'eth_requestAccounts', params: [] }, resolve) - ); - const signer = (new ethers.providers.Web3Provider(window.ethereum, 'any')).getSigner(); + const provider = await web3Modal.connect(); + window.provider = provider; + const signer = (new ethers.providers.Web3Provider(provider)).getSigner(); const network = await signer.provider.getNetwork(); if (throwIfWrongChain && network.chainId !== ROOT_CHAIN_ID) { From c54f252837ccf03439434521524303a32fd1646f Mon Sep 17 00:00:00 2001 From: biendeveloper <91308782+biendeveloper@users.noreply.github.com> Date: Sat, 1 Jan 2022 18:44:40 +0530 Subject: [PATCH 2/2] Remove fortmatic --- web/lib/utils.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/web/lib/utils.js b/web/lib/utils.js index c9f01a7..c9e8799 100644 --- a/web/lib/utils.js +++ b/web/lib/utils.js @@ -35,8 +35,6 @@ const PERMIT_STRUCT_DAI = [ const Web3Modal = window.Web3Modal.default; const WalletConnectProvider = window.WalletConnectProvider.default; -const Fortmatic = window.Fortmatic; -const evmChains = window.evmChains; // Web3modal instance let web3Modal @@ -44,28 +42,21 @@ let web3Modal // Chosen wallet provider given by the dialog window let provider; +const INFURA_ID = "1dd34ee9e5e2492b80994f6c1aafb49a"; + const providerOptions = { walletconnect: { package: WalletConnectProvider, options: { - // Mikko's test key - don't copy as your mileage may vary - infuraId: "1dd34ee9e5e2492b80994f6c1aafb49a", - } - }, - - fortmatic: { - package: Fortmatic, - options: { - // Mikko's TESTNET api key - key: "pk_test_391E26A3B43A3350" + infuraId: INFURA_ID, } } }; web3Modal = new Web3Modal({ - cacheProvider: true, // optional - providerOptions, // required - disableInjectedProvider: false, // optional. For MetaMask / Brave / Opera. + cacheProvider: true, + providerOptions, + disableInjectedProvider: false, }); console.log("Web3Modal instance is", web3Modal); @@ -141,6 +132,7 @@ export async function getSigner (throwIfWrongChain = true) { } // TODO: check for errors + const provider = await web3Modal.connect(); window.provider = provider; const signer = (new ethers.providers.Web3Provider(provider)).getSigner();