Skip to content

Commit 27eadfc

Browse files
alcercujaybuidl
authored andcommitted
feat(subgraph): add court info
1 parent ef71063 commit 27eadfc

File tree

2 files changed

+99
-56
lines changed

2 files changed

+99
-56
lines changed

subgraph/schema.graphql

+7-26
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ type Court @entity {
1919
timesPerPeriod: [BigInt!]!
2020
supportedDisputeKits: [DisputeKit!]!
2121
disputes: [Dispute!]! @derivedFrom(field: "subcourtID")
22+
numberDisputes: BigInt!
2223
stakedJurors: [JurorTokensPerSubcourt!]! @derivedFrom(field: "subcourt")
24+
numberStakedJurors: BigInt!
2325
tokens: [JurorTokensPerSubcourt!]! @derivedFrom(field: "subcourt")
26+
stake: BigInt!
27+
paidETH: BigInt!
28+
paidPNK: BigInt!
2429
}
2530

2631
type Juror @entity {
2732
id: ID! # Set to address
2833
tokens: [JurorTokensPerSubcourt!]! @derivedFrom(field: "juror")
34+
totalStake: BigInt!
2935
shifts: [TokenAndETHShift!]! @derivedFrom(field: "juror")
3036
draws: [Draw!]! @derivedFrom(field: "juror")
3137
votes: [Vote!]! @derivedFrom(field: "juror")
@@ -134,33 +140,8 @@ type OutgoingBatch @entity {
134140
batchMerkleRoot: String!
135141
}
136142

137-
type PNKStakedDataPoint @entity {
138-
id: ID! # Will be the timestamp except for the counter which will be 0
139-
value: BigInt!
140-
}
141-
142-
type ETHPaidDataPoint @entity {
143-
id: ID! # Will be the timestamp except for the counter which will be 0
144-
value: BigInt!
145-
}
146-
147-
type PNKRedistributedDataPoint @entity {
148-
id: ID! # Will be the timestamp except for the counter which will be 0
149-
value: BigInt!
150-
}
151-
152-
type ActiveJurorsDataPoint @entity {
153-
id: ID! # Will be the timestamp except for the counter which will be 0
154-
value: BigInt!
155-
}
156-
157-
type CasesDataPoint @entity {
158-
id: ID! # Will be the timestamp except for the counter which will be 0
159-
value: BigInt!
160-
}
161-
162143
type Counter @entity {
163-
id: ID! # Always 0
144+
id: ID! # Will be the timestamp except for the counter which will be 0
164145
stakedPNK: BigInt!
165146
redistributedPNK: BigInt!
166147
paidETH: BigInt!

subgraph/src/KlerosCore.ts

+92-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Address, BigInt } from "@graphprotocol/graph-ts";
2+
import { ZERO } from "./utils";
23
import {
34
KlerosCore,
45
AppealDecision,
@@ -51,6 +52,11 @@ export function handleSubcourtCreated(event: SubcourtCreated): void {
5152
event.params._supportedDisputeKits.map<string>((disputeKitID: BigInt) =>
5253
disputeKitID.toString()
5354
);
55+
subcourt.numberDisputes = ZERO;
56+
subcourt.numberStakedJurors = ZERO;
57+
subcourt.stake = ZERO;
58+
subcourt.paidETH = ZERO;
59+
subcourt.paidPNK = ZERO;
5460
subcourt.save();
5561
}
5662

@@ -59,11 +65,11 @@ export function handleSubcourtModified(event: SubcourtModified): void {
5965
if (court) {
6066
const contract = KlerosCore.bind(event.address);
6167
const courtContractState = contract.courts(event.params._subcourtID);
62-
court.hiddenVotes = courtContractState.getHiddenVotes();
63-
court.minStake = courtContractState.getMinStake();
64-
court.alpha = courtContractState.getAlpha();
65-
court.feeForJuror = courtContractState.getFeeForJuror();
66-
court.jurorsForCourtJump = courtContractState.getJurorsForCourtJump();
68+
court.hiddenVotes = courtContractState.value1;
69+
court.minStake = courtContractState.value2;
70+
court.alpha = courtContractState.value3;
71+
court.feeForJuror = courtContractState.value4;
72+
court.jurorsForCourtJump = courtContractState.value5;
6773
court.timesPerPeriod = contract.getTimesPerPeriod(event.params._subcourtID);
6874
court.save();
6975
}
@@ -122,7 +128,7 @@ export function handleAppealDecision(event: AppealDecision): void {
122128
round.dispute = disputeID.toString();
123129
round.tokensAtStakePerJuror = roundInfo.value0;
124130
round.totalFeesForJurors = roundInfo.value1;
125-
round.nbVotes = roundInfo.value1.div(subcourtStorage.getFeeForJuror());
131+
round.nbVotes = roundInfo.value1.div(subcourtStorage.value4);
126132
round.totalVoted = BigInt.fromI32(0);
127133
round.repartitions = roundInfo.value2;
128134
round.penalties = roundInfo.value3;
@@ -139,6 +145,7 @@ export function handleDisputeCreation(event: DisputeCreation): void {
139145
const dispute = new Dispute(disputeID.toString());
140146
const disputeStorage = contract.disputes(disputeID);
141147
const subcourtID = disputeStorage.value0;
148+
const subcourt = Court.load(subcourtID.toString());
142149
dispute.arbitrated = event.params._arbitrable;
143150
dispute.subcourtID = subcourtID.toString();
144151
dispute.period = "Evidence";
@@ -147,15 +154,17 @@ export function handleDisputeCreation(event: DisputeCreation): void {
147154
dispute.currentRound = 0;
148155
const roundInfo = contract.getRoundInfo(disputeID, BigInt.fromString("0"));
149156
const round = new Round(`${disputeID.toString()}-0`);
150-
const subcourtStorage = contract.courts(subcourtID);
151157
round.dispute = disputeID.toString();
152158
round.tokensAtStakePerJuror = roundInfo.value0;
153159
round.totalFeesForJurors = roundInfo.value1;
154-
round.nbVotes = roundInfo.value1.div(subcourtStorage.getFeeForJuror());
160+
round.nbVotes = subcourt ? roundInfo.value1.div(subcourt.feeForJuror) : ZERO;
155161
round.totalVoted = BigInt.fromI32(0);
156162
round.repartitions = roundInfo.value2;
157163
round.penalties = roundInfo.value3;
158164
round.disputeKitID = roundInfo.value5.toString();
165+
if (subcourt) {
166+
subcourt.numberDisputes = subcourt.numberDisputes.plus(BigInt.fromI32(1));
167+
}
159168
dispute.save();
160169
round.save();
161170
updateCases(BigInt.fromI32(1), event.block.timestamp);
@@ -170,6 +179,51 @@ export function handleNewPeriod(event: NewPeriod): void {
170179
}
171180
}
172181

182+
function updateJurorStake(
183+
jurorAddress: string,
184+
subcourtID: string,
185+
contract: KlerosCore,
186+
timestamp: BigInt
187+
): void {
188+
const juror = Juror.load(jurorAddress);
189+
const subcourt = Court.load(subcourtID);
190+
const jurorTokens = JurorTokensPerSubcourt.load(
191+
`${jurorAddress}-${subcourtID}`
192+
);
193+
if (juror && subcourt && jurorTokens) {
194+
const jurorBalance = contract.getJurorBalance(
195+
Address.fromString(jurorAddress),
196+
BigInt.fromString(subcourtID)
197+
);
198+
const previousStake = jurorTokens.staked;
199+
jurorTokens.staked = jurorBalance.value0;
200+
jurorTokens.locked = jurorBalance.value1;
201+
jurorTokens.save();
202+
const stakeDelta = jurorTokens.staked.minus(previousStake);
203+
const previousTotalStake = juror.totalStake;
204+
juror.totalStake = juror.totalStake.plus(stakeDelta);
205+
subcourt.stake = subcourt.stake.plus(stakeDelta);
206+
let activeJurorsDelta: BigInt;
207+
let numberStakedJurorsDelta: BigInt;
208+
if (previousTotalStake.equals(ZERO)) {
209+
activeJurorsDelta = BigInt.fromI32(1);
210+
numberStakedJurorsDelta = BigInt.fromI32(1);
211+
} else if (previousStake.equals(ZERO)) {
212+
activeJurorsDelta = ZERO;
213+
numberStakedJurorsDelta = BigInt.fromI32(1);
214+
} else {
215+
activeJurorsDelta = ZERO;
216+
numberStakedJurorsDelta = ZERO;
217+
}
218+
subcourt.numberStakedJurors = subcourt.numberStakedJurors.plus(
219+
numberStakedJurorsDelta
220+
);
221+
updateActiveJurors(activeJurorsDelta, timestamp);
222+
juror.save();
223+
subcourt.save();
224+
}
225+
}
226+
173227
export function handleDraw(event: DrawEvent): void {
174228
const disputeID = event.params._disputeID;
175229
const currentRound = event.params._roundID;
@@ -184,29 +238,16 @@ export function handleDraw(event: DrawEvent): void {
184238
draw.save();
185239
const dispute = Dispute.load(disputeID.toString());
186240
if (dispute) {
187-
updateJurorBalance(
241+
const contract = KlerosCore.bind(event.address);
242+
updateJurorStake(
188243
drawnAddress.toHexString(),
189244
dispute.subcourtID.toString(),
190-
event
245+
contract,
246+
event.block.timestamp
191247
);
192248
}
193249
}
194250

195-
function updateJurorBalance(
196-
address: string,
197-
subcourt: string,
198-
event: DrawEvent
199-
): void {
200-
const jurorTokens = new JurorTokensPerSubcourt(`${address}-${subcourt}`);
201-
const contract = KlerosCore.bind(event.address);
202-
const jurorBalance = contract.getJurorBalance(
203-
Address.fromString(address),
204-
BigInt.fromString(subcourt)
205-
);
206-
jurorTokens.locked = jurorBalance.value1;
207-
jurorTokens.save();
208-
}
209-
210251
export function handleStakeSet(event: StakeSet): void {
211252
const jurorAddress = event.params._address.toHexString();
212253
let juror = Juror.load(jurorAddress);
@@ -216,19 +257,25 @@ export function handleStakeSet(event: StakeSet): void {
216257
}
217258
juror.save();
218259
const subcourtID = event.params._subcourtID;
219-
const amountStaked = event.params._newTotalStake;
220260
const jurorTokensID = `${jurorAddress}-${subcourtID.toString()}`;
221261
let jurorTokens = JurorTokensPerSubcourt.load(jurorTokensID);
222262
let previousStake: BigInt;
223263
if (!jurorTokens) {
224264
jurorTokens = new JurorTokensPerSubcourt(jurorTokensID);
225265
jurorTokens.juror = jurorAddress;
226266
jurorTokens.subcourt = subcourtID.toString();
227-
jurorTokens.locked = BigInt.fromI32(0);
228-
previousStake = BigInt.fromI32(0);
267+
jurorTokens.staked = ZERO;
268+
jurorTokens.locked = ZERO;
269+
jurorTokens.save();
270+
previousStake = ZERO;
229271
} else previousStake = jurorTokens.staked;
230-
jurorTokens.staked = amountStaked;
231-
jurorTokens.save();
272+
updateJurorStake(
273+
jurorAddress,
274+
subcourtID.toString(),
275+
KlerosCore.bind(event.address),
276+
event.block.timestamp
277+
);
278+
const amountStaked = event.params._newTotalStake;
232279
updateStakedPNK(getDelta(previousStake, amountStaked), event.block.timestamp);
233280
}
234281

@@ -248,4 +295,19 @@ export function handleTokenAndETHShift(event: TokenAndETHShiftEvent): void {
248295
shift.tokenAmount = tokenAmount;
249296
shift.ethAmount = ethAmount;
250297
shift.save();
298+
const dispute = Dispute.load(disputeID.toString());
299+
if (dispute) {
300+
const subcourt = Court.load(dispute.subcourtID.toString());
301+
if (subcourt) {
302+
updateJurorStake(
303+
jurorAddress,
304+
subcourt.id,
305+
KlerosCore.bind(event.address),
306+
event.block.timestamp
307+
);
308+
subcourt.paidETH = subcourt.paidETH.plus(ethAmount);
309+
subcourt.paidPNK = subcourt.paidETH.plus(tokenAmount);
310+
subcourt.save();
311+
}
312+
}
251313
}

0 commit comments

Comments
 (0)