on master help sign in
use metamask as provider, if possible
commitd65ca9b2bad47544d379405776d3e7d5e1038367 authorLiam Fitzgerald <liam@urizen.co> dateFri 27 Mar 2020, 11:42 (7 years ago) parente11c85b
3 files changed+35−12
package.json+1−1 src/lib/network.js+12−1 src/store/network.js+22−10
package.json+1−1
@@ -81,7 +81,7 @@
8181 "checksum": "cd build && find . -exec shasum -a256 {} ';' > ../checksums.txt && cd ..",
8282 "zip": "zip -ur bridge-$npm_package_version.zip README.md bridge-https.py checksums.txt && cd build && zip -ur ../bridge-$npm_package_version.zip ./* && cd ..",
8383 "release": "npm install && npm run build && npm run checksum && npm run zip",
84− "pilot:ganache": "ganache-cli --blockTime 1 --networkId 1 --host '0.0.0.0' -m 'benefit crew supreme gesture quantum web media hazard theory mercy wing kitten' > /dev/null &",
84+ "pilot:ganache": "ganache-cli --blockTime 1 --networkId 5 --host '0.0.0.0' -m 'benefit crew supreme gesture quantum web media hazard theory mercy wing kitten' > /dev/null &",
8585 "pilot:setup": "npm run pilot:ganache && truffle deploy",
8686 "pilot": "HTTPS=true npm-run-all pilot:setup start pilot:cleanup --continue-on-error",
8787 "pilot-invites": "WITH_TEST_STATE=INVITES npm run pilot",
src/lib/network.js+12−1
@@ -16,4 +16,15 @@ const renderNetworkType = network =>
1616 ? 'Local Node'
1717 : 'Offline';
1818
19−export { NETWORK_TYPES, renderNetworkType };
19+const chainIdToNetworkType = chainId => {
20+ switch (chainId) {
21+ case '0x1':
22+ return NETWORK_TYPES.MAINNET;
23+ case '0x3':
24+ return NETWORK_TYPES.ROPSTEN;
25+ default:
26+ return NETWORK_TYPES.LOCAL;
27+ }
28+};
29+
30+export { NETWORK_TYPES, renderNetworkType, chainIdToNetworkType };
src/store/network.js+22−10
@@ -4,6 +4,7 @@ import React, {
44 useContext,
55 useMemo,
66 useState,
7+ useEffect,
78} from 'react';
89import { Just, Nothing } from 'folktale/maybe';
910import * as azimuth from 'azimuth-js';
@@ -11,25 +12,36 @@ import Web3 from 'web3';
1112import { includes } from 'lodash';
1213
1314import { CONTRACT_ADDRESSES } from '../lib/contracts';
14−import { NETWORK_TYPES } from '../lib/network';
15+import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
1516import { isDevelopment } from '../lib/flags';
1617import { BRIDGE_ERROR } from '../lib/error';
1718
1819function _useNetwork(initialNetworkType = null) {
19− const [networkType, _setNetworkType] = useState(initialNetworkType);
20+ const [networkType, setNetworkType] = useState(initialNetworkType);
2021
21− const setNetworkType = networkType => {
22− if (!includes(NETWORK_TYPES, networkType)) {
23− throw new Error(BRIDGE_ERROR.INVALID_NETWORK_TYPE);
24− }
25− _setNetworkType(networkType);
26− };
22+ const [metamask, setMetamask] = useState(false);
23+
24+ useEffect(() => {
25+ (async () => {
26+ if (window.ethereum) {
27+ try {
28+ await window.ethereum.enable();
29+ setMetamask(true);
30+ setNetworkType(chainIdToNetworkType(window.ethereum.chainId));
31+ } catch (e) {
32+ console.log('Metamask denied');
33+ }
34+ }
35+ })();
36+ }, []);
2737
2838 const { web3, contracts } = useMemo(() => {
2939 // given a web3 provider and contract addresses,
3040 // build the web3 and contracts objects
3141 const initWeb3 = (provider, contractAddresses) => {
32− const web3 = new Web3(provider);
42+ // use an in-window eth provider if possible
43+ const _provider = metamask ? window.ethereum : provider;
44+ const web3 = new Web3(_provider);
3345 const contracts = azimuth.initContracts(web3, contractAddresses);
3446
3547 return {
@@ -88,7 +100,7 @@ function _useNetwork(initialNetworkType = null) {
88100 };
89101 }
90102 }
91− }, [networkType]);
103+ }, [networkType, metamask]);
92104
93105 return { networkType, setNetworkType, web3, contracts };
94106}