@@ -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,93 @@ 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 = azimuth.contracts.newLinearStarRelease(
58+ contracts,
59+ web3,
60+ contractAddresses.linearSR
6961 );
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
62+ contracts = azimuth.contracts.newConditionalStarRelease(
63+ contracts,
64+ web3,
65+ contractAddresses.conditionalSR
7766 );
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(
67+ contracts = azimuth.contracts.newDelegatedSending(
68+ contracts,
69+ web3,
70+ contractAddresses.delegatedSending
71+ );
72+
73+ setWeb3(Just(web3));
74+ setContracts(Just(contracts));
75+ return;
76+ };
77+
78+ switch (networkType) {
79+ case NETWORK_TYPES.LOCAL: {
80+ const protocol = isDevelopment ? 'ws' : 'wss';
81+ const endpoint = `${protocol}://${document.location.hostname}:8545`;
82+
83+ initWeb3(
84+ new Web3.providers.WebsocketProvider(endpoint),
85+ CONTRACT_ADDRESSES.DEV
86+ );
87+ return;
88+ }
89+ case NETWORK_TYPES.ROPSTEN: {
90+ const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
91+
92+ initWeb3(
93+ new Web3.providers.HttpProvider(endpoint),
94+ CONTRACT_ADDRESSES.ROPSTEN
95+ );
96+ return;
97+ }
98+ case NETWORK_TYPES.MAINNET: {
99+ const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
100+
101+ initWeb3(
102+ new Web3.providers.HttpProvider(endpoint),
103+ CONTRACT_ADDRESSES.MAINNET
104+ );
105+ return;
106+ }
107+ case NETWORK_TYPES.OFFLINE:
108+ default: {
109+ // NB (jtobin):
110+ //
111+ // The 'offline' network type targets the mainnet contracts, but does
112+ // not actually use a provider to connect. We use a web3 instance to
113+ // initalise the contracts, but the network itself is set to Nothing.
114+ //
115+ // We may want to offer the ability to select a target network for
116+ // transactions when offline.
117+
118+ // Note: example.com:3456 doesn't actually point to anything, we just
119+ // need a provider to initialize the Web3 object
120+ initWeb3(
94121 new Web3.providers.HttpProvider('http://example.com:3456'),
95122 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
96− ),
97− web3: Nothing(),
123+ );
124+ setWeb3(Nothing());
98125 // ^ overwrite the web3 object from initWeb3 with a Nothing
99126 // to indicate that there is no valid web3 connection
100− };
127+ return;
128+ }
101129 }
102− }
130+ })();
103131 }, [networkType, metamask]);
104132
105133 return { networkType, setNetworkType, web3, contracts };
@@ -110,9 +138,24 @@ const NetworkContext = createContext(null);
110138// provider
111139export function NetworkProvider({ initialNetworkType, children }) {
112140 const value = _useNetwork(initialNetworkType);
113− return (
114− <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
115− );
141+
142+ if (Just.hasInstance(value.contracts)) {
143+ return (
144+ <NetworkContext.Provider value={value}>
145+ {children}
146+ </NetworkContext.Provider>
147+ );
148+ } else {
149+ return (
150+ <View inset>
151+ <Grid>
152+ <Grid.Item full className="mt8 t-center">
153+ <Blinky /> Connecting...
154+ </Grid.Item>
155+ </Grid>
156+ </View>
157+ );
158+ }
116159}
117160
118161// hook consumer