1
1
import { useQuery } from "@tanstack/react-query" ;
2
-
3
2
import { useGraphqlBatcher } from "context/GraphqlBatcher" ;
4
3
import { isUndefined } from "utils/index" ;
5
-
6
4
import { graphql } from "src/graphql" ;
7
5
import { HomePageBlockQuery } from "src/graphql/graphql" ;
8
- import useGenesisBlock from "../useGenesisBlock" ;
9
- export type { HomePageBlockQuery } ;
10
6
11
7
const homePageBlockQuery = graphql ( `
12
- query HomePageBlock($blockNumber: Int ) {
8
+ query HomePageBlock($pastTimestamp: BigInt ) {
13
9
presentCourts: courts(orderBy: id, orderDirection: asc) {
14
10
id
15
11
parent {
@@ -21,21 +17,19 @@ const homePageBlockQuery = graphql(`
21
17
feeForJuror
22
18
effectiveStake
23
19
}
24
- pastCourts: courts(orderBy: id, orderDirection: asc, block: { number: $blockNumber }) {
25
- id
26
- parent {
20
+ pastCourts: courtCounters(where: { timestamp_lte: $pastTimestamp }, orderBy: timestamp, orderDirection: desc) {
21
+ court {
27
22
id
28
23
}
29
- name
30
24
numberDisputes
31
25
numberVotes
32
- feeForJuror
33
26
effectiveStake
34
27
}
35
28
}
36
29
` ) ;
37
30
38
31
type Court = HomePageBlockQuery [ "presentCourts" ] [ number ] ;
32
+ type CourtCounter = HomePageBlockQuery [ "pastCourts" ] [ number ] ;
39
33
type CourtWithTree = Court & {
40
34
numberDisputes : number ;
41
35
numberVotes : number ;
@@ -58,56 +52,52 @@ export type HomePageBlockStats = {
58
52
courts : CourtWithTree [ ] ;
59
53
} ;
60
54
61
- export const useHomePageBlockQuery = ( blockNumber : number | undefined , allTime : boolean ) => {
62
- const genesisBlock = useGenesisBlock ( ) ;
63
- const isEnabled = ! isUndefined ( blockNumber ) || allTime || ! isUndefined ( genesisBlock ) ;
64
- const { graphqlBatcher } = useGraphqlBatcher ( ) ;
65
-
66
- return useQuery < HomePageBlockStats > ( {
67
- queryKey : [ `homePageBlockQuery${ blockNumber } -${ allTime } ` ] ,
68
- enabled : isEnabled ,
69
- staleTime : Infinity ,
70
- queryFn : async ( ) => {
71
- const targetBlock = Math . max ( blockNumber ! , genesisBlock ! ) ;
72
- const data = await graphqlBatcher . fetch ( {
73
- id : crypto . randomUUID ( ) ,
74
- document : homePageBlockQuery ,
75
- variables : { blockNumber : targetBlock } ,
76
- } ) ;
77
-
78
- return processData ( data , allTime ) ;
79
- } ,
80
- } ) ;
81
- } ;
55
+ const getCourtMostDisputes = ( courts : CourtWithTree [ ] ) =>
56
+ courts . toSorted ( ( a : CourtWithTree , b : CourtWithTree ) => b . numberDisputes - a . numberDisputes ) [ 0 ] ;
57
+ const getCourtBestDrawingChances = ( courts : CourtWithTree [ ] ) =>
58
+ courts . toSorted ( ( a , b ) => b . treeVotesPerPnk - a . treeVotesPerPnk ) [ 0 ] ;
59
+ const getBestExpectedRewardCourt = ( courts : CourtWithTree [ ] ) =>
60
+ courts . toSorted ( ( a , b ) => b . treeExpectedRewardPerPnk - a . treeExpectedRewardPerPnk ) [ 0 ] ;
82
61
83
62
const processData = ( data : HomePageBlockQuery , allTime : boolean ) => {
84
63
const presentCourts = data . presentCourts ;
85
64
const pastCourts = data . pastCourts ;
65
+
66
+ const pastCourtsMap = new Map < string , CourtCounter > ( ) ;
67
+ if ( ! allTime ) {
68
+ for ( const pastCourt of pastCourts ) {
69
+ const courtId = pastCourt . court . id ;
70
+ if ( ! pastCourtsMap . has ( courtId ) ) {
71
+ pastCourtsMap . set ( courtId , pastCourt ) ;
72
+ }
73
+ }
74
+ }
75
+
86
76
const processedCourts : CourtWithTree [ ] = Array ( presentCourts . length ) ;
87
- const processed = new Set ( ) ;
77
+ const processed = new Set < number > ( ) ;
88
78
89
79
const processCourt = ( id : number ) : CourtWithTree => {
90
80
if ( processed . has ( id ) ) return processedCourts [ id ] ;
91
81
92
82
processed . add ( id ) ;
93
- const court =
94
- ! allTime && id < data . pastCourts . length
95
- ? addTreeValuesWithDiff ( presentCourts [ id ] , pastCourts [ id ] )
96
- : addTreeValues ( presentCourts [ id ] ) ;
83
+ const court = presentCourts [ id ] ;
84
+ const pastCourt = pastCourtsMap . get ( court . id ) ;
85
+ const courtWithTree = ! allTime && pastCourt ? addTreeValuesWithDiff ( court , pastCourt ) : addTreeValues ( court ) ;
97
86
const parentIndex = court . parent ? Number ( court . parent . id ) - 1 : 0 ;
98
87
99
88
if ( id === parentIndex ) {
100
- processedCourts [ id ] = court ;
101
- return court ;
89
+ processedCourts [ id ] = courtWithTree ;
90
+ return courtWithTree ;
102
91
}
103
92
104
93
processedCourts [ id ] = {
105
- ...court ,
106
- treeNumberDisputes : court . treeNumberDisputes + processCourt ( parentIndex ) . treeNumberDisputes ,
107
- treeNumberVotes : court . treeNumberVotes + processCourt ( parentIndex ) . treeNumberVotes ,
108
- treeVotesPerPnk : court . treeVotesPerPnk + processCourt ( parentIndex ) . treeVotesPerPnk ,
109
- treeDisputesPerPnk : court . treeDisputesPerPnk + processCourt ( parentIndex ) . treeDisputesPerPnk ,
110
- treeExpectedRewardPerPnk : court . treeExpectedRewardPerPnk + processCourt ( parentIndex ) . treeExpectedRewardPerPnk ,
94
+ ...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 ,
111
101
} ;
112
102
113
103
return processedCourts [ id ] ;
@@ -148,21 +138,25 @@ const addTreeValues = (court: Court): CourtWithTree => {
148
138
} ;
149
139
} ;
150
140
151
- const addTreeValuesWithDiff = ( presentCourt : Court , pastCourt : Court ) : CourtWithTree => {
141
+ const addTreeValuesWithDiff = ( presentCourt : Court , pastCourt : CourtCounter | undefined ) : CourtWithTree => {
152
142
const presentCourtWithTree = addTreeValues ( presentCourt ) ;
153
- const pastCourtWithTree = addTreeValues ( pastCourt ) ;
154
- const diffNumberVotes = presentCourtWithTree . numberVotes - pastCourtWithTree . numberVotes ;
155
- const diffNumberDisputes = presentCourtWithTree . numberDisputes - pastCourtWithTree . numberDisputes ;
156
- const avgEffectiveStake = ( presentCourtWithTree . effectiveStake + pastCourtWithTree . effectiveStake ) / 2n ;
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 ) ;
146
+
147
+ const diffNumberVotes = presentCourtWithTree . numberVotes - pastNumberVotes ;
148
+ const diffNumberDisputes = presentCourtWithTree . numberDisputes - pastNumberDisputes ;
149
+ const avgEffectiveStake = ( presentCourtWithTree . effectiveStake + pastEffectiveStake ) / 2n ;
157
150
const votesPerPnk = diffNumberVotes / ( Number ( avgEffectiveStake ) / 1e18 ) || 0 ;
158
151
const disputesPerPnk = diffNumberDisputes / ( Number ( avgEffectiveStake ) / 1e18 ) || 0 ;
159
152
const expectedRewardPerPnk = votesPerPnk * ( Number ( presentCourt . feeForJuror ) / 1e18 ) ;
160
153
return {
161
154
...presentCourt ,
162
- numberDisputes : presentCourtWithTree . numberDisputes - pastCourtWithTree . numberDisputes ,
163
- treeNumberDisputes : presentCourtWithTree . treeNumberDisputes - pastCourtWithTree . treeNumberDisputes ,
155
+ numberDisputes : diffNumberDisputes ,
156
+ treeNumberDisputes : diffNumberDisputes ,
164
157
numberVotes : diffNumberVotes ,
165
- treeNumberVotes : presentCourtWithTree . treeNumberVotes - pastCourtWithTree . treeNumberVotes ,
158
+ treeNumberVotes : diffNumberVotes ,
159
+ feeForJuror : presentCourtWithTree . feeForJuror ,
166
160
effectiveStake : avgEffectiveStake ,
167
161
votesPerPnk,
168
162
treeVotesPerPnk : votesPerPnk ,
@@ -173,9 +167,21 @@ const addTreeValuesWithDiff = (presentCourt: Court, pastCourt: Court): CourtWith
173
167
} ;
174
168
} ;
175
169
176
- const getCourtMostDisputes = ( courts : CourtWithTree [ ] ) =>
177
- courts . toSorted ( ( a : CourtWithTree , b : CourtWithTree ) => b . numberDisputes - a . numberDisputes ) [ 0 ] ;
178
- const getCourtBestDrawingChances = ( courts : CourtWithTree [ ] ) =>
179
- courts . toSorted ( ( a , b ) => b . treeVotesPerPnk - a . treeVotesPerPnk ) [ 0 ] ;
180
- const getBestExpectedRewardCourt = ( courts : CourtWithTree [ ] ) =>
181
- courts . toSorted ( ( a , b ) => b . treeExpectedRewardPerPnk - a . treeExpectedRewardPerPnk ) [ 0 ] ;
170
+ export const useHomePageBlockQuery = ( pastTimestamp : bigint | undefined , allTime : boolean ) => {
171
+ const { graphqlBatcher } = useGraphqlBatcher ( ) ;
172
+ const isEnabled = ! isUndefined ( pastTimestamp ) || allTime ;
173
+
174
+ return useQuery < HomePageBlockStats > ( {
175
+ queryKey : [ `homePageBlockQuery${ pastTimestamp ?. toString ( ) } -${ allTime } ` ] ,
176
+ enabled : isEnabled ,
177
+ staleTime : Infinity ,
178
+ queryFn : async ( ) => {
179
+ const data = await graphqlBatcher . fetch ( {
180
+ id : crypto . randomUUID ( ) ,
181
+ document : homePageBlockQuery ,
182
+ variables : { pastTimestamp : allTime ? "0" : pastTimestamp ?. toString ( ) } ,
183
+ } ) ;
184
+ return processData ( data , allTime ) ;
185
+ } ,
186
+ } ) ;
187
+ } ;
0 commit comments