Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 17 additions & 21 deletions subgraph/core/src/entities/Court.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@ 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;
court.effectiveStake = court.effectiveStake.plus(delta);
court.save();
if (court.parent) {
updateEffectiveStake(court.parent as string, delta);
}
}

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();

if (court.parent && court.parent !== null) {
court = Court.load(court.parent as string);
} else {
break;
}
// 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, delta: BigInt): void {
let court = Court.load(courtID);
if (!court) return;
court.effectiveNumberStakedJurors = court.effectiveNumberStakedJurors.plus(delta);
court.save();
if (court.parent) {
updateEffectiveNumberStakedJurors(court.parent as string, delta);
}
}

Expand All @@ -48,6 +43,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
29 changes: 8 additions & 21 deletions 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 @@ -32,30 +32,16 @@ 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;

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.effectiveStake = jurorTokensPerCourt.effectiveStake.plus(delta);
jurorTokensPerCourt.save();

if (court.parent && court.parent !== null) {
if (court.parent) {
court = Court.load(court.parent as string);
} else {
break;
Expand Down Expand Up @@ -92,8 +78,9 @@ export function updateJurorStake(
updateActiveJurors(activeJurorsDelta, timestamp);
juror.save();
court.save();
updateEffectiveStake(courtID);
updateJurorEffectiveStake(jurorAddress, courtID);
updateEffectiveStake(courtID, stakeDelta);
updateJurorEffectiveStake(jurorAddress, courtID, stakeDelta);
updateEffectiveNumberStakedJurors(courtID, stakedJurorsDelta);
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