Skip to content

Commit 172ba43

Browse files
authored
Merge pull request #496 from kleros/fix(subgraph)/dispute-creation-event-order
Fix(subgraph): Remove low level store use and prepare for the event order
2 parents 2fe4f41 + 64339d7 commit 172ba43

File tree

10 files changed

+38
-80
lines changed

10 files changed

+38
-80
lines changed

subgraph/src/DisputeKitClassic.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,27 @@ import {
99
Withdrawal,
1010
} from "../generated/DisputeKitClassic/DisputeKitClassic";
1111
import { KlerosCore } from "../generated/KlerosCore/KlerosCore";
12-
import { ClassicEvidence } from "../generated/schema";
13-
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
1412
import {
15-
createClassicDisputeFromEvent,
16-
loadClassicDisputeWithLogs,
17-
} from "./entities/ClassicDispute";
13+
ClassicDispute,
14+
ClassicEvidence,
15+
ClassicRound,
16+
} from "../generated/schema";
17+
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
18+
import { createClassicDisputeFromEvent } from "./entities/ClassicDispute";
1819
import { ensureClassicEvidenceGroup } from "./entities/ClassicEvidenceGroup";
1920
import {
2021
createClassicRound,
21-
loadClassicRoundWithLogs,
2222
updateChoiceFundingFromContributionEvent,
2323
} from "./entities/ClassicRound";
2424
import { createClassicVote } from "./entities/ClassicVote";
25-
import { loadDisputeWithLogs } from "./entities/Dispute";
26-
import { ONE } from "./utils";
25+
import { ONE, ZERO } from "./utils";
2726

2827
export const DISPUTEKIT_ID = "1";
2928

3029
export function handleDisputeCreation(event: DisputeCreation): void {
31-
const dispute = loadDisputeWithLogs(event.params._coreDisputeID.toString());
32-
if (!dispute) return;
30+
const disputeID = event.params._coreDisputeID.toString();
3331
createClassicDisputeFromEvent(event);
34-
createClassicRound(dispute.id, dispute.currentRoundIndex);
32+
createClassicRound(disputeID, ZERO);
3533
}
3634

