11import { useQuery } from "@tanstack/react-query" ;
2-
32import { useGraphqlBatcher } from "context/GraphqlBatcher" ;
43import { isUndefined } from "utils/index" ;
5-
64import { graphql } from "src/graphql" ;
75import { HomePageBlockQuery } from "src/graphql/graphql" ;
8- import useGenesisBlock from "../useGenesisBlock" ;
9- export type { HomePageBlockQuery } ;
106
117const homePageBlockQuery = graphql ( `
12- query HomePageBlock($blockNumber: Int ) {
8+ query HomePageBlock($pastTimestamp: BigInt ) {
139 presentCourts: courts(orderBy: id, orderDirection: asc) {
1410 id
1511 parent {
@@ -21,21 +17,19 @@ const homePageBlockQuery = graphql(`
2117 feeForJuror
2218 effectiveStake
2319 }
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 {
2722 id
2823 }
29- name
3024 numberDisputes
3125 numberVotes
32- feeForJuror
3326 effectiveStake
3427 }
3528 }
3629` ) ;
3730
3831type Court = HomePageBlockQuery [ "presentCourts" ] [ number ] ;
32+ type CourtCounter = HomePageBlockQuery [ "pastCourts" ] [ number ] ;
3933type CourtWithTree = Court & {
4034 numberDisputes : number ;
4135 numberVotes : number ;
@@ -58,56 +52,52 @@ export type HomePageBlockStats = {
5852 courts : CourtWithTree [ ] ;
5953} ;
6054
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 ] ;
8261
8362const processData = ( data : HomePageBlockQuery , allTime : boolean ) => {
8463 const presentCourts = data . presentCourts ;
8564 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+
8676 const processedCourts : CourtWithTree [ ] = Array ( presentCourts . length ) ;
87- const processed = new Set ( ) ;
77+ const processed = new Set < number > ( ) ;
8878
8979 const processCourt = ( id : number ) : CourtWithTree => {
9080 if ( processed . has ( id ) ) return processedCourts [ id ] ;
9181
9282 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 ) ;
9786 const parentIndex = court . parent ? Number ( court . parent . id ) - 1 : 0 ;
9887
9988 if ( id === parentIndex ) {
100- processedCourts [ id ] = court ;
101- return court ;
89+ processedCourts [ id ] = courtWithTree ;
90+ return courtWithTree ;
10291 }
10392
10493 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 ,
111101 } ;
112102
113103 return processedCourts [ id ] ;
@@ -148,21 +138,25 @@ const addTreeValues = (court: Court): CourtWithTree => {
148138 } ;
149139} ;
150140
151- const addTreeValuesWithDiff = ( presentCourt : Court , pastCourt : Court ) : CourtWithTree => {
141+ const addTreeValuesWithDiff = ( presentCourt : Court , pastCourt : CourtCounter | undefined ) : CourtWithTree => {
152142 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 ;
157150 const votesPerPnk = diffNumberVotes / ( Number ( avgEffectiveStake ) / 1e18 ) || 0 ;
158151 const disputesPerPnk = diffNumberDisputes / ( Number ( avgEffectiveStake ) / 1e18 ) || 0 ;
159152 const expectedRewardPerPnk = votesPerPnk * ( Number ( presentCourt . feeForJuror ) / 1e18 ) ;
160153 return {
161154 ...presentCourt ,
162- numberDisputes : presentCourtWithTree . numberDisputes - pastCourtWithTree . numberDisputes ,
163- treeNumberDisputes : presentCourtWithTree . treeNumberDisputes - pastCourtWithTree . treeNumberDisputes ,
155+ numberDisputes : diffNumberDisputes ,
156+ treeNumberDisputes : diffNumberDisputes ,
164157 numberVotes : diffNumberVotes ,
165- treeNumberVotes : presentCourtWithTree . treeNumberVotes - pastCourtWithTree . treeNumberVotes ,
158+ treeNumberVotes : diffNumberVotes ,
159+ feeForJuror : presentCourtWithTree . feeForJuror ,
166160 effectiveStake : avgEffectiveStake ,
167161 votesPerPnk,
168162 treeVotesPerPnk : votesPerPnk ,
@@ -173,9 +167,21 @@ const addTreeValuesWithDiff = (presentCourt: Court, pastCourt: Court): CourtWith
173167 } ;
174168} ;
175169
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