Skip to content

Feat(subgraph): add more ruling information to dispute entity #1077

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 1 commit into from
Aug 2, 2023
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
3 changes: 3 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ type Dispute @entity {
arbitrated: Arbitrable!
period: Period!
ruled: Boolean!
currentRuling: BigInt!
tied: Boolean!
overridden: Boolean!
lastPeriodChange: BigInt!
rounds: [Round!]! @derivedFrom(field: "dispute")
currentRound: Round!
Expand Down
16 changes: 12 additions & 4 deletions subgraph/src/DisputeKitClassic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {
Withdrawal,
} from "../generated/DisputeKitClassic/DisputeKitClassic";
import { KlerosCore } from "../generated/KlerosCore/KlerosCore";
import { ClassicDispute, ClassicEvidence, ClassicRound } from "../generated/schema";
import { ClassicDispute, ClassicEvidence, ClassicRound, Dispute } from "../generated/schema";
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
import { createClassicDisputeFromEvent } from "./entities/ClassicDispute";
import { ensureClassicEvidenceGroup } from "./entities/ClassicEvidenceGroup";
import { createClassicRound, updateChoiceFundingFromContributionEvent, updateCounts } from "./entities/ClassicRound";
import {
createClassicRound,
updateChoiceFundingFromContributionEvent,
updateCountsAndGetCurrentRuling,
} from "./entities/ClassicRound";
import { createClassicVote } from "./entities/ClassicVote";
import { ensureUser } from "./entities/User";
import { ONE, ZERO } from "./utils";
Expand Down Expand Up @@ -44,11 +48,15 @@ export function handleEvidenceEvent(event: EvidenceEvent): void {

export function handleJustificationEvent(event: JustificationEvent): void {
const coreDisputeID = event.params._coreDisputeID.toString();
const coreDispute = Dispute.load(coreDisputeID);
const classicDisputeID = `${DISPUTEKIT_ID}-${coreDisputeID}`;
const classicDispute = ClassicDispute.load(classicDisputeID);
if (!classicDispute) return;
if (!classicDispute || !coreDispute) return;
const currentLocalRoundID = classicDispute.id + "-" + classicDispute.currentLocalRoundIndex.toString();
updateCounts(currentLocalRoundID, event.params._choice);
const currentRulingInfo = updateCountsAndGetCurrentRuling(currentLocalRoundID, event.params._choice);
coreDispute.currentRuling = currentRulingInfo.ruling;
coreDispute.tied = currentRulingInfo.tied;
coreDispute.save();
createClassicVote(currentLocalRoundID, event);
}

Expand Down
13 changes: 10 additions & 3 deletions subgraph/src/KlerosCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { updateCases, updatePaidETH, updateRedistributedPNK, updateCasesRuled, u
import { addUserActiveDispute, ensureUser } from "./entities/User";
import { updateJurorDelayedStake, updateJurorStake } from "./entities/JurorTokensPerCourt";
import { createDrawFromEvent } from "./entities/Draw";
import { createTokenAndEthShiftFromEvent, updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
import { updateTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
import { updateArbitrableCases } from "./entities/Arbitrable";
import { Court, Dispute } from "../generated/schema";
import { BigInt } from "@graphprotocol/graph-ts";
Expand Down Expand Up @@ -82,14 +82,21 @@ export function handleDisputeCreation(event: DisputeCreation): void {
}

export function handleNewPeriod(event: NewPeriod): void {
const disputeID = event.params._disputeID.toString();
const dispute = Dispute.load(disputeID);
const disputeID = event.params._disputeID;
const dispute = Dispute.load(disputeID.toString());
if (!dispute) return;
const newPeriod = getPeriodName(event.params._period);
if (dispute.period === "vote") {
updateCasesVoting(BigInt.fromI32(-1), event.block.timestamp);
} else if (newPeriod === "vote") {
updateCasesVoting(ONE, event.block.timestamp);
} else if (newPeriod === "execution") {
const contract = KlerosCore.bind(event.address);
const currentRulingInfo = contract.currentRuling(disputeID);
dispute.currentRuling = currentRulingInfo.getRuling();
dispute.overridden = currentRulingInfo.getOverridden();
dispute.tied = currentRulingInfo.getTied();
dispute.save();
}
dispute.period = newPeriod;
dispute.lastPeriodChange = event.block.timestamp;
Expand Down
20 changes: 10 additions & 10 deletions subgraph/src/entities/ClassicRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { Contribution } from "../../generated/DisputeKitClassic/DisputeKitClassi
import { ClassicRound } from "../../generated/schema";
import { ONE, ZERO } from "../utils";

export function createClassicRound(
disputeID: string,
numberOfChoices: BigInt,
roundIndex: BigInt
): void {
export function createClassicRound(disputeID: string, numberOfChoices: BigInt, roundIndex: BigInt): void {
const choicesLength = numberOfChoices.plus(ONE);
const localDisputeID = `1-${disputeID}`;
const id = `${localDisputeID}-${roundIndex.toString()}`;
Expand All @@ -24,9 +20,14 @@ export function createClassicRound(
classicRound.save();
}

export function updateCounts(id: string, choice: BigInt): void {
class CurrentRulingInfo {
ruling: BigInt;
tied: boolean;
}

export function updateCountsAndGetCurrentRuling(id: string, choice: BigInt): CurrentRulingInfo {
const round = ClassicRound.load(id);
if (!round) return;
if (!round) return { ruling: ZERO, tied: false };
const choiceNum = choice.toI32();
const updatedCount = round.counts[choiceNum].plus(ONE);
let newCounts: BigInt[] = [];
Expand All @@ -51,11 +52,10 @@ export function updateCounts(id: string, choice: BigInt): void {
}
round.totalVoted = round.totalVoted.plus(ONE);
round.save();
return { ruling: round.winningChoice, tied: round.tied };
}

export function updateChoiceFundingFromContributionEvent(
event: Contribution
): void {
export function updateChoiceFundingFromContributionEvent(event: Contribution): void {
const disputeKitID = "1";
const coreDisputeID = event.params._coreDisputeID.toString();
const coreRoundIndex = event.params._coreRoundID.toString();
Expand Down
8 changes: 4 additions & 4 deletions subgraph/src/entities/Dispute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
KlerosCore,
DisputeCreation,
} from "../../generated/KlerosCore/KlerosCore";
import { KlerosCore, DisputeCreation } from "../../generated/KlerosCore/KlerosCore";
import { Dispute } from "../../generated/schema";
import { ZERO } from "../utils";

Expand All @@ -14,6 +11,9 @@ export function createDisputeFromEvent(event: DisputeCreation): void {
dispute.arbitrated = event.params._arbitrable.toHexString();
dispute.period = "evidence";
dispute.ruled = false;
dispute.currentRuling = ZERO;
dispute.tied = true;
dispute.overridden = false;
dispute.lastPeriodChange = event.block.timestamp;
dispute.currentRoundIndex = ZERO;
const roundID = `${disputeID.toString()}-${ZERO.toString()}`;
Expand Down