-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdelegate.js
More file actions
347 lines (319 loc) · 11.9 KB
/
delegate.js
File metadata and controls
347 lines (319 loc) · 11.9 KB
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
import axios from "axios"; // Requests wrapper
import { useState } from "react"; // State management
import Layout from "components/layout"; // Layout wrapper
import APICTA from "components/api_cta"; // API CTA
import styles from "styles/page.module.scss"; // Page styles
import { web3p, delegate } from "containers"; // Context
import { BeatLoader } from "react-spinners"; // Loading state
export default function Delegate({
defaultAccounts,
defaultPages,
defaultDelegated,
}) {
const [loading, setLoading] = useState(false); // Accounts loading state
const [delegated] = useState(defaultDelegated); // Max delegated votes
const [pages, setPages] = useState(defaultPages); // Accounts pagination
const [customAddress, setCustomAddress] = useState(""); // Custom voting address
const [buttonLoading, setButtonLoading] = useState(null); // Delegation button loading state
const [accounts, setAccounts] = useState(defaultAccounts); // Accounts array
// Web3 + Authenticate function from context
const { web3, address, authenticate, isValidAddress } = web3p.useContainer();
const { currentDelegate, createDelegation } = delegate.useContainer();
/**
* Pagination handler
*/
const getNextPage = async () => {
// Toggle loading state
setLoading(true);
// Collect next page request string and request
const nextPage = `api/governance/accounts?page_size=10&page_number=${
pages.current + 1
}`;
const response = await axios.get(nextPage);
// Update proposals array with new proposals
setAccounts([...accounts, ...response.data.accounts]);
// Increment current page number in pages object
setPages((pages) => ({
// Destructure keys
...pages,
// Update current key with incremented value
current: pages.current + 1,
}));
// Toggle loading state
setLoading(false);
};
/**
* createDelegation with button loading state
* @param {string} address to delegate to
* @param {number} rank 0 === custom delegation, else rank === address rank in table
*/
const createDelegationWithLoading = async (address, rank) => {
// Toggle button loading to true
setButtonLoading(rank);
try {
// Call createDelegation
await createDelegation(address);
} catch {
// If MetaMask cancellation, force toggle loading to false
setButtonLoading(null);
}
// Else, toggle loading to false on success
setButtonLoading(null);
};
return (
<Layout>
{/* Page head */}
<div className={styles.head}>
<div>
{/* Description of delegating by signature */}
<h1>Delegate By Signature</h1>
<div>
<p>
Delegating by signature lets you delegate your COMP to community
members without having to send your transactions on-chain, saving
fees.
</p>
</div>
{/* Current delegation status if delegated */}
{currentDelegate ? (
// If delegated, display information
<div>
<h2>
<a
// Link to Compound Governance profile on tally
href={`https://www.tally.xyz/gov/compound/delegate/${currentDelegate}`}
target="_blank"
rel="noopener noreferrer"
>
{currentDelegate.substr(0, 5) +
// Address of delegate
"..." +
currentDelegate.slice(currentDelegate.length - 5)}
</a>
</h2>
<h3>Delegating To</h3>
</div>
) : null}
</div>
</div>
{/* Page body */}
<div className={styles.body}>
<div>
{/* Top addresses by voting weight card */}
<div className={styles.card}>
{/* Card header */}
<div>
<h4>Addresses by Voting Weight</h4>
</div>
{/* Card legend */}
<div className={styles.legend}>
<span>Rank</span>
<span>Vote Weight</span>
<span>Proposals Voted</span>
</div>
{/* Card delegates content */}
<div>
{accounts.map((delegate, i) => {
// For each delegate
return (
// Render delegate card
<div className={styles.delegate} key={i}>
{/* Delegate rank by vote weight */}
<div>
<span>{delegate.rank}</span>
</div>
{/* Delegate avatar */}
<div>
{delegate.image_url ? (
// If avatar provided, use image_url
<img src={delegate.image_url} alt="Delegate avatar" />
) : (
// Else generate avatar based on address
<img
src={`https://icotar.com/avatar/${delegate.address}.png?s=50`}
alt="Delegate avatar"
/>
)}
</div>
{/* Delegate name */}
<div>
<a
// Link name to Twitter or Etherscan if no twitter
href={
!!delegate.twitter && delegate.twitter != ""
? `https://twitter.com/${delegate.twitter}`
: `https://etherscan.io/address/${delegate.address}`
}
target="_blank"
rel="noopener noreferrer"
>
{
// If delegate has display name
delegate.display_name
? // Show custom display name
delegate.display_name
: // Else if delegate has associated crowd proposal
delegate.crowd_proposal
? // Show associated crowd proposal title
delegate.crowd_proposal.title
: // Else, show truncated address
delegate.address.substr(0, 4) +
"..." +
delegate.address.slice(
delegate.address.length - 4
)
}
</a>
</div>
{/* Delegate vote weight */}
<div>
<span>
{
// Calculate vote weight (total / delegate) * 100
(
(parseFloat(delegate.votes) / delegated) *
100
).toFixed(2)
}
%
</span>
</div>
{/* Delegate voted proposal count */}
<div>
<span>{delegate.proposals_voted}</span>
</div>
{/* Delegate action buttons */}
<div>
<button
// If web3 ? delegate function : authenticate state
onClick={
web3
? () =>
createDelegationWithLoading(
delegate.address,
delegate.rank
)
: authenticate
}
className={styles.info}
>
{web3 ? (
buttonLoading === delegate.rank ? (
<BeatLoader size={9} />
) : (
"Delegate"
)
) : (
"Authenticate"
)}
</button>
</div>
</div>
);
})}
</div>
{/* More accounts loading button */}
<div className={styles.cardMore}>
{/* Load more accounts button */}
<button onClick={getNextPage} disabled={loading}>
{loading ? "Loading..." : "Load More Delegates"}
</button>
</div>
</div>
{/* Custom address delegation card */}
<div className={styles.card}>
<div>
<h4>Custom Address</h4>
</div>
<div>
<div className={styles.customDelegate}>
{web3 ? (
<>
<input
type="text"
placeholder="0xac5720d6ee2d7872b88914c9c5fa9bf38e72faf6"
value={customAddress}
onChange={(e) => setCustomAddress(e.target.value)}
/>
{isValidAddress(customAddress) ? (
// If enterred address is valid, display delegation button
<button
// Delegate to input address
onClick={() =>
createDelegationWithLoading(customAddress, 0)
}
className={styles.info}
>
{buttonLoading === 0 ? (
<BeatLoader size={9} />
) : (
"Delegate"
)}
</button>
) : (
// Else, display invalid address button
<button disabled>Invalid Address</button>
)}
{/* Self delegation */}
<button
onClick={() => createDelegationWithLoading(address, -1)}
className={styles.info}
>
{buttonLoading === -1 ? (
<BeatLoader size={9} />
) : (
"Self-delegate"
)}
</button>
</>
) : (
<button onClick={authenticate} className={styles.info}>
Authenticate
</button>
)}
</div>
</div>
</div>
{/* Swagger API CTA card */}
<APICTA />
</div>
</div>
</Layout>
);
}
export async function getServerSideProps() {
// Collect first page data
const deployedUrl =
process.env.VERCEL_URL == "localhost:3000"
? `http://${process.env.VERCEL_URL}`
: `https://${process.env.VERCEL_URL}`;
const firstPage =
deployedUrl + "/api/governance/accounts?page_size=10&page_number=1";
const response = await axios.get(firstPage);
// Collect delegated vote count
const historyURL = `https://gateway-arbitrum.network.thegraph.com/api/${process.env.GRAPH_API_KEY}/subgraphs/id/GHB6EWsmMXy2AJaCodmK2AmZviitTZf3Tbo8YEfuh6St`;
const historyResponse = await axios.post(historyURL, {
query: `{
governance(id:"GOVERNANCE"){
delegatedVotes
}
}`,
});
// Return:
return {
props: {
// First 10 addresses
defaultAccounts: response.data.accounts,
defaultPages: {
// Current paginated proposal page (default: 1)
current: response.data.pagination_summary.page_number,
// Maximum number of paginated proposal pages
max: response.data.pagination_summary.total_pages,
},
// Total delegated vote count
defaultDelegated: parseFloat(
historyResponse.data.data.governance.delegatedVotes
),
},
};
}