-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDisputeKitClassic.ts
160 lines (139 loc) · 6.17 KB
/
DisputeKitClassic.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
import { BigInt } from "@graphprotocol/graph-ts";
import {
DisputeKitClassic,
DisputeCreation,
VoteCast,
Contribution as ContributionEvent,
ChoiceFunded,
Withdrawal,
CommitCast,
} from "../generated/DisputeKitClassic/DisputeKitClassic";
import { KlerosCore } from "../generated/KlerosCore/KlerosCore";
import { ClassicDispute, ClassicJustification, ClassicRound, ClassicVote, Dispute } from "../generated/schema";
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
import { createClassicDisputeFromEvent } from "./entities/ClassicDispute";
import {
createClassicRound,
ensureAnswer,
updateChoiceFundingFromContributionEvent,
updateCountsAndGetCurrentRuling,
} from "./entities/ClassicRound";
import { ensureClassicVote } from "./entities/ClassicVote";
import { ONE, ZERO } from "./utils";
export const DISPUTEKIT_ID = "1";
export function handleDisputeCreation(event: DisputeCreation): void {
const disputeID = event.params._coreDisputeID.toString();
createClassicDisputeFromEvent(event);
const numberOfChoices = event.params._numberOfChoices;
createClassicRound(disputeID, numberOfChoices, ZERO);
}
export function handleCommitCast(event: CommitCast): void {
const coreDisputeID = event.params._coreDisputeID;
const coreDispute = Dispute.load(coreDisputeID.toString());
const classicDisputeID = `${DISPUTEKIT_ID}-${coreDisputeID}`;
const classicDispute = ClassicDispute.load(classicDisputeID);
if (!classicDispute || !coreDispute) return;
const currentLocalRoundID = classicDispute.id + "-" + classicDispute.currentLocalRoundIndex.toString();
const voteIDs = event.params._voteIDs;
for (let i = 0; i < voteIDs.length; i++) {
const classicVote = ensureClassicVote(
currentLocalRoundID,
event.params._juror.toHexString(),
voteIDs[i],
coreDispute
);
classicVote.commited = true;
classicVote.commit = event.params._commit;
classicVote.save();
}
}
export function handleVoteCast(event: VoteCast): void {
const juror = event.params._juror.toHexString();
const coreDisputeID = event.params._coreDisputeID.toString();
const coreDispute = Dispute.load(coreDisputeID);
const classicDisputeID = `${DISPUTEKIT_ID}-${coreDisputeID}`;
const classicDispute = ClassicDispute.load(classicDisputeID);
if (!classicDispute || !coreDispute) return;
const choice = event.params._choice;
const currentLocalRoundID = classicDispute.id + "-" + classicDispute.currentLocalRoundIndex.toString();
const voteIDs = event.params._voteIDs;
const justification = new ClassicJustification(`${currentLocalRoundID}-${voteIDs.toString()}`);
justification.juror = juror;
justification.coreDispute = coreDisputeID;
justification.localRound = currentLocalRoundID;
justification.choice = choice;
justification.reference = event.params._justification;
justification.transactionHash = event.transaction.hash.toHexString();
justification.timestamp = event.block.timestamp;
justification.save();
const currentRulingInfo = updateCountsAndGetCurrentRuling(
currentLocalRoundID,
choice,
BigInt.fromI32(voteIDs.length)
);
coreDispute.currentRuling = currentRulingInfo.ruling;
coreDispute.tied = currentRulingInfo.tied;
coreDispute.save();
let classicVote: ClassicVote;
for (let i = 0; i < voteIDs.length; i++) {
classicVote = ensureClassicVote(currentLocalRoundID, juror, voteIDs[i], coreDispute);
classicVote.voted = true;
classicVote.choice = choice;
classicVote.justification = justification.id;
classicVote.save();
}
}
export function handleContributionEvent(event: ContributionEvent): void {
ensureClassicContributionFromEvent(event);
updateChoiceFundingFromContributionEvent(event);
}
export function handleChoiceFunded(event: ChoiceFunded): void {
const coreDisputeID = event.params._coreDisputeID.toString();
const coreRoundIndex = event.params._coreRoundID.toString();
const choice = event.params._choice;
const roundID = `${DISPUTEKIT_ID}-${coreDisputeID}-${coreRoundIndex}`;
const localRound = ClassicRound.load(roundID);
if (!localRound) return;
const answer = ensureAnswer(roundID, choice);
const currentFeeRewards = localRound.feeRewards;
const deltaFeeRewards = answer.paidFee;
localRound.feeRewards = currentFeeRewards.plus(deltaFeeRewards);
localRound.fundedChoices = localRound.fundedChoices.concat([choice]);
answer.funded = true;
answer.save();
if (localRound.fundedChoices.length > 1) {
const disputeKitClassic = DisputeKitClassic.bind(event.address);
const klerosCore = KlerosCore.bind(disputeKitClassic.core());
// cannot use core.appealCost as that will give the cost for the newly created round
const numberOfRounds = klerosCore.getNumberOfRounds(BigInt.fromString(coreDisputeID));
const roundInfo = klerosCore.getRoundInfo(BigInt.fromString(coreDisputeID), numberOfRounds.minus(ONE));
const appealCost = roundInfo.totalFeesForJurors;
localRound.feeRewards = localRound.feeRewards.minus(appealCost);
const localDispute = ClassicDispute.load(`${DISPUTEKIT_ID}-${coreDisputeID}`);
if (!localDispute) return;
const newRoundIndex = localDispute.currentLocalRoundIndex.plus(ONE);
const numberOfChoices = localDispute.numberOfChoices;
localDispute.currentLocalRoundIndex = newRoundIndex;
localDispute.save();
createClassicRound(coreDisputeID, numberOfChoices, newRoundIndex);
}
localRound.save();
}
export function handleWithdrawal(event: Withdrawal): void {
const contribution = ensureClassicContributionFromEvent(event);
if (!contribution) return;
contribution.rewardWithdrawn = true;
contribution.rewardAmount = event.params._amount;
// check if all appeal fees have been withdrawn
const coreDisputeID = event.params._coreDisputeID.toString();
const coreRoundIndex = event.params._coreRoundID.toString();
const roundID = `${DISPUTEKIT_ID}-${coreDisputeID}-${coreRoundIndex}`;
const localRound = ClassicRound.load(roundID);
if (!localRound) return;
localRound.totalFeeDispersed = localRound.totalFeeDispersed.plus(event.params._amount);
if (localRound.totalFeeDispersed.equals(localRound.feeRewards)) {
localRound.appealFeesDispersed = true;
}
contribution.save();
localRound.save();
}