Skip to content
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
26 changes: 12 additions & 14 deletions subgraph/src/DisputeKitClassic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@ import {
Withdrawal,
} from "../generated/DisputeKitClassic/DisputeKitClassic";
import { KlerosCore } from "../generated/KlerosCore/KlerosCore";
import { ClassicEvidence } from "../generated/schema";
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
import {
createClassicDisputeFromEvent,
loadClassicDisputeWithLogs,
} from "./entities/ClassicDispute";
ClassicDispute,
ClassicEvidence,
ClassicRound,
} from "../generated/schema";
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
import { createClassicDisputeFromEvent } from "./entities/ClassicDispute";
import { ensureClassicEvidenceGroup } from "./entities/ClassicEvidenceGroup";
import {
createClassicRound,
loadClassicRoundWithLogs,
updateChoiceFundingFromContributionEvent,
} from "./entities/ClassicRound";
import { createClassicVote } from "./entities/ClassicVote";
import { loadDisputeWithLogs } from "./entities/Dispute";
import { ONE } from "./utils";
import { ONE, ZERO } from "./utils";

export const DISPUTEKIT_ID = "1";

export function handleDisputeCreation(event: DisputeCreation): void {
const dispute = loadDisputeWithLogs(event.params._coreDisputeID.toString());
if (!dispute) return;
const disputeID = event.params._coreDisputeID.toString();
createClassicDisputeFromEvent(event);
createClassicRound(dispute.id, dispute.currentRoundIndex);
createClassicRound(disputeID, ZERO);
}

