-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinterhack-grant.ride
More file actions
789 lines (683 loc) · 21.2 KB
/
interhack-grant.ride
File metadata and controls
789 lines (683 loc) · 21.2 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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
let QUORUM = 1 # 1% votes needed
let mainContract = addressFromStringValue("3NCsqf4pFGtECs3vM4j1nKqL6VA3HdiNGeZ") # HERE TO SET MEMBERSHIP SMART CONTRACT ADDRESS
func tryGetInteger(key: String) = {
let val = match getInteger(this, key) {
case b:Int => b
case _ => 0
}
val
}
func tryGetString(key: String) = {
let val = match getString(this, key) {
case b:String => b
case _ => ""
}
val
}
func getGroup() = {
let val = match getString(mainContract, "working_group_members") {
case b:String => b
case _ => ""
}
val
}
func getMembers() = {
let val = match getString(mainContract, "dao_members") {
case b:String => b
case _ => ""
}
val
}
func getDAOSize() = {
let val = match getInteger(mainContract, "dao_size") {
case b:Int => b
case _ => 0
}
val
}
func isGroupMember(a: Address) = {
# Grants & Funding group membership check
let val = match getInteger(mainContract, "working_group_member_"+a.toString()+"_weight") {
case b:Int => b
case _ => 0
}
val > 0
}
func canMemberVote(address: Address) = {
# DAO membership check
let val = match getInteger(mainContract, "dao_member_"+address.toString()+"_weight") {
case b:Int => b
case _ => 0
}
val > 0
}
func isJuryMember(address: Address, taskId: String) = {
# let juryListString = tryGetString("tasks_"+taskId+"_jury")
# let juryList = juryListString.split(";")
# indexOf(juryList, address.toString()) != unit
canMemberVote(address)
}
func getVoteValue(s: String) = {
if (s == "like") then {
1
} else {
if (s == "dislike") then {
-1
} else {
throw("you need to vote with 'like' or 'dislike' value")
}
}
}
func addTaskConditions(address: Address) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else true
}
func addRewardConditions(address: Address, taskId: String) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else if (tryGetString("tasks_"+taskId+"_status") != "") then {
throw("grant isn`t new")
} else true
}
func voteForTaskProposalConditions(address: Address, taskId: String) = {
if (!canMemberVote(address)) then {
throw("you can't vote")
} else if (tryGetString("tasks_"+taskId+"_status") != "proposed") then {
throw("voting is closed or not started")
} else if (tryGetInteger("tasks_"+taskId+"_voted_"+address.toString()) != 0) then {
throw("you have already voted")
} else true
}
func finishTaskProposalVotingConditions(address: Address, taskId: String) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else if (tryGetString("tasks_"+taskId+"_status") != "proposed") then {
throw("voting is not active")
} else if ((tryGetInteger("tasks_"+taskId+"_voting_amount") * 100) / getDAOSize() < QUORUM) then {
throw("more than " + QUORUM.toString() + "% members have to vote before finishing the voting")
} else true
}
func applyForTaskConditions(address: Address, taskId: String, teamIdentifier: String) = {
if (tryGetString("tasks_"+taskId+"_status") != "ready_to_apply") then {
throw("this grant has not yet been approved")
} else if (tryGetString("tasks_"+taskId+"_app_"+teamIdentifier+"_leader") == address.toString()) then {
throw("you are applied")
} else true
}
func voteForApplicantConditions(address: Address, taskId: String, teamIdentifier: String) = {
let voteHash = drop(address.toString(), 10) + drop(teamIdentifier, 10)
if (!canMemberVote(address)) then {
throw("you can't vote")
} else if (tryGetString("tasks_"+taskId+"_status") != "ready_to_apply") then {
throw("voting is not active")
} else if (tryGetInteger("tasks_"+taskId+"_vote_"+voteHash) != 0) then {
throw("you have already voted on this team")
} else true
}
func finishApplicantsVotingConditions(address: Address, taskId: String, applicantsList: List[String]) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else if (tryGetString("tasks_"+taskId+"_status") != "ready_to_apply") then {
throw("voting is not active")
} else if (size(applicantsList) == 0) then {
throw("no one has yet responded to the grant")
} else true
}
func startWorkConditions(address: Address, taskId: String, teamIdentifier: String) = {
if (tryGetString("tasks_"+taskId+"_status") != "team_chosen") then {
throw("winner is not define")
} else if (address.toString() != tryGetString("tasks_"+taskId+"_app_"+teamIdentifier+"_leader")) then {
throw("team leader only")
} else true
}
func acceptWorkResultConditions(address: Address, taskId: String, winnerIdentifier: String) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else if (tryGetString("tasks_"+taskId+"_status") != "work_finished") then {
throw("work is not finished")
} else if (tryGetInteger("tasks_"+taskId+"_app_"+winnerIdentifier+"_score_solution") < 1) then {
throw("negative assessment")
} else true
}
func enableSubmissionsConditions(address: Address, taskId: String) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else true
}
func submitSolutionConditions(address: Address, taskId: String, teamIdentifier: String) = {
if (tryGetString("tasks_"+taskId+"_status") != "work_started") then {
throw("work is not started")
} else if (address.toString() != tryGetString("tasks_"+taskId+"_app_"+teamIdentifier+"_leader")) then {
throw("team leader only")
} else if (tryGetInteger("tasks_"+taskId+"_app_"+teamIdentifier+"_score_applicant") < 1) then {
throw("your team is not approved")
} else if (tryGetString("tasks_"+taskId+"_app_"+teamIdentifier+"_process") == "work_submitted") then {
throw("you have already presented the solution")
} else true
}
func stopSubmissionsConditions(address: Address, taskId: String) = {
if (!(isGroupMember(address))) then {
throw("grants working group access only")
} else if (tryGetString("tasks_"+taskId+"_status") != "work_started") then {
throw("work is not started")
} else true
}
func voteForSolutionConditions(address: Address, taskId: String) = {
if (!(isJuryMember(address, taskId))) then {
throw("you can't vote")
} else if (tryGetString("tasks_"+taskId+"_status") != "work_finished") then {
throw("work is not started")
} else true
}
# Set status -> undefined
# [taskId]: {
# title,
# link
# }
@Callable(i)
func addTask(title: String, link: String) = {
if (addTaskConditions(i.caller)) then {
WriteSet([
DataEntry("tasks_" + i.transactionId.toBase58String() + "_title", title),
DataEntry("tasks_" + i.transactionId.toBase58String() + "_link", link),
DataEntry("tasks_" + i.transactionId.toBase58String() + "_createdAt", lastBlock.timestamp)
])
} else throw("checks are not passed")
}
# Set status -> proposed
# [taskId]: {
# title,
# link,
# reward
# }
# transaction waves on address
@Callable(i)
func addReward(taskId: String) = {
# will also contain task description / requirements / reward payment model
if (addRewardConditions(i.caller, taskId)) then {
let reward = i.payment.extract().amount
WriteSet([
# Parsed in dAPP to tasks[id].reward
DataEntry("tasks_"+taskId+"_reward", reward),
# Parsed in dAPP to tasks[id].status
DataEntry("tasks_"+taskId+"_status", "proposed")
])
} else throw("checks are not passed")
}
# Unchanged status -> proposal
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# }
@Callable(i)
func voteForTaskProposal(taskId: String, voteValue: String) = {
if (voteForTaskProposalConditions(i.caller, taskId)) then {
let voteKey = "tasks_"+taskId+"_voted_"+i.caller.toString()
# Parsed in dAPP to tasks[id].voting.state -> integer
let votingKey = "tasks_"+taskId+"_voting_state"
let votingState = tryGetInteger(votingKey)
# Parsed in dAPP to tasks[id].voting.amount -> integer
let votesAmountKey = "tasks_"+taskId+"_voting_amount"
let votesAmountState = tryGetInteger(votingKey) + 1
let voteValueInt = getVoteValue(voteValue)
let newVotingState = votingState + voteValueInt
WriteSet([
DataEntry(votingKey, newVotingState),
DataEntry(voteKey, voteValueInt),
DataEntry(votesAmountKey, votesAmountState)
])
} else throw("checks are not passed")
}
# Set status -> ready_to_apply | rejected
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# }
@Callable(i)
func finishTaskProposalVoting(taskId: String) = {
if (finishTaskProposalVotingConditions(i.caller, taskId)) then {
let votingKey = "tasks_"+taskId+"_voting_state"
WriteSet([
# Parsed in dAPP to tasks[id].status -> string
DataEntry("tasks_" + taskId + "_status",
if (tryGetInteger(votingKey) > 0)
then "ready_to_apply"
else "rejected"
)
])
} else throw("checks are not passed")
}
# Unchanged status -> ready_to_apply
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id
# }
# }
# }
@Callable(i)
func applyForTask(taskId: String, teamName: String, link: String) = {
let teamIdentifier = drop(i.caller.toString(), 10)
if (applyForTaskConditions(i.caller, taskId, teamIdentifier)) then {
let listKey = "tasks_"+taskId+"_applicants"
let currentApplicantsList = tryGetString(listKey)
# Use address leader as identifier team
let newApplicantsList = currentApplicantsList+";"+i.caller.toString()
WriteSet([
# Parsed in dAPP to tasks[id].app[id].name -> string
DataEntry("tasks_"+taskId+"_app_"+teamIdentifier+"_name", teamName),
DataEntry("tasks_"+taskId+"_app_"+teamIdentifier+"_link", link),
# Parsed in dAPP to tasks[id].app[id].leader -> id
DataEntry("tasks_"+taskId+"_app_"+teamIdentifier+"_leader", i.caller.toString()),
# Parsed in dAPP to tasks[id].app[id].id -> key
DataEntry("tasks_"+taskId+"_app_"+teamIdentifier+"_id", teamIdentifier),
# Parsed in dAPP to tasks[id].apps -> list id (separator ;)
DataEntry(listKey, newApplicantsList)
])
} else throw("checks are not passed")
}
# Unchanged status -> ready_to_apply
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# }
# votes {
# applicant
# }
# voted {
# applicant
# }
# }
@Callable(i)
func voteForApplicant(taskId: String, teamIdentifier: String, voteValue: String) = {
if (voteForApplicantConditions(i.caller, taskId, teamIdentifier)) then {
let listKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_voted_applicant"
let newApplicantsList = tryGetString(listKey)+";"+i.caller.toString()
let voteHash = drop(i.caller.toString(), 20) + teamIdentifier
let voteKey = "tasks_"+taskId+"_vh_"+voteHash
if (tryGetString(voteKey) == i.caller.toString()) then {
throw("you have already voted on this team")
} else {
let teamVotesAmountKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_votes_applicant"
let teamScoreKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_score_applicant"
let teamVotesAmount = 1 + tryGetInteger(teamVotesAmountKey)
let newTeamScore = getVoteValue(voteValue) + tryGetInteger(teamScoreKey)
WriteSet([
# Parsed in dAPP to tasks[id].app[teamId].votes -> integer
DataEntry(listKey, newApplicantsList),
DataEntry(voteKey, i.caller.toString()),
DataEntry(teamVotesAmountKey, teamVotesAmount),
# Parsed in dAPP to tasks[id].app[teamId].score -> integer
DataEntry(teamScoreKey, newTeamScore)
])
}
} else throw("checks are not passed")
}
# Set status -> ready_to_submit
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# }
# votes {
# applicant
# }
# voted {
# applicant
# }
# }
# }
# }
@Callable(i)
func finishApplicantsVoting(taskId: String) = {
let listKeys = tryGetString("tasks_" + taskId + "_applicants")
let applicantsList = listKeys.split(";")
if (finishApplicantsVotingConditions(i.caller, taskId, applicantsList)) then {
WriteSet([
# Parsed in dAPP to tasks[id].status -> string
DataEntry("tasks_"+taskId+"_status", "ready_to_submit")
])
} else throw("checks are not passed")
# Complex logic
# func filterApproved(accumulated: String, a: String) = {
# let teamIdentifier = drop(a, 10)
# let teamVotesScoreKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_score"
# let teamVotesScore = tryGetInteger(teamVotesScoreKey)
# if (teamVotesScore > 0) then a+";"+accumulated else accumulated
# }
# if (finishApplicantsVotingConditions(i.caller, taskId, applicantsList)) then {
# let chosenTeams = FOLD<10>(applicantsList, "", filterApproved)
# WriteSet([
# # Parsed in dAPP to tasks[id].status -> string
# DataEntry("tasks_"+taskId+"_status", "ready_to_submit"),
# # Parsed in dAPP to tasks[id].performers -> LIST[String]
# DataEntry("tasks_"+taskId+"_performers", chosenTeams)
# ])
# } else throw("checks are not passed")
}
# Set status -> work_started
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# }
# votes {
# applicant
# }
# voted {
# applicant
# }
# }
# }
#> jury
# }
@Callable(i)
func enableSubmissions(taskId: String, juryList: String) = {
if (enableSubmissionsConditions(i.caller, taskId)) then {
WriteSet([
DataEntry("tasks_"+taskId+"_status", "work_started"),
DataEntry("tasks_"+taskId+"_jury", juryList)
])
} else throw("checks are not passed")
}
# Unchange status -> work_started
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# }
# votes {
# applicant
# }
# voted {
# applicant
# }
#> process,
#> soluton
# }
# }
# jury
# }
@Callable(i)
func submitSolution(taskId: String, solutionLink: String) = {
let teamIdentifier = drop(i.caller.toString(), 10)
if (submitSolutionConditions(i.caller, taskId, teamIdentifier)) then {
WriteSet([
DataEntry("tasks_"+taskId+"_app_"+teamIdentifier+"_process", "work_submitted"),
DataEntry("tasks_"+taskId+"_app_"+teamIdentifier+"_solution", solutionLink)
])
} else throw("checks are not passed")
}
# Set status -> work_finished
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# }
# votes {
# applicant
# }
# voted {
# applicant
# }
# process,
# soluton
# }
# }
# jury
# }
@Callable(i)
func stopSubmissions(taskId: String) = {
if (stopSubmissionsConditions(i.caller, taskId)) then {
WriteSet([
DataEntry("tasks_"+taskId+"_status", "work_finished")
])
} else throw("checks are not passed")
}
# Unchanged status -> work_finished
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# solution
# }
# votes {
# applicant
# solution
# }
# voted {
# applicant
# solution
# }
# process,
# soluton
# }
# }
# jury
# }
@Callable(i)
func voteForSolution(taskId: String, teamIdentifier: String, voteValue: String) = {
if (voteForSolutionConditions(i.caller, taskId)) then {
let listKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_voted_solution"
let newVotedList = tryGetString(listKey)+";"+i.caller.toString()
let voteHash = drop(i.caller.toString(), 20) + teamIdentifier
let voteKey = "tasks_"+taskId+"_vs_"+voteHash
if (tryGetString(voteKey) == i.caller.toString()) then {
throw("you have already voted on this team")
} else {
let teamVotesAmountKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_votes_solution"
let teamScoreKey = "tasks_"+taskId+"_app_"+teamIdentifier+"_score_solution"
let teamVotesAmount = 1 + tryGetInteger(teamVotesAmountKey)
let newTeamScore = getVoteValue(voteValue) + tryGetInteger(teamScoreKey)
WriteSet([
# Parsed in dAPP to tasks[id].app[teamId].votes -> integer
DataEntry(listKey, newVotedList),
DataEntry(voteKey, i.caller.toString()),
DataEntry(teamVotesAmountKey, teamVotesAmount),
# Parsed in dAPP to tasks[id].app[teamId].score -> integer
DataEntry(teamScoreKey, newTeamScore)
])
}
} else throw("checks are not passed")
}
# Set status -> solution_chosen
# [taskId]: {
# title,
# link,
# reward,
# voting = {
# amount,
# state
# },
# voted = {
# [address] = undefined | 1
# }
# applicants
# app = {
# [id] = {
# name,
# link,
# leader,
# id,
# score {
# applicant
# solution
# }
# votes {
# applicant
# solution
# }
# voted {
# applicant
# solution
# }
#> process,
# soluton
# }
# }
# jury
#> report
# }
@Callable(i)
func acceptWorkResult(taskId: String, winnerIdentifier: String, reportLink: String) = {
if (acceptWorkResultConditions(i.caller, taskId, winnerIdentifier)) then {
let rewardAmount = tryGetInteger("tasks_"+taskId+"_reward")
let teamAddress = this.getStringValue("tasks_"+taskId+"_app_"+winnerIdentifier+"_leader")
ScriptResult(
WriteSet([
DataEntry("tasks_"+taskId+"_status", "solution_chosen"),
DataEntry("tasks_"+taskId+"_app_"+winnerIdentifier+"_process", "winner"),
# report is created by Working Group and stored into SC
DataEntry("tasks_"+taskId+"_report", reportLink)
]),
TransferSet([
ScriptTransfer(teamAddress.addressFromStringValue(), rewardAmount, unit)
])
)
} else throw("checks are not passed")
}
@Callable(i)
func rejectTask(taskId: String) = {
# called by Working Group Member to undo the task
# only in extraordinary cases
if (!isGroupMember(i.caller)) then {
throw("grants working group access only")
} else {
WriteSet([
DataEntry("tasks_"+taskId+"_status", "rejected")
])
}
}
# @Verifier(tx)
# func standardVerifier() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)