on dc70bbd help sign in
bridge/src/store/network.js
119 lines 3.7 kB history raw
1import React, {
2 createContext,
3 forwardRef,
4 useContext,
5 useMemo,
6 useState,
7} from 'react';
8import { Just, Nothing } from 'folktale/maybe';
9import * as azimuth from 'azimuth-js';
10import Web3 from 'web3';
11import { includes } from 'lodash';
13import { CONTRACT_ADDRESSES } from '../lib/contracts';
14import { NETWORK_TYPES } from '../lib/network';
15import { isDevelopment } from '../lib/flags';
16import { BRIDGE_ERROR } from '../lib/error';
18function _useNetwork(initialNetworkType = null) {
19 const [networkType, _setNetworkType] = useState(initialNetworkType);
21 const setNetworkType = networkType => {
22 if (!includes(NETWORK_TYPES, networkType)) {
23 throw new Error(BRIDGE_ERROR.INVALID_NETWORK_TYPE);
24 }
25 _setNetworkType(networkType);
26 };
28 const { web3, contracts } = useMemo(() => {
29 // given a web3 provider and contract addresses,
30 // build the web3 and contracts objects
31 const initWeb3 = (provider, contractAddresses) => {
32 const web3 = new Web3(provider);
33 const contracts = azimuth.initContracts(web3, contractAddresses);
35 return {
36 web3: Just(web3),
37 contracts: Just(contracts),
38 };
39 };
41 switch (networkType) {
42 case NETWORK_TYPES.LOCAL: {
43 const protocol = isDevelopment ? 'ws' : 'wss';
44 const endpoint = `${protocol}://${document.location.hostname}:8545`;
46 return initWeb3(
47 new Web3.providers.WebsocketProvider(endpoint),
48 CONTRACT_ADDRESSES.DEV
49 );
50 }
51 case NETWORK_TYPES.ROPSTEN: {
52 const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
54 return initWeb3(
55 new Web3.providers.HttpProvider(endpoint),
56 CONTRACT_ADDRESSES.ROPSTEN
57 );
58 }
59 case NETWORK_TYPES.MAINNET: {
60 const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
62 return initWeb3(
63 new Web3.providers.HttpProvider(endpoint),
64 CONTRACT_ADDRESSES.MAINNET
65 );
66 }
67 case NETWORK_TYPES.OFFLINE:
68 default: {
69 // NB (jtobin):
70 //
71 // The 'offline' network type targets the mainnet contracts, but does not
72 // actually use a provider to connect. We use a web3 instance to
73 // initalise the contracts, but the network itself is set to Nothing.
74 //
75 // We may want to offer the ability to select a target network for
76 // transactions when offline.
78 // Note: example.com:3456 doesn't actually point to anything, we just need
79 // a provider to initialize the Web3 object
80 return {
81 ...initWeb3(
82 new Web3.providers.HttpProvider('http://example.com:3456'),
83 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
84 ),
85 web3: Nothing(),
86 // ^ overwrite the web3 object from initWeb3 with a Nothing
87 // to indicate that there is no valid web3 connection
88 };
89 }
90 }
91 }, [networkType]);
93 return { networkType, setNetworkType, web3, contracts };
96const NetworkContext = createContext(null);
98// provider
99export function NetworkProvider({ initialNetworkType, children }) {
100 const value = _useNetwork(initialNetworkType);
101 return (
102 <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
103 );
106// hook consumer
107export function useNetwork() {
108 return useContext(NetworkContext);
111// hoc consumer
112export const withNetwork = Component =>
113 forwardRef((props, ref) => {
114 return (
115 <NetworkContext.Consumer>
116 {value => <Component ref={ref} {...value} {...props} />}
117 </NetworkContext.Consumer>
118 );
119 });