on master help sign in
Merge pull request #571 from urbit/m/softcoded-contracts

network: get ecliptic & polls addresses from chain

commit74b4047e4448a8005fc6e186963020096b09237d authorfang <github@fang.io> dateFri 4 Jun 2021, 12:49 (5 years ago) parenta3fd1ac588ce69
3 files changed+104−67
package.json+1−1 src/lib/contracts.js−6 src/store/network.js+103−60
package.json+1−1
@@ -21,7 +21,7 @@
2121 "@tlon/sigil-js": "^1.4.1",
2222 "@welldone-software/why-did-you-render": "^3.2.3",
2323 "async-retry": "^1.2.3",
24− "azimuth-js": "^0.19.0",
24+ "azimuth-js": "^0.22.1",
2525 "azimuth-solidity": "^1.2.2",
2626 "bip32": "^1.0.2",
2727 "bip39": "^2.5.0",
src/lib/contracts.js−6
@@ -1,24 +1,18 @@
11const CONTRACT_ADDRESSES = {
22 DEV: {
3− ecliptic: '0x56db68f29203ff44a803faa2404a44ecbb7a7480',
43 azimuth: '0x863d9c2e5c4c133596cfac29d55255f0d0f86381',
5− polls: '0x935452c45eda2958976a429c9733c40302995efd',
64 delegatedSending: '0xb71c0b6cee1bcae56dfe95cd9d3e41ddd7eafc43',
75 linearSR: '0x03C3dC12BE658158D1d7F9e66E08ec4099c568e4',
86 conditionalSR: '0x35EB3B102d9C1B69Ac1469C1B1FE1799850CD3EB',
97 },
108 ROPSTEN: {
11− ecliptic: '0x8b9f86a28921d9c705b3113a755fb979e1bd1bce',
129 azimuth: '0x308ab6a6024cf198b57e008d0ac9ad0219886579',
13− polls: '0xf5DA85f0d285A0F88af2388DD177A221872C8971',
1410 delegatedSending: '0x3e8ca510354bc2fdbbd6150252d93105c9c27bbe',
1511 linearSR: '0x1f8edd031ee414740aedb39b84fb8f2f66ca422f',
1612 conditionalSR: '0x1f8edd031ee414740aedb39b84fb8f2f66ca422f',
1713 },
1814 MAINNET: {
19− ecliptic: '0x6ac07b7c4601b5ce11de8dfe6335b871c7c4dd4d',
2015 azimuth: '0x223c067f8cf28ae173ee5cafea60ca44c335fecb',
21− polls: '0x7fecab617c868bb5996d99d95200d2fa708218e4',
2216 delegatedSending: '0xF7908Ab1F1e352F83c5ebc75051c0565AEaea5FB',
2317 linearSR: '0x86cd9cd0992f04231751e3761de45cecea5d1801',
2418 conditionalSR: '0x8c241098c3d3498fe1261421633fd57986d74aea',
src/store/network.js+103−60
@@ -16,10 +16,16 @@ import { NETWORK_TYPES, chainIdToNetworkType } from '../lib/network';
1616import { isDevelopment } from '../lib/flags';
1717import { BRIDGE_ERROR } from '../lib/error';
1818
19+import { Grid } from 'indigo-react';
20+import View from 'components/View';
21+import Blinky from 'components/Blinky';
22+
1923function _useNetwork(initialNetworkType = null) {
2024 const [networkType, setNetworkType] = useState(initialNetworkType);
2125
2226 const [metamask, setMetamask] = useState(false);
27+ const [web3, setWeb3] = useState(Nothing());
28+ const [contracts, setContracts] = useState(Nothing());
2329
2430 useEffect(() => {
2531 (async () => {
@@ -35,71 +41,93 @@ function _useNetwork(initialNetworkType = null) {
3541 })();
3642 }, []);
3743
38− const { web3, contracts } = useMemo(() => {
39− // given a web3 provider and contract addresses,
40− // build the web3 and contracts objects
41− const initWeb3 = (provider, contractAddresses) => {
42− // use an in-window eth provider if possible
43− const _provider = metamask ? window.ethereum : provider;
44− const web3 = new Web3(_provider);
45− const contracts = azimuth.initContracts(web3, contractAddresses);
46−
47− return {
48− web3: Just(web3),
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
44+ useEffect(() => {
45+ (async () => {
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);
52+
53+ let contracts = await azimuth.initContractsPartial(
54+ web3,
55+ contractAddresses.azimuth
6156 );
62− }
63− case NETWORK_TYPES.ROPSTEN: {
64− const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
65−
66− return initWeb3(
67− new Web3.providers.HttpProvider(endpoint),
68− CONTRACT_ADDRESSES.ROPSTEN
57+ contracts = azimuth.contracts.newLinearStarRelease(
58+ contracts,
59+ web3,
60+ contractAddresses.linearSR
6961 );
70− }
71− case NETWORK_TYPES.MAINNET: {
72− const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
73−
74− return initWeb3(
75− new Web3.providers.HttpProvider(endpoint),
76− CONTRACT_ADDRESSES.MAINNET
62+ contracts = azimuth.contracts.newConditionalStarRelease(
63+ contracts,
64+ web3,
65+ contractAddresses.conditionalSR
7766 );
78− }
79− case NETWORK_TYPES.OFFLINE:
80− default: {
81− // NB (jtobin):
82− //
83− // The 'offline' network type targets the mainnet contracts, but does not
84− // actually use a provider to connect. We use a web3 instance to
85− // initalise the contracts, but the network itself is set to Nothing.
86− //
87− // We may want to offer the ability to select a target network for
88− // transactions when offline.
89−
90− // Note: example.com:3456 doesn't actually point to anything, we just need
91− // a provider to initialize the Web3 object
92− return {
93− ...initWeb3(
67+ contracts = azimuth.contracts.newDelegatedSending(
68+ contracts,
69+ web3,
70+ contractAddresses.delegatedSending
71+ );
72+
73+ setWeb3(Just(web3));
74+ setContracts(Just(contracts));
75+ return;
76+ };
77+
78+ switch (networkType) {
79+ case NETWORK_TYPES.LOCAL: {
80+ const protocol = isDevelopment ? 'ws' : 'wss';
81+ const endpoint = `${protocol}://${document.location.hostname}:8545`;
82+
83+ initWeb3(
84+ new Web3.providers.WebsocketProvider(endpoint),
85+ CONTRACT_ADDRESSES.DEV
86+ );
87+ return;
88+ }
89+ case NETWORK_TYPES.ROPSTEN: {
90+ const endpoint = `https://ropsten.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
91+
92+ initWeb3(
93+ new Web3.providers.HttpProvider(endpoint),
94+ CONTRACT_ADDRESSES.ROPSTEN
95+ );
96+ return;
97+ }
98+ case NETWORK_TYPES.MAINNET: {
99+ const endpoint = `https://mainnet.infura.io/v3/${process.env.REACT_APP_INFURA_ENDPOINT}`;
100+
101+ initWeb3(
102+ new Web3.providers.HttpProvider(endpoint),
103+ CONTRACT_ADDRESSES.MAINNET
104+ );
105+ return;
106+ }
107+ case NETWORK_TYPES.OFFLINE:
108+ default: {
109+ // NB (jtobin):
110+ //
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.
114+ //
115+ // We may want to offer the ability to select a target network for
116+ // transactions when offline.
117+
118+ // Note: example.com:3456 doesn't actually point to anything, we just
119+ // need a provider to initialize the Web3 object
120+ initWeb3(
94121 new Web3.providers.HttpProvider('http://example.com:3456'),
95122 isDevelopment ? CONTRACT_ADDRESSES.DEV : CONTRACT_ADDRESSES.MAINNET
96− ),
97− web3: Nothing(),
123+ );
124+ setWeb3(Nothing());
98125 // ^ overwrite the web3 object from initWeb3 with a Nothing
99126 // to indicate that there is no valid web3 connection
100− };
127+ return;
128+ }
101129 }
102− }
130+ })();
103131 }, [networkType, metamask]);
104132
105133 return { networkType, setNetworkType, web3, contracts };
@@ -110,9 +138,24 @@ const NetworkContext = createContext(null);
110138// provider
111139export function NetworkProvider({ initialNetworkType, children }) {
112140 const value = _useNetwork(initialNetworkType);
113− return (
114− <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
115− );
141+
142+ if (Just.hasInstance(value.contracts)) {
143+ return (
144+ <NetworkContext.Provider value={value}>
145+ {children}
146+ </NetworkContext.Provider>
147+ );
148+ } else {
149+ return (
150+ <View inset>
151+ <Grid>
152+ <Grid.Item full className="mt8 t-center">
153+ <Blinky /> Connecting...
154+ </Grid.Item>
155+ </Grid>
156+ </View>
157+ );
158+ }
116159}
117160
118161// hook consumer