export function handleEvidenceEvent(event: EvidenceEvent): void {
Expand All @@ -52,7 +50,7 @@ export function handleEvidenceEvent(event: EvidenceEvent): void {
export function handleJustificationEvent(event: JustificationEvent): void {
const coreDisputeID = event.params._coreDisputeID.toString();
const classicDisputeID = `${DISPUTEKIT_ID}-${coreDisputeID}`;
const classicDispute = loadClassicDisputeWithLogs(classicDisputeID);
const classicDispute = ClassicDispute.load(classicDisputeID);
if (!classicDispute) return;
const currentLocalRoundID = `${
classicDispute.id
Expand All @@ -70,7 +68,7 @@ export function handleChoiceFunded(event: ChoiceFunded): void {
const coreRoundIndex = event.params._coreRoundID.toString();
const roundID = `${DISPUTEKIT_ID}-${coreDisputeID}-${coreRoundIndex}`;

const localRound = loadClassicRoundWithLogs(roundID);
const localRound = ClassicRound.load(roundID);
if (!localRound) return;

const currentFeeRewards = localRound.feeRewards;
Expand All @@ -85,7 +83,7 @@ export function handleChoiceFunded(event: ChoiceFunded): void {
const appealCost = klerosCore.appealCost(BigInt.fromString(coreDisputeID));
localRound.feeRewards = localRound.feeRewards.minus(appealCost);

const localDispute = loadClassicDisputeWithLogs(
const localDispute = ClassicDispute.load(
`${DISPUTEKIT_ID}-${coreDisputeID}`
);
if (!localDispute) return;
Expand Down
28 changes: 11 additions & 17 deletions subgraph/src/KlerosCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,12 @@ import {
TokenAndETHShift as TokenAndETHShiftEvent,
} from "../generated/KlerosCore/KlerosCore";
import { ZERO, ONE } from "./utils";
import {
createCourtFromEvent,
getFeeForJuror,
loadCourtWithLogs,
} from "./entities/Court";
import { createCourtFromEvent, getFeeForJuror } from "./entities/Court";
import {
createDisputeKitFromEvent,
filterSupportedDisputeKits,
} from "./entities/DisputeKit";
import {
createDisputeFromEvent,
loadDisputeWithLogs,
} from "./entities/Dispute";
import { createDisputeFromEvent } from "./entities/Dispute";
import { createRoundFromRoundInfo } from "./entities/Round";
import {
updateCases,
Expand All @@ -41,6 +34,7 @@ import {
import { createDrawFromEvent } from "./entities/Draw";
import { createTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
import { updateArbitrableCases } from "./entities/Arbitrable";
import { Court, Dispute } from "../generated/schema";

function getPeriodName(index: i32): string {
const periodArray = ["evidence", "commit", "vote", "appeal", "execution"];
Expand All @@ -54,7 +48,7 @@ export function handleCourtCreated(event: CourtCreated): void {
export function handleCourtModified(event: CourtModified): void {
const contract = KlerosCore.bind(event.address);
const courtContractState = contract.courts(event.params._courtID);
const court = loadCourtWithLogs(event.params._courtID.toString());
const court = Court.load(event.params._courtID.toString());
if (!court) return;
court.hiddenVotes = courtContractState.value1;
court.minStake = courtContractState.value2;
Expand All @@ -70,7 +64,7 @@ export function handleDisputeKitCreated(event: DisputeKitCreated): void {
}

export function handleDisputeKitEnabled(event: DisputeKitEnabled): void {
const court = loadCourtWithLogs(event.params._courtID.toString());
const court = Court.load(event.params._courtID.toString());
if (!court) return;
const isEnable = event.params._enable;
const disputeKitID = event.params._disputeKitID.toString();
Expand All @@ -85,7 +79,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
const disputeID = event.params._disputeID;
const disputeStorage = contract.disputes(disputeID);
const courtID = disputeStorage.value0.toString();
const court = loadCourtWithLogs(courtID);
const court = Court.load(courtID);
if (!court) return;
court.numberDisputes = court.numberDisputes.plus(ONE);
court.save();
Expand All @@ -99,7 +93,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {

export function handleNewPeriod(event: NewPeriod): void {
const disputeID = event.params._disputeID.toString();
const dispute = loadDisputeWithLogs(disputeID);
const dispute = Dispute.load(disputeID);
if (!dispute) return;
dispute.period = getPeriodName(event.params._period);
dispute.lastPeriodChange = event.block.timestamp;
Expand All @@ -109,7 +103,7 @@ export function handleNewPeriod(event: NewPeriod): void {
export function handleAppealDecision(event: AppealDecision): void {
const contract = KlerosCore.bind(event.address);
const disputeID = event.params._disputeID;
const dispute = loadDisputeWithLogs(disputeID.toString());
const dispute = Dispute.load(disputeID.toString());
if (!dispute) return;
const newRoundIndex = dispute.currentRoundIndex.plus(ONE);
const roundID = `${disputeID}-${newRoundIndex.toString()}`;
Expand All @@ -124,7 +118,7 @@ export function handleAppealDecision(event: AppealDecision): void {
export function handleDraw(event: DrawEvent): void {
createDrawFromEvent(event);
const disputeID = event.params._disputeID.toString();
const dispute = loadDisputeWithLogs(disputeID);
const dispute = Dispute.load(disputeID);
if (!dispute) return;
const contract = KlerosCore.bind(event.address);
updateJurorStake(
Expand Down Expand Up @@ -164,9 +158,9 @@ export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
updateRedistributedPNK(tokenAmount, event.block.timestamp);
}
updatePaidETH(ethAmount, event.block.timestamp);
const dispute = loadDisputeWithLogs(disputeID);
const dispute = Dispute.load(disputeID);
if (!dispute) return;
const court = loadCourtWithLogs(dispute.court);
const court = Court.load(dispute.court);
if (!court) return;
updateJurorStake(
jurorAddress,
Expand Down
7 changes: 2 additions & 5 deletions subgraph/src/entities/ClassicDispute.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { DisputeCreation } from "../../generated/DisputeKitClassic/DisputeKitClassic";
import { ClassicDispute } from "../../generated/schema";
import { loadWithLogs } from "../utils";

export function loadClassicDisputeWithLogs(id: string): ClassicDispute | null {
return loadWithLogs("ClassicDispute", id) as ClassicDispute;
}
import { ZERO } from "../utils";

export function createClassicDisputeFromEvent(event: DisputeCreation): void {
const coreDisputeID = event.params._coreDisputeID.toString();
const classicDispute = new ClassicDispute(`1-${coreDisputeID}`);
classicDispute.coreDispute = coreDisputeID;
classicDispute.currentLocalRoundIndex = ZERO;
classicDispute.numberOfChoices = event.params._numberOfChoices;
classicDispute.jumped = false;
classicDispute.extraData = event.params._extraData;
Expand Down
12 changes: 5 additions & 7 deletions subgraph/src/entities/ClassicRound.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { BigInt } from "@graphprotocol/graph-ts";
import { Contribution } from "../../generated/DisputeKitClassic/DisputeKitClassic";
import { ClassicRound } from "../../generated/schema";
import { loadWithLogs, ZERO } from "../utils";

export function loadClassicRoundWithLogs(id: string): ClassicRound | null {
return loadWithLogs("ClassicRound", id) as ClassicRound;
}
import { ZERO } from "../utils";

export function createClassicRound(
disputeID: string,
roundIndex: BigInt
): void {
const id = `1-${disputeID}-${roundIndex.toString()}`;
const localDisputeID = `1-${disputeID}`;
const id = `${localDisputeID}-${roundIndex.toString()}`;
const classicRound = new ClassicRound(id);
classicRound.localDispute = localDisputeID;
classicRound.votes = [];
classicRound.winningChoice = ZERO;
classicRound.counts = [];
Expand All @@ -33,7 +31,7 @@ export function updateChoiceFundingFromContributionEvent(
const coreRoundIndex = event.params._coreRoundID.toString();
const roundID = `${disputeKitID}-${coreDisputeID}-${coreRoundIndex}`;

const classicRound = loadClassicRoundWithLogs(roundID);
const classicRound = ClassicRound.load(roundID);
if (!classicRound) return;

const choice = event.params._choice;
Expand Down
8 changes: 2 additions & 6 deletions subgraph/src/entities/Court.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { BigInt } from "@graphprotocol/graph-ts";
import { CourtCreated } from "../../generated/KlerosCore/KlerosCore";
import { Court } from "../../generated/schema";
import { loadWithLogs, ZERO } from "../utils";

export function loadCourtWithLogs(id: string): Court | null {
return loadWithLogs("Court", id) as Court;
}
import { ZERO } from "../utils";

export function createCourtFromEvent(event: CourtCreated): void {
const court = new Court(event.params._courtID.toString());
Expand All @@ -28,7 +24,7 @@ export function createCourtFromEvent(event: CourtCreated): void {
}

export function getFeeForJuror(id: string): BigInt {
const court = loadCourtWithLogs(id);
const court = Court.load(id);
if (!court) return ZERO;
return court.feeForJuror;
}
6 changes: 1 addition & 5 deletions subgraph/src/entities/Dispute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import {
DisputeCreation,
} from "../../generated/KlerosCore/KlerosCore";
import { Dispute } from "../../generated/schema";
import { loadWithLogs, ZERO } from "../utils";

export function loadDisputeWithLogs(id: string): Dispute | null {
return loadWithLogs("Dispute", id) as Dispute;
}
import { ZERO } from "../utils";

export function createDisputeFromEvent(event: DisputeCreation): void {
const contract = KlerosCore.bind(event.address);
Expand Down
8 changes: 2 additions & 6 deletions subgraph/src/entities/DisputeKit.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { DisputeKitCreated } from "../../generated/KlerosCore/KlerosCore";
import { DisputeKit } from "../../generated/schema";
import { ZERO, ONE, loadWithLogs } from "../utils";

export function loadDisputeKitWithLogs(id: string): DisputeKit | null {
return loadWithLogs("DisputeKit", id) as DisputeKit;
}
import { ZERO, ONE } from "../utils";

export function createDisputeKitFromEvent(event: DisputeKitCreated): void {
const disputeKit = new DisputeKit(event.params._disputeKitID.toString());
disputeKit.parent = event.params._parent.toString();
disputeKit.address = event.params._disputeKitAddress;
disputeKit.needsFreezing = false;
const parent = loadDisputeKitWithLogs(event.params._parent.toString());
const parent = DisputeKit.load(event.params._parent.toString());
disputeKit.depthLevel = parent ? parent.depthLevel.plus(ONE) : ZERO;
disputeKit.save();
}
Expand Down
5 changes: 2 additions & 3 deletions subgraph/src/entities/JurorTokensPerCourt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BigInt, Address } from "@graphprotocol/graph-ts";
import { KlerosCore } from "../../generated/KlerosCore/KlerosCore";
import { JurorTokensPerCourt } from "../../generated/schema";
import { Court, JurorTokensPerCourt } from "../../generated/schema";
import { updateActiveJurors, getDelta } from "../datapoint";
import { ensureUser } from "./Juror";
import { ZERO } from "../utils";
import { loadCourtWithLogs } from "./Court";

export function ensureJurorTokensPerCourt(
jurorAddress: string,
Expand Down Expand Up @@ -50,7 +49,7 @@ export function updateJurorStake(
timestamp: BigInt
): void {
const juror = ensureUser(jurorAddress);
const court = loadCourtWithLogs(courtID);
const court = Court.load(courtID);
if (!court) return;
const jurorTokens = ensureJurorTokensPerCourt(jurorAddress, courtID);
const jurorBalance = contract.getJurorBalance(
Expand Down
5 changes: 0 additions & 5 deletions subgraph/src/entities/Round.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { BigInt } from "@graphprotocol/graph-ts";
import { KlerosCore__getRoundInfoResult } from "../../generated/KlerosCore/KlerosCore";
import { Round } from "../../generated/schema";
import { loadWithLogs } from "../utils";

export function loadRoundWithLogs(id: string): Round | null {
return loadWithLogs("Round", id) as Round;
}

export function createRoundFromRoundInfo(
disputeID: BigInt,
Expand Down
13 changes: 1 addition & 12 deletions subgraph/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import { BigInt, Entity, store, log } from "@graphprotocol/graph-ts";
import { BigInt } from "@graphprotocol/graph-ts";

export const ZERO = BigInt.fromI32(0);
export const ONE = BigInt.fromI32(1);

export function loadWithLogs(entityName: string, id: string): Entity | null {
const entity = store.get(entityName, id);

if (!entity) {
log.error("{} not found with id: {}", [entityName, id]);
return null;
}

return entity;
}