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 all 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
26 changes: 6 additions & 20 deletions subgraph/core/src/entities/Court.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,15 @@ import { ZERO } from "../utils";

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

while (court) {
let totalStake = court.stake;

const childrenCourts = court.children.load();

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

court.effectiveStake = totalStake;
court.save();
court.effectiveStake = court.effectiveStake.plus(delta);
court.save();

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

Expand All @@ -48,6 +33,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
46 changes: 19 additions & 27 deletions subgraph/core/src/entities/JurorTokensPerCourt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,26 @@ export function createJurorTokensPerCourt(jurorAddress: string, courtID: string)
return jurorTokens;
}

export function updateJurorEffectiveStake(jurorAddress: string, courtID: string): void {
export function updateJurorEffectiveStake(jurorAddress: string, courtID: string, delta: BigInt): void {
let court = Court.load(courtID);
if (!court) {
return;
if (!court) return;

const jurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, court.id);
const previousEffectiveStake = jurorTokensPerCourt.effectiveStake;
const newEffectiveStake = previousEffectiveStake.plus(delta);

if (previousEffectiveStake.equals(ZERO) && newEffectiveStake.gt(ZERO)) {
court.effectiveNumberStakedJurors = court.effectiveNumberStakedJurors.plus(ONE);
} else if (previousEffectiveStake.gt(ZERO) && newEffectiveStake.equals(ZERO)) {
court.effectiveNumberStakedJurors = court.effectiveNumberStakedJurors.minus(ONE);
}

while (court) {
const jurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, court.id);
let totalStake = jurorTokensPerCourt.staked;
const childrenCourts = court.children.load();

for (let i = 0; i < childrenCourts.length; i++) {
const childCourtID = childrenCourts[i].id;
const childCourt = Court.load(childCourtID);
if (childCourt) {
const childJurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, childCourt.id);
totalStake = totalStake.plus(childJurorTokensPerCourt.effectiveStake);
}
}

jurorTokensPerCourt.effectiveStake = totalStake;
jurorTokensPerCourt.save();

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

if (court.parent) {
updateJurorEffectiveStake(jurorAddress, court.parent as string, delta);
}
}

Expand Down Expand Up @@ -92,8 +84,8 @@ export function updateJurorStake(
updateActiveJurors(activeJurorsDelta, timestamp);
juror.save();
court.save();
updateEffectiveStake(courtID);
updateJurorEffectiveStake(jurorAddress, courtID);
updateEffectiveStake(courtID, stakeDelta);
updateJurorEffectiveStake(jurorAddress, courtID, stakeDelta);
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: 1 addition & 1 deletion web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

### Pre-Requisites

If you haven't already, you need to follow all the previous steps of the **Contributing** section of the repo's [Contribution Guidelines](../CONTRIBUTING.md)
If you haven't already, you need to follow all the previous steps of the **Contributing** section of the repo's [Contribution Guidelines](../CONTRIBUTING.md).

### Getting Started

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
5 changes: 3 additions & 2 deletions web/src/hooks/queries/useHomePageQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ const homePageQuery = graphql(`
disputes(first: 3) {
id
}
counters(where: { id_gt: $timeframe }) {
counters(first: 366, where: { id_gt: $timeframe }) {
id
stakedPNK
paidETH
redistributedPNK
activeJurors
cases
}
courts(orderBy: id, orderDirection: asc) {
courts(first: 1000, orderBy: id, orderDirection: asc) {
id
name
numberDisputes
feeForJuror
effectiveStake
stake
}
}
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
4 changes: 2 additions & 2 deletions web/src/pages/Home/CourtOverview/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ const Chart: React.FC = () => {

const processedStakedPNKData = courtsChartData?.reduce(
(accData: StakedPNKByCourtsChartData, current) => {
if (BigInt(current.stake) > 0) {
if (BigInt(current.effectiveStake) > 0) {
return {
labels: [...accData.labels, current.name ?? ""],
stakes: [...accData.stakes, parseFloat(formatUnits(current.stake, 18))],
stakes: [...accData.stakes, parseFloat(formatUnits(current.effectiveStake, 18))],
totalStake: accData.totalStake + parseFloat(formatUnits(current.stake, 18)),
};
}
Expand Down
Loading