@@ -4,6 +4,7 @@ import React, {
4 useContext,
4 useContext,
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';
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';
18function _useNetwork(initialNetworkType = null) {
19function _useNetwork(initialNetworkType = null) {
19− const [networkType, _setNetworkType] = useState(initialNetworkType);
20+ const [networkType, setNetworkType] = useState(initialNetworkType);
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(() => {
26+ if (window.ethereum) {
28+ await window.ethereum.enable();
30+ setNetworkType(chainIdToNetworkType(window.ethereum.chainId));
32+ console.log('Metamask denied');
25− _setNetworkType(networkType);
34+ }
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);
@@ -88,7 +100,7 @@ function _useNetwork(initialNetworkType = null) {
91− }, [networkType]);
103+ }, [networkType, metamask]);
93 return { networkType, setNetworkType, web3, contracts };
105 return { networkType, setNetworkType, web3, contracts };