Skip to content

Commit 04f316d

Browse files
authored
Merge pull request #1951 from kleros/chore/true-court-stake-and-active-jurors
chore: effective stake instead of specific court stake, and effective number of staked jurors (counting child courts)
2 parents 74142c1 + 0cc67e1 commit 04f316d

File tree

9 files changed

+38
-56
lines changed

9 files changed

+38
-56
lines changed

Diff for: subgraph/core/schema.graphql

+1
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!

Diff for: subgraph/core/src/entities/Court.ts

+6-20
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,15 @@ import { ZERO } from "../utils";
55

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

12-
while (court) {
13-
let totalStake = court.stake;
14-
15-
const childrenCourts = court.children.load();
16-
17-
for (let i = 0; i < childrenCourts.length; i++) {
18-
const childCourt = Court.load(childrenCourts[i].id);
19-
if (childCourt) {
20-
totalStake = totalStake.plus(childCourt.effectiveStake);
21-
}
22-
}
23-
24-
court.effectiveStake = totalStake;
25-
court.save();
12+
court.effectiveStake = court.effectiveStake.plus(delta);
13+
court.save();
2614

27-
if (court.parent && court.parent !== null) {
28-
court = Court.load(court.parent as string);
29-
} else {
30-
break;
31-
}
15+
if (court.parent) {
16+
updateEffectiveStake(court.parent as string, delta);
3217
}
3318
}
3419

@@ -48,6 +33,7 @@ export function createCourtFromEvent(event: CourtCreated): void {
4833
court.numberAppealingDisputes = ZERO;
4934
court.numberVotes = ZERO;
5035
court.numberStakedJurors = ZERO;
36+
court.effectiveNumberStakedJurors = ZERO;
5137
court.stake = ZERO;
5238
court.effectiveStake = ZERO;
5339
court.delayedStake = ZERO;

Diff for: subgraph/core/src/entities/JurorTokensPerCourt.ts

+19-27
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,26 @@ export function createJurorTokensPerCourt(jurorAddress: string, courtID: string)
3232
return jurorTokens;
3333
}
3434

35-
export function updateJurorEffectiveStake(jurorAddress: string, courtID: string): void {
35+
export function updateJurorEffectiveStake(jurorAddress: string, courtID: string, delta: BigInt): void {
3636
let court = Court.load(courtID);
37-
if (!court) {
38-
return;
37+
if (!court) return;
38+
39+
const jurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, court.id);
40+
const previousEffectiveStake = jurorTokensPerCourt.effectiveStake;
41+
const newEffectiveStake = previousEffectiveStake.plus(delta);
42+
43+
if (previousEffectiveStake.equals(ZERO) && newEffectiveStake.gt(ZERO)) {
44+
court.effectiveNumberStakedJurors = court.effectiveNumberStakedJurors.plus(ONE);
45+
} else if (previousEffectiveStake.gt(ZERO) && newEffectiveStake.equals(ZERO)) {
46+
court.effectiveNumberStakedJurors = court.effectiveNumberStakedJurors.minus(ONE);
3947
}
4048

41-
while (court) {
42-
const jurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, court.id);
43-
let totalStake = jurorTokensPerCourt.staked;
44-
const childrenCourts = court.children.load();
45-
46-
for (let i = 0; i < childrenCourts.length; i++) {
47-
const childCourtID = childrenCourts[i].id;
48-
const childCourt = Court.load(childCourtID);
49-
if (childCourt) {
50-
const childJurorTokensPerCourt = ensureJurorTokensPerCourt(jurorAddress, childCourt.id);
51-
totalStake = totalStake.plus(childJurorTokensPerCourt.effectiveStake);
52-
}
53-
}
54-
55-
jurorTokensPerCourt.effectiveStake = totalStake;
56-
jurorTokensPerCourt.save();
57-
58-
if (court.parent && court.parent !== null) {
59-
court = Court.load(court.parent as string);
60-
} else {
61-
break;
62-
}
49+
jurorTokensPerCourt.effectiveStake = newEffectiveStake;
50+
jurorTokensPerCourt.save();
51+
court.save();
52+
53+
if (court.parent) {
54+
updateJurorEffectiveStake(jurorAddress, court.parent as string, delta);
6355
}
6456
}
6557

@@ -92,8 +84,8 @@ export function updateJurorStake(
9284
updateActiveJurors(activeJurorsDelta, timestamp);
9385
juror.save();
9486
court.save();
95-
updateEffectiveStake(courtID);
96-
updateJurorEffectiveStake(jurorAddress, courtID);
87+
updateEffectiveStake(courtID, stakeDelta);
88+
updateJurorEffectiveStake(jurorAddress, courtID, stakeDelta);
9789
updateCourtStateVariable(courtID, court.effectiveStake, timestamp, "effectiveStake");
9890
}
9991

Diff for: 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.15.0",
3+
"version": "0.15.2",
44
"drtVersion": "0.12.0",
55
"license": "MIT",
66
"scripts": {

Diff for: web/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
### Pre-Requisites
2525

26-
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)
26+
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).
2727

