Skip to content

Commit 1b015b8

Browse files
committed
feat: add effective number staked jurors, subgraph, frontend
1 parent e7aacc1 commit 1b015b8

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

subgraph/core/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ type Court @entity {
150150
numberVotes: BigInt!
151151
stakedJurors: [JurorTokensPerCourt!]! @derivedFrom(field: "court")
152152
numberStakedJurors: BigInt!
153+
effectiveNumberStakedJurors: BigInt!
153154
stake: BigInt!
154155
effectiveStake: BigInt!
155156
delayedStake: BigInt!

subgraph/core/src/entities/Court.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ export function updateEffectiveStake(courtID: string): void {
3232
}
3333
}
3434

35+
// This function calculates the "effective" numberStakedJurors, which is the specific numberStakedJurors
36+
// of the current court + the specific numberStakedJurors of all of its children courts
37+
export function updateEffectiveNumberStakedJurors(courtID: string): void {
38+
let court = Court.load(courtID);
39+
if (!court) return;
40+
41+
while (court) {
42+
let totalJurors = court.numberStakedJurors;
43+
44+
const childrenCourts = court.children.load();
45+
46+
for (let i = 0; i < childrenCourts.length; i++) {
47+
const childCourt = Court.load(childrenCourts[i].id);
48+
if (childCourt) {
49+
totalJurors = totalJurors.plus(childCourt.effectiveNumberStakedJurors);
50+
}
51+
}
52+
53+
court.effectiveNumberStakedJurors = totalJurors;
54+
court.save();
55+
56+
if (court.parent && court.parent !== null) {
57+
court = Court.load(court.parent as string);
58+
} else {
59+
break;
60+
}
61+
}
62+
}
63+
3564
export function createCourtFromEvent(event: CourtCreated): void {
3665
const court = new Court(event.params._courtID.toString());
3766
court.hiddenVotes = event.params._hiddenVotes;
@@ -48,6 +77,7 @@ export function createCourtFromEvent(event: CourtCreated): void {
4877
court.numberAppealingDisputes = ZERO;
4978
court.numberVotes = ZERO;
5079
court.numberStakedJurors = ZERO;
80+
court.effectiveNumberStakedJurors = ZERO;
5181
court.stake = ZERO;
5282
court.effectiveStake = ZERO;
5383
court.delayedStake = ZERO;

subgraph/core/src/entities/JurorTokensPerCourt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { updateActiveJurors, getDelta, updateStakedPNK, updateCourtStateVariable
44
import { ensureUser } from "./User";
55
import { ONE, ZERO } from "../utils";
66
import { SortitionModule } from "../../generated/SortitionModule/SortitionModule";
7-
import { updateEffectiveStake } from "./Court";
7+
import { updateEffectiveNumberStakedJurors, updateEffectiveStake } from "./Court";
88

99
export function ensureJurorTokensPerCourt(jurorAddress: string, courtID: string): JurorTokensPerCourt {
1010
const id = `${jurorAddress}-${courtID}`;
@@ -93,6 +93,7 @@ export function updateJurorStake(
9393
juror.save();
9494
court.save();
9595
updateEffectiveStake(courtID);
96+
updateEffectiveNumberStakedJurors(courtID);
9697
updateJurorEffectiveStake(jurorAddress, courtID);
9798
updateCourtStateVariable(courtID, court.effectiveStake, timestamp, "effectiveStake");
9899
}

subgraph/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kleros/kleros-v2-subgraph",
3-
"version": "0.15.0",
3+
"version": "0.15.2",
44
"drtVersion": "0.12.0",
55
"license": "MIT",
66
"scripts": {

web/src/hooks/queries/useCourtDetails.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const courtDetailsQuery = graphql(`
1717
numberClosedDisputes
1818
numberAppealingDisputes
1919
numberStakedJurors
20+
effectiveNumberStakedJurors
2021
numberVotes
2122
stake
2223
effectiveStake

web/src/pages/Courts/CourtDetails/Stats/stats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const stats: IStat[] = [
8181
},
8282
{
8383
title: "Active Jurors",
84-
getText: (data) => data?.numberStakedJurors,
84+
getText: (data) => data?.effectiveNumberStakedJurors,
8585
color: "green",
8686
icon: StyledJurorIcon,
8787
},

0 commit comments

Comments
 (0)