8import { Just, Nothing } from 'folktale/maybe';
9import * as azimuth from 'azimuth-js';
10import Web3 from 'web3';
12import { CONTRACT_ADDRESSES } from '../lib/contracts';
13import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
14import { isDevelopment } from '../lib/flags';
16import { Grid } from 'indigo-react';
17import View from 'components/View';
18import Blinky from 'components/Blinky';
20function _useNetwork(initialNetworkType = null) {
21 const [networkType, setNetworkType] = useState(initialNetworkType);
23 const [metamask, setMetamask] = useState(false);
24 const [web3, setWeb3] = useState(Nothing());
25 const [contracts, setContracts] = useState(Nothing());
29 if (window.ethereum) {
32 setNetworkType(chainIdToNetworkType(window.ethereum.chainId));
34 console.log('Metamask denied');
42 // given a web3 provider and contract addresses,
43 // build the web3 and contracts objects
44 const initWeb3 = async (provider, contractAddresses) => {
45 // use an in-window eth provider if possible
46 const _provider = metamask ? window.ethereum : provider;
47 const web3 = new Web3(_provider);
49 let contracts = await azimuth.initContractsPartial(
51 contractAddresses.azimuth
53 contracts = azimuth.contracts.newLinearStarRelease(
56 contractAddresses.linearSR
58 contracts = azimuth.contracts.newConditionalStarRelease(
61 contractAddresses.conditionalSR
63 contracts = azimuth.contracts.newDelegatedSending(
66 contractAddresses.delegatedSending
70 setContracts(Just(contracts));
74 switch (networkType) {
75 case NETWORK_TYPES.LOCAL: {
76 const protocol = isDevelopment ? 'ws' : 'wss';
77 const endpoint = `${protocol}://${document.location.hostname}:8545`;
80 new Web3.providers.WebsocketProvider(endpoint),
81 CONTRACT_ADDRESSES.DEV
85 case NETWORK_TYPES.ROPSTEN: {
86 const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
89 new Web3.providers.HttpProvider(endpoint),
90 CONTRACT_ADDRESSES.ROPSTEN
94 case NETWORK_TYPES.MAINNET: {
95 const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
98 new Web3.providers.HttpProvider(endpoint),
99 CONTRACT_ADDRESSES.MAINNET
103 case NETWORK_TYPES.OFFLINE:
107 // The 'offline' network type targets the mainnet contracts, but does
108 // not actually use a provider to connect. We use a web3 instance to
109 // initalise the contracts, but the network itself is set to Nothing.
111 // We may want to offer the ability to select a target network for
112 // transactions when offline.
114 // Note: example.com:3456 doesn't actually point to anything, we just
115 // need a provider to initialize the Web3 object
117 new Web3.providers.HttpProvider('http://example.com:3456'),
118 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
121 // ^ overwrite the web3 object from initWeb3 with a Nothing
122 // to indicate that there is no valid web3 connection
127 }, [networkType, metamask]);
129 return { networkType, setNetworkType, setMetamask, web3, contracts };
132const NetworkContext = createContext(null);
135export function NetworkProvider({ initialNetworkType, children }) {
136 const value = _useNetwork(initialNetworkType);
138 if (Just.hasInstance(value.contracts)) {
140 <NetworkContext.Provider value={value}>
142 </NetworkContext.Provider>
148 <Grid.Item full className="mt8 t-center">
149 <Blinky /> Connecting...
158export function useNetwork() {
159 return useContext(NetworkContext);
163export const withNetwork = Component =>
164 forwardRef((props, ref) => {
166 <NetworkContext.Consumer>
167 {value => <Component ref={ref} {...value} {...props} />}
168 </NetworkContext.Consumer>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list