Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3 Modal with walletconnect #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
!LICENSE
web/lib/config.js
web/lib/rollup-config.js
node_modules
6 changes: 6 additions & 0 deletions web/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<meta property='og:image' content='/lib/assets/preview-v2.jpg'>
<meta property='og:title' content='Habitat - Communities'>
<title>Habitat - Communities</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/web3.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/index.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/umd/index.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/@walletconnect/[email protected]/dist/umd/index.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/fortmatic.js"></script>
<script src='/lib/common.js'></script>
<script type='module' src='/lib/HabitatBackground.js'></script>
<script type='module' src='/lib/HabitatSidebar.js'></script>
Expand Down
42 changes: 33 additions & 9 deletions web/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ const PERMIT_STRUCT_DAI = [
{ name: 'allowed', type: 'bool' },
];

const Web3Modal = window.Web3Modal.default;
const WalletConnectProvider = window.WalletConnectProvider.default;

// Web3modal instance
let web3Modal

// Chosen wallet provider given by the dialog window
let provider;

const INFURA_ID = "1dd34ee9e5e2492b80994f6c1aafb49a";

const providerOptions = {
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: INFURA_ID,
}
}
};

web3Modal = new Web3Modal({
cacheProvider: true,
providerOptions,
disableInjectedProvider: false,
});

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)']
Expand Down Expand Up @@ -92,7 +120,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) {
Expand All @@ -103,15 +131,11 @@ 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) {
Expand Down