/
App.js
430 lines (396 loc) · 12.7 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import React from 'react'
import createHistory from 'history/createHashHistory'
import { contractAddresses, web3Providers } from './environment'
import { parsePath } from './routing'
import initWrapper, {
initDaoBuilder,
pollMainAccount,
pollNetwork,
pollConnectivity,
} from './aragonjs-wrapper'
import Wrapper from './Wrapper'
import Onboarding from './onboarding/Onboarding'
import { getWeb3, getUnknownBalance, identifyProvider } from './web3-utils'
import { log } from './utils'
import { PermissionsProvider } from './contexts/PermissionsContext'
import { FavoriteDaosProvider } from './contexts/FavoriteDaosContext'
import { ModalProvider } from './components/ModalManager/ModalManager'
import DeprecatedBanner from './components/DeprecatedBanner/DeprecatedBanner'
import { IdentityProvider } from './components/IdentityManager/IdentityManager'
import { LocalIdentityModalProvider } from './components/LocalIdentityModal/LocalIdentityModalManager'
import LocalIdentityModal from './components/LocalIdentityModal/LocalIdentityModal'
import {
APPS_STATUS_ERROR,
APPS_STATUS_READY,
APPS_STATUS_LOADING,
DAO_CREATION_STATUS_NONE,
DAO_CREATION_STATUS_SUCCESS,
DAO_CREATION_STATUS_ERROR,
} from './symbols'
class App extends React.Component {
state = {
account: '',
apps: [],
appsStatus: APPS_STATUS_LOADING,
balance: getUnknownBalance(),
buildData: null, // data returned by aragon.js when a DAO is created
connected: false,
daoAddress: { address: '', domain: '' },
// daoCreationStatus is one of:
// - DAO_CREATION_STATUS_NONE
// - DAO_CREATION_STATUS_SUCCESS
// - DAO_CREATION_STATUS_ERROR
daoCreationStatus: DAO_CREATION_STATUS_NONE,
fatalError: null,
identityIntent: null,
locator: {},
permissions: {},
permissionsLoading: true,
prevLocator: null,
selectorNetworks: [
['main', 'Ethereum Mainnet', 'https://mainnet.aragon.org/'],
['rinkeby', 'Ethereum Testnet (Rinkeby)', 'https://rinkeby.aragon.org/'],
],
showDeprecatedBanner: false,
transactionBag: null,
walletNetwork: '',
walletWeb3: null,
wrapper: null,
}
history = createHistory()
componentDidMount() {
const { pathname, search } = this.history.location
this.handleHistoryChange({ pathname, search })
this.history.listen(this.handleHistoryChange)
this.setState({
walletWeb3: getWeb3(web3Providers.wallet),
walletProviderId: identifyProvider(web3Providers.wallet),
})
pollMainAccount(web3Providers.wallet, {
onAccount: (account = null) => {
this.setState({ account })
if (account && this.state.wrapper) {
this.state.wrapper.setAccounts([account])
}
},
onBalance: balance => {
this.setState({ balance })
},
})
pollNetwork(web3Providers.wallet, walletNetwork => {
this.setState({ walletNetwork })
})
// Only the default, because the app can work without the wallet
pollConnectivity([web3Providers.default], connected => {
this.setState({ connected })
})
}
// Enable the web3 provider. There is no way to reliably know the enabled
// state of a provider, so we assume that if there is a provider but no
// account, the provider is locked and / or not enabled.
handleRequestEnable = () => {
const provider = web3Providers.wallet
if (!provider) {
return
}
// For providers supporting .enable() (EIP 1102 draft).
if (typeof provider.enable === 'function') {
provider.enable()
return
}
// For providers supporting EIP 1102 (final).
if (typeof provider.send === 'function') {
// Some providers (Metamask) don’t return a promise as defined in EIP
// 1102, so we can’t rely on it to know the connected accounts.
provider.send('eth_requestAccounts')
}
}
// Handle URL changes
handleHistoryChange = ({ pathname, search }) => {
this.updateLocator(parsePath(pathname, search))
}
// Change the URL if needed
historyPush = path => {
if (path !== this.state.locator.path) {
this.history.push(path)
}
}
// Change the URL to the previous one
historyBack = () => {
if (this.state.prevLocator) {
this.history.goBack()
} else {
this.history.replace('/')
}
}
updateLocator = locator => {
const { locator: prevLocator } = this.state
if (locator.mode === 'home' || locator.mode === 'setup') {
this.updateDaoBuilder()
}
// New DAO: need to reinit the wrapper
if (locator.dao && (!prevLocator || locator.dao !== prevLocator.dao)) {
this.updateDao(locator.dao)
}
// Moving from a DAO to somewhere else (like onboarding):
// need to cancel the subscribtions.
if (!locator.dao && prevLocator && prevLocator.dao) {
this.updateDao(null)
}
this.setState({ locator, prevLocator })
}
async updateDaoBuilder() {
const daoBuilder = initDaoBuilder(
web3Providers.wallet,
contractAddresses.ensRegistry
)
this.setState({ daoBuilder })
}
handleResetDaoBuilder = () => {
this.setState({
daoCreationStatus: DAO_CREATION_STATUS_NONE,
buildData: null,
})
}
handleBuildDao = async (templateName, organizationName, data) => {
const { daoBuilder } = this.state
try {
const [token, dao] = await daoBuilder.build(
templateName,
organizationName,
data
)
const domain = `${organizationName}.aragonid.eth`
this.setState({
daoCreationStatus: DAO_CREATION_STATUS_SUCCESS,
buildData: { token, dao, domain },
})
log('DAO created', dao, token, domain)
} catch (err) {
log(err)
this.setState({ daoCreationStatus: DAO_CREATION_STATUS_ERROR })
}
}
updateDao(dao = null) {
// Cancel the subscriptions / unload the wrapper
if (dao === null && this.state.wrapper) {
this.state.wrapper.cancel()
this.setState({ wrapper: null })
return
}
// Reset the DAO state
this.setState({
appsStatus: APPS_STATUS_LOADING,
apps: [],
daoAddress: { address: '', domain: '' },
})
log('Init DAO', dao)
initWrapper(dao, contractAddresses.ensRegistry, {
provider: web3Providers.default,
walletProvider: web3Providers.wallet,
onError: err => {
log(`Wrapper init, recoverable error: ${err.name}. ${err.message}.`)
this.setState({ appsStatus: APPS_STATUS_ERROR })
},
onDaoAddress: ({ address, domain }) => {
log('dao address', address)
log('dao domain', domain)
this.setState({ daoAddress: { address, domain } })
},
onWeb3: web3 => {
log('web3', web3)
},
onApps: apps => {
log('apps updated', apps)
this.setState({
apps,
appsStatus: APPS_STATUS_READY,
})
},
onPermissions: permissions => {
log('permissions updated', permissions)
this.setState({
permissions,
permissionsLoading: false,
})
},
onForwarders: forwarders => {
log('forwarders', forwarders)
},
onTransaction: transactionBag => {
log('transaction bag', transactionBag)
this.setState({ transactionBag })
},
onIdentityIntent: async identityIntent => {
// set the state for modifying a specific address identity
let name = null
try {
const identity = await this.handleIdentityResolve(
identityIntent.address
)
name = identity.name
} catch (e) {}
this.setState({
identityIntent: {
label: name,
...identityIntent,
},
})
},
})
.then(wrapper => {
log('wrapper', wrapper)
this.setState({ wrapper })
return wrapper
})
.catch(err => {
log(`Wrapper init, fatal error: ${err.name}. ${err.message}.`)
this.setState({ fatalError: err })
})
}
handleRequestAppsReload = () => {
this.setState({ appsStatus: APPS_STATUS_LOADING, apps: [] })
clearTimeout(this._requestAppsTimer)
this._requestAppsTimer = setTimeout(() => {
this.updateDao(this.state.locator.dao)
}, 1000)
}
handleIdentityCancel = () => {
const { identityIntent } = this.state
identityIntent.reject(new Error('Identity modification cancelled'))
this.setState({ identityIntent: null })
}
handleIdentitySave = ({ address, label }) => {
const { identityIntent } = this.state
this.state.wrapper
.modifyAddressIdentity(address, { name: label })
.then(identityIntent.resolve)
.then(() => this.setState({ identityIntent: null }))
.catch(identityIntent.reject)
}
handleIdentityResolve = address => {
// returns promise
if (this.state.wrapper) {
return this.state.wrapper.resolveAddressIdentity(address)
} else {
// wrapper has not been initialized
// re-request in 100 ms
return new Promise(resolve => {
setTimeout(async () => {
resolve(await this.handleIdentityResolve(address))
}, 100)
})
}
}
handleCompleteOnboarding = () => {
const { domain } = this.state.buildData
this.historyPush(`/${domain}`)
}
handleOpenOrganization = address => {
this.historyPush(`/${address}`)
}
handleOpenLocalIdentityModal = address => {
return this.state.wrapper.requestAddressIdentityModification(address)
}
render() {
const {
account,
apps,
appsStatus,
balance,
connected,
daoAddress,
daoCreationStatus,
fatalError,
identityIntent,
locator,
permissions,
permissionsLoading,
selectorNetworks,
showDeprecatedBanner,
transactionBag,
walletNetwork,
walletProviderId,
walletWeb3,
wrapper,
} = this.state
const { mode, dao } = locator
if (!mode) return null
if (mode === 'invalid') {
throw new Error(
`URL contained invalid organization name or address (${dao}).\nPlease modify it to be a valid ENS name or address.`
)
}
if (fatalError !== null) {
throw fatalError
}
const { address: intentAddress = null, label: intentLabel = '' } =
identityIntent || {}
return (
<IdentityProvider onResolve={this.handleIdentityResolve}>
<ModalProvider>
<LocalIdentityModalProvider
onShowLocalIdentityModal={this.handleOpenLocalIdentityModal}
>
<LocalIdentityModal
address={intentAddress}
label={intentLabel}
opened={identityIntent !== null}
onCancel={this.handleIdentityCancel}
onSave={this.handleIdentitySave}
/>
<FavoriteDaosProvider>
<PermissionsProvider
wrapper={wrapper}
apps={apps}
permissions={permissions}
>
<Wrapper
account={account}
apps={apps}
appsStatus={appsStatus}
banner={
showDeprecatedBanner && <DeprecatedBanner dao={dao} />
}
connected={connected}
daoAddress={daoAddress}
historyBack={this.historyBack}
historyPush={this.historyPush}
identityIntent={identityIntent}
locator={locator}
onRequestAppsReload={this.handleRequestAppsReload}
onRequestEnable={this.handleRequestEnable}
permissionsLoading={permissionsLoading}
transactionBag={transactionBag}
walletNetwork={walletNetwork}
walletWeb3={walletWeb3}
wrapper={wrapper}
/>
</PermissionsProvider>
<Onboarding
banner={
showDeprecatedBanner && (
<DeprecatedBanner dao={dao} lightMode />
)
}
visible={mode === 'home' || mode === 'setup'}
account={account}
balance={balance}
walletNetwork={walletNetwork}
walletProviderId={walletProviderId}
onBuildDao={this.handleBuildDao}
daoCreationStatus={daoCreationStatus}
onComplete={this.handleCompleteOnboarding}
onOpenOrganization={this.handleOpenOrganization}
onRequestEnable={this.handleRequestEnable}
onResetDaoBuilder={this.handleResetDaoBuilder}
selectorNetworks={selectorNetworks}
/>
</FavoriteDaosProvider>
</LocalIdentityModalProvider>
</ModalProvider>
</IdentityProvider>
)
}
}
export default App