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.linearSR = azimuth.newLinearStarRelease(
59 contractAddresses.linearSR
61 contracts.conditionalSR = azimuth.newConditionalStarRelease(
63 contractAddresses.conditionalSR
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
104 case NETWORK_TYPES.OFFLINE:
108 // The 'offline' network type targets the mainnet contracts, but does
109 // not actually use a provider to connect. We use a web3 instance to
110 // initalise the contracts, but the network itself is set to Nothing.
112 // We may want to offer the ability to select a target network for
113 // transactions when offline.
115 // Note: example.com:3456 doesn't actually point to anything, we just
116 // need a provider to initialize the Web3 object
118 new Web3.providers.HttpProvider('http://example.com:3456'),
119 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
122 // ^ overwrite the web3 object from initWeb3 with a Nothing
123 // to indicate that there is no valid web3 connection
128 }, [networkType, metamask]);
130 return { networkType, setNetworkType, web3, contracts };
133const NetworkContext = createContext(null);
136export function NetworkProvider({ initialNetworkType, children }) {
137 const value = _useNetwork(initialNetworkType);
139 if (Just.hasInstance(value.contracts)) {
141 <NetworkContext.Provider value={value}>
143 </NetworkContext.Provider>
149 <Grid.Item full className="mt8 t-center">
150 <Blinky /> Connecting...
159export function useNetwork() {
160 return useContext(NetworkContext);
164export const withNetwork = Component =>
165 forwardRef((props, ref) => {
167 <NetworkContext.Consumer>
168 {value => <Component ref={ref} {...value} {...props} />}
169 </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