@@ -16,10 +16,16 @@ import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
16import { isDevelopment } from '../lib/flags';
16import { isDevelopment } from '../lib/flags';
17import { BRIDGE_ERROR } from '../lib/error';
17import { BRIDGE_ERROR } from '../lib/error';
19+import { Grid } from 'indigo-react';
20+import View from 'components/View';
21+import Blinky from 'components/Blinky';
19function _useNetwork(initialNetworkType = null) {
23function _useNetwork(initialNetworkType = null) {
20 const [networkType, setNetworkType] = useState(initialNetworkType);
24 const [networkType, setNetworkType] = useState(initialNetworkType);
22 const [metamask, setMetamask] = useState(false);
26 const [metamask, setMetamask] = useState(false);
27+ const [web3, setWeb3] = useState(Nothing());
28+ const [contracts, setContracts] = useState(Nothing());
24 useEffect(() => {
30 useEffect(() => {
25 (async () => {
31 (async () => {
@@ -35,71 +41,93 @@ function _useNetwork(initialNetworkType = null) {
38− const { web3, contracts } = useMemo(() => {
44+ useEffect(() => {
39− // given a web3 provider and contract addresses,
46+ // given a web3 provider and contract addresses,
40− // build the web3 and contracts objects
47+ // build the web3 and contracts objects
41− const initWeb3 = (provider, contractAddresses) => {
48+ const initWeb3 = async (provider, contractAddresses) => {
42− // use an in-window eth provider if possible
49+ // use an in-window eth provider if possible
43− const _provider = metamask ? window.ethereum : provider;
50+ const _provider = metamask ? window.ethereum : provider;
44− const web3 = new Web3(_provider);
51+ const web3 = new Web3(_provider);
45− const contracts = azimuth.initContracts(web3, contractAddresses);
52+
46−
53+ let contracts = await azimuth.initContractsPartial(
48− web3: Just(web3),
55+ contractAddresses.azimuth
49− contracts: Just(contracts),
53− switch (networkType) {
54− case NETWORK_TYPES.LOCAL: {
55− const protocol = isDevelopment ? 'ws' : 'wss';
56− const endpoint = `${protocol}://${document.location.hostname}:8545`;
59− new Web3.providers.WebsocketProvider(endpoint),
60− CONTRACT_ADDRESSES.DEV
62− }
57+ contracts = azimuth.contracts.newLinearStarRelease(
63− case NETWORK_TYPES.ROPSTEN: {
58+ contracts,
64− const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
59+ web3,
65−
60+ contractAddresses.linearSR
67− new Web3.providers.HttpProvider(endpoint),
68− CONTRACT_ADDRESSES.ROPSTEN
70− }
62+ contracts = azimuth.contracts.newConditionalStarRelease(
71− case NETWORK_TYPES.MAINNET: {
63+ contracts,
72− const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
64+ web3,
73−
65+ contractAddresses.conditionalSR
75− new Web3.providers.HttpProvider(endpoint),
76− CONTRACT_ADDRESSES.MAINNET
67+ contracts = azimuth.contracts.newDelegatedSending(
70+ contractAddresses.delegatedSending
74+ setContracts(Just(contracts));
78+ switch (networkType) {
79+ case NETWORK_TYPES.LOCAL: {
80+ const protocol = isDevelopment ? 'ws' : 'wss';
81+ const endpoint = `${protocol}://${document.location.hostname}:8545`;
84+ new Web3.providers.WebsocketProvider(endpoint),
85+ CONTRACT_ADDRESSES.DEV
89+ case NETWORK_TYPES.ROPSTEN: {
90+ const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
93+ new Web3.providers.HttpProvider(endpoint),
94+ CONTRACT_ADDRESSES.ROPSTEN
98+ case NETWORK_TYPES.MAINNET: {
99+ const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
102+ new Web3.providers.HttpProvider(endpoint),
103+ CONTRACT_ADDRESSES.MAINNET
79− case NETWORK_TYPES.OFFLINE:
107+ case NETWORK_TYPES.OFFLINE:
80− default: {
108+ default: {
81− // NB (jtobin):
109+ // NB (jtobin):
83− // The 'offline' network type targets the mainnet contracts, but does not
111+ // The 'offline' network type targets the mainnet contracts, but does
84− // actually use a provider to connect. We use a web3 instance to
112+ // not actually use a provider to connect. We use a web3 instance to
85− // initalise the contracts, but the network itself is set to Nothing.
113+ // initalise the contracts, but the network itself is set to Nothing.
87− // We may want to offer the ability to select a target network for
115+ // We may want to offer the ability to select a target network for
88− // transactions when offline.
116+ // transactions when offline.
90− // Note: example.com:3456 doesn't actually point to anything, we just need
118+ // Note: example.com:3456 doesn't actually point to anything, we just
91− // a provider to initialize the Web3 object
119+ // need a provider to initialize the Web3 object
93− ...initWeb3(
120+ initWeb3(
94 new Web3.providers.HttpProvider('http://example.com:3456'),
121 new Web3.providers.HttpProvider('http://example.com:3456'),
95 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
122 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
97− web3: Nothing(),
124+ setWeb3(Nothing());
98 // ^ overwrite the web3 object from initWeb3 with a Nothing
125 // ^ overwrite the web3 object from initWeb3 with a Nothing
99 // to indicate that there is no valid web3 connection
126 // to indicate that there is no valid web3 connection
103 }, [networkType, metamask]);
131 }, [networkType, metamask]);
105 return { networkType, setNetworkType, web3, contracts };
133 return { networkType, setNetworkType, web3, contracts };
@@ -110,9 +138,24 @@ const NetworkContext = createContext(null);
110// provider
138// provider
111export function NetworkProvider({ initialNetworkType, children }) {
139export function NetworkProvider({ initialNetworkType, children }) {
112 const value = _useNetwork(initialNetworkType);
140 const value = _useNetwork(initialNetworkType);
142+ if (Just.hasInstance(value.contracts)) {
114− <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
144+ <NetworkContext.Provider value={value}>
146+ </NetworkContext.Provider>
152+ <Grid.Item full className="mt8 t-center">
153+ <Blinky /> Connecting...
118// hook consumer
161// hook consumer