-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathKlerosCore.ts
295 lines (256 loc) · 11 KB
/
KlerosCore.ts
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
import {
KlerosCore,
AppealDecision,
DisputeCreation,
DisputeKitCreated,
DisputeKitEnabled,
CourtCreated,
CourtModified,
Draw as DrawEvent,
NewPeriod,
TokenAndETHShift as TokenAndETHShiftEvent,
CourtJump,
Ruling,
AcceptedFeeToken,
} from "../generated/KlerosCore/KlerosCore";
import { ZERO, ONE } from "./utils";
import { createCourtFromEvent } from "./entities/Court";
import { createDisputeKitFromEvent, filterSupportedDisputeKits } from "./entities/DisputeKit";
import { createDisputeFromEvent } from "./entities/Dispute";
import { createRoundFromRoundInfo, updateRoundTimeline } from "./entities/Round";
import {
updateCases,
updateCasesAppealing,
updateCasesRuled,
updateCasesVoting,
updateTotalLeaderboardJurors,
} from "./datapoint";
import { addUserActiveDispute, computeCoherenceScore, ensureUser } from "./entities/User";
import { updateJurorStake } from "./entities/JurorTokensPerCourt";
import { createDrawFromEvent } from "./entities/Draw";
import { updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
import { updateArbitrableCases } from "./entities/Arbitrable";
import { ClassicVote, Counter, Court, Dispute, Draw, Round, User } from "../generated/schema";
import { BigInt } from "@graphprotocol/graph-ts";
import { updatePenalty } from "./entities/Penalty";
import { ensureFeeToken } from "./entities/FeeToken";
import { getAndIncrementPeriodCounter } from "./entities/PeriodIndexCounter";
import { SortitionModule } from "../generated/SortitionModule/SortitionModule";
function getPeriodName(index: i32): string {
const periodArray = ["evidence", "commit", "vote", "appeal", "execution"];
return periodArray.at(index) || "None";
}
export function handleCourtCreated(event: CourtCreated): void {
createCourtFromEvent(event);
}
export function handleCourtModified(event: CourtModified): void {
const court = Court.load(event.params._courtID.toString());
if (!court) return;
court.hiddenVotes = event.params._hiddenVotes;
court.minStake = event.params._minStake;
court.alpha = event.params._alpha;
court.feeForJuror = event.params._feeForJuror;
court.jurorsForCourtJump = event.params._jurorsForCourtJump;
court.timesPerPeriod = event.params._timesPerPeriod;
court.save();
}
export function handleDisputeKitCreated(event: DisputeKitCreated): void {
createDisputeKitFromEvent(event);
}
export function handleDisputeKitEnabled(event: DisputeKitEnabled): void {
const court = Court.load(event.params._courtID.toString());
if (!court) return;
const isEnable = event.params._enable;
const disputeKitID = event.params._disputeKitID.toString();
court.supportedDisputeKits = isEnable
? court.supportedDisputeKits.concat([disputeKitID])
: filterSupportedDisputeKits(court.supportedDisputeKits, disputeKitID);
court.save();
}
export function handleDisputeCreation(event: DisputeCreation): void {
const contract = KlerosCore.bind(event.address);
const disputeID = event.params._disputeID;
const disputeStorage = contract.disputes(disputeID);
const courtID = disputeStorage.value0.toString();
const court = Court.load(courtID);
if (!court) return;
court.numberDisputes = court.numberDisputes.plus(ONE);
const roundInfo = contract.getRoundInfo(disputeID, ZERO);
court.numberVotes = court.numberVotes.plus(roundInfo.nbVotes);
court.save();
createDisputeFromEvent(event);
createRoundFromRoundInfo(KlerosCore.bind(event.address), disputeID, ZERO, roundInfo);
const arbitrable = event.params._arbitrable.toHexString();
updateArbitrableCases(arbitrable, ONE);
updateCases(ONE, event.block.timestamp);
}
export function handleNewPeriod(event: NewPeriod): void {
const disputeID = event.params._disputeID;
const dispute = Dispute.load(disputeID.toString());
if (!dispute) return;
const court = Court.load(dispute.court);
if (!court) return;
if (dispute.period.includes("vote")) {
court.numberVotingDisputes = court.numberVotingDisputes.minus(ONE);
updateCasesVoting(BigInt.fromI32(-1), event.block.timestamp);
} else if (dispute.period.includes("appeal")) {
let juror: User;
for (let i = 0; i < dispute.jurors.entries.length; i++) {
juror = ensureUser(dispute.jurors.entries[i].value.toString());
juror.totalAppealingDisputes = juror.totalAppealingDisputes.minus(ONE);
juror.save();
}
court.numberAppealingDisputes = court.numberAppealingDisputes.minus(ONE);
updateCasesAppealing(BigInt.fromI32(-1), event.block.timestamp);
}
const newPeriod = getPeriodName(event.params._period);
if (newPeriod === "vote") {
court.numberVotingDisputes = court.numberVotingDisputes.plus(ONE);
updateCasesVoting(ONE, event.block.timestamp);
} else if (newPeriod === "appeal") {
let juror: User;
for (let i = 0; i < dispute.jurors.entries.length; i++) {
juror = ensureUser(dispute.jurors.entries[i].value.toString());
juror.totalAppealingDisputes = juror.totalAppealingDisputes.plus(ONE);
juror.save();
}
court.numberAppealingDisputes = court.numberAppealingDisputes.plus(ONE);
updateCasesAppealing(ONE, event.block.timestamp);
} else if (newPeriod === "execution") {
const contract = KlerosCore.bind(event.address);
const currentRulingInfo = contract.currentRuling(disputeID);
dispute.currentRuling = currentRulingInfo.getRuling();
dispute.overridden = currentRulingInfo.getOverridden();
dispute.tied = currentRulingInfo.getTied();
const rounds = dispute.rounds.load();
for (let i = 0; i < rounds.length; i++) {
const round = Round.load(rounds[i].id);
if (!round) continue;
const draws = round.drawnJurors.load();
// Iterate over all draws in the round
for (let j = 0; j < draws.length; j++) {
const draw = Draw.load(draws[j].id);
if (!draw) continue;
const juror = ensureUser(draw.juror);
juror.totalResolvedVotes = juror.totalResolvedVotes.plus(ONE);
// Increment totalLeaderboardJurors in the Counter entity if this is the first resolved vote for the juror
if (juror.totalResolvedVotes.equals(ONE)) {
updateTotalLeaderboardJurors(ONE, event.block.timestamp);
}
// Since this is a ClassicVote entity, this will only work for the Classic DisputeKit (which has ID "1").
const vote = ClassicVote.load(`${round.disputeKit}-${draw.id}`);
if (!vote) {
// Recalculate coherenceScore
juror.coherenceScore = computeCoherenceScore(juror.totalCoherentVotes, juror.totalResolvedVotes);
juror.save();
continue;
}
if (vote.choice === null) continue;
// Check if the vote choice matches the final ruling
if (vote.choice!.equals(dispute.currentRuling)) {
juror.totalCoherentVotes = juror.totalCoherentVotes.plus(ONE);
}
// Recalculate coherenceScore
juror.coherenceScore = computeCoherenceScore(juror.totalCoherentVotes, juror.totalResolvedVotes);
juror.save();
}
}
}
dispute.period = newPeriod;
dispute.lastPeriodChange = event.block.timestamp;
dispute.lastPeriodChangeBlockNumber = event.block.number;
dispute.periodNotificationIndex = getAndIncrementPeriodCounter(newPeriod);
if (newPeriod !== "execution") {
dispute.periodDeadline = event.block.timestamp.plus(court.timesPerPeriod[event.params._period]);
} else {
dispute.periodDeadline = BigInt.fromU64(U64.MAX_VALUE);
}
updateRoundTimeline(disputeID.toString(), newPeriod, event.block.timestamp);
dispute.save();
court.save();
}
export function handleRuling(event: Ruling): void {
updateCasesRuled(ONE, event.block.timestamp);
const disputeID = event.params._disputeID;
const dispute = Dispute.load(disputeID.toString());
if (!dispute) return;
dispute.ruled = true;
dispute.rulingTransactionHash = event.transaction.hash.toHexString();
dispute.rulingTimestamp = event.block.timestamp;
dispute.save();
const court = Court.load(dispute.court);
if (!court) return;
court.numberClosedDisputes = court.numberClosedDisputes.plus(ONE);
court.save();
}
export function handleAppealDecision(event: AppealDecision): void {
const contract = KlerosCore.bind(event.address);
const disputeID = event.params._disputeID;
const dispute = Dispute.load(disputeID.toString());
if (!dispute) return;
const newRoundIndex = dispute.currentRoundIndex.plus(ONE);
const roundID = `${disputeID}-${newRoundIndex.toString()}`;
dispute.currentRoundIndex = newRoundIndex;
dispute.currentRound = roundID;
dispute.save();
const roundInfo = contract.getRoundInfo(disputeID, newRoundIndex);
const disputeStorage = contract.disputes(disputeID);
const courtID = disputeStorage.value0.toString();
const court = Court.load(courtID);
if (!court) return;
court.numberVotes = court.numberVotes.plus(roundInfo.nbVotes);
court.save();
createRoundFromRoundInfo(KlerosCore.bind(event.address), disputeID, newRoundIndex, roundInfo);
}
export function handleCourtJump(event: CourtJump): void {
const dispute = Dispute.load(event.params._disputeID.toString());
if (!dispute) return;
dispute.court = event.params._toCourtID.toString();
dispute.save();
}
export function handleDraw(event: DrawEvent): void {
createDrawFromEvent(event);
const disputeID = event.params._disputeID.toString();
const dispute = Dispute.load(disputeID);
if (!dispute) return;
const klerosCore = KlerosCore.bind(event.address);
const sortitionModule = SortitionModule.bind(klerosCore.sortitionModule());
const jurorAddress = event.params._address.toHexString();
updateJurorStake(jurorAddress, dispute.court, sortitionModule, event.block.timestamp);
addUserActiveDispute(jurorAddress, disputeID);
const roundIndex = event.params._roundID;
const roundID = `${disputeID}-${roundIndex.toString()}`;
const currentRound = Round.load(roundID);
if (!currentRound) return;
if (currentRound.nbVotes.toI32() === currentRound.drawnJurors.load().length) {
currentRound.jurorsDrawn = true;
currentRound.save();
}
}
export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
updatePenalty(event);
updateTokenAndEthShiftFromEvent(event);
const jurorAddress = event.params._account.toHexString();
const disputeID = event.params._disputeID.toString();
const dispute = Dispute.load(disputeID);
if (!dispute) return;
const court = Court.load(dispute.court);
if (!court) return;
const klerosCore = KlerosCore.bind(event.address);
const sortitionModule = SortitionModule.bind(klerosCore.sortitionModule());
updateJurorStake(jurorAddress, court.id, sortitionModule, event.block.timestamp);
const roundIndex = event.params._roundID;
const roundID = `${disputeID}-${roundIndex.toString()}`;
const round = Round.load(roundID);
if (!round) return;
const roundInfo = klerosCore.getRoundInfo(event.params._disputeID, roundIndex);
const repartitions = roundInfo.repartitions;
const nbVotes = roundInfo.nbVotes;
if (repartitions >= nbVotes) {
round.jurorRewardsDispersed = true;
round.save();
}
}
export function handleAcceptedFeeToken(event: AcceptedFeeToken): void {
ensureFeeToken(event.params._token, event.address);
}