on master help sign in
use metamask as provider, if possible
d65ca9bLiam Fitzgerald
package.json+1−1
@@ -81,7 +81,7 @@
81 "checksum": "cd build && find . -exec shasum -a256 {} ';' > ../checksums.txt && cd ..",
81 "checksum": "cd build && find . -exec shasum -a256 {} ';' > ../checksums.txt && cd ..",
82 "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 ..",
82 "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 ..",
83 "release": "npm install && npm run build && npm run checksum && npm run zip",
83 "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 &",
85 "pilot:setup": "npm run pilot:ganache && truffle deploy",
85 "pilot:setup": "npm run pilot:ganache && truffle deploy",
86 "pilot": "HTTPS=true npm-run-all pilot:setup start pilot:cleanup --continue-on-error",
86 "pilot": "HTTPS=true npm-run-all pilot:setup start pilot:cleanup --continue-on-error",
87 "pilot-invites": "WITH_TEST_STATE=INVITES npm run pilot",
87 "pilot-invites": "WITH_TEST_STATE=INVITES npm run pilot",
src/lib/network.js+12−1
@@ -16,4 +16,15 @@ const renderNetworkType = network =>
16 ? 'Local Node'
16 ? 'Local Node'
17 : 'Offline';
17 : 'Offline';
18
18
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+
19−export { NETWORK_TYPES, renderNetworkType };
30+export { NETWORK_TYPES, renderNetworkType, chainIdToNetworkType };
src/store/network.js+22−10
@@ -4,6 +4,7 @@ import React, {
4 useContext,
4 useContext,
5 useMemo,
5 useMemo,
6 useState,
6 useState,
7+ useEffect,
7} from 'react';
8} from 'react';
8import { Just, Nothing } from 'folktale/maybe';
9import { Just, Nothing } from 'folktale/maybe';
9import * as azimuth from 'azimuth-js';
10import * as azimuth from 'azimuth-js';
@@ -11,25 +12,36 @@ import Web3 from 'web3';
11import { includes } from 'lodash';
12import { includes } from 'lodash';
12
13
13import { CONTRACT_ADDRESSES } from '../lib/contracts';
14import { CONTRACT_ADDRESSES } from '../lib/contracts';
14−import { NETWORK_TYPES } from '../lib/network';
15+import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
15import { isDevelopment } from '../lib/flags';
16import { isDevelopment } from '../lib/flags';
16import { BRIDGE_ERROR } from '../lib/error';
17import { BRIDGE_ERROR } from '../lib/error';
17
18
18function _useNetwork(initialNetworkType = null) {
19function _useNetwork(initialNetworkType = null) {
19− const [networkType, _setNetworkType] = useState(initialNetworkType);
20+ const [networkType, setNetworkType] = useState(initialNetworkType);
20
21
21− const setNetworkType = networkType => {
22+ const [metamask, setMetamask] = useState(false);
22− if (!includes(NETWORK_TYPES, networkType)) {
23+
23− throw new Error(BRIDGE_ERROR.INVALID_NETWORK_TYPE);
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');
24− }
33+ }
25− _setNetworkType(networkType);
34+ }
35+ })();
26− };
36+ }, []);
27
37
28 const { web3, contracts } = useMemo(() => {
38 const { web3, contracts } = useMemo(() => {
29 // given a web3 provider and contract addresses,
39 // given a web3 provider and contract addresses,
30 // build the web3 and contracts objects
40 // build the web3 and contracts objects
31 const initWeb3 = (provider, contractAddresses) => {
41 const initWeb3 = (provider, contractAddresses) => {
42+ // use an in-window eth provider if possible
43+ const _provider = metamask ? window.ethereum : provider;
32− const web3 = new Web3(provider);
44+ const web3 = new Web3(_provider);
33 const contracts = azimuth.initContracts(web3, contractAddresses);
45 const contracts = azimuth.initContracts(web3, contractAddresses);
34
46
35 return {
47 return {
@@ -88,7 +100,7 @@ function _useNetwork(initialNetworkType = null) {
88 };
100 };
89 }
101 }
90 }
102 }
91− }, [networkType]);
103+ }, [networkType, metamask]);
92
104
93 return { networkType, setNetworkType, web3, contracts };
105 return { networkType, setNetworkType, web3, contracts };
94}
106}