This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
generated from PolymathNetwork/dapp-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
278 lines (262 loc) · 7.81 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
import React, { useContext, useEffect, Fragment } from 'react'
import { usePolymathSdk, User, Network, useTokenSelector} from '@polymathnetwork/react'
import { Feature } from '@polymathnetwork/sdk'
import { Store } from './index'
import { Layout, Spin, Alert, Button, Descriptions, Badge, Divider } from 'antd'
import PMDisplay from './PMDisplay'
import { _split } from './index'
const { Content, Header, Sider } = Layout
const PERMISSIONS_FEATURE = Feature.Permissions
export const reducer = (state, action) => {
console.log('ACTION', action)
switch (action.type) {
case 'ASYNC_START':
return {
...state,
loading: true,
loadingMessage: action.msg,
error: undefined,
}
case 'ASYNC_COMPLETE':
const { type, ...payload } = action
return {
...state,
...payload,
loading: false,
loadingMessage: '',
error: undefined
}
case 'ERROR':
case 'ASYNC_ERROR':
const { error } = action
return {
...state,
loading: false,
loadingMessage: '',
error,
}
case 'TOKEN_SELECTED':
return {
...state,
delegates: undefined,
records: undefined,
pmEnabled: undefined,
error: undefined,
features: undefined,
}
default:
throw new Error(`Unrecognized action type: ${action.type}`)
}
}
function Features({features, pmEnabled, onClick}) {
return (
<Descriptions column={4} style={{marginBottom: 50}}>
<Descriptions.Item key='Permissions' label='Permissions'>
{ pmEnabled
? <Badge status='success' text='enabled' />
: <Button type="primary" onClick={onClick}>Enable</Button> }
</Descriptions.Item>
}
{Object.keys(features).map(feat => {
return (<Descriptions.Item key={feat} label={_split(feat)}>
<Badge status={features[feat] ? 'success' : 'error'} text={features[feat] ? 'enabled' : 'disabled'} />
</Descriptions.Item>
)}
)}
</Descriptions> )
}
async function asyncAction(dispatch, func, msg = '') {
try {
dispatch({type: 'ASYNC_START', msg})
const rets = await func()
dispatch({type: 'ASYNC_COMPLETE', ...rets})
}
catch (error) {
dispatch({type: 'ASYNC_ERROR', error: error.message})
}
}
function App() {
const [state, dispatch] = useContext(Store)
let {error: sdkError, sdk, networkId, walletAddress} = usePolymathSdk()
let {
error: tokenSelectorError,
tokenSelector,
tokens,
tokenIndex,
} = useTokenSelector(sdk, walletAddress)
let {
loading,
loadingMessage,
error,
pmEnabled,
records,
features,
availableRoles
} = state.AppReducer
const token = tokens[tokenIndex]
error = error || sdkError || tokenSelectorError
if (!error && !loadingMessage) {
if (!sdk) {
loading = true
loadingMessage = 'Initializing Polymath SDK'
}
else if (!tokens.length) {
loading = true
loadingMessage = 'Loading your security tokens'
}
}
// Load features status / available roles
useEffect(() => {
async function getFeaturesStatus() {
const featuresStatus = await token.features.getStatus()
let availableRoles = []
const pmEnabled = featuresStatus[PERMISSIONS_FEATURE]
delete featuresStatus[PERMISSIONS_FEATURE]
if (pmEnabled) {
availableRoles = await token.permissions.getAvailableRoles()
}
return {
availableRoles, features: featuresStatus, pmEnabled
}
}
if (token && !features) {
asyncAction(dispatch, () => getFeaturesStatus(), 'Loading features status')
}
}, [dispatch, features, token])
// Load delegates
useEffect(() => {
async function getDelegates() {
const delegates = await token.permissions.getAllDelegates()
const records = delegates.reduce((acc, delegate, i) => {
return acc.concat(delegate.roles.map(role => ({
address: delegates[i].address,
description: delegates[i].description,
role
})))
}, [])
return {
delegates,
records
}
}
if (token && pmEnabled) {
asyncAction(dispatch, () => getDelegates(), 'Loading delegates')
}
}, [pmEnabled, dispatch, token])
async function togglePM(enable) {
try {
dispatch({type: 'ASYNC_START', msg: 'Toggle role management'})
if (enable) {
// Enable module
const queue = await token.features.enable({feature: PERMISSIONS_FEATURE})
await queue.run()
} else {
// Disable module
const queue = await token.features.disable({feature: PERMISSIONS_FEATURE})
await queue.run()
}
dispatch({type: 'ASYNC_COMPLETE', pmEnabled: !enable})
dispatch({type: 'TOKEN_SELECTED'})
} catch (error) {
console.error(error)
dispatch({
type: 'ASYNC_ERROR',
error: error.message
})
}
}
const revokeRole = async (address, role) => {
try {
dispatch({type: 'ASYNC_START', msg: `Revoking ${role} role from ${address}`})
const queue = await token.permissions.revokeRole({ delegateAddress: address, role })
await queue.run()
dispatch({type: 'ASYNC_COMPLETE'})
dispatch({type: 'TOKEN_SELECTED'})
} catch (error) {
console.error(error)
dispatch({
type: 'ASYNC_ERROR',
error: error.message
})
}
}
const assignRole = async (address, role, description) => {
try {
dispatch({type: 'ASYNC_START', msg: `Assigning ${role} role to ${address}`})
const queue = await token.permissions.assignRole({ delegateAddress: address, role, description})
await queue.run()
dispatch({type: 'ASYNC_COMPLETE'})
dispatch({type: 'TOKEN_SELECTED'})
} catch (error) {
console.error(error)
dispatch({
type: 'ASYNC_ERROR',
error: error.message
})
}
}
return (
<div>
<Spin spinning={loading} tip={loadingMessage} size="large">
<Layout>
<Header style={{
backgroundColor: 'white',
display: 'flex',
flexDirection: 'row',
justifyContent: 'flex-end',
alignItems: 'center'
}}>
<Network networkId={networkId} />
<User walletAddress={walletAddress} />
</Header>
<Layout>
<Sider width={350}
style={{
padding: 50,
backgroundColor: '#FAFDFF'
}}
>
{ walletAddress && tokens &&
<div style={{
display: 'flex',
flexDirection: 'column',
width: 250,
justifyContent: 'flex-start'
}}>
{tokenSelector({
onTokenSelect: () => dispatch({type: 'TOKEN_SELECTED'})
})}
</div>
}
</Sider>
<Content style={{
padding: 50,
backgroundColor: '#FAFDFF'
}}>
{error && <Alert
message={error}
type="error"
closable
showIcon
/>}
{ token && features &&
<Fragment>
<Divider orientation="left">Token features</Divider>
<Features features={features} pmEnabled={pmEnabled} onClick={togglePM} />
</Fragment> }
{ token && availableRoles && records && <React.Fragment>
<Divider orientation="left">Delegates (administrators and operators)</Divider>
<PMDisplay
records={records}
roles={availableRoles}
revokeRole={revokeRole}
assignRole={assignRole}/>
</React.Fragment> }
</Content>
</Layout>
</Layout>
</Spin>
</div>
)
}
export default App