Skip to content

Commit 0e47701

Browse files
committed
fix: order and timestamp bug
1 parent 8bdf724 commit 0e47701

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

subgraph/core/src/datapoint.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function updateCourtCumulativeMetric(courtId: string, delta: BigInt, time
102102
currentCounter.numberDisputes = ZERO;
103103
currentCounter.numberVotes = ZERO;
104104
currentCounter.effectiveStake = ZERO;
105-
currentCounter.timestamp = timestamp;
105+
currentCounter.timestamp = ZERO;
106106
}
107107
if (metric === "numberDisputes") {
108108
currentCounter.numberDisputes = currentCounter.numberDisputes.plus(delta);
@@ -140,7 +140,7 @@ export function updateCourtStateVariable(courtId: string, newValue: BigInt, time
140140
currentCounter.numberDisputes = ZERO;
141141
currentCounter.numberVotes = ZERO;
142142
currentCounter.effectiveStake = newValue;
143-
currentCounter.timestamp = timestamp;
143+
currentCounter.timestamp = ZERO;
144144
} else {
145145
if (variable === "effectiveStake") {
146146
currentCounter.effectiveStake = newValue;

web/src/hooks/queries/useHomePageBlockQuery.ts

+49-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { HomePageBlockQuery } from "src/graphql/graphql";
66

77
const homePageBlockQuery = graphql(`
88
query HomePageBlock($pastTimestamp: BigInt) {
9-
presentCourts: courts(orderBy: id, orderDirection: asc) {
9+
presentCourts: courts(orderBy: id, orderDirection: asc, first: 1000) {
1010
id
1111
parent {
1212
id
@@ -17,13 +17,19 @@ const homePageBlockQuery = graphql(`
1717
feeForJuror
1818
effectiveStake
1919
}
20-
pastCourts: courtCounters(where: { timestamp_lte: $pastTimestamp }, orderBy: timestamp, orderDirection: desc) {
20+
pastCourts: courtCounters(
21+
where: { timestamp_lte: $pastTimestamp }
22+
orderBy: timestamp
23+
orderDirection: desc
24+
first: 1000
25+
) {
2126
court {
2227
id
2328
}
2429
numberDisputes
2530
numberVotes
2631
effectiveStake
32+
timestamp
2733
}
2834
}
2935
`);
@@ -53,16 +59,17 @@ export type HomePageBlockStats = {
5359
};
5460

5561
const getCourtMostDisputes = (courts: CourtWithTree[]) =>
56-
courts.toSorted((a: CourtWithTree, b: CourtWithTree) => b.numberDisputes - a.numberDisputes)[0];
62+
courts.toSorted((a, b) => b.numberDisputes - a.numberDisputes)[0];
5763
const getCourtBestDrawingChances = (courts: CourtWithTree[]) =>
5864
courts.toSorted((a, b) => b.treeVotesPerPnk - a.treeVotesPerPnk)[0];
5965
const getBestExpectedRewardCourt = (courts: CourtWithTree[]) =>
6066
courts.toSorted((a, b) => b.treeExpectedRewardPerPnk - a.treeExpectedRewardPerPnk)[0];
6167

6268
const processData = (data: HomePageBlockQuery, allTime: boolean) => {
63-
const presentCourts = data.presentCourts;
69+
const presentCourts = [...data.presentCourts].sort((a, b) => Number(a.id) - Number(b.id));
6470
const pastCourts = data.pastCourts;
6571

72+
const presentCourtsMap = new Map(presentCourts.map((c) => [c.id, c]));
6673
const pastCourtsMap = new Map<string, CourtCounter>();
6774
if (!allTime) {
6875
for (const pastCourt of pastCourts) {
@@ -73,41 +80,40 @@ const processData = (data: HomePageBlockQuery, allTime: boolean) => {
7380
}
7481
}
7582

76-
const processedCourts: CourtWithTree[] = Array(presentCourts.length);
77-
const processed = new Set<number>();
83+
const processedCourtsMap = new Map<string, CourtWithTree>();
84+
const processCourt = (courtId: string): CourtWithTree => {
85+
if (processedCourtsMap.has(courtId)) return processedCourtsMap.get(courtId)!;
7886

79-
const processCourt = (id: number): CourtWithTree => {
80-
if (processed.has(id)) return processedCourts[id];
87+
const court = presentCourtsMap.get(courtId)!;
88+
const pastCourt = pastCourtsMap.get(courtId);
8189

82-
processed.add(id);
83-
const court = presentCourts[id];
84-
const pastCourt = pastCourtsMap.get(court.id);
8590
const courtWithTree = !allTime && pastCourt ? addTreeValuesWithDiff(court, pastCourt) : addTreeValues(court);
86-
const parentIndex = court.parent ? Number(court.parent.id) - 1 : 0;
8791

88-
if (id === parentIndex) {
89-
processedCourts[id] = courtWithTree;
92+
const parentId = court.parent?.id;
93+
if (!parentId || courtId === parentId) {
94+
processedCourtsMap.set(courtId, courtWithTree);
9095
return courtWithTree;
9196
}
9297

93-
processedCourts[id] = {
98+
const parentCourt = processCourt(parentId);
99+
const fullTreeCourt: CourtWithTree = {
94100
...courtWithTree,
95-
treeNumberDisputes: courtWithTree.treeNumberDisputes + processCourt(parentIndex).treeNumberDisputes,
96-
treeNumberVotes: courtWithTree.treeNumberVotes + processCourt(parentIndex).treeNumberVotes,
97-
treeVotesPerPnk: courtWithTree.treeVotesPerPnk + processCourt(parentIndex).treeVotesPerPnk,
98-
treeDisputesPerPnk: courtWithTree.treeDisputesPerPnk + processCourt(parentIndex).treeDisputesPerPnk,
99-
treeExpectedRewardPerPnk:
100-
courtWithTree.treeExpectedRewardPerPnk + processCourt(parentIndex).treeExpectedRewardPerPnk,
101+
treeNumberDisputes: courtWithTree.treeNumberDisputes + parentCourt.treeNumberDisputes,
102+
treeNumberVotes: courtWithTree.treeNumberVotes + parentCourt.treeNumberVotes,
103+
treeVotesPerPnk: courtWithTree.treeVotesPerPnk + parentCourt.treeVotesPerPnk,
104+
treeDisputesPerPnk: courtWithTree.treeDisputesPerPnk + parentCourt.treeDisputesPerPnk,
105+
treeExpectedRewardPerPnk: courtWithTree.treeExpectedRewardPerPnk + parentCourt.treeExpectedRewardPerPnk,
101106
};
102107

103-
return processedCourts[id];
108+
processedCourtsMap.set(courtId, fullTreeCourt);
109+
return fullTreeCourt;
104110
};
105111

106112
for (const court of presentCourts.toReversed()) {
107-
processCourt(Number(court.id) - 1);
113+
processCourt(court.id);
108114
}
109115

110-
processedCourts.reverse();
116+
const processedCourts = [...processedCourtsMap.values()].sort((a, b) => Number(a.id) - Number(b.id));
111117

112118
return {
113119
mostDisputedCourt: getCourtMostDisputes(processedCourts),
@@ -140,16 +146,32 @@ const addTreeValues = (court: Court): CourtWithTree => {
140146

141147
const addTreeValuesWithDiff = (presentCourt: Court, pastCourt: CourtCounter | undefined): CourtWithTree => {
142148
const presentCourtWithTree = addTreeValues(presentCourt);
143-
const pastNumberVotes = pastCourt ? Number(pastCourt.numberVotes) : 0;
144-
const pastNumberDisputes = pastCourt ? Number(pastCourt.numberDisputes) : 0;
145-
const pastEffectiveStake = pastCourt ? BigInt(pastCourt.effectiveStake) : BigInt(0);
149+
150+
if (!pastCourt) {
151+
console.warn(`Missing snapshot for court ${presentCourt.id}, falling back to live`);
152+
return presentCourtWithTree;
153+
}
154+
155+
const pastNumberVotes = Number(pastCourt.numberVotes);
156+
const pastNumberDisputes = Number(pastCourt.numberDisputes);
157+
const pastEffectiveStake = BigInt(pastCourt.effectiveStake);
146158

147159
const diffNumberVotes = presentCourtWithTree.numberVotes - pastNumberVotes;
148160
const diffNumberDisputes = presentCourtWithTree.numberDisputes - pastNumberDisputes;
161+
162+
const hasLiveActivity = presentCourtWithTree.numberDisputes > 0 || presentCourtWithTree.numberVotes > 0;
163+
const hasSnapshotActivity = diffNumberDisputes > 0 || diffNumberVotes > 0;
164+
165+
if (!hasSnapshotActivity && hasLiveActivity) {
166+
console.warn(`Snapshot shows no delta for court ${presentCourt.id}, using live`);
167+
return presentCourtWithTree;
168+
}
169+
149170
const avgEffectiveStake = (presentCourtWithTree.effectiveStake + pastEffectiveStake) / 2n;
150171
const votesPerPnk = diffNumberVotes / (Number(avgEffectiveStake) / 1e18) || 0;
151172
const disputesPerPnk = diffNumberDisputes / (Number(avgEffectiveStake) / 1e18) || 0;
152173
const expectedRewardPerPnk = votesPerPnk * (Number(presentCourt.feeForJuror) / 1e18);
174+
153175
return {
154176
...presentCourt,
155177
numberDisputes: diffNumberDisputes,

0 commit comments

Comments
 (0)