on master help sign in
network: get ecliptic & polls addresses from chain
6ff95bcfang
src/lib/contracts.js−6
@@ -1,24 +1,18 @@
1const CONTRACT_ADDRESSES = {
1const CONTRACT_ADDRESSES = {
2 DEV: {
2 DEV: {
3− ecliptic: '0x56db68f29203ff44a803faa2404a44ecbb7a7480',
4 azimuth: '0x863d9c2e5c4c133596cfac29d55255f0d0f86381',
3 azimuth: '0x863d9c2e5c4c133596cfac29d55255f0d0f86381',
5− polls: '0x935452c45eda2958976a429c9733c40302995efd',
6 delegatedSending: '0xb71c0b6cee1bcae56dfe95cd9d3e41ddd7eafc43',
4 delegatedSending: '0xb71c0b6cee1bcae56dfe95cd9d3e41ddd7eafc43',
7 linearSR: '0x03C3dC12BE658158D1d7F9e66E08ec4099c568e4',
5 linearSR: '0x03C3dC12BE658158D1d7F9e66E08ec4099c568e4',
8 conditionalSR: '0x35EB3B102d9C1B69Ac1469C1B1FE1799850CD3EB',
6 conditionalSR: '0x35EB3B102d9C1B69Ac1469C1B1FE1799850CD3EB',
9 },
7 },
10 ROPSTEN: {
8 ROPSTEN: {
11− ecliptic: '0x8b9f86a28921d9c705b3113a755fb979e1bd1bce',
12 azimuth: '0x308ab6a6024cf198b57e008d0ac9ad0219886579',
9 azimuth: '0x308ab6a6024cf198b57e008d0ac9ad0219886579',
13− polls: '0xf5DA85f0d285A0F88af2388DD177A221872C8971',
14 delegatedSending: '0x3e8ca510354bc2fdbbd6150252d93105c9c27bbe',
10 delegatedSending: '0x3e8ca510354bc2fdbbd6150252d93105c9c27bbe',
15 linearSR: '0x1f8edd031ee414740aedb39b84fb8f2f66ca422f',
11 linearSR: '0x1f8edd031ee414740aedb39b84fb8f2f66ca422f',
16 conditionalSR: '0x1f8edd031ee414740aedb39b84fb8f2f66ca422f',
12 conditionalSR: '0x1f8edd031ee414740aedb39b84fb8f2f66ca422f',
17 },
13 },
18 MAINNET: {
14 MAINNET: {
19− ecliptic: '0x6ac07b7c4601b5ce11de8dfe6335b871c7c4dd4d',
20 azimuth: '0x223c067f8cf28ae173ee5cafea60ca44c335fecb',
15 azimuth: '0x223c067f8cf28ae173ee5cafea60ca44c335fecb',
21− polls: '0x7fecab617c868bb5996d99d95200d2fa708218e4',
22 delegatedSending: '0xF7908Ab1F1e352F83c5ebc75051c0565AEaea5FB',
16 delegatedSending: '0xF7908Ab1F1e352F83c5ebc75051c0565AEaea5FB',
23 linearSR: '0x86cd9cd0992f04231751e3761de45cecea5d1801',
17 linearSR: '0x86cd9cd0992f04231751e3761de45cecea5d1801',
24 conditionalSR: '0x8c241098c3d3498fe1261421633fd57986d74aea',
18 conditionalSR: '0x8c241098c3d3498fe1261421633fd57986d74aea',
src/store/network.js+100−60
@@ -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';
18
18
19+import { Grid } from 'indigo-react';
20+import View from 'components/View';
21+import Blinky from 'components/Blinky';
22+
19function _useNetwork(initialNetworkType = null) {
23function _useNetwork(initialNetworkType = null) {
20 const [networkType, setNetworkType] = useState(initialNetworkType);
24 const [networkType, setNetworkType] = useState(initialNetworkType);
21
25
22 const [metamask, setMetamask] = useState(false);
26 const [metamask, setMetamask] = useState(false);
27+ const [web3, setWeb3] = useState(Nothing());
28+ const [contracts, setContracts] = useState(Nothing());
23
29
24 useEffect(() => {
30 useEffect(() => {
25 (async () => {
31 (async () => {
@@ -35,71 +41,90 @@ function _useNetwork(initialNetworkType = null) {
35 })();
41 })();
36 }, []);
42 }, []);
37
43
38− const { web3, contracts } = useMemo(() => {
44+ useEffect(() => {
45+ (async () => {
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(
47− return {
54+ web3,
48− web3: Just(web3),
55+ contractAddresses.azimuth
49− contracts: Just(contracts),
50− };
51− };
52−
53− switch (networkType) {
54− case NETWORK_TYPES.LOCAL: {
55− const protocol = isDevelopment ? 'ws' : 'wss';
56− const endpoint = `${protocol}://${document.location.hostname}:8545`;
57−
58− return initWeb3(
59− new Web3.providers.WebsocketProvider(endpoint),
60− CONTRACT_ADDRESSES.DEV
61 );
56 );
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
65−
66− return initWeb3(
67− new Web3.providers.HttpProvider(endpoint),
68− CONTRACT_ADDRESSES.ROPSTEN
69 );
60 );
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
73−
74− return initWeb3(
75− new Web3.providers.HttpProvider(endpoint),
76− CONTRACT_ADDRESSES.MAINNET
77 );
64 );
65+ contracts.delegatedSending = azimuth.newDelegatedSending(
66+ web3,
67+ contractAddresses.delegatedSending
68+ );
69+
70+ setWeb3(Just(web3));
71+ setContracts(Just(contracts));
72+ return;
78− }
73+ };
74+
75+ switch (networkType) {
76+ case NETWORK_TYPES.LOCAL: {
77+ const protocol = isDevelopment ? 'ws' : 'wss';
78+ const endpoint = `${protocol}://${document.location.hostname}:8545`;
79+
80+ initWeb3(
81+ new Web3.providers.WebsocketProvider(endpoint),
82+ CONTRACT_ADDRESSES.DEV
83+ );
84+ return;
85+ }
86+ case NETWORK_TYPES.ROPSTEN: {
87+ const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
88+
89+ initWeb3(
90+ new Web3.providers.HttpProvider(endpoint),
91+ CONTRACT_ADDRESSES.ROPSTEN
92+ );
93+ return;
94+ }
95+ case NETWORK_TYPES.MAINNET: {
96+ const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
97+
98+ initWeb3(
99+ new Web3.providers.HttpProvider(endpoint),
100+ CONTRACT_ADDRESSES.MAINNET
101+ );
102+ return;
103+ }
79− case NETWORK_TYPES.OFFLINE:
104+ case NETWORK_TYPES.OFFLINE:
80− default: {
105+ default: {
81− // NB (jtobin):
106+ // NB (jtobin):
82− //
107+ //
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.
86− //
111+ //
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.
89−
114+
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
92− return {
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
96− ),
120+ );
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
124+ return;
100− };
125+ }
101 }
126 }
102− }
127+ })();
103 }, [networkType, metamask]);
128 }, [networkType, metamask]);
104
129
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);
113− return (
138+
139+ if (Just.hasInstance(value.contracts)) {
140+ return (
114− <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
141+ <NetworkContext.Provider value={value}>
142+ {children}
143+ </NetworkContext.Provider>
144+ );
145+ } else {
146+ return (
147+ <View inset>
148+ <Grid>
149+ <Grid.Item full className="mt8 t-center">
150+ <Blinky /> Connecting...
151+ </Grid.Item>
152+ </Grid>
153+ </View>
115− );
154+ );
155+ }
116}
156}
117
157
118// hook consumer
158// hook consumer