on master help sign in
feat: clean up web3 and contracts object construction
5c24a13Matt Condon (shrugs)
src/bridge/store/network.js+60−53
@@ -26,60 +26,67 @@ function _useNetwork(initialNetworkType = null) {
26 };
26 };
27
27
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);
35−
34+
35+ return {
36+ web3: Maybe.Just(web3),
36− return { web3: Maybe.Just(web3), contracts: Maybe.Just(contracts) };
37+ contracts: Maybe.Just(contracts),
38+ };
37− }
39+ };
38−
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+ }
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}`;
41−
53+
54+ return initWeb3(
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
45−
57+ );
46− return { web3: Maybe.Just(web3), contracts: Maybe.Just(contracts) };
58+ }
47− }
48−
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}`;
51−
61+
62+ return initWeb3(
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
55−
65+ );
56− return { web3: Maybe.Just(web3), contracts: Maybe.Just(contracts) };
66+ }
57− }
58−
59− if (networkType === NETWORK_TYPES.OFFLINE) {
67+ case NETWORK_TYPES.OFFLINE:
68+ default: {
60− // NB (jtobin):
69+ // NB (jtobin):
61− //
70+ //
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.
65− //
74+ //
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.
68−
77+
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
80+ return {
81+ ...initWeb3(
71− const provider = new Web3.providers.HttpProvider(
82+ new Web3.providers.HttpProvider('http://example.com:3456'),
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;
83+ isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
79−
84+ ),
80− const contracts = azimuth.initContracts(web3, target);
81−
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
88+ };
89+ }
83 }
90 }
84 }, [networkType]);
91 }, [networkType]);
85
92
src/bridge/views/Network.js+4−4
@@ -13,14 +13,14 @@ const kNetworkOptions = [
13 title: 'Main Network (default)',
13 title: 'Main Network (default)',
14 value: NETWORK_TYPES.MAINNET,
14 value: NETWORK_TYPES.MAINNET,
15 },
15 },
16− {
17− title: 'Local Node',
18− value: NETWORK_TYPES.LOCAL,
19− },
20 {
16 {
21 title: 'Ropsten',
17 title: 'Ropsten',
22 value: NETWORK_TYPES.ROPSTEN,
18 value: NETWORK_TYPES.ROPSTEN,
23 },
19 },
20+ {
21+ title: 'Local Node',
22+ value: NETWORK_TYPES.LOCAL,
23+ },
24 {
24 {
25 type: 'divider',
25 type: 'divider',
26 },
26 },