8import { Just, Nothing } from 'folktale/maybe';
9import * as azimuth from 'azimuth-js';
10import Web3 from 'web3';
11import { includes } from 'lodash';
13import { CONTRACT_ADDRESSES } from '../lib/contracts';
14import { NETWORK_TYPES } from '../lib/network';
15import { isDevelopment } from '../lib/flags';
16import { BRIDGE_ERROR } from '../lib/error';
18function _useNetwork(initialNetworkType = null) {
19 const [networkType, _setNetworkType] = useState(initialNetworkType);
21 const setNetworkType = networkType => {
22 if (!includes(NETWORK_TYPES, networkType)) {
23 throw new Error(BRIDGE_ERROR.INVALID_NETWORK_TYPE);
25 _setNetworkType(networkType);
28 const { web3, contracts } = useMemo(() => {
29 // given a web3 provider and contract addresses,
30 // build the web3 and contracts objects
31 const initWeb3 = (provider, contractAddresses) => {
32 const web3 = new Web3(provider);
33 const contracts = azimuth.initContracts(web3, contractAddresses);
37 contracts: Just(contracts),
41 switch (networkType) {
42 case NETWORK_TYPES.LOCAL: {
43 const protocol = isDevelopment ? 'ws' : 'wss';
44 const endpoint = `${protocol}://localhost:8545`;
47 new Web3.providers.WebsocketProvider(endpoint),
48 CONTRACT_ADDRESSES.DEV
51 case NETWORK_TYPES.ROPSTEN: {
52 const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
55 new Web3.providers.HttpProvider(endpoint),
56 CONTRACT_ADDRESSES.ROPSTEN
59 case NETWORK_TYPES.MAINNET: {
60 const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
63 new Web3.providers.HttpProvider(endpoint),
64 CONTRACT_ADDRESSES.MAINNET
67 case NETWORK_TYPES.OFFLINE:
71 // The 'offline' network type targets the mainnet contracts, but does not
72 // actually use a provider to connect. We use a web3 instance to
73 // initalise the contracts, but the network itself is set to Nothing.
75 // We may want to offer the ability to select a target network for
76 // transactions when offline.
78 // Note: example.com:3456 doesn't actually point to anything, we just need
79 // a provider to initialize the Web3 object
82 new Web3.providers.HttpProvider('http://example.com:3456'),
83 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
86 // ^ overwrite the web3 object from initWeb3 with a Nothing
87 // to indicate that there is no valid web3 connection
93 return { networkType, setNetworkType, web3, contracts };
96const NetworkContext = createContext(null);
99export function NetworkProvider({ initialNetworkType, children }) {
100 const value = _useNetwork(initialNetworkType);
102 <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
107export function useNetwork() {
108 return useContext(NetworkContext);
112export const withNetwork = Component =>
113 forwardRef((props, ref) => {
115 <NetworkContext.Consumer>
116 {value => <Component ref={ref} {...value} {...props} />}
117 </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