/
deploy.js
598 lines (568 loc) · 18.6 KB
/
deploy.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import { Card, Button, Alert, Icon, Steps } from 'antd'
import { Link } from 'react-router-dom'
import PropTypes from 'prop-types'
import React, { useContext, useState } from 'react'
import { ethers } from 'ethers'
import { parseEther, getContractAddress, bigNumberify } from 'ethers/utils'
import styled from 'styled-components/macro'
import { useWeb3Context } from 'web3-react'
import _GTCRFactory from 'assets/abis/LightGTCRFactory.json'
import ipfsPublish from 'utils/ipfs-publish'
import { WalletContext } from 'contexts/wallet-context'
import { ZERO_ADDRESS, isVowel } from 'utils/string'
import useWindowDimensions from 'hooks/window-dimensions'
import SubmitModal from '../item-details/modals/submit'
import useTcrView from 'hooks/tcr-view'
import {
defaultEvidenceDisplayUri,
defaultTcrAddresses,
lgtcrFactoryAddresses,
txBatcherAddresses
} from 'config/tcr-addresses'
const _txBatcher = [
{
constant: false,
inputs: [
{ name: 'targets', type: 'address[]' },
{ name: 'values', type: 'uint256[]' },
{ name: 'datas', type: 'bytes[]' }
],
name: 'batchSend',
outputs: [],
payable: true,
stateMutability: 'payable',
type: 'function'
}
]
const StyledDiv = styled.div`
word-break: break-all;
`
const StyledSteps = styled(Steps)`
margin: 24px 0;
`
const StyledAlert = styled(Alert)`
margin-bottom: 24px;
`
const StyledCard = styled(Card)`
& > .ant-card-body {
display: flex;
flex-direction: column;
}
`
const StyledActions = styled.div`
display: flex;
justify-content: flex-end;
`
const StyledButton = styled(Button)`
margin-left: 12px;
text-transform: capitalize;
`
const getTcrMetaEvidence = async (
tcrState,
parentTCRAddress,
evidenceDisplayInterfaceURI
) => {
const {
tcrTitle,
tcrDescription,
columns,
itemName,
itemNamePlural,
tcrPrimaryDocument,
tcrLogo,
relColumns,
relItemName,
relItemNamePlural,
relTcrPrimaryDocument,
isTCRofTCRs,
relTcrDisabled
} = tcrState
const metadata = {
tcrTitle,
tcrDescription,
columns,
itemName: itemName.toLowerCase(),
itemNamePlural: itemNamePlural.toLowerCase(),
logoURI: tcrLogo,
requireRemovalEvidence: true,
isTCRofTCRs,
relTcrDisabled
}
const relTcrTitle = `${tcrTitle} enabled badges`
const relMetadata = {
tcrTitle: relTcrTitle,
tcrDescription: `A List of lists related to ${tcrTitle}`,
columns: relColumns,
itemName: relItemName.toLowerCase(),
itemNamePlural: relItemNamePlural.toLowerCase(),
isConnectedTCR: true,
requireRemovalEvidence: true,
isTCRofTCRs: true,
parentTCRAddress,
relTcrDisabled: true
}
const commonMetaEvidenceProps = {
category: 'Curated Lists',
question: `Does the ${(itemName && itemName.toLowerCase()) ||
'item'} comply with the required criteria?`,
fileURI: tcrPrimaryDocument,
evidenceDisplayInterfaceURI,
metadata
}
const commonRelMetaEvidenceProps = {
...commonMetaEvidenceProps,
question: `Does the ${relItemName} comply with the required criteria?`,
fileURI: relTcrDisabled
? process.env.REACT_APP_REJECT_ALL_POLICY_URI
: relTcrPrimaryDocument,
metadata: relMetadata,
_v: '1.0.0',
evidenceDisplayInterfaceRequiredParams: [
'disputeID',
'arbitrableContractAddress',
'arbitratorContractAddress',
'arbitrableChainID',
'arbitrableJsonRpcUrl'
]
}
const registrationMetaEvidence = {
name: 'reg-meta-evidence.json',
data: {
title: `Add ${
itemName
? isVowel(itemName[0])
? `an ${itemName.toLowerCase()}`
: `a ${itemName.toLowerCase()}`
: 'an item'
} to ${tcrTitle}`,
description: `Someone requested to add ${
itemName
? isVowel(itemName[0])
? `an ${itemName.toLowerCase()}`
: `a ${itemName.toLowerCase()}`
: 'an item'
} to ${tcrTitle}`,
rulingOptions: {
titles: ['Yes, Add It', "No, Don't Add It"],
descriptions: [
`Select this if you think the ${(itemName &&
itemName.toLowerCase()) ||
'item'} complies with the required criteria and should be added.`,
`Select this if you think the ${(itemName &&
itemName.toLowerCase()) ||
'item'} does not comply with the required criteria and should not be added.`
]
},
...commonMetaEvidenceProps
}
}
const clearingMetaEvidence = {
name: 'clr-meta-evidence.json',
data: {
title: `Remove ${
itemName
? isVowel(itemName[0])
? `an ${itemName.toLowerCase()}`
: `a ${itemName.toLowerCase()}`
: 'an item'
} from ${tcrTitle}`,
description: `Someone requested to remove ${
itemName
? isVowel(itemName[0])
? `an ${itemName.toLowerCase()}`
: `a ${itemName.toLowerCase()}`
: 'an item'
} from ${tcrTitle}`,
rulingOptions: {
titles: ['Yes, Remove It', "No, Don't Remove It"],
descriptions: [
`Select this if you think the ${(itemName &&
itemName.toLowerCase()) ||
'item'} does not comply with the required criteria and should be removed.`,
`Select this if you think the ${(itemName &&
itemName.toLowerCase()) ||
'item'} complies with the required criteria and should not be removed.`
]
},
...commonMetaEvidenceProps
}
}
const relRegistrationMetaEvidence = {
name: 'rel-reg-meta-evidence.json',
data: {
title: `Add a ${relItemName} to ${relTcrTitle}`,
description: `Someone requested to add a ${relItemName} to ${relTcrTitle}.`,
rulingOptions: {
titles: ['Yes, Add It', "No, Don't Add It"],
descriptions: [
`Select this if you think the ${relItemName} complies with the required criteria and should be added.`,
`Select this if you think the ${relItemName} does not comply with the required criteria and should not be added.`
]
},
...commonRelMetaEvidenceProps
}
}
const relClearingMetaEvidence = {
name: 'rel-clr-meta-evidence.json',
data: {
title: `Remove a ${relItemName} from ${relTcrTitle}`,
description: `Someone requested to remove a ${relItemName} from ${relTcrTitle}.`,
rulingOptions: {
titles: ['Yes, Remove It', "No, Don't Remove It"],
descriptions: [
`Select this if you think the ${relItemName} does not comply with the required criteria and should be removed.`,
`Select this if you think the ${relItemName} complies with the required criteria and should not be removed.`
]
},
...commonRelMetaEvidenceProps
}
}
const enc = new TextEncoder()
const files = [
registrationMetaEvidence,
clearingMetaEvidence,
relRegistrationMetaEvidence,
relClearingMetaEvidence
].map(({ name, data }) => ({
name,
data: enc.encode(JSON.stringify(data))
}))
const ipfsMetaEvidenceObjects = (
await Promise.all(files.map(({ name, data }) => ipfsPublish(name, data)))
).map(
ipfsMetaEvidenceObject =>
`/ipfs/${ipfsMetaEvidenceObject[1].hash + ipfsMetaEvidenceObject[0].path}`
)
const [
registrationMetaEvidencePath,
clearingMetaEvidencePath,
relRegistrationMetaEvidencePath,
relClearingMetaEvidencePath
] = ipfsMetaEvidenceObjects
return {
registrationMetaEvidencePath,
clearingMetaEvidencePath,
relRegistrationMetaEvidencePath,
relClearingMetaEvidencePath
}
}
const Deploy = ({ setTxState, tcrState, setTcrState }) => {
const { networkId, library } = useWeb3Context()
const { pushWeb3Action } = useContext(WalletContext)
const { width } = useWindowDimensions()
const [currentStep, setCurrentStep] = useState(0)
const [txSubmitted, setTxSubmitted] = useState()
const [deployedTCRAddress, setDeployedTCRAddress] = useState()
const [submissionFormOpen, setSubmissionFormOpen] = useState()
const factoryAddress = lgtcrFactoryAddresses[networkId]
const defaultTCRAddress = defaultTcrAddresses[networkId]
const batcherAddress = txBatcherAddresses[networkId]
const evidenceDisplayInterfaceURI = defaultEvidenceDisplayUri[networkId]
const {
submissionDeposit,
metaEvidence,
challengePeriodDuration
} = useTcrView(defaultTCRAddress)
const onDeploy = () => {
pushWeb3Action(async (_, signer) => {
// We link the related badges TCR to its parent together by the parents address.
// To use the transaction batcher and deploy both contracts at once, we must know
// the related badges TCR address in advance.
const txCount = await library.getTransactionCount(factoryAddress)
const parentTCRAddress = getContractAddress({
from: factoryAddress,
nonce: txCount + 1
})
const {
registrationMetaEvidencePath,
clearingMetaEvidencePath,
relRegistrationMetaEvidencePath,
relClearingMetaEvidencePath
} = await getTcrMetaEvidence(
tcrState,
parentTCRAddress,
evidenceDisplayInterfaceURI
)
// To deploy two contracts at once, we use a transaction batcher.
// See github.com/kleros/action-callback-bots for an example.
const factory = new ethers.Contract(factoryAddress, _GTCRFactory, signer)
const txBatcher = new ethers.Contract(batcherAddress, _txBatcher, signer)
const relTCRArgs = [
tcrState.relArbitratorAddress,
tcrState.relArbitratorExtraData, // Arbitrator extra data.
ZERO_ADDRESS,
relRegistrationMetaEvidencePath,
relClearingMetaEvidencePath,
tcrState.relGovernorAddress,
[
parseEther(
tcrState.relTcrDisabled
? '10000000000000' // Use a very large deposit to make submissions impossible.
: tcrState.relSubmissionBaseDeposit.toString()
),
parseEther(tcrState.relRemovalBaseDeposit.toString()),
parseEther(tcrState.relSubmissionChallengeBaseDeposit.toString()),
parseEther(tcrState.relRemovalChallengeBaseDeposit.toString())
],
Number(tcrState.relChallengePeriodDuration) * 60 * 60,
[
Math.ceil(Number(tcrState.relSharedStakeMultiplier)) * 100,
Math.ceil(Number(tcrState.relWinnerStakeMultiplier)) * 100,
Math.ceil(Number(tcrState.relLoserStakeMultiplier)) * 100
], // Shared, winner and loser stake multipliers in basis points.
ZERO_ADDRESS
]
const relData = factory.interface.functions.deploy.encode(relTCRArgs)
const relTCRAddress = getContractAddress({
from: factoryAddress,
nonce: txCount
})
const TCRArgs = [
tcrState.arbitratorAddress,
tcrState.arbitratorExtraData, // Arbitrator extra data.
relTCRAddress,
registrationMetaEvidencePath,
clearingMetaEvidencePath,
tcrState.governorAddress,
[
parseEther(tcrState.submissionBaseDeposit.toString()),
parseEther(tcrState.removalBaseDeposit.toString()),
parseEther(tcrState.submissionChallengeBaseDeposit.toString()),
parseEther(tcrState.removalChallengeBaseDeposit.toString())
],
Number(tcrState.challengePeriodDuration) * 60 * 60,
[
Math.ceil(Number(tcrState.sharedStakeMultiplier)) * 100,
Math.ceil(Number(tcrState.winnerStakeMultiplier)) * 100,
Math.ceil(Number(tcrState.loserStakeMultiplier)) * 100
], // Shared, winner and loser stake multipliers in basis points.
ZERO_ADDRESS
]
const tcrData = factory.interface.functions.deploy.encode(TCRArgs)
const targets = [factory.address, factory.address]
const values = [bigNumberify(0), bigNumberify(0)]
const datas = [relData, tcrData]
const deployTx = await txBatcher.batchSend(targets, values, datas, {
gasLimit: 8000000
})
setCurrentStep(1)
setTxState({ txHash: deployTx.hash, status: 'pending', networkId })
setTxSubmitted(deployTx.hash)
return {
tx: deployTx,
actionMessage: 'Deploying List',
deployTCR: true,
onTxMined: async ({ contractAddress }) => {
setTxState({
txHash: deployTx.hash,
status: 'mined',
contractAddress
})
setTcrState(prevState => ({
...prevState,
finished: true
}))
setCurrentStep(2)
setDeployedTCRAddress(contractAddress)
}
}
})
}
return (
<>
<StyledCard title="Deploy the list">
{currentStep === 0 && (
<StyledAlert
showIcon
type="info"
closable
message="On your marks..."
description="When you are ready, click deploy. You may also want to add it to The Registry so people can find it. If so, don't close the window and wait for the transaction to mine."
/>
)}
{currentStep === 1 && (
<StyledAlert
showIcon
type="info"
closable
message="Deploy in progress. Please do not close the window until the process is finished."
/>
)}
<StyledSteps
current={currentStep}
direction={width < 750 ? 'vertical' : 'horizontal'}
>
<Steps.Step
title="Start"
description={currentStep > 0 && 'Finished'}
icon={<Icon type="fire" />}
/>
<Steps.Step
title="Deploying list"
description={currentStep > 1 && 'Finished'}
icon={
currentStep < 1 ? (
<Icon type="star" />
) : currentStep === 1 ? (
<Icon type="loading" />
) : (
<Icon type="check" />
)
}
/>
<Steps.Step title="Finished!" icon={<Icon type="flag" />} />
</StyledSteps>
{currentStep === 2 && (
<StyledAlert
type="success"
showIcon
message="Success!"
description={
<>
<StyledDiv>
Your list was created at the following address:{' '}
<Link
to={`/tcr/${networkId}/${tcrState.transactions[txSubmitted].contractAddress}`}
>
{tcrState.transactions[txSubmitted].contractAddress}
</Link>
.
</StyledDiv>
<StyledDiv>
You may want to bookmark its address or, if it adheres to the
listing criteria,{' '}
<Button
type="link"
onClick={setSubmissionFormOpen}
style={{ padding: 0 }}
>
submit it to{' '}
{(metaEvidence && metaEvidence.metadata.tcrTitle) ||
'Curated Lists'}{' '}
so other users can find it.
</Button>
</StyledDiv>
</>
}
/>
)}
{currentStep === 0 && (
<span
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end'
}}
>
<StyledActions>
<StyledButton
type="primary"
onClick={onDeploy}
icon={
currentStep === 0 || currentStep === 2 ? 'fire' : 'loading'
}
>
Deploy!
</StyledButton>
</StyledActions>
</span>
)}
</StyledCard>
{metaEvidence && (
<SubmitModal
initialValues={[deployedTCRAddress]}
visible={!!submissionFormOpen}
onCancel={() => setSubmissionFormOpen(false)}
submissionDeposit={submissionDeposit}
tcrAddress={defaultTCRAddress}
metaEvidence={metaEvidence}
challengePeriodDuration={challengePeriodDuration}
/>
)}
</>
)
}
Deploy.propTypes = {
setTxState: PropTypes.func.isRequired,
setTcrState: PropTypes.func.isRequired,
tcrState: PropTypes.shape({
transactions: PropTypes.objectOf(
PropTypes.shape({
status: PropTypes.oneOf(['pending', 'mined', null]),
contractAddress: PropTypes.string
})
).isRequired,
arbitratorAddress: PropTypes.string.isRequired,
arbitratorExtraData: PropTypes.string.isRequired,
governorAddress: PropTypes.string.isRequired,
submissionChallengeBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
removalChallengeBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
submissionBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
removalBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
challengePeriodDuration: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
sharedStakeMultiplier: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
winnerStakeMultiplier: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
loserStakeMultiplier: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relArbitratorAddress: PropTypes.string.isRequired,
relArbitratorExtraData: PropTypes.string.isRequired,
relGovernorAddress: PropTypes.string.isRequired,
relSubmissionChallengeBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relRemovalChallengeBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relSubmissionBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relRemovalBaseDeposit: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relChallengePeriodDuration: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relSharedStakeMultiplier: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relWinnerStakeMultiplier: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relLoserStakeMultiplier: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]).isRequired,
relTcrDisabled: PropTypes.bool
}).isRequired
}
export default Deploy