9import { Just, Nothing } from 'folktale/maybe';
10import * as azimuth from 'azimuth-js';
11import Web3 from 'web3';
12import { includes } from 'lodash';
14import { CONTRACT_ADDRESSES } from '../lib/contracts';
15import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
16import { isDevelopment } from '../lib/flags';
17import { BRIDGE_ERROR } from '../lib/error';
19import { Grid } from 'indigo-react';
20import View from 'components/View';
21import Blinky from 'components/Blinky';
23function _useNetwork(initialNetworkType = null) {
24 const [networkType, setNetworkType] = useState(initialNetworkType);
26 const [metamask, setMetamask] = useState(false);
27 const [web3, setWeb3] = useState(Nothing());
28 const [contracts, setContracts] = useState(Nothing());
32 if (window.ethereum) {
34 await window.ethereum.enable();
36 setNetworkType(chainIdToNetworkType(window.ethereum.chainId));
38 console.log('Metamask denied');
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);
53 let contracts = await azimuth.initContractsPartial(
55 contractAddresses.azimuth
57 contracts = azimuth.contracts.newLinearStarRelease(
60 contractAddresses.linearSR
62 contracts = azimuth.contracts.newConditionalStarRelease(
65 contractAddresses.conditionalSR
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
107 case NETWORK_TYPES.OFFLINE:
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.
115 // We may want to offer the ability to select a target network for
116 // transactions when offline.
118 // Note: example.com:3456 doesn't actually point to anything, we just
119 // need a provider to initialize the Web3 object
121 new Web3.providers.HttpProvider('http://example.com:3456'),
122 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
125 // ^ overwrite the web3 object from initWeb3 with a Nothing
126 // to indicate that there is no valid web3 connection
131 }, [networkType, metamask]);
133 return { networkType, setNetworkType, web3, contracts };
136const NetworkContext = createContext(null);
139export function NetworkProvider({ initialNetworkType, children }) {
140 const value = _useNetwork(initialNetworkType);
142 if (Just.hasInstance(value.contracts)) {
144 <NetworkContext.Provider value={value}>
146 </NetworkContext.Provider>
152 <Grid.Item full className="mt8 t-center">
153 <Blinky /> Connecting...
162export function useNetwork() {
163 return useContext(NetworkContext);
167export const withNetwork = Component =>
168 forwardRef((props, ref) => {
170 <NetworkContext.Consumer>
171 {value => <Component ref={ref} {...value} {...props} />}
172 </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