3735
export function handleEvidenceEvent(event: EvidenceEvent): void {
@@ -52,7 +50,7 @@ export function handleEvidenceEvent(event: EvidenceEvent): void {
5250
export function handleJustificationEvent(event: JustificationEvent): void {
5351
const coreDisputeID = event.params._coreDisputeID.toString();
5452
const classicDisputeID = `${DISPUTEKIT_ID}-${coreDisputeID}`;
55-
const classicDispute = loadClassicDisputeWithLogs(classicDisputeID);
53+
const classicDispute = ClassicDispute.load(classicDisputeID);
5654
if (!classicDispute) return;
5755
const currentLocalRoundID = `${
5856
classicDispute.id
@@ -70,7 +68,7 @@ export function handleChoiceFunded(event: ChoiceFunded): void {
7068
const coreRoundIndex = event.params._coreRoundID.toString();
7169
const roundID = `${DISPUTEKIT_ID}-${coreDisputeID}-${coreRoundIndex}`;
7270

73-
const localRound = loadClassicRoundWithLogs(roundID);
71+
const localRound = ClassicRound.load(roundID);
7472
if (!localRound) return;
7573

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

88-
const localDispute = loadClassicDisputeWithLogs(
86+
const localDispute = ClassicDispute.load(
8987
`${DISPUTEKIT_ID}-${coreDisputeID}`
9088
);
9189
if (!localDispute) return;

subgraph/src/KlerosCore.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,12 @@ import {
1212
TokenAndETHShift as TokenAndETHShiftEvent,
1313
} from "../generated/KlerosCore/KlerosCore";
1414
import { ZERO, ONE } from "./utils";
15-
import {
16-
createCourtFromEvent,
17-
getFeeForJuror,
18-
loadCourtWithLogs,
19-
} from "./entities/Court";
15+
import { createCourtFromEvent, getFeeForJuror } from "./entities/Court";
2016
import {
2117
createDisputeKitFromEvent,
2218
filterSupportedDisputeKits,
2319
} from "./entities/DisputeKit";
24-
import {
25-
createDisputeFromEvent,
26-
loadDisputeWithLogs,
27-
} from "./entities/Dispute";
20+
import { createDisputeFromEvent } from "./entities/Dispute";
2821
import { createRoundFromRoundInfo } from "./entities/Round";
2922
import {
3023
updateCases,
@@ -41,6 +34,7 @@ import {
4134
import { createDrawFromEvent } from "./entities/Draw";
4235
import { createTokenAndEthShiftFromEvent } from "./entities/TokenAndEthShift";
4336
import { updateArbitrableCases } from "./entities/Arbitrable";
37+
import { Court, Dispute } from "../generated/schema";
4438

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

7266
export function handleDisputeKitEnabled(event: DisputeKitEnabled): void {
73-
const court = loadCourtWithLogs(event.params._courtID.toString());
67+
const court = Court.load(event.params._courtID.toString());
7468
if (!court) return;
7569
const isEnable = event.params._enable;
7670
const disputeKitID = event.params._disputeKitID.toString();
@@ -85,7 +79,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
8579
const disputeID = event.params._disputeID;
8680
const disputeStorage = contract.disputes(disputeID);
8781
const courtID = disputeStorage.value0.toString();
88-
const court = loadCourtWithLogs(courtID);
82+
const court = Court.load(courtID);
8983
if (!court) return;
9084
court.numberDisputes = court.numberDisputes.plus(ONE);
9185
court.save();
@@ -99,7 +93,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
9993

10094
export function handleNewPeriod(event: NewPeriod): void {
10195
const disputeID = event.params._disputeID.toString();
102-
const dispute = loadDisputeWithLogs(disputeID);
96+
const dispute = Dispute.load(disputeID);
10397
if (!dispute) return;
10498
dispute.period = getPeriodName(event.params._period);
10599
dispute.lastPeriodChange = event.block.timestamp;
@@ -109,7 +103,7 @@ export function handleNewPeriod(event: NewPeriod): void {
109103
export function handleAppealDecision(event: AppealDecision): void {
110104
const contract = KlerosCore.bind(event.address);
111105
const disputeID = event.params._disputeID;
112-
const dispute = loadDisputeWithLogs(disputeID.toString());
106+
const dispute = Dispute.load(disputeID.toString());
113107
if (!dispute) return;
114108
const newRoundIndex = dispute.currentRoundIndex.plus(ONE);
115109
const roundID = `${disputeID}-${newRoundIndex.toString()}`;
@@ -124,7 +118,7 @@ export function handleAppealDecision(event: AppealDecision): void {
124118
export function handleDraw(event: DrawEvent): void {
125119
createDrawFromEvent(event);
126120
const disputeID = event.params._disputeID.toString();
127-
const dispute = loadDisputeWithLogs(disputeID);
121+
const dispute = Dispute.load(disputeID);
128122
if (!dispute) return;
129123
const contract = KlerosCore.bind(event.address);
130124
updateJurorStake(
@@ -164,9 +158,9 @@ export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
164158
updateRedistributedPNK(tokenAmount, event.block.timestamp);
165159
}
166160
updatePaidETH(ethAmount, event.block.timestamp);
167-
const dispute = loadDisputeWithLogs(disputeID);
161+
const dispute = Dispute.load(disputeID);
168162
if (!dispute) return;
169-
const court = loadCourtWithLogs(dispute.court);
163+
const court = Court.load(dispute.court);
170164
if (!court) return;
171165
updateJurorStake(
172166
jurorAddress,

subgraph/src/entities/ClassicDispute.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { DisputeCreation } from "../../generated/DisputeKitClassic/DisputeKitClassic";
22
import { ClassicDispute } from "../../generated/schema";
3-
import { loadWithLogs } from "../utils";
4-
5-
export function loadClassicDisputeWithLogs(id: string): ClassicDispute | null {
6-
return loadWithLogs("ClassicDispute", id) as ClassicDispute;
7-
}
3+
import { ZERO } from "../utils";
84

95
export function createClassicDisputeFromEvent(event: DisputeCreation): void {
106
const coreDisputeID = event.params._coreDisputeID.toString();
117
const classicDispute = new ClassicDispute(`1-${coreDisputeID}`);
128
classicDispute.coreDispute = coreDisputeID;
9+
classicDispute.currentLocalRoundIndex = ZERO;
1310
classicDispute.numberOfChoices = event.params._numberOfChoices;
1411
classicDispute.jumped = false;
1512
classicDispute.extraData = event.params._extraData;

subgraph/src/entities/ClassicRound.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { BigInt } from "@graphprotocol/graph-ts";
22
import { Contribution } from "../../generated/DisputeKitClassic/DisputeKitClassic";
33
import { ClassicRound } from "../../generated/schema";
4-
import { loadWithLogs, ZERO } from "../utils";
5-
6-
export function loadClassicRoundWithLogs(id: string): ClassicRound | null {
7-
return loadWithLogs("ClassicRound", id) as ClassicRound;
8-
}
4+
import { ZERO } from "../utils";
95

106
export function createClassicRound(
117
disputeID: string,
128
roundIndex: BigInt
139
): void {
14-
const id = `1-${disputeID}-${roundIndex.toString()}`;
10+
const localDisputeID = `1-${disputeID}`;
11+
const id = `${localDisputeID}-${roundIndex.toString()}`;
1512
const classicRound = new ClassicRound(id);
13+
classicRound.localDispute = localDisputeID;
1614
classicRound.votes = [];
1715
classicRound.winningChoice = ZERO;
1816
classicRound.counts = [];
@@ -33,7 +31,7 @@ export function updateChoiceFundingFromContributionEvent(
3331
const coreRoundIndex = event.params._coreRoundID.toString();
3432
const roundID = `${disputeKitID}-${coreDisputeID}-${coreRoundIndex}`;
3533

36-
const classicRound = loadClassicRoundWithLogs(roundID);
34+
const classicRound = ClassicRound.load(roundID);
3735
if (!classicRound) return;
3836

3937
const choice = event.params._choice;

subgraph/src/entities/Court.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { BigInt } from "@graphprotocol/graph-ts";
22
import { CourtCreated } from "../../generated/KlerosCore/KlerosCore";
33
import { Court } from "../../generated/schema";
4-
import { loadWithLogs, ZERO } from "../utils";
5-
6-
export function loadCourtWithLogs(id: string): Court | null {
7-
return loadWithLogs("Court", id) as Court;
8-
}
4+
import { ZERO } from "../utils";
95

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

3026
export function getFeeForJuror(id: string): BigInt {
31-
const court = loadCourtWithLogs(id);
27+
const court = Court.load(id);
3228
if (!court) return ZERO;
3329
return court.feeForJuror;
3430
}

subgraph/src/entities/Dispute.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import {
33
DisputeCreation,
44
} from "../../generated/KlerosCore/KlerosCore";
55
import { Dispute } from "../../generated/schema";
6-
import { loadWithLogs, ZERO } from "../utils";
7-
8-
export function loadDisputeWithLogs(id: string): Dispute | null {
9-
return loadWithLogs("Dispute", id) as Dispute;
10-
}
6+
import { ZERO } from "../utils";
117

128
export function createDisputeFromEvent(event: DisputeCreation): void {
139
const contract = KlerosCore.bind(event.address);

subgraph/src/entities/DisputeKit.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { DisputeKitCreated } from "../../generated/KlerosCore/KlerosCore";
22
import { DisputeKit } from "../../generated/schema";
3-
import { ZERO, ONE, loadWithLogs } from "../utils";
4-
5-
export function loadDisputeKitWithLogs(id: string): DisputeKit | null {
6-
return loadWithLogs("DisputeKit", id) as DisputeKit;
7-
}
3+
import { ZERO, ONE } from "../utils";
84

95
export function createDisputeKitFromEvent(event: DisputeKitCreated): void {
106
const disputeKit = new DisputeKit(event.params._disputeKitID.toString());
117
disputeKit.parent = event.params._parent.toString();
128
disputeKit.address = event.params._disputeKitAddress;
139
disputeKit.needsFreezing = false;
14-
const parent = loadDisputeKitWithLogs(event.params._parent.toString());
10+
const parent = DisputeKit.load(event.params._parent.toString());
1511
disputeKit.depthLevel = parent ? parent.depthLevel.plus(ONE) : ZERO;
1612
disputeKit.save();
1713
}

subgraph/src/entities/JurorTokensPerCourt.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { BigInt, Address } from "@graphprotocol/graph-ts";
22
import { KlerosCore } from "../../generated/KlerosCore/KlerosCore";
3-
import { JurorTokensPerCourt } from "../../generated/schema";
3+
import { Court, JurorTokensPerCourt } from "../../generated/schema";
44
import { updateActiveJurors, getDelta } from "../datapoint";
55
import { ensureUser } from "./Juror";
66
import { ZERO } from "../utils";
7-
import { loadCourtWithLogs } from "./Court";
87

98
export function ensureJurorTokensPerCourt(
109
jurorAddress: string,
@@ -50,7 +49,7 @@ export function updateJurorStake(
5049
timestamp: BigInt
5150
): void {
5251
const juror = ensureUser(jurorAddress);
53-
const court = loadCourtWithLogs(courtID);
52+
const court = Court.load(courtID);
5453
if (!court) return;
5554
const jurorTokens = ensureJurorTokensPerCourt(jurorAddress, courtID);
5655
const jurorBalance = contract.getJurorBalance(

subgraph/src/entities/Round.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { BigInt } from "@graphprotocol/graph-ts";
22
import { KlerosCore__getRoundInfoResult } from "../../generated/KlerosCore/KlerosCore";
33
import { Round } from "../../generated/schema";
4-
import { loadWithLogs } from "../utils";
5-
6-
export function loadRoundWithLogs(id: string): Round | null {
7-
return loadWithLogs("Round", id) as Round;
8-
}
94

105
export function createRoundFromRoundInfo(
116
disputeID: BigInt,

subgraph/src/utils.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
import { BigInt, Entity, store, log } from "@graphprotocol/graph-ts";
1+
import { BigInt } from "@graphprotocol/graph-ts";
22

33
export const ZERO = BigInt.fromI32(0);
44
export const ONE = BigInt.fromI32(1);
5-
6-
export function loadWithLogs(entityName: string, id: string): Entity | null {
7-
const entity = store.get(entityName, id);
8-
9-
if (!entity) {
10-
log.error("{} not found with id: {}", [entityName, id]);
11-
return null;
12-
}
13-
14-
return entity;
15-
}

0 commit comments

Comments
 (0)