Skip to content

Commit ad9a270

Browse files
chore: refactor subgraph
1 parent e7783ff commit ad9a270

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

subgraph/src/PolicyRegistry.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,32 @@
11
import { PolicyUpdate } from "../generated/PolicyRegistry/PolicyRegistry";
22
import { Court } from "../generated/schema";
3-
import { ipfs, log, json, Bytes } from "@graphprotocol/graph-ts";
3+
import { ipfs, log, json, Bytes, TypedMap, JSONValue } from "@graphprotocol/graph-ts";
44

55
export function handlePolicyUpdate(event: PolicyUpdate): void {
66
const courtID = event.params._courtID.toString();
77
const court = Court.load(courtID);
88
if (court) {
99
court.policy = event.params._policy;
1010
court.name = event.params._courtName;
11+
court.save();
1112

1213
let jsonStr = ipfs.cat(event.params._policy);
13-
if (!jsonStr) {
14-
log.error("Failed to fetch policy #{} SubcourtID: {}", [event.params._policy, event.params._courtID.toString()]);
15-
court.save();
16-
return;
17-
}
18-
19-
let jsonObjValueAndSuccess = json.try_fromBytes(jsonStr as Bytes);
20-
if (!jsonObjValueAndSuccess.isOk) {
21-
log.error(`Error getting json object value for policy #{} SubcourtID: {}`, [
22-
event.params._policy,
23-
event.params._courtID.toString(),
24-
]);
25-
court.save();
26-
return;
27-
}
28-
29-
let jsonObj = jsonObjValueAndSuccess.value.toObject();
30-
if (!jsonObj) {
31-
log.error(`Error converting object value for policy #{} SubcourtID: {}`, [
32-
event.params._policy,
33-
event.params._courtID.toString(),
34-
]);
35-
court.save();
36-
return;
37-
}
14+
if (!jsonStr) return;
3815

39-
const name = jsonObj.get("name");
40-
court.name = name ? name.toString() : null;
16+
const jsonObjValueAndSuccess = json.try_fromBytes(jsonStr as Bytes);
17+
if (!jsonObjValueAndSuccess.isOk) return;
4118

42-
const description = jsonObj.get("description");
43-
court.description = description ? description.toString() : null;
19+
const jsonObj = jsonObjValueAndSuccess.value.toObject();
20+
if (!jsonObj) return;
4421

45-
const requiredSkills = jsonObj.get("requiredSkills");
46-
court.requiredSkills = requiredSkills ? requiredSkills.toString() : null;
22+
court.name = tryGetValue(jsonObj, "name");
23+
court.description = tryGetValue(jsonObj, "description");
24+
court.requiredSkills = tryGetValue(jsonObj, "requiredSkills");
4725
court.save();
4826
}
4927
}
28+
29+
function tryGetValue(jsonObj: TypedMap<string, JSONValue>, key: string): string | null {
30+
const value = jsonObj.get(key);
31+
return value ? value.toString() : null;
32+
}

subgraph/src/entities/Dispute.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { KlerosCore, DisputeCreation } from "../../generated/KlerosCore/KlerosCo
22
import { Court, Dispute, PeriodIndexCounter } from "../../generated/schema";
33
import { ZERO, ONE } from "../utils";
44
import { BigInt } from "@graphprotocol/graph-ts";
5+
import { ensurePeriodIndexCounter } from "./PeriodIndexCounter";
56

67
export function createDisputeFromEvent(event: DisputeCreation): void {
78
const contract = KlerosCore.bind(event.address);
@@ -19,14 +20,10 @@ export function createDisputeFromEvent(event: DisputeCreation): void {
1920
dispute.overridden = false;
2021
dispute.lastPeriodChangeTs = event.block.timestamp;
2122
dispute.lastPeriodChangeBlockNumber = event.block.number;
22-
let counter = PeriodIndexCounter.load("evidence");
23-
if (!counter) {
24-
counter = new PeriodIndexCounter("evidence");
25-
counter.counter = BigInt.fromI32(0);
26-
}
27-
dispute.periodNotificationIndex = counter.counter;
28-
counter.counter = counter.counter.plus(ONE);
29-
counter.save();
23+
const PeriodIndexCounter = ensurePeriodIndexCounter(dispute.period);
24+
dispute.periodNotificationIndex = PeriodIndexCounter.counter;
25+
PeriodIndexCounter.counter = PeriodIndexCounter.counter.plus(ONE);
26+
PeriodIndexCounter.save();
3027
const court = Court.load(courtID);
3128
if (!court) return;
3229
dispute.periodDeadline = event.block.timestamp.plus(court.timesPerPeriod[0]);

subgraph/src/entities/Draw.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Draw as DrawEvent } from "../../generated/KlerosCore/KlerosCore";
22
import { Draw, PeriodIndexCounter, User } from "../../generated/schema";
33
import { BigInt } from "@graphprotocol/graph-ts";
4+
import { ensurePeriodIndexCounter } from "./PeriodIndexCounter";
45

56
export function createDrawFromEvent(event: DrawEvent): void {
67
const disputeID = event.params._disputeID.toString();
@@ -10,18 +11,14 @@ export function createDrawFromEvent(event: DrawEvent): void {
1011
const drawID = `${disputeID}-${roundIndex.toString()}-${voteID.toString()}`;
1112
const draw = new Draw(drawID);
1213
draw.blockNumber = event.block.number;
13-
let counter = PeriodIndexCounter.load("draw");
14-
if (!counter) {
15-
counter = new PeriodIndexCounter("draw");
16-
counter.counter = BigInt.fromI32(0);
17-
}
1814
const user = User.load(event.params._address.toHexString());
1915
if (user) {
2016
const duplicateNotification = user.disputes.includes(disputeID);
2117
if (!duplicateNotification) {
22-
draw.drawNotificationIndex = counter.counter;
23-
counter.counter = counter.counter.plus(BigInt.fromI32(1));
24-
counter.save();
18+
const periodIndex = ensurePeriodIndexCounter("draw");
19+
draw.drawNotificationIndex = periodIndex.counter;
20+
periodIndex.counter = periodIndex.counter.plus(BigInt.fromI32(1));
21+
periodIndex.save();
2522
}
2623
}
2724
draw.dispute = disputeID;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PeriodIndexCounter } from "../../generated/schema";
2+
import { BigInt } from "@graphprotocol/graph-ts";
3+
4+
export function ensurePeriodIndexCounter(id: string): PeriodIndexCounter {
5+
let counter = PeriodIndexCounter.load(id);
6+
if (!counter) {
7+
counter = new PeriodIndexCounter(id);
8+
counter.counter = BigInt.fromI32(0);
9+
}
10+
return counter;
11+
}

0 commit comments

Comments
 (0)