Skip to content

Commit 79b1d0b

Browse files
authored
Merge pull request #490 from kleros/refactor(subgraph)/remove-code-duplications
Refactor(subgraph): remove code duplication when loading entities
2 parents 4e9c6ca + 9155160 commit 79b1d0b

File tree

10 files changed

+57
-110
lines changed

10 files changed

+57
-110
lines changed

subgraph/src/DisputeKitClassic.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ import { ClassicEvidence } from "../generated/schema";
1313
import { ensureClassicContributionFromEvent } from "./entities/ClassicContribution";
1414
import {
1515
createClassicDisputeFromEvent,
16-
loadClassicDisputeWithLog,
16+
loadClassicDisputeWithLogs,
1717
} from "./entities/ClassicDispute";
1818
import { ensureClassicEvidenceGroup } from "./entities/ClassicEvidenceGroup";
1919
import {
2020
createClassicRound,
21-
loadClassicRoundWithLog,
21+
loadClassicRoundWithLogs,
2222
updateChoiceFundingFromContributionEvent,
2323
} from "./entities/ClassicRound";
2424
import { createClassicVote } from "./entities/ClassicVote";
25-
import { loadDisputeWithLog } from "./entities/Dispute";
25+
import { loadDisputeWithLogs } from "./entities/Dispute";
2626
import { ONE } from "./utils";
2727

2828
export const DISPUTEKIT_ID = "1";
2929

