Skip to content

Commit da331f7

Browse files
authored
Merge pull request #1702 from kleros/feat(subgraph,web)/calculate-coherency-at-vote-level
feat(subgraph,web): fix coherent votes at vote level instead of dispute level
2 parents ad34b56 + 38efc1b commit da331f7

File tree

17 files changed

+142
-90
lines changed

17 files changed

+142
-90
lines changed

subgraph/core/schema.graphql

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ type User @entity {
7979
resolvedDisputes: [Dispute!]!
8080
totalResolvedDisputes: BigInt!
8181
totalDisputes: BigInt!
82-
totalCoherent: BigInt!
83-
coherenceScore: BigInt!
8482
totalAppealingDisputes: BigInt!
83+
totalCoherentVotes: BigInt!
84+
totalResolvedVotes: BigInt!
85+
coherenceScore: BigInt!
8586
votes: [Vote!]! @derivedFrom(field: "juror")
8687
contributions: [Contribution!]! @derivedFrom(field: "contributor")
8788
evidences: [Evidence!]! @derivedFrom(field: "sender")

subgraph/core/src/KlerosCore.ts

+37-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import { createDisputeKitFromEvent, filterSupportedDisputeKits } from "./entitie
1919
import { createDisputeFromEvent } from "./entities/Dispute";
2020
import { createRoundFromRoundInfo, updateRoundTimeline } from "./entities/Round";
2121
import { updateCases, updateCasesAppealing, updateCasesRuled, updateCasesVoting } from "./datapoint";
22-
import { addUserActiveDispute, ensureUser } from "./entities/User";
22+
import { addUserActiveDispute, computeCoherenceScore, ensureUser } from "./entities/User";
2323
import { updateJurorStake } from "./entities/JurorTokensPerCourt";
2424
import { createDrawFromEvent } from "./entities/Draw";
2525
import { updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
2626
import { updateArbitrableCases } from "./entities/Arbitrable";
27-
import { Court, Dispute, Round, User } from "../generated/schema";
27+
import { ClassicVote, Court, Dispute, Draw, Round, User } from "../generated/schema";
2828
import { BigInt } from "@graphprotocol/graph-ts";
2929
import { updatePenalty } from "./entities/Penalty";
3030
import { ensureFeeToken } from "./entities/FeeToken";
@@ -127,6 +127,41 @@ export function handleNewPeriod(event: NewPeriod): void {
127127
dispute.currentRuling = currentRulingInfo.getRuling();
128128
dispute.overridden = currentRulingInfo.getOverridden();
129129
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+
}
130165
}
131166

132167
dispute.period = newPeriod;

subgraph/core/src/entities/TokenAndEthShift.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function updateTokenAndEthShiftFromEvent(event: TokenAndETHShiftEvent): v
2929
const previousEthAmount = shift.ethAmount;
3030
const newEthAmount = previousEthAmount.plus(ethAmount);
3131
shift.ethAmount = newEthAmount;
32-
resolveUserDispute(jurorAddress.toHexString(), previousEthAmount, newEthAmount, disputeID.toString());
32+
resolveUserDispute(jurorAddress.toHexString(), disputeID.toString());
3333
court.paidETH = court.paidETH.plus(ethAmount);
3434
updatePaidETH(ethAmount, event.block.timestamp);
3535
if (pnkAmount.gt(ZERO)) {

subgraph/core/src/entities/User.ts

+6-20
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { BigInt, BigDecimal } from "@graphprotocol/graph-ts";
22
import { User } from "../../generated/schema";
33
import { ONE, ZERO } from "../utils";
44

5-
export function computeCoherenceScore(totalCoherent: BigInt, totalResolvedDisputes: BigInt): BigInt {
5+
export function computeCoherenceScore(totalCoherentVotes: BigInt, totalResolvedVotes: BigInt): BigInt {
66
const smoothingFactor = BigDecimal.fromString("10");
77

8-
let denominator = totalResolvedDisputes.toBigDecimal().plus(smoothingFactor);
9-
let coherencyRatio = totalCoherent.toBigDecimal().div(denominator);
8+
let denominator = totalResolvedVotes.toBigDecimal().plus(smoothingFactor);
9+
let coherencyRatio = totalCoherentVotes.toBigDecimal().div(denominator);
1010

1111
const coherencyScore = coherencyRatio.times(BigDecimal.fromString("100"));
1212

@@ -36,7 +36,8 @@ export function createUserFromAddress(id: string): User {
3636
user.totalResolvedDisputes = ZERO;
3737
user.totalAppealingDisputes = ZERO;
3838
user.totalDisputes = ZERO;
39-
user.totalCoherent = ZERO;
39+
user.totalCoherentVotes = ZERO;
40+
user.totalResolvedVotes = ZERO;
4041
user.coherenceScore = ZERO;
4142
user.save();
4243

@@ -54,28 +55,13 @@ export function addUserActiveDispute(id: string, disputeID: string): void {
5455
user.save();
5556
}
5657

57-
export function resolveUserDispute(id: string, previousFeeAmount: BigInt, feeAmount: BigInt, disputeID: string): void {
58+
export function resolveUserDispute(id: string, disputeID: string): void {
5859
const user = ensureUser(id);
5960
if (user.resolvedDisputes.includes(disputeID)) {
60-
if (previousFeeAmount.gt(ZERO)) {
61-
if (feeAmount.le(ZERO)) {
62-
user.totalCoherent = user.totalCoherent.minus(ONE);
63-
}
64-
} else if (previousFeeAmount.le(ZERO)) {
65-
if (feeAmount.gt(ZERO)) {
66-
user.totalCoherent = user.totalCoherent.plus(ONE);
67-
}
68-
}
69-
user.coherenceScore = computeCoherenceScore(user.totalCoherent, user.totalResolvedDisputes);
70-
user.save();
7161
return;
7262
}
7363
user.resolvedDisputes = user.resolvedDisputes.concat([disputeID]);
7464
user.totalResolvedDisputes = user.totalResolvedDisputes.plus(ONE);
75-
if (feeAmount.gt(ZERO)) {
76-
user.totalCoherent = user.totalCoherent.plus(ONE);
77-
}
7865
user.activeDisputes = user.activeDisputes.minus(ONE);
79-
user.coherenceScore = computeCoherenceScore(user.totalCoherent, user.totalResolvedDisputes);
8066
user.save();
8167
}

subgraph/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kleros/kleros-v2-subgraph",
3-
"version": "0.7.4",
3+
"version": "0.7.6",
44
"license": "MIT",
55
"scripts": {
66
"update:core:arbitrum-sepolia-devnet": "./scripts/update.sh arbitrumSepoliaDevnet arbitrum-sepolia core/subgraph.yaml",

web/src/components/Popup/MiniGuides/JurorLevels.tsx

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,32 @@ const userLevelData = [
6363
{
6464
level: 1,
6565
title: "Phytagoras",
66-
totalCoherent: 6,
67-
totalResolvedDisputes: 10,
66+
totalCoherentVotes: 6,
67+
totalResolvedVotes: 10,
6868
},
6969
{
7070
level: 2,
7171
title: "Socrates",
72-
totalCoherent: 7,
73-
totalResolvedDisputes: 10,
72+
totalCoherentVotes: 7,
73+
totalResolvedVotes: 10,
7474
},
7575
{
7676
level: 3,
7777
title: "Plato",
78-
totalCoherent: 8,
79-
totalResolvedDisputes: 10,
78+
totalCoherentVotes: 8,
79+
totalResolvedVotes: 10,
8080
},
8181
{
8282
level: 4,
8383
title: "Aristotle",
84-
totalCoherent: 9,
85-
totalResolvedDisputes: 10,
84+
totalCoherentVotes: 9,
85+
totalResolvedVotes: 10,
8686
},
8787
{
8888
level: 0,
8989
title: "Diogenes",
90-
totalCoherent: 3,
91-
totalResolvedDisputes: 10,
90+
totalCoherentVotes: 3,
91+
totalResolvedVotes: 10,
9292
},
9393
];
9494

@@ -114,8 +114,8 @@ const RightContent: React.FC<{ currentPage: number }> = ({ currentPage }) => {
114114
<PixelArt level={userData.level} width="189px" height="189px" />
115115
<Coherency
116116
userLevelData={userData}
117-
totalCoherent={userData.totalCoherent}
118-
totalResolvedDisputes={userData.totalResolvedDisputes}
117+
totalCoherentVotes={userData.totalCoherentVotes}
118+
totalResolvedVotes={userData.totalResolvedVotes}
119119
isMiniGuide={true}
120120
/>
121121
</Card>

web/src/hooks/queries/useTopUsersByCoherenceScore.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const topUsersByCoherenceScoreQuery = graphql(`
1212
users(first: $first, orderBy: $orderBy, orderDirection: $orderDirection) {
1313
id
1414
coherenceScore
15-
totalCoherent
15+
totalCoherentVotes
16+
totalResolvedVotes
1617
totalResolvedDisputes
1718
}
1819
}

web/src/hooks/queries/useUser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const userFragment = graphql(`
1212
totalDisputes
1313
totalResolvedDisputes
1414
totalAppealingDisputes
15-
totalCoherent
15+
totalCoherentVotes
16+
totalResolvedVotes
1617
coherenceScore
1718
tokens {
1819
court {

web/src/pages/Dashboard/JurorInfo/Coherency.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ interface ICoherency {
3030
level: number;
3131
title: string;
3232
};
33-
totalCoherent: number;
34-
totalResolvedDisputes: number;
33+
totalCoherentVotes: number;
34+
totalResolvedVotes: number;
3535
isMiniGuide: boolean;
3636
}
3737

38-
const Coherency: React.FC<ICoherency> = ({ userLevelData, totalCoherent, totalResolvedDisputes, isMiniGuide }) => {
38+
const Coherency: React.FC<ICoherency> = ({ userLevelData, totalCoherentVotes, totalResolvedVotes, isMiniGuide }) => {
3939
const votesContent = (
4040
<label>
4141
Coherent Votes:
4242
<small>
4343
{" "}
44-
{totalCoherent}/{totalResolvedDisputes}{" "}
44+
{totalCoherentVotes}/{totalResolvedVotes}{" "}
4545
</small>
4646
</label>
4747
);
@@ -51,7 +51,7 @@ const Coherency: React.FC<ICoherency> = ({ userLevelData, totalCoherent, totalRe
5151
<small>{userLevelData.title}</small>
5252
<label>Level {userLevelData.level}</label>
5353
<CircularProgress
54-
progress={parseFloat(((totalCoherent / Math.max(totalResolvedDisputes, 1)) * 100).toFixed(2))}
54+
progress={parseFloat(((totalCoherentVotes / Math.max(totalResolvedVotes, 1)) * 100).toFixed(2))}
5555
/>
5656
{!isMiniGuide ? (
5757
<WithHelpTooltip place="left" {...{ tooltipMsg }}>

web/src/pages/Dashboard/JurorInfo/Header.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ const StyledLink = styled.a`
6565
interface IHeader {
6666
levelTitle: string;
6767
levelNumber: number;
68-
totalCoherent: number;
69-
totalResolvedDisputes: number;
68+
totalCoherentVotes: number;
69+
totalResolvedVotes: number;
7070
}
7171

72-
const Header: React.FC<IHeader> = ({ levelTitle, levelNumber, totalCoherent, totalResolvedDisputes }) => {
72+
const Header: React.FC<IHeader> = ({ levelTitle, levelNumber, totalCoherentVotes, totalResolvedVotes }) => {
7373
const [isJurorLevelsMiniGuideOpen, toggleJurorLevelsMiniGuide] = useToggle(false);
7474

75-
const coherencePercentage = parseFloat(((totalCoherent / Math.max(totalResolvedDisputes, 1)) * 100).toFixed(2));
75+
const coherencePercentage = parseFloat(((totalCoherentVotes / Math.max(totalResolvedVotes, 1)) * 100).toFixed(2));
7676
const courtUrl = window.location.origin;
77-
const xPostText = `Hey I've been busy as a Juror on the Kleros court, check out my score: \n\nLevel: ${levelNumber} (${levelTitle})\nCoherence Percentage: ${coherencePercentage}%\nCoherent Votes: ${totalCoherent}/${totalResolvedDisputes}\n\nBe a juror with me! ➡️ ${courtUrl}`;
77+
const xPostText = `Hey I've been busy as a Juror on the Kleros court, check out my score: \n\nLevel: ${levelNumber} (${levelTitle})\nCoherence Percentage: ${coherencePercentage}%\nCoherent Votes: ${totalCoherentVotes}/${totalResolvedVotes}\n\nBe a juror with me! ➡️ ${courtUrl}`;
7878
const xShareUrl = `https://twitter.com/intent/tweet?text=${encodeURIComponent(xPostText)}`;
7979

8080
return (
@@ -86,7 +86,7 @@ const Header: React.FC<IHeader> = ({ levelTitle, levelNumber, totalCoherent, tot
8686
toggleMiniGuide={toggleJurorLevelsMiniGuide}
8787
MiniGuideComponent={JurorLevels}
8888
/>
89-
{totalResolvedDisputes > 0 ? (
89+
{totalResolvedVotes > 0 ? (
9090
<StyledLink href={xShareUrl} target="_blank" rel="noreferrer">
9191
<StyledXIcon /> <span>Share your juror score</span>
9292
</StyledLink>

web/src/pages/Dashboard/JurorInfo/index.tsx

+5-10
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,22 @@ const JurorInfo: React.FC = () => {
4444
const { data } = useUserQuery(address?.toLowerCase() as `0x${string}`);
4545
// TODO check graph schema
4646
const coherenceScore = data?.user ? parseInt(data?.user?.coherenceScore) : 0;
47-
const totalCoherent = data?.user ? parseInt(data?.user?.totalCoherent) : 0;
47+
const totalCoherentVotes = data?.user ? parseInt(data?.user?.totalCoherentVotes) : 0;
48+
const totalResolvedVotes = data?.user ? parseInt(data?.user?.totalResolvedVotes) : 0;
4849
const totalResolvedDisputes = data?.user ? parseInt(data?.user?.totalResolvedDisputes) : 0;
4950

50-
const userLevelData = getUserLevelData(coherenceScore);
51+
const userLevelData = getUserLevelData(coherenceScore, totalResolvedDisputes);
5152

5253
return (
5354
<Container>
5455
<Header
5556
levelTitle={userLevelData.title}
5657
levelNumber={userLevelData.level}
57-
totalCoherent={totalCoherent}
58-
totalResolvedDisputes={totalResolvedDisputes}
58+
{...{ totalCoherentVotes, totalResolvedVotes }}
5959
/>
6060
<Card>
6161
<PixelArt level={userLevelData.level} width="189px" height="189px" />
62-
<Coherency
63-
userLevelData={userLevelData}
64-
totalCoherent={totalCoherent}
65-
totalResolvedDisputes={totalResolvedDisputes}
66-
isMiniGuide={false}
67-
/>
62+
<Coherency userLevelData={userLevelData} isMiniGuide={false} {...{ totalCoherentVotes, totalResolvedVotes }} />
6863
<JurorRewards />
6964
</Card>
7065
</Container>

web/src/pages/Home/TopJurors/JurorCard/Coherency.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const Container = styled.div`
1111
`;
1212

1313
interface ICoherency {
14-
totalCoherent: number;
15-
totalResolvedDisputes: number;
14+
totalCoherentVotes: number;
15+
totalResolvedVotes: number;
1616
}
1717

18-
const Coherency: React.FC<ICoherency> = ({ totalCoherent, totalResolvedDisputes }) => {
19-
const coherenceRatio = `${totalCoherent}/${totalResolvedDisputes}`;
18+
const Coherency: React.FC<ICoherency> = ({ totalCoherentVotes, totalResolvedVotes }) => {
19+
const coherenceRatio = `${totalCoherentVotes}/${totalResolvedVotes}`;
2020

2121
return <Container>{coherenceRatio}</Container>;
2222
};

web/src/pages/Home/TopJurors/JurorCard/DesktopCard.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ const Container = styled.div`
3333
interface IDesktopCard {
3434
rank: number;
3535
address: string;
36-
totalCoherent: number;
36+
totalCoherentVotes: number;
37+
totalResolvedVotes: number;
3738
totalResolvedDisputes: number;
3839
coherenceScore: number;
3940
}
4041

4142
const DesktopCard: React.FC<IDesktopCard> = ({
4243
rank,
4344
address,
44-
totalCoherent,
45+
totalCoherentVotes,
46+
totalResolvedVotes,
4547
totalResolvedDisputes,
4648
coherenceScore,
4749
}) => {
@@ -50,8 +52,8 @@ const DesktopCard: React.FC<IDesktopCard> = ({
5052
<Rank rank={rank} />
5153
<JurorTitle address={address} />
5254
<Rewards address={address} />
53-
<Coherency totalCoherent={totalCoherent} totalResolvedDisputes={totalResolvedDisputes} />
54-
<JurorLevel coherenceScore={coherenceScore} />
55+
<Coherency {...{ totalCoherentVotes, totalResolvedVotes }} />
56+
<JurorLevel {...{ coherenceScore, totalResolvedDisputes }} />
5557
</Container>
5658
);
5759
};

web/src/pages/Home/TopJurors/JurorCard/JurorLevel.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ const StyledLabel = styled.label`
4040

4141
interface IJurorLevel {
4242
coherenceScore: number;
43+
totalResolvedDisputes: number;
4344
}
4445

45-
const JurorLevel: React.FC<IJurorLevel> = ({ coherenceScore }) => {
46-
const userLevelData = getUserLevelData(coherenceScore);
46+
const JurorLevel: React.FC<IJurorLevel> = ({ coherenceScore, totalResolvedDisputes }) => {
47+
const userLevelData = getUserLevelData(coherenceScore, totalResolvedDisputes);
4748
const level = userLevelData.level;
4849

4950
return (

0 commit comments

Comments
 (0)