Skip to content

Commit 1e43686

Browse files
committed
feat(web): add-counters-and-user-entity
1 parent 3497933 commit 1e43686

File tree

9 files changed

+116
-192
lines changed

9 files changed

+116
-192
lines changed

subgraph/schema.graphql

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
enum Status {
22
"The item is not registered on the TCR and there are no pending requests."
3-
Absent
3+
absent
44
"The item is registered and there are no pending requests."
5-
Registered
5+
registered
66
"The item is not registered on the TCR, but there is a pending registration request."
7-
RegistrationRequested
7+
registrationRequested
88
"The item is registered on the TCR, but there is a pending removal request. These are sometimes also called removal requests."
9-
ClearingRequested
9+
clearingRequested
1010
}
1111

1212
enum Ruling {
@@ -23,26 +23,33 @@ type Arbitrator @entity {
2323
id: ID!
2424
}
2525

26+
type Counter @entity {
27+
id: ID!
28+
totalRegistries: BigInt!
29+
totalItems: BigInt!
30+
totalDeposits: BigInt!
31+
numberOfCurators: BigInt!
32+
}
33+
34+
type User @entity {
35+
id: ID! # address
36+
registry: [Registry!]! @derivedFrom(field: "registerer")
37+
items: [Item!]! @derivedFrom(field: "registerer")
38+
requests: [Request!]! @derivedFrom(field: "requester")
39+
challenges: [Request!]! @derivedFrom(field: "challenger")
40+
}
41+
2642
type Registry @entity {
2743
"The registry address"
2844
id: ID!
2945
"The items submitted to this list"
3046
items: [Item!]! @derivedFrom(field: "registry")
3147
"The requests submitted to this list"
3248
requests: [Request!]! @derivedFrom(field: "registry")
33-
"The total number of items in absent state."
34-
numberOfAbsent: BigInt!
35-
"The total number of items in registered state."
36-
numberOfRegistered: BigInt!
37-
"The total number of items in the registration requested state."
38-
numberOfRegistrationRequested: BigInt!
39-
numberOfClearingRequested: BigInt!
40-
"The total number of items in the challenged registration state."
41-
numberOfChallengedRegistrations: BigInt!
42-
"The total number of items in the challenged removal state."
43-
numberOfChallengedClearing: BigInt!
4449
"Connected TCR. Can be the 0 address. In practice, will never be null."
4550
connectedTCR: Bytes
51+
"The address that registered the curate"
52+
registerer: User!
4653
}
4754

4855
type Item @entity {
@@ -83,9 +90,11 @@ type Item @entity {
8390
"Whether the item is currently disputed."
8491
disputed: Boolean!
8592
"The account that made the latest request to the item."
86-
latestRequester: Bytes!
93+
latestRequester: User
8794
"The account that challenged the latest request, if any."
88-
latestChallenger: Bytes!
95+
latestChallenger: User
96+
"The user that requested the first request / registration"
97+
registerer: User!
8998
}
9099

91100
type _Schema_
@@ -117,10 +126,10 @@ type Request @entity {
117126
submissionTime: BigInt!
118127
"True if the request was executed and/or any raised disputes were resolved."
119128
resolved: Boolean!
120-
"The address of the party that made a request"
121-
requester: Bytes!
122-
"The address of the party that challenged the request"
123-
challenger: Bytes!
129+
"The party that made a request"
130+
requester: User!
131+
"The party that challenged the request"
132+
challenger: User
124133
"The arbitrator trusted to solve disputes for this request."
125134
arbitrator: Bytes!
126135
"The extra data for the trusted arbitrator of this request."

subgraph/src/Counters.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

subgraph/src/Curate.ts

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable prefer-const */
22
import { log } from "@graphprotocol/graph-ts";
3-
import { Item, ItemProp, Request, Registry } from "../generated/schema";
3+
import { Item, Request, Registry } from "../generated/schema";
44

55
import {
66
ItemStatusChange,
@@ -11,18 +11,10 @@ import {
1111
Curate,
1212
DisputeRequest,
1313
} from "../generated/templates/Curate/Curate";
14-
import {
15-
CLEARING_REQUESTED_CODE,
16-
ONE,
17-
REGISTRATION_REQUESTED_CODE,
18-
ZERO,
19-
getExtendedStatus,
20-
getFinalRuling,
21-
getStatus,
22-
} from "./utils";
23-
import { updateCounters } from "./Counters";
14+
import { ItemStatus, ONE, ZERO, getFinalRuling, getStatus } from "./utils";
2415
import { createRequestFromEvent } from "./entities/Request";
2516
import { createItemFromEvent } from "./entities/Item";
17+
import { ensureUser } from "./entities/User";
2618

2719
// Items on a TCR can be in 1 of 4 states:
2820
// - (0) Absent: The item is not registered on the TCR and there are no pending requests.
@@ -68,37 +60,13 @@ export function handleRequestSubmitted(event: RequestSubmitted): void {
6860
return;
6961
}
7062

71-
// `previousStatus` and `newStatus` are used for accounting.
72-
// Note that if this is the very first request of an item,
73-
// item.status and item.dispute are dirty because they were set by
74-
// handleNewItem, executed before this handler and so `previousStatus`
75-
// would be wrong. We use a condition to detect if its the very
76-
// first request and if so, ignore its contents (see below in accounting).
77-
let previousStatus = getExtendedStatus(item.disputed, item.status);
78-
let newStatus = getExtendedStatus(item.disputed, item.status); // TODO
79-
8063
item.numberOfRequests = item.numberOfRequests.plus(ONE);
8164
item.status = getStatus(itemInfo.value0);
82-
item.latestRequester = event.transaction.from;
65+
item.latestRequester = ensureUser(event.transaction.from.toHexString()).id;
8366
item.latestRequestResolutionTime = ZERO;
8467
item.latestRequestSubmissionTime = event.block.timestamp;
8568

8669
createRequestFromEvent(event);
87-
88-
// Accounting.
89-
if (itemInfo.value1.equals(ONE)) {
90-
let registry = Registry.load(event.address.toHexString());
91-
if (!registry) {
92-
log.error(`Registry at address {} not found`, [event.address.toHexString()]);
93-
return;
94-
}
95-
// This is the first request for this item, which must be
96-
// a registration request.
97-
registry.numberOfRegistrationRequested = registry.numberOfRegistrationRequested.plus(ONE);
98-
registry.save();
99-
} else {
100-
updateCounters(previousStatus, newStatus, event.address);
101-
}
10270
item.save();
10371
}
10472

@@ -107,7 +75,7 @@ export function handleStatusUpdated(event: ItemStatusChange): void {
10775
// All other status updates are handled elsewhere.
10876
let curate = Curate.bind(event.address);
10977
let itemInfo = curate.getItemInfo(event.params._itemID);
110-
if (itemInfo.value0 == REGISTRATION_REQUESTED_CODE || itemInfo.value0 == CLEARING_REQUESTED_CODE) {
78+
if (itemInfo.value0 == ItemStatus.REGISTRATION_REQUESTED || itemInfo.value0 == ItemStatus.CLEARING_REQUESTED) {
11179
// Request not yet resolved. No-op as changes are handled
11280
// elsewhere. (RequestSubmitted handler)
11381
return;
@@ -120,16 +88,8 @@ export function handleStatusUpdated(event: ItemStatusChange): void {
12088
return;
12189
}
12290

123-
// We take the previous and new extended statuses for accounting purposes.
124-
let previousStatus = getExtendedStatus(item.disputed, item.status);
12591
item.status = getStatus(itemInfo.value0);
12692
item.disputed = false;
127-
let newStatus = getExtendedStatus(item.disputed, item.status);
128-
129-
if (previousStatus != newStatus) {
130-
// Accounting.
131-
updateCounters(previousStatus, newStatus, event.address);
132-
}
13393

13494
if (event.params._updatedDirectly) {
13595
// Direct actions (e.g. addItemDirectly and removeItemDirectly)
@@ -204,11 +164,9 @@ export function handleRequestChallenged(event: DisputeRequest): void {
204164
return;
205165
}
206166
let itemInfo = curate.getItemInfo(itemID);
207-
let previousStatus = getExtendedStatus(item.disputed, item.status);
208167
item.disputed = true;
209-
item.latestChallenger = event.transaction.from;
168+
item.latestChallenger = ensureUser(event.transaction.from.toHexString()).id;
210169
item.status = getStatus(itemInfo.value0);
211-
let newStatus = getExtendedStatus(item.disputed, item.status);
212170

213171
let requestIndex = item.numberOfRequests.minus(ONE);
214172
let requestID = graphItemID + "-" + requestIndex.toString();
@@ -219,12 +177,9 @@ export function handleRequestChallenged(event: DisputeRequest): void {
219177
}
220178

221179
request.disputed = true;
222-
request.challenger = event.transaction.from;
180+
request.challenger = ensureUser(event.transaction.from.toHexString()).id;
223181
request.disputeID = event.params._arbitrableDisputeID;
224182

225-
// Accounting.
226-
updateCounters(previousStatus, newStatus, event.address);
227-
228183
request.save();
229184
item.save();
230185
}

subgraph/src/CurateFactory.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/* eslint-disable prefer-const */
22
import { NewGTCR } from "../generated/CurateFactory/CurateFactory";
3-
import { Registry } from "../generated/schema";
3+
import { Registry, User } from "../generated/schema";
44
import { Curate } from "../generated/templates";
5-
import { ZERO } from "./utils";
5+
import { ensureCounter } from "./entities/Counters";
6+
import { ensureUser } from "./entities/User";
7+
import { ONE } from "./utils";
68

79
export function handleNewCurate(event: NewGTCR): void {
810
Curate.create(event.params._address);
911

1012
let registry = new Registry(event.params._address.toHexString());
13+
let counter = ensureCounter();
1114

12-
registry.numberOfAbsent = ZERO;
13-
registry.numberOfRegistered = ZERO;
14-
registry.numberOfRegistrationRequested = ZERO;
15-
registry.numberOfClearingRequested = ZERO;
16-
registry.numberOfChallengedRegistrations = ZERO;
17-
registry.numberOfChallengedClearing = ZERO;
15+
counter.totalRegistries = counter.totalRegistries.plus(ONE);
16+
let doesCuratorExist = User.load(event.transaction.from.toHexString());
17+
if (!doesCuratorExist) counter.numberOfCurators = counter.numberOfCurators.plus(ONE);
18+
19+
registry.registerer = ensureUser(event.transaction.from.toHexString()).id;
20+
21+
counter.save();
1822
registry.save();
1923
}

subgraph/src/entities/Counters.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Counter } from "../../generated/schema";
2+
import { ZERO } from "../utils";
3+
4+
export function ensureCounter(): Counter {
5+
const counter = Counter.load("0");
6+
if (counter) return counter;
7+
8+
let newCounter = new Counter("0");
9+
newCounter.totalDeposits = ZERO;
10+
newCounter.totalItems = ZERO;
11+
newCounter.totalRegistries = ZERO;
12+
newCounter.numberOfCurators = ZERO;
13+
newCounter.save();
14+
return newCounter;
15+
}

subgraph/src/entities/Item.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { Bytes, ipfs, json, log } from "@graphprotocol/graph-ts";
2-
import { Item, ItemProp, Registry } from "../../generated/schema";
2+
import { Item, ItemProp, Registry, User } from "../../generated/schema";
33
import { Curate, NewItem } from "../../generated/templates/Curate/Curate";
4-
import { JSONValueToBool, JSONValueToMaybeString, ZERO, ZERO_ADDRESS, getStatus } from "../utils";
4+
import { JSONValueToBool, JSONValueToMaybeString, ONE, ZERO, ZERO_ADDRESS, getStatus } from "../utils";
5+
import { ensureCounter } from "./Counters";
6+
import { ensureUser } from "./User";
57

68
export function createItemFromEvent(event: NewItem): void {
79
// We assume this is an item added via addItemDirectly and care
810
// only about saving the item json data.
911
// If it was emitted via addItem, all the missing/wrong data regarding
1012
// things like submission time, arbitrator and deposit will be set in
1113
// handleRequestSubmitted.
12-
//
13-
// Accounting for items added or removed directly is done
14-
// inside handleStatusUpdated.
14+
1515
let graphItemID = event.params._itemID.toHexString() + "@" + event.address.toHexString();
1616
let curate = Curate.bind(event.address);
1717
let registry = Registry.load(event.address.toHexString());
@@ -20,6 +20,13 @@ export function createItemFromEvent(event: NewItem): void {
2020
return;
2121
}
2222

23+
let counter = ensureCounter();
24+
25+
counter.totalItems = counter.totalItems.plus(ONE);
26+
let doesCuratorExist = User.load(event.transaction.from.toHexString());
27+
if (!doesCuratorExist) counter.numberOfCurators = counter.numberOfCurators.plus(ONE);
28+
counter.save();
29+
2330
let itemInfo = curate.getItemInfo(event.params._itemID);
2431

2532
let item = new Item(graphItemID);
@@ -30,8 +37,7 @@ export function createItemFromEvent(event: NewItem): void {
3037
item.registryAddress = event.address;
3138
item.disputed = false;
3239
item.status = getStatus(itemInfo.value0);
33-
item.latestRequester = ZERO_ADDRESS;
34-
item.latestChallenger = ZERO_ADDRESS;
40+
item.registerer = ensureUser(event.transaction.from.toHexString()).id;
3541
item.latestRequestResolutionTime = ZERO;
3642
item.latestRequestSubmissionTime = ZERO;
3743

0 commit comments

Comments
 (0)