Skip to content

Commit 3497933

Browse files
committed
feat(subgraph): complete-subgraph-first-iteration
1 parent 9f89478 commit 3497933

File tree

3 files changed

+78
-258
lines changed

3 files changed

+78
-258
lines changed

subgraph/schema.graphql

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,6 @@ enum Ruling {
1818
Reject
1919
}
2020

21-
type EvidenceGroup @entity {
22-
"evidenceGroupId@tcrAddress"
23-
id: ID!
24-
"Evidences posted to this evidenceGroupId"
25-
evidences: [Evidence!]! @derivedFrom(field: "evidenceGroup")
26-
"Number of evidences posted in this group"
27-
numberOfEvidence: BigInt!
28-
}
29-
30-
type Evidence @entity {
31-
"evidenceGroupId@tcrAddress-number"
32-
id: ID!
33-
"The arbitrator's address."
34-
arbitrator: Bytes!
35-
"The evidence group"
36-
evidenceGroup: EvidenceGroup!
37-
"The address of the party that sent this piece of evidence."
38-
party: Bytes!
39-
"The URI of the evidence file."
40-
URI: String!
41-
"This is the <number>th evidence submitted (starting at 0) for <request>."
42-
number: BigInt!
43-
"When was this evidence posted"
44-
timestamp: BigInt!
45-
"Tx hash of the evidence submission"
46-
txHash: Bytes!
47-
"Name of the evidence"
48-
name: String
49-
"Title of the evidence"
50-
title: String
51-
"Description of the evidence"
52-
description: String
53-
"URI of the attached file"
54-
fileURI: String
55-
"File extension of the attached file"
56-
fileTypeExtension: String
57-
}
58-
5921
type Arbitrator @entity {
6022
"The address of the arbitrator"
6123
id: ID!
@@ -167,12 +129,6 @@ type Request @entity {
167129
deposit: BigInt!
168130
"The outcome of the dispute, if any. Note that unsuccessful appeal fundings can invert the arbitrator ruling (so this may differ from the ruling given by the arbitrator)."
169131
disputeOutcome: Ruling!
170-
"Tracks each round of a dispute in the form rounds[roundID]."
171-
rounds: [Round!]! @derivedFrom(field: "request")
172-
"The evidence group for this request."
173-
evidenceGroup: EvidenceGroup!
174-
"The total number of rounds on this request."
175-
numberOfRounds: BigInt!
176132
"Whether it was requested to add or remove the item to/from the list."
177133
requestType: Status!
178134
"The item this request belongs to."
@@ -190,58 +146,3 @@ type Request @entity {
190146
"The hash of the transaction that solved this request."
191147
resolutionTx: Bytes
192148
}
193-
194-
type Round @entity {
195-
id: ID!
196-
"The total amount of appeal fees contributed to the requester in this round."
197-
amountPaidRequester: BigInt!
198-
"The total amount of appeal fees contributed to the challenger in this round."
199-
amountPaidChallenger: BigInt!
200-
"Whether the requester is fully funded."
201-
hasPaidRequester: Boolean!
202-
"Whether the challenger is fully funded."
203-
hasPaidChallenger: Boolean!
204-
"When was the last contribution for requester (hack for curate bot)"
205-
lastFundedRequester: BigInt!
206-
"When was the last contribution for challenger (hack for curate bot)"
207-
lastFundedChallenger: BigInt!
208-
"Sum of reimbursable fees and stake rewards available to the parties that made contributions to the side that ultimately wins a dispute."
209-
feeRewards: BigInt!
210-
"The request to which this round belongs."
211-
request: Request!
212-
"The time the appeal period starts, if in the appeal period."
213-
appealPeriodStart: BigInt!
214-
"The time the appeal period ends, if in the appeal period."
215-
appealPeriodEnd: BigInt!
216-
"The time the round received the ruling."
217-
rulingTime: BigInt!
218-
"The ruling given by the arbitrator."
219-
ruling: Ruling!
220-
"The tx hash of the moment appealing became possible"
221-
txHashAppealPossible: Bytes
222-
"The tx hash of the moment the round was appealed"
223-
txHashAppealDecision: Bytes
224-
"The moment the round was created."
225-
creationTime: BigInt!
226-
"The contributions made to this round."
227-
contributions: [Contribution!]! @derivedFrom(field: "round")
228-
"The number of contributions made to this round"
229-
numberOfContributions: BigInt!
230-
"Whether this round was appealed."
231-
appealed: Boolean!
232-
"When this round was appealed, if it was appealed"
233-
appealedAt: BigInt
234-
}
235-
236-
type Contribution @entity {
237-
"The contribution ID."
238-
id: ID!
239-
"The round the contribution was made to."
240-
round: Round!
241-
"The address that made the contribution."
242-
contributor: Bytes!
243-
"Whether there are any withdrawable contributions."
244-
withdrawable: Boolean!
245-
"To which side the contribution was made."
246-
side: BigInt!
247-
}

subgraph/src/Curate.ts

Lines changed: 70 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable prefer-const */
2-
import { Bytes, BigInt, Address, log } from "@graphprotocol/graph-ts";
3-
import { Item, ItemProp, Request, Round, Registry, Contribution, EvidenceGroup } from "../generated/schema";
2+
import { log } from "@graphprotocol/graph-ts";
3+
import { Item, ItemProp, Request, Registry } from "../generated/schema";
44

55
import {
66
ItemStatusChange,
@@ -9,14 +9,13 @@ import {
99
Ruling,
1010
ConnectedTCRSet as ConnectedTCRSetEvent,
1111
Curate,
12+
DisputeRequest,
1213
} from "../generated/templates/Curate/Curate";
1314
import {
1415
CLEARING_REQUESTED_CODE,
15-
NONE,
1616
ONE,
1717
REGISTRATION_REQUESTED_CODE,
1818
ZERO,
19-
ZERO_ADDRESS,
2019
getExtendedStatus,
2120
getFinalRuling,
2221
getStatus,
@@ -76,7 +75,7 @@ export function handleRequestSubmitted(event: RequestSubmitted): void {
7675
// would be wrong. We use a condition to detect if its the very
7776
// first request and if so, ignore its contents (see below in accounting).
7877
let previousStatus = getExtendedStatus(item.disputed, item.status);
79-
let newStatus = getExtendedStatus(item.disputed, item.status);
78+
let newStatus = getExtendedStatus(item.disputed, item.status); // TODO
8079

8180
item.numberOfRequests = item.numberOfRequests.plus(ONE);
8281
item.status = getStatus(itemInfo.value0);
@@ -110,7 +109,7 @@ export function handleStatusUpdated(event: ItemStatusChange): void {
110109
let itemInfo = curate.getItemInfo(event.params._itemID);
111110
if (itemInfo.value0 == REGISTRATION_REQUESTED_CODE || itemInfo.value0 == CLEARING_REQUESTED_CODE) {
112111
// Request not yet resolved. No-op as changes are handled
113-
// elsewhere.
112+
// elsewhere. (RequestSubmitted handler)
114113
return;
115114
}
116115

@@ -139,95 +138,27 @@ export function handleStatusUpdated(event: ItemStatusChange): void {
139138

140139
return;
141140
}
142-
item.save();
143-
144-
// TODO : handle this in Ruling Event
145-
// item.latestRequestResolutionTime = event.block.timestamp;
146-
147-
// let requestIndex = item.numberOfRequests.minus(BigInt.fromI32(1));
148-
// let requestInfo = tcr.getRequestInfo(event.params._itemID, requestIndex);
149141

150-
// let requestID = graphItemID + '-' + requestIndex.toString();
151-
// let request = Request.load(requestID);
152-
// if (!request) {
153-
// log.error(`Request {} not found.`, [requestID]);
154-
// return;
155-
// }
142+
item.latestRequestResolutionTime = event.block.timestamp;
156143

157-
// request.resolved = true;
158-
// request.resolutionTime = event.block.timestamp;
159-
// request.resolutionTx = event.transaction.hash;
160-
// // requestInfo.value6 is request.ruling.
161-
// request.disputeOutcome = getFinalRuling(requestInfo.value6);
144+
let requestIndex = item.numberOfRequests.minus(ONE);
145+
let requestInfo = curate.getRequestInfo(event.params._itemID, requestIndex);
162146

163-
// // Iterate over every contribution and mark it as withdrawable if it is.
164-
// // Start from the second round as the first is automatically withdrawn
165-
// // when the request resolves.
166-
// for (
167-
// let i = BigInt.fromI32(1);
168-
// i.lt(request.numberOfRounds);
169-
// i = i.plus(BigInt.fromI32(1))
170-
// ) {
171-
// // Iterate over every round of the request.
172-
// let roundID = requestID + '-' + i.toString();
173-
// let round = Round.load(roundID);
174-
// if (!round) {
175-
// log.error(`Round {} not found.`, [roundID]);
176-
// return;
177-
// }
178-
179-
// for (
180-
// let j = BigInt.fromI32(0);
181-
// j.lt(round.numberOfContributions);
182-
// j = j.plus(BigInt.fromI32(1))
183-
// ) {
184-
// // Iterate over every contribution of the round.
185-
// let contributionID = roundID + '-' + j.toString();
186-
// let contribution = Contribution.load(contributionID);
187-
// if (!contribution) {
188-
// log.error(`Contribution {} not found.`, [contributionID]);
189-
// return;
190-
// }
191-
192-
// if (requestInfo.value6 == NO_RULING_CODE) {
193-
// // The final ruling is refuse to rule. There is no winner
194-
// // or loser so every contribution is withdrawable.
195-
// contribution.withdrawable = true;
196-
// } else if (requestInfo.value6 == REQUESTER_CODE) {
197-
// // The requester won so only contributions to the requester
198-
// // are withdrawable.
199-
// // The only exception is in the case the last round the loser
200-
// // (challenger in this case) raised some funds but not enough
201-
// // to be fully funded before the deadline. In this case
202-
// // the contributors get to withdraw.
203-
// if (contribution.side == BigInt.fromI32(REQUESTER_CODE)) {
204-
// contribution.withdrawable = true;
205-
// } else if (i.equals(request.numberOfRounds.minus(BigInt.fromI32(1)))) {
206-
// // Contribution was made to the challenger (loser) and this
207-
// // is the last round.
208-
// contribution.withdrawable = true;
209-
// }
210-
// } else {
211-
// // The challenger won so only contributions to the challenger
212-
// // are withdrawable.
213-
// // The only exception is in the case the last round the loser
214-
// // (requester in this case) raised some funds but not enough
215-
// // to be fully funded before the deadline. In this case
216-
// // the contributors get to withdraw.
217-
// if (contribution.side == BigInt.fromI32(CHALLENGER_CODE)) {
218-
// contribution.withdrawable = true;
219-
// } else if (i.equals(request.numberOfRounds.minus(BigInt.fromI32(1)))) {
220-
// // Contribution was made to the requester (loser) and this
221-
// // is the last round.
222-
// contribution.withdrawable = true;
223-
// }
224-
// }
147+
let requestID = graphItemID + "-" + requestIndex.toString();
148+
let request = Request.load(requestID);
149+
if (!request) {
150+
log.error(`Request {} not found.`, [requestID]);
151+
return;
152+
}
225153

226-
// contribution.save();
227-
// }
228-
// }
154+
request.resolved = true;
155+
request.resolutionTime = event.block.timestamp;
156+
request.resolutionTx = event.transaction.hash;
157+
// requestInfo.value5 is request.ruling.
158+
request.disputeOutcome = getFinalRuling(requestInfo.value5);
229159

230-
// request.save();
160+
item.save();
161+
request.save();
231162
}
232163

233164
export function handleConnectedTCRSet(event: ConnectedTCRSetEvent): void {
@@ -241,77 +172,59 @@ export function handleConnectedTCRSet(event: ConnectedTCRSetEvent): void {
241172
registry.save();
242173
}
243174

244-
// export function handleRuling(event: Ruling): void {
245-
// let tcr = LightGeneralizedTCR.bind(event.address);
246-
// let itemID = tcr.arbitratorDisputeIDToItemID(
247-
// event.params._arbitrator,
248-
// event.params._disputeID,
249-
// );
250-
// let graphItemID = itemID.toHexString() + '@' + event.address.toHexString();
251-
// let item = Item.load(graphItemID);
252-
// if (!item) {
253-
// log.error(`Ruling Item {} not found. tx {}`, [
254-
// graphItemID,
255-
// event.transaction.hash.toHexString(),
256-
// ]);
257-
// return;
258-
// }
259-
260-
// let requestID =
261-
// item.id + '-' + item.numberOfRequests.minus(BigInt.fromI32(1)).toString();
262-
// let request = Request.load(requestID);
263-
// if (!request) {
264-
// log.error(`Ruling Request {} not found. tx {}`, [
265-
// requestID,
266-
// event.transaction.hash.toHexString(),
267-
// ]);
268-
// return;
269-
// }
270-
271-
// request.finalRuling = event.params._ruling;
272-
// request.resolutionTime = event.block.timestamp;
273-
// request.save();
274-
// }
175+
export function handleRuling(event: Ruling): void {
176+
let curate = Curate.bind(event.address);
177+
let itemID = curate.arbitratorDisputeIDToItemID(event.params._arbitrator, event.params._disputeID);
178+
let graphItemID = itemID.toHexString() + "@" + event.address.toHexString();
179+
let item = Item.load(graphItemID);
180+
if (!item) {
181+
log.error(`Ruling Item {} not found. tx {}`, [graphItemID, event.transaction.hash.toHexString()]);
182+
return;
183+
}
275184

276-
// TODO: link this to DisputeRequest?
277-
// export function handleRequestChallenged(event: Dispute): void {
278-
// let curate = Curate.bind(event.address);
279-
// let itemID = curate.arbitratorDisputeIDToItemID(
280-
// event.params._arbitrator,
281-
// event.params._disputeID,
282-
// );
283-
// let graphItemID = itemID.toHexString() + '@' + event.address.toHexString();
284-
// let item = Item.load(graphItemID);
285-
// if (!item) {
286-
// log.warning(`Item {} not found.`, [graphItemID]);
287-
// return;
288-
// }
185+
let requestID = item.id + "-" + item.numberOfRequests.minus(ONE).toString();
186+
let request = Request.load(requestID);
187+
if (!request) {
188+
log.error(`Ruling Request {} not found. tx {}`, [requestID, event.transaction.hash.toHexString()]);
189+
return;
190+
}
289191

290-
// let previousStatus = getExtendedStatus(item.disputed, item.status);
291-
// item.disputed = true;
292-
// item.latestChallenger = event.transaction.from;
293-
// let newStatus = getExtendedStatus(item.disputed, item.status);
192+
request.finalRuling = event.params._ruling;
193+
request.resolutionTime = event.block.timestamp;
194+
request.save();
195+
}
294196

295-
// let requestIndex = item.numberOfRequests.minus(BigInt.fromI32(1));
296-
// let requestID = graphItemID + '-' + requestIndex.toString();
297-
// let request = Request.load(requestID);
298-
// if (!request) {
299-
// log.error(`Request {} not found.`, [requestID]);
300-
// return;
301-
// }
197+
export function handleRequestChallenged(event: DisputeRequest): void {
198+
let curate = Curate.bind(event.address);
199+
let itemID = curate.arbitratorDisputeIDToItemID(event.params._arbitrator, event.params._arbitrableDisputeID);
200+
let graphItemID = itemID.toHexString() + "@" + event.address.toHexString();
201+
let item = Item.load(graphItemID);
202+
if (!item) {
203+
log.warning(`Item {} not found.`, [graphItemID]);
204+
return;
205+
}
206+
let itemInfo = curate.getItemInfo(itemID);
207+
let previousStatus = getExtendedStatus(item.disputed, item.status);
208+
item.disputed = true;
209+
item.latestChallenger = event.transaction.from;
210+
item.status = getStatus(itemInfo.value0);
211+
let newStatus = getExtendedStatus(item.disputed, item.status);
302212

303-
// request.disputed = true;
304-
// request.challenger = event.transaction.from;
305-
// request.numberOfRounds = BigInt.fromI32(2);
306-
// request.disputeID = event.params._disputeID;
213+
let requestIndex = item.numberOfRequests.minus(ONE);
214+
let requestID = graphItemID + "-" + requestIndex.toString();
215+
let request = Request.load(requestID);
216+
if (!request) {
217+
log.error(`Request {} not found.`, [requestID]);
218+
return;
219+
}
307220

308-
// let newRoundID = requestID + '-1'; // When a dispute is created, the new round is always id 1
309-
// let newRound = buildNewRound(newRoundID, request.id, event.block.timestamp);
221+
request.disputed = true;
222+
request.challenger = event.transaction.from;
223+
request.disputeID = event.params._arbitrableDisputeID;
310224

311-
// // Accounting.
312-
// updateCounters(previousStatus, newStatus, event.address);
225+
// Accounting.
226+
updateCounters(previousStatus, newStatus, event.address);
313227

314-
// newRound.save();
315-
// request.save();
316-
// item.save();
317-
// }
228+
request.save();
229+
item.save();
230+
}

0 commit comments

Comments
 (0)