@@ -6,7 +6,7 @@ import { HomePageBlockQuery } from "src/graphql/graphql";
6
6
7
7
const homePageBlockQuery = graphql ( `
8
8
query HomePageBlock($pastTimestamp: BigInt) {
9
- presentCourts: courts(orderBy: id, orderDirection: asc) {
9
+ presentCourts: courts(orderBy: id, orderDirection: asc, first: 1000 ) {
10
10
id
11
11
parent {
12
12
id
@@ -17,13 +17,19 @@ const homePageBlockQuery = graphql(`
17
17
feeForJuror
18
18
effectiveStake
19
19
}
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
+ ) {
21
26
court {
22
27
id
23
28
}
24
29
numberDisputes
25
30
numberVotes
26
31
effectiveStake
32
+ timestamp
27
33
}
28
34
}
29
35
` ) ;
@@ -53,16 +59,17 @@ export type HomePageBlockStats = {
53
59
} ;
54
60
55
61
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 ] ;
57
63
const getCourtBestDrawingChances = ( courts : CourtWithTree [ ] ) =>
58
64
courts . toSorted ( ( a , b ) => b . treeVotesPerPnk - a . treeVotesPerPnk ) [ 0 ] ;
59
65
const getBestExpectedRewardCourt = ( courts : CourtWithTree [ ] ) =>
60
66
courts . toSorted ( ( a , b ) => b . treeExpectedRewardPerPnk - a . treeExpectedRewardPerPnk ) [ 0 ] ;
61
67
62
68
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 ) ) ;
64
70
const pastCourts = data . pastCourts ;
65
71
72
+ const presentCourtsMap = new Map ( presentCourts . map ( ( c ) => [ c . id , c ] ) ) ;
66
73
const pastCourtsMap = new Map < string , CourtCounter > ( ) ;
67
74
if ( ! allTime ) {
68
75
for ( const pastCourt of pastCourts ) {
@@ -73,41 +80,40 @@ const processData = (data: HomePageBlockQuery, allTime: boolean) => {
73
80
}
74
81
}
75
82
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 ) ! ;
78
86
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 ) ;
81
89
82
- processed . add ( id ) ;
83
- const court = presentCourts [ id ] ;
84
- const pastCourt = pastCourtsMap . get ( court . id ) ;
85
90
const courtWithTree = ! allTime && pastCourt ? addTreeValuesWithDiff ( court , pastCourt ) : addTreeValues ( court ) ;
86
- const parentIndex = court . parent ? Number ( court . parent . id ) - 1 : 0 ;
87
91
88
- if ( id === parentIndex ) {
89
- processedCourts [ id ] = courtWithTree ;
92
+ const parentId = court . parent ?. id ;
93
+ if ( ! parentId || courtId === parentId ) {
94
+ processedCourtsMap . set ( courtId , courtWithTree ) ;
90
95
return courtWithTree ;
91
96
}
92
97
93
- processedCourts [ id ] = {
98
+ const parentCourt = processCourt ( parentId ) ;
99
+ const fullTreeCourt : CourtWithTree = {
94
100
...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 ,
101
106
} ;
102
107
103
- return processedCourts [ id ] ;
108
+ processedCourtsMap . set ( courtId , fullTreeCourt ) ;
109
+ return fullTreeCourt ;
104
110
} ;
105
111
106
112
for ( const court of presentCourts . toReversed ( ) ) {
107
- processCourt ( Number ( court . id ) - 1 ) ;
113
+ processCourt ( court . id ) ;
108
114
}
109
115
110
- processedCourts . reverse ( ) ;
116
+ const processedCourts = [ ... processedCourtsMap . values ( ) ] . sort ( ( a , b ) => Number ( a . id ) - Number ( b . id ) ) ;
111
117
112
118
return {
113
119
mostDisputedCourt : getCourtMostDisputes ( processedCourts ) ,
@@ -140,16 +146,32 @@ const addTreeValues = (court: Court): CourtWithTree => {
140
146
141
147
const addTreeValuesWithDiff = ( presentCourt : Court , pastCourt : CourtCounter | undefined ) : CourtWithTree => {
142
148
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 ) ;
146
158
147
159
const diffNumberVotes = presentCourtWithTree . numberVotes - pastNumberVotes ;
148
160
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
+
149
170
const avgEffectiveStake = ( presentCourtWithTree . effectiveStake + pastEffectiveStake ) / 2n ;
150
171
const votesPerPnk = diffNumberVotes / ( Number ( avgEffectiveStake ) / 1e18 ) || 0 ;
151
172
const disputesPerPnk = diffNumberDisputes / ( Number ( avgEffectiveStake ) / 1e18 ) || 0 ;
152
173
const expectedRewardPerPnk = votesPerPnk * ( Number ( presentCourt . feeForJuror ) / 1e18 ) ;
174
+
153
175
return {
154
176
...presentCourt ,
155
177
numberDisputes : diffNumberDisputes ,
0 commit comments