Skip to content

chore: effective stake instead of specific court stake, and effective number of staked jurors (counting child courts) #1951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions subgraph/core/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type Court @entity {
numberVotes: BigInt!
stakedJurors: [JurorTokensPerCourt!]! @derivedFrom(field: "court")
numberStakedJurors: BigInt!
effectiveNumberStakedJurors: BigInt!
stake: BigInt!
effectiveStake: BigInt!
delayedStake: BigInt!
Expand Down
30 changes: 30 additions & 0 deletions subgraph/core/src/entities/Court.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ export function updateEffectiveStake(courtID: string): void {
}
}

// This function calculates the "effective" numberStakedJurors, which is the specific numberStakedJurors
// of the current court + the specific numberStakedJurors of all of its children courts
export function updateEffectiveNumberStakedJurors(courtID: string): void {
let court = Court.load(courtID);
if (!court) return;

while (court) {
let totalJurors = court.numberStakedJurors;

const childrenCourts = court.children.load();

for (let i = 0; i < childrenCourts.length; i++) {
const childCourt = Court.load(childrenCourts[i].id);
if (childCourt) {
totalJurors = totalJurors.plus(childCourt.effectiveNumberStakedJurors);
}
}

court.effectiveNumberStakedJurors = totalJurors;
court.save();

if (court.parent && court.parent !== null) {
court = Court.load(court.parent as string);
} else {
break;
}
}
}

export function createCourtFromEvent(event: CourtCreated): void {
const court = new Court(event.params._courtID.toString());
court.hiddenVotes = event.params._hiddenVotes;
Expand All @@ -48,6 +77,7 @@ export function createCourtFromEvent(event: CourtCreated): void {
court.numberAppealingDisputes = ZERO;
court.numberVotes = ZERO;
court.numberStakedJurors = ZERO;
court.effectiveNumberStakedJurors = ZERO;
court.stake = ZERO;
court.effectiveStake = ZERO;
court.delayedStake = ZERO;
Expand Down
3 changes: 2 additions & 1 deletion subgraph/core/src/entities/JurorTokensPerCourt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { updateActiveJurors, getDelta, updateStakedPNK, updateCourtStateVariable
import { ensureUser } from "./User";
import { ONE, ZERO } from "../utils";
import { SortitionModule } from "../../generated/SortitionModule/SortitionModule";
import { updateEffectiveStake } from "./Court";
import { updateEffectiveNumberStakedJurors, updateEffectiveStake } from "./Court";

export function ensureJurorTokensPerCourt(jurorAddress: string, courtID: string): JurorTokensPerCourt {
const id = `${jurorAddress}-${courtID}`;
Expand Down Expand Up @@ -93,6 +93,7 @@ export function updateJurorStake(
juror.save();
court.save();
updateEffectiveStake(courtID);
updateEffectiveNumberStakedJurors(courtID);
updateJurorEffectiveStake(jurorAddress, courtID);
updateCourtStateVariable(courtID, court.effectiveStake, timestamp, "effectiveStake");
}
Expand Down
2 changes: 1 addition & 1 deletion subgraph/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kleros/kleros-v2-subgraph",
"version": "0.15.0",
"version": "0.15.2",
"drtVersion": "0.12.0",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions web/src/hooks/queries/useCourtDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ const courtDetailsQuery = graphql(`
numberClosedDisputes
numberAppealingDisputes
numberStakedJurors
effectiveNumberStakedJurors
numberVotes
stake
effectiveStake
paidETH
paidPNK
timesPerPeriod
Expand Down
6 changes: 3 additions & 3 deletions web/src/pages/Courts/CourtDetails/Stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ export const stats: IStat[] = [
{
title: "PNK Staked",
coinId: 0,
getText: (data) => `${formatPNK(data?.stake)} PNK`,
getSubtext: (data, coinPrice) => formatUSD(Number(formatUnitsWei(data?.stake)) * (coinPrice ?? 0)),
getText: (data) => `${formatPNK(data?.effectiveStake)} PNK`,
getSubtext: (data, coinPrice) => formatUSD(Number(formatUnitsWei(data?.effectiveStake)) * (coinPrice ?? 0)),
color: "green",
icon: PNKIcon,
},
{
title: "Active Jurors",
getText: (data) => data?.numberStakedJurors,
getText: (data) => data?.effectiveNumberStakedJurors,
color: "green",
icon: StyledJurorIcon,
},
Expand Down
Loading