2828
### Getting Started
2929

Diff for: web/src/hooks/queries/useCourtDetails.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ const courtDetailsQuery = graphql(`
1717
numberClosedDisputes
1818
numberAppealingDisputes
1919
numberStakedJurors
20+
effectiveNumberStakedJurors
2021
numberVotes
2122
stake
23+
effectiveStake
2224
paidETH
2325
paidPNK
2426
timesPerPeriod

Diff for: web/src/hooks/queries/useHomePageQuery.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ const homePageQuery = graphql(`
1111
disputes(first: 3) {
1212
id
1313
}
14-
counters(where: { id_gt: $timeframe }) {
14+
counters(first: 366, where: { id_gt: $timeframe }) {
1515
id
1616
stakedPNK
1717
paidETH
1818
redistributedPNK
1919
activeJurors
2020
cases
2121
}
22-
courts(orderBy: id, orderDirection: asc) {
22+
courts(first: 1000, orderBy: id, orderDirection: asc) {
2323
id
2424
name
2525
numberDisputes
2626
feeForJuror
27+
effectiveStake
2728
stake
2829
}
2930
}

Diff for: web/src/pages/Courts/CourtDetails/Stats/stats.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ export const stats: IStat[] = [
7474
{
7575
title: "PNK Staked",
7676
coinId: 0,
77-
getText: (data) => `${formatPNK(data?.stake)} PNK`,
78-
getSubtext: (data, coinPrice) => formatUSD(Number(formatUnitsWei(data?.stake)) * (coinPrice ?? 0)),
77+
getText: (data) => `${formatPNK(data?.effectiveStake)} PNK`,
78+
getSubtext: (data, coinPrice) => formatUSD(Number(formatUnitsWei(data?.effectiveStake)) * (coinPrice ?? 0)),
7979
color: "green",
8080
icon: PNKIcon,
8181
},
8282
{
8383
title: "Active Jurors",
84-
getText: (data) => data?.numberStakedJurors,
84+
getText: (data) => data?.effectiveNumberStakedJurors,
8585
color: "green",
8686
icon: StyledJurorIcon,
8787
},

Diff for: web/src/pages/Home/CourtOverview/Chart.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ const Chart: React.FC = () => {
8888

8989
const processedStakedPNKData = courtsChartData?.reduce(
9090
(accData: StakedPNKByCourtsChartData, current) => {
91-
if (BigInt(current.stake) > 0) {
91+
if (BigInt(current.effectiveStake) > 0) {
9292
return {
9393
labels: [...accData.labels, current.name ?? ""],
94-
stakes: [...accData.stakes, parseFloat(formatUnits(current.stake, 18))],
94+
stakes: [...accData.stakes, parseFloat(formatUnits(current.effectiveStake, 18))],
9595
totalStake: accData.totalStake + parseFloat(formatUnits(current.stake, 18)),
9696
};
9797
}

0 commit comments

Comments
 (0)