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