@@ -19,12 +19,12 @@ import { createDisputeKitFromEvent, filterSupportedDisputeKits } from "./entitie
1919import { createDisputeFromEvent } from "./entities/Dispute" ;
2020import { createRoundFromRoundInfo , updateRoundTimeline } from "./entities/Round" ;
2121import { updateCases , updateCasesAppealing , updateCasesRuled , updateCasesVoting } from "./datapoint" ;
22- import { addUserActiveDispute , ensureUser } from "./entities/User" ;
22+ import { addUserActiveDispute , computeCoherenceScore , ensureUser } from "./entities/User" ;
2323import { updateJurorStake } from "./entities/JurorTokensPerCourt" ;
2424import { createDrawFromEvent } from "./entities/Draw" ;
2525import { updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift" ;
2626import { updateArbitrableCases } from "./entities/Arbitrable" ;
27- import { Court , Dispute , Round , User } from "../generated/schema" ;
27+ import { ClassicVote , Court , Dispute , Draw , Round , User } from "../generated/schema" ;
2828import { BigInt } from "@graphprotocol/graph-ts" ;
2929import { updatePenalty } from "./entities/Penalty" ;
3030import { ensureFeeToken } from "./entities/FeeToken" ;
@@ -75,9 +75,12 @@ export function handleDisputeCreation(event: DisputeCreation): void {
7575 const court = Court . load ( courtID ) ;
7676 if ( ! court ) return ;
7777 court . numberDisputes = court . numberDisputes . plus ( ONE ) ;
78+
79+ const roundInfo = contract . getRoundInfo ( disputeID , ZERO ) ;
80+ court . numberVotes = court . numberVotes . plus ( roundInfo . nbVotes ) ;
81+
7882 court . save ( ) ;
7983 createDisputeFromEvent ( event ) ;
80- const roundInfo = contract . getRoundInfo ( disputeID , ZERO ) ;
8184 createRoundFromRoundInfo ( KlerosCore . bind ( event . address ) , disputeID , ZERO , roundInfo ) ;
8285 const arbitrable = event . params . _arbitrable . toHexString ( ) ;
8386 updateArbitrableCases ( arbitrable , ONE ) ;
@@ -124,6 +127,41 @@ export function handleNewPeriod(event: NewPeriod): void {
124127 dispute . currentRuling = currentRulingInfo . getRuling ( ) ;
125128 dispute . overridden = currentRulingInfo . getOverridden ( ) ;
126129 dispute . tied = currentRulingInfo . getTied ( ) ;
130+
131+ const rounds = dispute . rounds . load ( ) ;
132+ for ( let i = 0 ; i < rounds . length ; i ++ ) {
133+ const round = Round . load ( rounds [ i ] . id ) ;
134+ if ( ! round ) continue ;
135+
136+ const draws = round . drawnJurors . load ( ) ;
137+ // Iterate over all draws in the round
138+ for ( let j = 0 ; j < draws . length ; j ++ ) {
139+ const draw = Draw . load ( draws [ j ] . id ) ;
140+ if ( ! draw ) continue ;
141+
142+ // Since this is a ClassicVote entity, this will only work for the Classic DisputeKit (which has ID "1").
143+ const vote = ClassicVote . load ( `${ round . disputeKit } -${ draw . id } ` ) ;
144+
145+ if ( ! vote ) continue ;
146+
147+ const juror = ensureUser ( draw . juror ) ;
148+ juror . totalResolvedVotes = juror . totalResolvedVotes . plus ( ONE ) ;
149+
150+ if ( vote . choice === null ) continue ;
151+
152+ // Check if the vote choice matches the final ruling
153+ if ( vote . choice ! . equals ( dispute . currentRuling ) ) {
154+ juror . totalCoherentVotes = juror . totalCoherentVotes . plus ( ONE ) ;
155+ }
156+
157+ // Recalculate coherenceScore
158+ if ( juror . totalResolvedVotes . gt ( ZERO ) ) {
159+ juror . coherenceScore = computeCoherenceScore ( juror . totalCoherentVotes , juror . totalResolvedVotes ) ;
160+ }
161+
162+ juror . save ( ) ;
163+ }
164+ }
127165 }
128166
129167 dispute . period = newPeriod ;
@@ -164,6 +202,15 @@ export function handleAppealDecision(event: AppealDecision): void {
164202 dispute . currentRound = roundID ;
165203 dispute . save ( ) ;
166204 const roundInfo = contract . getRoundInfo ( disputeID , newRoundIndex ) ;
205+
206+ const disputeStorage = contract . disputes ( disputeID ) ;
207+ const courtID = disputeStorage . value0 . toString ( ) ;
208+ const court = Court . load ( courtID ) ;
209+ if ( ! court ) return ;
210+
211+ court . numberVotes = court . numberVotes . plus ( roundInfo . nbVotes ) ;
212+ court . save ( ) ;
213+
167214 createRoundFromRoundInfo ( KlerosCore . bind ( event . address ) , disputeID , newRoundIndex , roundInfo ) ;
168215}
169216
0 commit comments