@@ -16,10 +16,16 @@ import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
1616import { isDevelopment } from '../lib/flags';
1717import { BRIDGE_ERROR } from '../lib/error';
1818
19+import { Grid } from 'indigo-react';
20+import View from 'components/View';
21+import Blinky from 'components/Blinky';
22+
1923function _useNetwork(initialNetworkType = null) {
2024 const [networkType, setNetworkType] = useState(initialNetworkType);
2125
2226 const [metamask, setMetamask] = useState(false);
27+ const [web3, setWeb3] = useState(Nothing());
28+ const [contracts, setContracts] = useState(Nothing());
2329
2430 useEffect(() => {
2531 (async () => {
@@ -35,71 +41,90 @@ function _useNetwork(initialNetworkType = null) {
3541 })();
3642 }, []);
3743
38− const { web3, contracts } = useMemo(() => {
39− // given a web3 provider and contract addresses,
40− // build the web3 and contracts objects
41− const initWeb3 = (provider, contractAddresses) => {
42− // use an in-window eth provider if possible
43− const _provider = metamask ? window.ethereum : provider;
44− const web3 = new Web3(_provider);
45− const contracts = azimuth.initContracts(web3, contractAddresses);
46−
47− return {
48− web3: Just(web3),
49− contracts: Just(contracts),
50− };
51− };
52−
53− switch (networkType) {
54− case NETWORK_TYPES.LOCAL: {
55− const protocol = isDevelopment ? 'ws' : 'wss';
56− const endpoint = `${protocol}://${document.location.hostname}:8545`;
57−
58− return initWeb3(
59− new Web3.providers.WebsocketProvider(endpoint),
60− CONTRACT_ADDRESSES.DEV
44+ useEffect(() => {
45+ (async () => {
46+ // given a web3 provider and contract addresses,
47+ // build the web3 and contracts objects
48+ const initWeb3 = async (provider, contractAddresses) => {
49+ // use an in-window eth provider if possible
50+ const _provider = metamask ? window.ethereum : provider;
51+ const web3 = new Web3(_provider);
52+
53+ let contracts = await azimuth.initContractsPartial(
54+ web3,
55+ contractAddresses.azimuth
6156 );
62− }
63− case NETWORK_TYPES.ROPSTEN: {
64− const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
65−
66− return initWeb3(
67− new Web3.providers.HttpProvider(endpoint),
68− CONTRACT_ADDRESSES.ROPSTEN
57+ contracts.linearSR = azimuth.newLinearStarRelease(
58+ web3,
59+ contractAddresses.linearSR
6960 );
70− }
71− case NETWORK_TYPES.MAINNET: {
72− const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
73−
74− return initWeb3(
75− new Web3.providers.HttpProvider(endpoint),
76− CONTRACT_ADDRESSES.MAINNET
61+ contracts.conditionalSR = azimuth.newConditionalStarRelease(
62+ web3,
63+ contractAddresses.conditionalSR
7764 );
78− }
79− case NETWORK_TYPES.OFFLINE:
80− default: {
81− // NB (jtobin):
82− //
83− // The 'offline' network type targets the mainnet contracts, but does not
84− // actually use a provider to connect. We use a web3 instance to
85− // initalise the contracts, but the network itself is set to Nothing.
86− //
87− // We may want to offer the ability to select a target network for
88− // transactions when offline.
89−
90− // Note: example.com:3456 doesn't actually point to anything, we just need
91− // a provider to initialize the Web3 object
92− return {
93− ...initWeb3(
65+ contracts.delegatedSending = azimuth.newDelegatedSending(
66+ web3,
67+ contractAddresses.delegatedSending
68+ );
69+
70+ setWeb3(Just(web3));
71+ setContracts(Just(contracts));
72+ return;
73+ };
74+
75+ switch (networkType) {
76+ case NETWORK_TYPES.LOCAL: {
77+ const protocol = isDevelopment ? 'ws' : 'wss';
78+ const endpoint = `${protocol}://${document.location.hostname}:8545`;
79+
80+ initWeb3(
81+ new Web3.providers.WebsocketProvider(endpoint),
82+ CONTRACT_ADDRESSES.DEV
83+ );
84+ return;
85+ }
86+ case NETWORK_TYPES.ROPSTEN: {
87+ const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
88+
89+ initWeb3(
90+ new Web3.providers.HttpProvider(endpoint),
91+ CONTRACT_ADDRESSES.ROPSTEN
92+ );
93+ return;
94+ }
95+ case NETWORK_TYPES.MAINNET: {
96+ const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
97+
98+ initWeb3(
99+ new Web3.providers.HttpProvider(endpoint),
100+ CONTRACT_ADDRESSES.MAINNET
101+ );
102+ return;
103+ }
104+ case NETWORK_TYPES.OFFLINE:
105+ default: {
106+ // NB (jtobin):
107+ //
108+ // The 'offline' network type targets the mainnet contracts, but does
109+ // not actually use a provider to connect. We use a web3 instance to
110+ // initalise the contracts, but the network itself is set to Nothing.
111+ //
112+ // We may want to offer the ability to select a target network for
113+ // transactions when offline.
114+
115+ // Note: example.com:3456 doesn't actually point to anything, we just
116+ // need a provider to initialize the Web3 object
117+ initWeb3(
94118 new Web3.providers.HttpProvider('http://example.com:3456'),
95119 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
96− ),
97− web3: Nothing(),
120+ );
121+ setWeb3(Nothing());
98122 // ^ overwrite the web3 object from initWeb3 with a Nothing
99123 // to indicate that there is no valid web3 connection
100− };
124+ return;
125+ }
101126 }
102− }
127+ })();
103128 }, [networkType, metamask]);
104129
105130 return { networkType, setNetworkType, web3, contracts };
@@ -110,9 +135,24 @@ const NetworkContext = createContext(null);
110135// provider
111136export function NetworkProvider({ initialNetworkType, children }) {
112137 const value = _useNetwork(initialNetworkType);
113− return (
114− <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
115− );
138+
139+ if (Just.hasInstance(value.contracts)) {
140+ return (
141+ <NetworkContext.Provider value={value}>
142+ {children}
143+ </NetworkContext.Provider>
144+ );
145+ } else {
146+ return (
147+ <View inset>
148+ <Grid>
149+ <Grid.Item full className="mt8 t-center">
150+ <Blinky /> Connecting...
151+ </Grid.Item>
152+ </Grid>
153+ </View>
154+ );
155+ }
116156}
117157
118158// hook consumer