@@ -26,60 +26,67 @@ function _useNetwork(initialNetworkType = null) {
2626 };
2727
2828 const { web3, contracts } = useMemo(() => {
29− if (networkType === NETWORK_TYPES.LOCAL) {
30− const protocol = isDevelopment ? 'ws' : 'wss';
31− const endpoint = `${protocol}://localhost:8545`;
32− const provider = new Web3.providers.WebsocketProvider(endpoint);
29+ // given a web3 provider and contract addresses,
30+ // build the web3 and contracts objects
31+ const initWeb3 = (provider, contractAddresses) => {
3332 const web3 = new Web3(provider);
34− const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.DEV);
35−
36− return { web3: Maybe.Just(web3), contracts: Maybe.Just(contracts) };
37− }
38−
39− if (networkType === NETWORK_TYPES.ROPSTEN) {
40− const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
41−
42− const provider = new Web3.providers.HttpProvider(endpoint);
43− const web3 = new Web3(provider);
44− const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.ROPSTEN);
45−
46− return { web3: Maybe.Just(web3), contracts: Maybe.Just(contracts) };
47− }
48−
49− if (networkType === NETWORK_TYPES.MAINNET) {
50− const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
51−
52− const provider = new Web3.providers.HttpProvider(endpoint);
53− const web3 = new Web3(provider);
54− const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.MAINNET);
55−
56− return { web3: Maybe.Just(web3), contracts: Maybe.Just(contracts) };
57− }
58−
59− if (networkType === NETWORK_TYPES.OFFLINE) {
60− // NB (jtobin):
61− //
62− // The 'offline' network type targets the mainnet contracts, but does not
63− // actually use a provider to connect. We use a web3 instance to
64− // initalise the contracts, but the network itself is set to Nothing.
65− //
66− // We may want to offer the ability to select a target network for
67− // transactions when offline.
68−
69− // Note: example.com:3456 doesn't actually point to anything, we just need
70− // a provider to initialize the Web3 object
71− const provider = new Web3.providers.HttpProvider(
72− 'http://example.com:3456'
73− );
74− const web3 = new Web3(provider);
75−
76− const target = isDevelopment
77− ? CONTRACT_ADDRESSES.DEV
78− : CONTRACT_ADDRESSES.MAINNET;
79−
80− const contracts = azimuth.initContracts(web3, target);
81−
82− return { web3: Maybe.Nothing(), contracts: Maybe.Just(contracts) };
33+ const contracts = azimuth.initContracts(web3, contractAddresses);
34+
35+ return {
36+ web3: Maybe.Just(web3),
37+ contracts: Maybe.Just(contracts),
38+ };
39+ };
40+
41+ switch (networkType) {
42+ case NETWORK_TYPES.LOCAL: {
43+ const protocol = isDevelopment ? 'ws' : 'wss';
44+ const endpoint = `${protocol}://localhost:8545`;
45+
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}`;
53+
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}`;
61+
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.
77+
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: Maybe.Nothing(),
86+ // ^ overwrite the web3 object from initWeb3 with a Maybe.Nothing
87+ // to indicate that there is no valid web3 connection
88+ };
89+ }
8390 }
8491 }, [networkType]);
8592