3030
export function handleDisputeCreation(event: DisputeCreation): void {
31-
const dispute = loadDisputeWithLog(event.params._coreDisputeID.toString());
31+
const dispute = loadDisputeWithLogs(event.params._coreDisputeID.toString());
3232
if (!dispute) return;
3333
createClassicDisputeFromEvent(event);
3434
createClassicRound(dispute.id, dispute.currentRoundIndex);
@@ -52,7 +52,7 @@ export function handleEvidenceEvent(event: EvidenceEvent): void {
5252
export function handleJustificationEvent(event: JustificationEvent): void {
5353
const coreDisputeID = event.params._coreDisputeID.toString();
5454
const classicDisputeID = `${DISPUTEKIT_ID}-${coreDisputeID}`;
55-
const classicDispute = loadClassicDisputeWithLog(classicDisputeID);
55+
const classicDispute = loadClassicDisputeWithLogs(classicDisputeID);
5656
if (!classicDispute) return;
5757
const currentLocalRoundID = `${
5858
classicDispute.id
@@ -70,7 +70,7 @@ export function handleChoiceFunded(event: ChoiceFunded): void {
7070
const coreRoundIndex = event.params._coreRoundID.toString();
7171
const roundID = `${DISPUTEKIT_ID}-${coreDisputeID}-${coreRoundIndex}`;
7272

73-
const localRound = loadClassicRoundWithLog(roundID);
73+
const localRound = loadClassicRoundWithLogs(roundID);
7474
if (!localRound) return;
7575

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

88-
const localDispute = loadClassicDisputeWithLog(
88+
const localDispute = loadClassicDisputeWithLogs(
8989
`${DISPUTEKIT_ID}-${coreDisputeID}`
9090
);
9191
if (!localDispute) return;

subgraph/src/KlerosCore.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import { ZERO, ONE } from "./utils";
1515
import {
1616
createCourtFromEvent,
1717
getFeeForJuror,
18-
loadCourtWithLog,
18+
loadCourtWithLogs,
1919
} from "./entities/Court";
2020
import {
2121
createDisputeKitFromEvent,
2222
filterSupportedDisputeKits,
2323
} from "./entities/DisputeKit";
24-
import { createDisputeFromEvent, loadDisputeWithLog } from "./entities/Dispute";
24+
import {
25+
createDisputeFromEvent,
26+
loadDisputeWithLogs,
27+
} from "./entities/Dispute";
2528
import { createRoundFromRoundInfo } from "./entities/Round";
2629
import {
2730
updateCases,
@@ -50,7 +53,7 @@ export function handleCourtCreated(event: CourtCreated): void {
5053
export function handleCourtModified(event: CourtModified): void {
5154
const contract = KlerosCore.bind(event.address);
5255
const courtContractState = contract.courts(event.params._courtID);
53-
const court = loadCourtWithLog(event.params._courtID.toString());
56+
const court = loadCourtWithLogs(event.params._courtID.toString());
5457
if (!court) return;
5558
court.hiddenVotes = courtContractState.value1;
5659
court.minStake = courtContractState.value2;
@@ -66,7 +69,7 @@ export function handleDisputeKitCreated(event: DisputeKitCreated): void {
6669
}
6770

6871
export function handleDisputeKitEnabled(event: DisputeKitEnabled): void {
69-
const court = loadCourtWithLog(event.params._courtID.toString());
72+
const court = loadCourtWithLogs(event.params._courtID.toString());
7073
if (!court) return;
7174
const isEnable = event.params._enable;
7275
const disputeKitID = event.params._disputeKitID.toString();
@@ -81,7 +84,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
8184
const disputeID = event.params._disputeID;
8285
const disputeStorage = contract.disputes(disputeID);
8386
const courtID = disputeStorage.value0.toString();
84-
const court = loadCourtWithLog(courtID);
87+
const court = loadCourtWithLogs(courtID);
8588
if (!court) return;
8689
court.numberDisputes = court.numberDisputes.plus(ONE);
8790
court.save();
@@ -93,7 +96,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
9396

9497
export function handleNewPeriod(event: NewPeriod): void {
9598
const disputeID = event.params._disputeID.toString();
96-
const dispute = loadDisputeWithLog(disputeID);
99+
const dispute = loadDisputeWithLogs(disputeID);
97100
if (!dispute) return;
98101
dispute.period = getPeriodName(event.params._period);
99102
dispute.lastPeriodChange = event.block.timestamp;
@@ -103,7 +106,7 @@ export function handleNewPeriod(event: NewPeriod): void {
103106
export function handleAppealDecision(event: AppealDecision): void {
104107
const contract = KlerosCore.bind(event.address);
105108
const disputeID = event.params._disputeID;
106-
const dispute = loadDisputeWithLog(disputeID.toString());
109+
const dispute = loadDisputeWithLogs(disputeID.toString());
107110
if (!dispute) return;
108111
const newRoundIndex = dispute.currentRoundIndex.plus(ONE);
109112
const roundID = `${disputeID}-${newRoundIndex.toString()}`;
@@ -118,7 +121,7 @@ export function handleAppealDecision(event: AppealDecision): void {
118121
export function handleDraw(event: DrawEvent): void {
119122
createDrawFromEvent(event);
120123
const disputeID = event.params._disputeID.toString();
121-
const dispute = loadDisputeWithLog(disputeID);
124+
const dispute = loadDisputeWithLogs(disputeID);
122125
if (!dispute) return;
123126
const contract = KlerosCore.bind(event.address);
124127
updateJurorStake(
@@ -158,9 +161,9 @@ export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
158161
updateRedistributedPNK(tokenAmount, event.block.timestamp);
159162
}
160163
updatePaidETH(ethAmount, event.block.timestamp);
161-
const dispute = loadDisputeWithLog(disputeID);
164+
const dispute = loadDisputeWithLogs(disputeID);
162165
if (!dispute) return;
163-
const court = loadCourtWithLog(dispute.court);
166+
const court = loadCourtWithLogs(dispute.court);
164167
if (!court) return;
165168
updateJurorStake(
166169
jurorAddress,

subgraph/src/entities/ClassicDispute.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import { log } from "@graphprotocol/graph-ts";
21
import { DisputeCreation } from "../../generated/DisputeKitClassic/DisputeKitClassic";
32
import { ClassicDispute } from "../../generated/schema";
3+
import { loadWithLogs } from "../utils";
44

5-
export function loadClassicDisputeWithLog(id: string): ClassicDispute | null {
6-
const classicDispute = ClassicDispute.load(id);
7-
if (!classicDispute) {
8-
log.error("ClassicDispute not found with id: {}", [id]);
9-
return null;
10-
}
11-
return classicDispute;
5+
export function loadClassicDisputeWithLogs(id: string): ClassicDispute | null {
6+
return loadWithLogs("ClassicDispute", id) as ClassicDispute;
127
}
138

149
export function createClassicDisputeFromEvent(event: DisputeCreation): void {

subgraph/src/entities/ClassicRound.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { BigInt, log } from "@graphprotocol/graph-ts";
1+
import { BigInt } from "@graphprotocol/graph-ts";
22
import { Contribution } from "../../generated/DisputeKitClassic/DisputeKitClassic";
33
import { ClassicRound } from "../../generated/schema";
4-
import { ZERO } from "../utils";
4+
import { loadWithLogs, ZERO } from "../utils";
55

6-
export function loadClassicRoundWithLog(id: string): ClassicRound | null {
7-
const classicRound = ClassicRound.load(id);
8-
if (!classicRound) {
9-
log.error("ClassicRound not found with id: {}", [id]);
10-
return null;
11-
}
12-
return classicRound;
6+
export function loadClassicRoundWithLogs(id: string): ClassicRound | null {
7+
return loadWithLogs("ClassicRound", id) as ClassicRound;
138
}
149

1510
export function createClassicRound(
@@ -38,7 +33,7 @@ export function updateChoiceFundingFromContributionEvent(
3833
const coreRoundIndex = event.params._coreRoundID.toString();
3934
const roundID = `${disputeKitID}-${coreDisputeID}-${coreRoundIndex}`;
4035

41-
const classicRound = loadClassicRoundWithLog(roundID);
36+
const classicRound = loadClassicRoundWithLogs(roundID);
4237
if (!classicRound) return;
4338

4439
const choice = event.params._choice;

subgraph/src/entities/Court.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import { BigInt, log } from "@graphprotocol/graph-ts";
1+
import { BigInt } from "@graphprotocol/graph-ts";
22
import { CourtCreated } from "../../generated/KlerosCore/KlerosCore";
33
import { Court } from "../../generated/schema";
4-
import { ZERO } from "../utils";
4+
import { loadWithLogs, ZERO } from "../utils";
55

6-
export function loadCourtWithLog(id: string): Court | null {
7-
const court = Court.load(id);
8-
9-
if (!court) {
10-
log.error("Court not found with id: {}", [id]);
11-
return null;
12-
}
13-
14-
return court;
6+
export function loadCourtWithLogs(id: string): Court | null {
7+
return loadWithLogs("Court", id) as Court;
158
}
169

1710
export function createCourtFromEvent(event: CourtCreated): void {
@@ -35,7 +28,7 @@ export function createCourtFromEvent(event: CourtCreated): void {
3528
}
3629

3730
export function getFeeForJuror(id: string): BigInt {
38-
const court = loadCourtWithLog(id);
31+
const court = loadCourtWithLogs(id);
3932
if (!court) return ZERO;
4033
return court.feeForJuror;
4134
}

subgraph/src/entities/Dispute.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import { log } from "@graphprotocol/graph-ts";
21
import {
32
KlerosCore,
43
DisputeCreation,
54
} from "../../generated/KlerosCore/KlerosCore";
65
import { Dispute } from "../../generated/schema";
7-
import { ZERO } from "../utils";
6+
import { loadWithLogs, ZERO } from "../utils";
87

9-
export function loadDisputeWithLog(id: string): Dispute | null {
10-
const dispute = Dispute.load(id);
11-
12-
if (!dispute) {
13-
log.error("Dispute not found with id: {}", [id]);
14-
return null;
15-
}
16-
17-
return dispute;
8+
export function loadDisputeWithLogs(id: string): Dispute | null {
9+
return loadWithLogs("Dispute", id) as Dispute;
1810
}
1911

2012
export function createDisputeFromEvent(event: DisputeCreation): void {

subgraph/src/entities/DisputeKit.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
1-
import { Bytes } from "@graphprotocol/graph-ts";
21
import { DisputeKitCreated } from "../../generated/KlerosCore/KlerosCore";
32
import { DisputeKit } from "../../generated/schema";
4-
import { ZERO, ONE } from "../utils";
3+
import { ZERO, ONE, loadWithLogs } from "../utils";
54

6-
export function ensureDisputeKit(id: string): DisputeKit {
7-
let disputeKit = DisputeKit.load(id);
8-
9-
if (disputeKit) {
10-
return disputeKit;
11-
}
12-
// Should never reach here
13-
disputeKit = new DisputeKit(id);
14-
disputeKit.parent = "0";
15-
disputeKit.address = Bytes.fromHexString("0x0");
16-
disputeKit.needsFreezing = false;
17-
const parent = ensureDisputeKit("0");
18-
disputeKit.depthLevel = id === "0" ? ZERO : parent.depthLevel.plus(ONE);
19-
disputeKit.save();
20-
21-
return disputeKit;
5+
export function loadDisputeKitWithLogs(id: string): DisputeKit | null {
6+
return loadWithLogs("DisputeKit", id) as DisputeKit;
227
}
238

249
export function createDisputeKitFromEvent(event: DisputeKitCreated): void {
2510
const disputeKit = new DisputeKit(event.params._disputeKitID.toString());
2611
disputeKit.parent = event.params._parent.toString();
2712
disputeKit.address = event.params._disputeKitAddress;
2813
disputeKit.needsFreezing = false;
29-
const parent = DisputeKit.load(event.params._parent.toString());
14+
const parent = loadDisputeKitWithLogs(event.params._parent.toString());
3015
disputeKit.depthLevel = parent ? parent.depthLevel.plus(ONE) : ZERO;
3116
disputeKit.save();
3217
}

subgraph/src/entities/JurorTokensPerCourt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { JurorTokensPerCourt } from "../../generated/schema";
44
import { updateActiveJurors, getDelta } from "../datapoint";
55
import { ensureUser } from "./Juror";
66
import { ZERO } from "../utils";
7-
import { loadCourtWithLog } from "./Court";
7+
import { loadCourtWithLogs } from "./Court";
88

99
export function ensureJurorTokensPerCourt(
1010
jurorAddress: string,
@@ -50,7 +50,7 @@ export function updateJurorStake(
5050
timestamp: BigInt
5151
): void {
5252
const juror = ensureUser(jurorAddress);
53-
const court = loadCourtWithLog(courtID);
53+
const court = loadCourtWithLogs(courtID);
5454
if (!court) return;
5555
const jurorTokens = ensureJurorTokensPerCourt(jurorAddress, courtID);
5656
const jurorBalance = contract.getJurorBalance(

subgraph/src/entities/Round.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
import { BigInt } from "@graphprotocol/graph-ts";
22
import { KlerosCore__getRoundInfoResult } from "../../generated/KlerosCore/KlerosCore";
33
import { Round } from "../../generated/schema";
4-
import { ZERO } from "../utils";
4+
import { loadWithLogs } from "../utils";
55

6-
export function ensureRound(id: string): Round {
7-
let round = Round.load(id);
8-
9-
if (round) {
10-
return round;
11-
}
12-
// Should never reach here
13-
round = new Round(id);
14-
round.disputeKit = "0";
15-
round.tokensAtStakePerJuror = ZERO;
16-
round.totalFeesForJurors = ZERO;
17-
round.nbVotes = ZERO;
18-
round.repartitions = ZERO;
19-
round.penalties = ZERO;
20-
round.dispute = "0";
21-
round.save();
22-
23-
return round;
6+
export function loadRoundWithLogs(id: string): Round | null {
7+
return loadWithLogs("Round", id) as Round;
248
}
259

2610
export function createRoundFromRoundInfo(
@@ -40,14 +24,3 @@ export function createRoundFromRoundInfo(
4024
round.dispute = disputeID.toString();
4125
round.save();
4226
}
43-
44-
export function filterSupportedDisputeKits(
45-
supportedDisputeKits: string[],
46-
disputeKitID: string
47-
): string[] {
48-
let result: string[] = [];
49-
for (let i = 0; i < supportedDisputeKits.length; i++)
50-
if (supportedDisputeKits[i] !== disputeKitID)
51-
result = result.concat([supportedDisputeKits[i]]);
52-
return result;
53-
}

subgraph/src/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import { BigInt } from "@graphprotocol/graph-ts";
1+
import { BigInt, Entity, store, log } 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)