@@ -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,90 @@ 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.linearSR = azimuth.newLinearStarRelease(
63− case NETWORK_TYPES.ROPSTEN: {
58+ web3,
64− const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
59+ contractAddresses.linearSR
67− new Web3.providers.HttpProvider(endpoint),
68− CONTRACT_ADDRESSES.ROPSTEN
70− }
61+ contracts.conditionalSR = azimuth.newConditionalStarRelease(
71− case NETWORK_TYPES.MAINNET: {
62+ web3,
72− const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
63+ contractAddresses.conditionalSR
75− new Web3.providers.HttpProvider(endpoint),
76− CONTRACT_ADDRESSES.MAINNET
65+ contracts.delegatedSending = azimuth.newDelegatedSending(
67+ contractAddresses.delegatedSending
71+ setContracts(Just(contracts));
75+ switch (networkType) {
76+ case NETWORK_TYPES.LOCAL: {
77+ const protocol = isDevelopment ? 'ws' : 'wss';
78+ const endpoint = `${protocol}://${document.location.hostname}:8545`;
81+ new Web3.providers.WebsocketProvider(endpoint),
82+ CONTRACT_ADDRESSES.DEV
86+ case NETWORK_TYPES.ROPSTEN: {
87+ const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
90+ new Web3.providers.HttpProvider(endpoint),
91+ CONTRACT_ADDRESSES.ROPSTEN
95+ case NETWORK_TYPES.MAINNET: {
96+ const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
99+ new Web3.providers.HttpProvider(endpoint),
100+ CONTRACT_ADDRESSES.MAINNET
79− case NETWORK_TYPES.OFFLINE:
104+ case NETWORK_TYPES.OFFLINE:
80− default: {
105+ default: {
81− // NB (jtobin):
106+ // NB (jtobin):
83− // The 'offline' network type targets the mainnet contracts, but does not
108+ // The 'offline' network type targets the mainnet contracts, but does
84− // actually use a provider to connect. We use a web3 instance to
109+ // 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.
110+ // 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
112+ // We may want to offer the ability to select a target network for
88− // transactions when offline.
113+ // transactions when offline.
90− // Note: example.com:3456 doesn't actually point to anything, we just need
115+ // Note: example.com:3456 doesn't actually point to anything, we just
91− // a provider to initialize the Web3 object
116+ // need a provider to initialize the Web3 object
93− ...initWeb3(
117+ initWeb3(
94 new Web3.providers.HttpProvider('http://example.com:3456'),
118 new Web3.providers.HttpProvider('http://example.com:3456'),
95 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
119 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
97− web3: Nothing(),
121+ setWeb3(Nothing());
98 // ^ overwrite the web3 object from initWeb3 with a Nothing
122 // ^ overwrite the web3 object from initWeb3 with a Nothing
99 // to indicate that there is no valid web3 connection
123 // to indicate that there is no valid web3 connection
103 }, [networkType, metamask]);
128 }, [networkType, metamask]);
105 return { networkType, setNetworkType, web3, contracts };
130 return { networkType, setNetworkType, web3, contracts };
@@ -110,9 +135,24 @@ const NetworkContext = createContext(null);
110// provider
135// provider
111export function NetworkProvider({ initialNetworkType, children }) {
136export function NetworkProvider({ initialNetworkType, children }) {
112 const value = _useNetwork(initialNetworkType);
137 const value = _useNetwork(initialNetworkType);
139+ if (Just.hasInstance(value.contracts)) {
114− <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
141+ <NetworkContext.Provider value={value}>
143+ </NetworkContext.Provider>
149+ <Grid.Item full className="mt8 t-center">
150+ <Blinky /> Connecting...
118// hook consumer
158// hook consumer