Skip to content

Commit 3ef595d

Browse files
committed
refactor(subgraph): abstract-answer-fields-to-answer-entity
1 parent e0e5fca commit 3ef595d

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

subgraph/core/schema.graphql

+11-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ interface Evidence {
6363
fileTypeExtension: String
6464
}
6565

66+
6667
############
6768
# Entities #
6869
############
@@ -265,17 +266,25 @@ type ClassicDispute implements DisputeKitDispute @entity {
265266
extraData: Bytes!
266267
}
267268

269+
type Answer @entity {
270+
id: ID! # classicRound.id-answerId
271+
answerId: BigInt!
272+
count: BigInt!
273+
paidFee: BigInt!
274+
funded: Boolean!
275+
localRound: ClassicRound!
276+
}
277+
268278
type ClassicRound implements DisputeKitRound @entity {
269279
id: ID! # disputeKit.id-coreDispute-dispute.rounds.length
270280
localDispute: DisputeKitDispute!
271281
votes: [Vote!]! @derivedFrom(field: "localRound")
282+
answers: [Answer!]! @derivedFrom(field: "localRound")
272283

273284
winningChoice: BigInt!
274-
counts: [BigInt!]!
275285
tied: Boolean!
276286
totalVoted: BigInt!
277287
totalCommited: BigInt!
278-
paidFees: [BigInt!]!
279288
contributions: [ClassicContribution!]! @derivedFrom(field: "localRound")
280289
feeRewards: BigInt!
281290
totalFeeDispersed: BigInt!

subgraph/core/src/DisputeKitClassic.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ensureClassicContributionFromEvent } from "./entities/ClassicContributi
1414
import { createClassicDisputeFromEvent } from "./entities/ClassicDispute";
1515
import {
1616
createClassicRound,
17+
ensureAnswer,
1718
updateChoiceFundingFromContributionEvent,
1819
updateCountsAndGetCurrentRuling,
1920
} from "./entities/ClassicRound";
@@ -101,11 +102,16 @@ export function handleChoiceFunded(event: ChoiceFunded): void {
101102
const localRound = ClassicRound.load(roundID);
102103
if (!localRound) return;
103104

105+
const answer = ensureAnswer(roundID, choice);
106+
104107
const currentFeeRewards = localRound.feeRewards;
105-
const deltaFeeRewards = localRound.paidFees[choice.toI32()];
108+
const deltaFeeRewards = answer.paidFee;
106109
localRound.feeRewards = currentFeeRewards.plus(deltaFeeRewards);
107110
localRound.fundedChoices = localRound.fundedChoices.concat([choice]);
108111

112+
answer.funded = true;
113+
answer.save();
114+
109115
if (localRound.fundedChoices.length > 1) {
110116
const disputeKitClassic = DisputeKitClassic.bind(event.address);
111117
const klerosCore = KlerosCore.bind(disputeKitClassic.core());
+29-26
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import { BigInt } from "@graphprotocol/graph-ts";
22
import { Contribution } from "../../generated/DisputeKitClassic/DisputeKitClassic";
3-
import { ClassicRound } from "../../generated/schema";
3+
import { Answer, ClassicRound } from "../../generated/schema";
44
import { ONE, ZERO } from "../utils";
55

66
export function createClassicRound(disputeID: string, numberOfChoices: BigInt, roundIndex: BigInt): void {
7-
const choicesLength = numberOfChoices.plus(ONE);
87
const localDisputeID = `1-${disputeID}`;
98
const id = `${localDisputeID}-${roundIndex.toString()}`;
109
const classicRound = new ClassicRound(id);
1110
classicRound.localDispute = localDisputeID;
1211
classicRound.winningChoice = ZERO;
13-
classicRound.counts = new Array<BigInt>(choicesLength.toI32()).fill(ZERO);
1412
classicRound.tied = true;
1513
classicRound.totalVoted = ZERO;
1614
classicRound.totalCommited = ZERO;
17-
classicRound.paidFees = new Array<BigInt>(choicesLength.toI32()).fill(ZERO);
1815
classicRound.feeRewards = ZERO;
1916
classicRound.appealFeesDispersed = false;
2017
classicRound.totalFeeDispersed = ZERO;
@@ -27,21 +24,31 @@ class CurrentRulingInfo {
2724
tied: boolean;
2825
}
2926

27+
export function ensureAnswer(localRoundId: string, answerId: BigInt): Answer {
28+
const id = `${localRoundId}-${answerId}`;
29+
let answer = Answer.load(id);
30+
if (answer) return answer;
31+
answer = new Answer(id);
32+
answer.answerId = answerId;
33+
answer.count = ZERO;
34+
answer.paidFee = ZERO;
35+
answer.funded = false;
36+
answer.localRound = localRoundId;
37+
return answer;
38+
}
39+
3040
export function updateCountsAndGetCurrentRuling(id: string, choice: BigInt, delta: BigInt): CurrentRulingInfo {
3141
const round = ClassicRound.load(id);
3242
if (!round) return { ruling: ZERO, tied: false };
33-
const choiceNum = choice.toI32();
34-
const newChoiceCount = round.counts[choiceNum].plus(delta);
35-
let newCounts: BigInt[] = [];
36-
for (let i = 0; i < round.counts.length; i++) {
37-
if (BigInt.fromI32(i).equals(choice)) {
38-
newCounts.push(newChoiceCount);
39-
} else {
40-
newCounts.push(round.counts[i]);
41-
}
42-
}
43-
round.counts = newCounts;
44-
const currentWinningCount = round.counts[round.winningChoice.toI32()];
43+
const answer = ensureAnswer(id, choice);
44+
45+
answer.count = answer.count.plus(delta);
46+
47+
const newChoiceCount = answer.count;
48+
49+
const winningAnswer = ensureAnswer(id, round.winningChoice);
50+
const currentWinningCount = winningAnswer.count;
51+
4552
if (choice.equals(round.winningChoice)) {
4653
if (round.tied) round.tied = false;
4754
} else {
@@ -53,6 +60,8 @@ export function updateCountsAndGetCurrentRuling(id: string, choice: BigInt, delt
5360
}
5461
}
5562
round.totalVoted = round.totalVoted.plus(delta);
63+
64+
answer.save();
5665
round.save();
5766
return { ruling: round.winningChoice, tied: round.tied };
5867
}
@@ -68,15 +77,9 @@ export function updateChoiceFundingFromContributionEvent(event: Contribution): v
6877

6978
const choice = event.params._choice;
7079
const amount = event.params._amount;
71-
const currentPaidFees = classicRound.paidFees[choice.toI32()];
72-
let newPaidFees: BigInt[] = [];
73-
for (let i = 0; i < classicRound.paidFees.length; i++) {
74-
if (BigInt.fromI32(i).equals(choice)) {
75-
newPaidFees.push(currentPaidFees.plus(amount));
76-
} else {
77-
newPaidFees.push(classicRound.paidFees[i]);
78-
}
79-
}
80-
classicRound.paidFees = newPaidFees;
80+
const answer = ensureAnswer(roundID, choice);
81+
answer.paidFee = answer.paidFee.plus(amount);
82+
83+
answer.save();
8184
classicRound.save();
8285
}

0 commit comments

Comments
 (0)